upgrade.taxonomy.test 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * Test taxonomy upgrades.
  4. */
  5. class UpgradePathTaxonomyTestCase extends UpgradePathTestCase {
  6. public static function getInfo() {
  7. return array(
  8. 'name' => 'Taxonomy upgrade path',
  9. 'description' => 'Taxonomy upgrade path tests.',
  10. 'group' => 'Upgrade path',
  11. );
  12. }
  13. public function setUp() {
  14. // Path to the database dump.
  15. $this->databaseDumpFiles = array(
  16. drupal_get_path('module', 'simpletest') . '/tests/upgrade/drupal-6.filled.database.php',
  17. );
  18. parent::setUp();
  19. }
  20. /**
  21. * Retrieve an array mapping allowed vocabulary id to field name for
  22. * all taxonomy_term_reference fields for which an instance exists
  23. * for the specified entity type and bundle.
  24. */
  25. function instanceVocabularies($entity_type, $bundle) {
  26. $instances = array();
  27. foreach (field_info_instances($entity_type, $bundle) as $instance) {
  28. $field = field_info_field($instance['field_name']);
  29. if ($field['type'] == 'taxonomy_term_reference') {
  30. foreach ($field['settings']['allowed_values'] as $tree) {
  31. // Prefer valid taxonomy term reference fields for a given vocabulary
  32. // when they exist.
  33. if (empty($instances[$tree['vocabulary']]) || $instances[$tree['vocabulary']] == 'taxonomyextra') {
  34. $instances[$tree['vocabulary']] = $field['field_name'];
  35. }
  36. }
  37. }
  38. }
  39. return $instances;
  40. }
  41. /**
  42. * Basic tests for the taxonomy upgrade.
  43. */
  44. public function testTaxonomyUpgrade() {
  45. $this->assertTrue($this->performUpgrade(), 'The upgrade was completed successfully.');
  46. // Visit the front page to assert for PHP warning and errors.
  47. $this->drupalGet('');
  48. // Check that taxonomy_vocabulary_node_type and taxonomy_term_node have been
  49. // removed.
  50. $this->assertFalse(db_table_exists('taxonomy_vocabulary_node_type'), 'taxonomy_vocabulary_node_type has been removed.');
  51. $this->assertFalse(db_table_exists('taxonomy_term_node'), 'taxonomy_term_node has been removed.');
  52. // Check that taxonomy_index has not stored nids of unpublished nodes.
  53. $nids = db_query('SELECT nid from {node} WHERE status = :status', array(':status' => NODE_NOT_PUBLISHED))->fetchCol();
  54. $indexed_nids = db_query('SELECT DISTINCT nid from {taxonomy_index}')->fetchCol();
  55. $this->assertFalse(array_intersect($nids, $indexed_nids), 'No unpublished nid present in taxonomy_index');
  56. // Check that the node type 'page' has been associated to a taxonomy
  57. // reference field for each vocabulary.
  58. $voc_keys = array();
  59. foreach (taxonomy_get_vocabularies() as $vocab) {
  60. $voc_keys[] = $vocab->machine_name;
  61. }
  62. $instances = $this->instanceVocabularies('node', 'page');
  63. $inst_keys = array_keys($instances);
  64. sort($voc_keys);
  65. sort($inst_keys);
  66. $this->assertEqual($voc_keys, $inst_keys, 'Node type page has instances for every vocabulary.');
  67. // Ensure instance variables are getting through.
  68. foreach ($instances as $instance) {
  69. $this->assertTrue(isset($instance['required']), 'The required setting was preserved during the upgrade path.');
  70. $this->assertTrue($instance['description'], 'The description was preserved during the upgrade path');
  71. }
  72. // Node type 'story' was not explicitly in $vocabulary->nodes but
  73. // each node of type 'story' was associated to one or more terms.
  74. // Check that the node type 'story' has been associated only to
  75. // the taxonomyextra field.
  76. $instances = $this->instanceVocabularies('node', 'story');
  77. $field_names = array_flip($instances);
  78. $this->assertEqual(count($field_names), 1, 'Only one taxonomy term field instance exists for story nodes');
  79. $this->assertEqual(key($field_names), 'taxonomyextra', 'Only the excess taxonomy term field is used on story nodes');
  80. // Check that the node type 'poll' has been associated to no taxonomy
  81. // reference field.
  82. $instances = $this->instanceVocabularies('node', 'poll');
  83. $this->assertTrue(empty($instances), 'Node type poll has no taxonomy term reference field instances.');
  84. // Check that each node of type 'page' and 'story' is associated to all the
  85. // terms except terms whose ID is equal to the node ID or is equal to the
  86. // node ID subtracted from 49.
  87. $nodes = node_load_multiple(array(), array('type' => 'page'));
  88. $nodes += node_load_multiple(array(), array('type' => 'story'));
  89. $terms = db_select('taxonomy_term_data', 'td')
  90. ->fields('td')
  91. ->orderBy('vid')
  92. ->orderBy('tid')
  93. ->execute()
  94. ->fetchAllAssoc('tid');
  95. field_attach_prepare_view('node', $nodes, 'full');
  96. foreach ($nodes as $nid => $node) {
  97. $node->content = field_attach_view('node', $node, 'full');
  98. $render = drupal_render($node->content);
  99. $this->drupalSetContent($render);
  100. $this->verbose($render);
  101. $vocabulary_seen = array();
  102. foreach ($terms as $tid => $term) {
  103. // In our test database, each node is arbitrary associated with all
  104. // terms except two: one whose tid is ($nid) and one whose tid is
  105. // (49 - $nid).
  106. $should_be_displayed = ($tid != $nid) && ($tid + $nid != 49);
  107. // Only vocabularies 13 to 24 are properly associated with the node
  108. // type 'page'. All other node types are not associated with any
  109. // vocabulary, but still are associated with terms. Those terms
  110. // will be moved to the taxonomy extra field.
  111. if ($node->type == 'page' && $term->vid >= 13 && $term->vid <= 24) {
  112. $vocabulary = taxonomy_vocabulary_load($term->vid);
  113. $field_class = 'field-name-' . strtr('taxonomy_' . $vocabulary->machine_name, '_', '-');;
  114. }
  115. else {
  116. $field_class = 'field-name-taxonomyextra';
  117. }
  118. // Odd vocabularies are single, so any additional term will be moved
  119. // to the taxonomy extra field.
  120. if ($should_be_displayed) {
  121. if ($term->vid % 2 == 1 && !empty($vocabulary_seen[$term->vid])) {
  122. $field_class = 'field-name-taxonomyextra';
  123. }
  124. $vocabulary_seen[$term->vid] = TRUE;
  125. }
  126. $args = array(
  127. '%name' => $term->name,
  128. '@field' => $field_class,
  129. '%nid' => $nid,
  130. );
  131. // Use link rather than term name because migrated term names can be
  132. // substrings of other term names. e.g. "term 1 of vocabulary 2" is
  133. // found when "term 1 of vocabulary 20" is output.
  134. $term_path = url('taxonomy/term/' . $term->tid);
  135. if (!$should_be_displayed) {
  136. // Look for any link with the term path.
  137. $links = $this->xpath('//a[@href=:term_path]', array(':term_path' => $term_path));
  138. $this->assertFalse($links, format_string('Term %name (@field) is not displayed on node %nid', $args));
  139. }
  140. else {
  141. // Look for a link with the term path inside the correct field.
  142. // We search for "SPACE + class + SPACE" to avoid matching a substring
  143. // of the class.
  144. $links = $this->xpath('//div[contains(concat(" ", normalize-space(@class), " "), :field_class)]//a[@href=:term_path]', array(':field_class' => ' ' . $field_class . ' ', ':term_path' => $term_path));
  145. $this->assertTrue($links, format_string('Term %name (@field) is displayed on node %nid', $args));
  146. }
  147. }
  148. // nid 1, revision 1 had a bogus record in {term_node} pointing to term
  149. // ID 0. Make sure we ignored this instead of generating a bogus term.
  150. if ($node->nid == 1) {
  151. $link = l($term->name, 'taxonomy/term/0');
  152. $this->assertNoRaw($link, format_string('Bogus term (tid 0) is not displayed on node 1 vid %old_vid.', $args));
  153. }
  154. // The first 12 nodes have two revisions. For nodes with
  155. // revisions, check that the oldest revision is associated only
  156. // to terms whose ID is equal to the node ID or 49 less the node ID.
  157. $revisions = node_revision_list($node);
  158. if ($node->nid < 13) {
  159. $this->assertEqual(count($revisions), 2, format_string('Node %nid has two revisions.', $args));
  160. $last_rev = end($revisions);
  161. $args['%old_vid'] = $last_rev->vid;
  162. $node_old = node_load($node->nid, $last_rev->vid);
  163. field_attach_prepare_view('node', array($node_old->nid => $node_old), 'full');
  164. $node_old->content = field_attach_view('node', $node_old, 'full');
  165. $render = drupal_render($node_old->content);
  166. $this->drupalSetContent($render);
  167. $this->verbose($render);
  168. $term = $terms[$node->nid];
  169. $link = l($term->name, 'taxonomy/term/' . $term->tid);
  170. $this->assertRaw($link, format_string('Term %name (@field) is displayed on node %nid vid %old_vid.', $args));
  171. $term = $terms[49-$node->nid];
  172. $link = l($term->name, 'taxonomy/term/' . $term->tid);
  173. $this->assertRaw($link, format_string('Term %name (@field) is displayed on node %nid %old_vid.', $args));
  174. }
  175. else {
  176. $this->assertEqual(count($revisions), 1, format_string('Node %nid has one revision.', $args));
  177. }
  178. }
  179. }
  180. }