upgrade.taxonomy.test 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. if ($this->skipUpgradeTest) {
  46. return;
  47. }
  48. $this->assertTrue($this->performUpgrade(), 'The upgrade was completed successfully.');
  49. // Visit the front page to assert for PHP warning and errors.
  50. $this->drupalGet('');
  51. // Check that taxonomy_vocabulary_node_type and taxonomy_term_node have been
  52. // removed.
  53. $this->assertFalse(db_table_exists('taxonomy_vocabulary_node_type'), 'taxonomy_vocabulary_node_type has been removed.');
  54. $this->assertFalse(db_table_exists('taxonomy_term_node'), 'taxonomy_term_node has been removed.');
  55. // Check that taxonomy_index has not stored nids of unpublished nodes.
  56. $nids = db_query('SELECT nid from {node} WHERE status = :status', array(':status' => NODE_NOT_PUBLISHED))->fetchCol();
  57. $indexed_nids = db_query('SELECT DISTINCT nid from {taxonomy_index}')->fetchCol();
  58. $this->assertFalse(array_intersect($nids, $indexed_nids), 'No unpublished nid present in taxonomy_index');
  59. // Check that the node type 'page' has been associated to a taxonomy
  60. // reference field for each vocabulary.
  61. $voc_keys = array();
  62. foreach (taxonomy_get_vocabularies() as $vocab) {
  63. $voc_keys[] = $vocab->machine_name;
  64. }
  65. $instances = $this->instanceVocabularies('node', 'page');
  66. $inst_keys = array_keys($instances);
  67. sort($voc_keys);
  68. sort($inst_keys);
  69. $this->assertEqual($voc_keys, $inst_keys, 'Node type page has instances for every vocabulary.');
  70. // Ensure instance variables are getting through.
  71. foreach (array_unique($instances) as $instance) {
  72. $field_instance = field_info_instance('node', $instance, 'page');
  73. $this->assertTrue(isset($field_instance['required']), 'The required setting was preserved during the upgrade path.');
  74. $this->assertTrue($field_instance['description'], 'The description was preserved during the upgrade path');
  75. }
  76. // Node type 'story' was not explicitly in $vocabulary->nodes but
  77. // each node of type 'story' was associated to one or more terms.
  78. // Check that the node type 'story' has been associated only to
  79. // the taxonomyextra field.
  80. $instances = $this->instanceVocabularies('node', 'story');
  81. $field_names = array_flip($instances);
  82. $this->assertEqual(count($field_names), 1, 'Only one taxonomy term field instance exists for story nodes');
  83. $this->assertEqual(key($field_names), 'taxonomyextra', 'Only the excess taxonomy term field is used on story nodes');
  84. // Check that the node type 'poll' has been associated to no taxonomy
  85. // reference field.
  86. $instances = $this->instanceVocabularies('node', 'poll');
  87. $this->assertTrue(empty($instances), 'Node type poll has no taxonomy term reference field instances.');
  88. // Check that each node of type 'page' and 'story' is associated to all the
  89. // terms except terms whose ID is equal to the node ID or is equal to the
  90. // node ID subtracted from 49.
  91. $nodes = node_load_multiple(array(), array('type' => 'page'));
  92. $nodes += node_load_multiple(array(), array('type' => 'story'));
  93. $terms = db_select('taxonomy_term_data', 'td')
  94. ->fields('td')
  95. ->orderBy('vid')
  96. ->orderBy('tid')
  97. ->execute()
  98. ->fetchAllAssoc('tid');
  99. field_attach_prepare_view('node', $nodes, 'full');
  100. foreach ($nodes as $nid => $node) {
  101. $node->content = field_attach_view('node', $node, 'full');
  102. $render = drupal_render($node->content);
  103. $this->drupalSetContent($render);
  104. $this->verbose($render);
  105. $vocabulary_seen = array();
  106. foreach ($terms as $tid => $term) {
  107. // In our test database, each node is arbitrary associated with all
  108. // terms except two: one whose tid is ($nid) and one whose tid is
  109. // (49 - $nid).
  110. $should_be_displayed = ($tid != $nid) && ($tid + $nid != 49);
  111. // Only vocabularies 13 to 24 are properly associated with the node
  112. // type 'page'. All other node types are not associated with any
  113. // vocabulary, but still are associated with terms. Those terms
  114. // will be moved to the taxonomy extra field.
  115. if ($node->type == 'page' && $term->vid >= 13 && $term->vid <= 24) {
  116. $vocabulary = taxonomy_vocabulary_load($term->vid);
  117. $field_class = 'field-name-' . strtr('taxonomy_' . $vocabulary->machine_name, '_', '-');;
  118. }
  119. else {
  120. $field_class = 'field-name-taxonomyextra';
  121. }
  122. // Odd vocabularies are single, so any additional term will be moved
  123. // to the taxonomy extra field.
  124. if ($should_be_displayed) {
  125. if ($term->vid % 2 == 1 && !empty($vocabulary_seen[$term->vid])) {
  126. $field_class = 'field-name-taxonomyextra';
  127. }
  128. $vocabulary_seen[$term->vid] = TRUE;
  129. }
  130. $args = array(
  131. '%name' => $term->name,
  132. '@field' => $field_class,
  133. '%nid' => $nid,
  134. );
  135. // Use link rather than term name because migrated term names can be
  136. // substrings of other term names. e.g. "term 1 of vocabulary 2" is
  137. // found when "term 1 of vocabulary 20" is output.
  138. $term_path = url('taxonomy/term/' . $term->tid);
  139. if (!$should_be_displayed) {
  140. // Look for any link with the term path.
  141. $links = $this->xpath('//a[@href=:term_path]', array(':term_path' => $term_path));
  142. $this->assertFalse($links, format_string('Term %name (@field) is not displayed on node %nid', $args));
  143. }
  144. else {
  145. // Look for a link with the term path inside the correct field.
  146. // We search for "SPACE + class + SPACE" to avoid matching a substring
  147. // of the class.
  148. $links = $this->xpath('//div[contains(concat(" ", normalize-space(@class), " "), :field_class)]//a[@href=:term_path]', array(':field_class' => ' ' . $field_class . ' ', ':term_path' => $term_path));
  149. $this->assertTrue($links, format_string('Term %name (@field) is displayed on node %nid', $args));
  150. }
  151. }
  152. // nid 1, revision 1 had a bogus record in {term_node} pointing to term
  153. // ID 0. Make sure we ignored this instead of generating a bogus term.
  154. if ($node->nid == 1) {
  155. $link = l($term->name, 'taxonomy/term/0');
  156. $this->assertNoRaw($link, format_string('Bogus term (tid 0) is not displayed on node 1 vid %old_vid.', $args));
  157. }
  158. // The first 12 nodes have two revisions. For nodes with
  159. // revisions, check that the oldest revision is associated only
  160. // to terms whose ID is equal to the node ID or 49 less the node ID.
  161. $revisions = node_revision_list($node);
  162. if ($node->nid < 13) {
  163. $this->assertEqual(count($revisions), 2, format_string('Node %nid has two revisions.', $args));
  164. $last_rev = end($revisions);
  165. $args['%old_vid'] = $last_rev->vid;
  166. $node_old = node_load($node->nid, $last_rev->vid);
  167. field_attach_prepare_view('node', array($node_old->nid => $node_old), 'full');
  168. $node_old->content = field_attach_view('node', $node_old, 'full');
  169. $render = drupal_render($node_old->content);
  170. $this->drupalSetContent($render);
  171. $this->verbose($render);
  172. $term = $terms[$node->nid];
  173. $link = l($term->name, 'taxonomy/term/' . $term->tid);
  174. $this->assertRaw($link, format_string('Term %name (@field) is displayed on node %nid vid %old_vid.', $args));
  175. $term = $terms[49-$node->nid];
  176. $link = l($term->name, 'taxonomy/term/' . $term->tid);
  177. $this->assertRaw($link, format_string('Term %name (@field) is displayed on node %nid %old_vid.', $args));
  178. }
  179. else {
  180. $this->assertEqual(count($revisions), 1, format_string('Node %nid has one revision.', $args));
  181. }
  182. }
  183. }
  184. }