upgrade.taxonomy.test 9.0 KB

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