upgrade.taxonomy.test 8.5 KB

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