Projet

Général

Profil

Paste
Télécharger (8,93 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / simpletest / tests / upgrade / upgrade.taxonomy.test @ f7a2490e

1
<?php
2

    
3
/**
4
 * Test taxonomy upgrades.
5
 */
6
class UpgradePathTaxonomyTestCase extends UpgradePathTestCase {
7
  public static function getInfo() {
8
    return array(
9
      'name'  => 'Taxonomy upgrade path',
10
      'description'  => 'Taxonomy upgrade path tests.',
11
      'group' => 'Upgrade path',
12
    );
13
  }
14

    
15
  public function setUp() {
16
    // Path to the database dump.
17
    $this->databaseDumpFiles = array(
18
     drupal_get_path('module', 'simpletest') . '/tests/upgrade/drupal-6.filled.database.php',
19
    );
20
    parent::setUp();
21
  }
22

    
23
  /**
24
   * Retrieve an array mapping allowed vocabulary id to field name for
25
   * all taxonomy_term_reference fields for which an instance exists
26
   * for the specified entity type and bundle.
27
   */
28
  function instanceVocabularies($entity_type, $bundle) {
29
    $instances = array();
30
    foreach (field_info_instances($entity_type, $bundle) as $instance) {
31
      $field = field_info_field($instance['field_name']);
32
      if ($field['type'] == 'taxonomy_term_reference') {
33
        foreach ($field['settings']['allowed_values'] as $tree) {
34
          // Prefer valid taxonomy term reference fields for a given vocabulary
35
          // when they exist.
36
          if (empty($instances[$tree['vocabulary']]) || $instances[$tree['vocabulary']] == 'taxonomyextra') {
37
            $instances[$tree['vocabulary']] = $field['field_name'];
38
          }
39
        }
40
      }
41
    }
42
    return $instances;
43
  }
44

    
45
  /**
46
   * Basic tests for the taxonomy upgrade.
47
   */
48
  public function testTaxonomyUpgrade() {
49
    $this->assertTrue($this->performUpgrade(), 'The upgrade was completed successfully.');
50

    
51
    // Visit the front page to assert for PHP warning and errors.
52
    $this->drupalGet('');
53

    
54
    // Check that taxonomy_vocabulary_node_type and taxonomy_term_node have been
55
    // removed.
56
    $this->assertFalse(db_table_exists('taxonomy_vocabulary_node_type'), 'taxonomy_vocabulary_node_type has been removed.');
57
    $this->assertFalse(db_table_exists('taxonomy_term_node'), 'taxonomy_term_node has been removed.');
58

    
59
    // Check that taxonomy_index has not stored nids of unpublished nodes.
60
    $nids = db_query('SELECT nid from {node} WHERE status = :status', array(':status' => NODE_NOT_PUBLISHED))->fetchCol();
61
    $indexed_nids = db_query('SELECT DISTINCT nid from {taxonomy_index}')->fetchCol();
62
    $this->assertFalse(array_intersect($nids, $indexed_nids), 'No unpublished nid present in taxonomy_index');
63

    
64
    // Check that the node type 'page' has been associated to a taxonomy
65
    // reference field for each vocabulary.
66
    $voc_keys = array();
67
    foreach (taxonomy_get_vocabularies() as $vocab) {
68
      $voc_keys[] = $vocab->machine_name;
69
    }
70
    $instances = $this->instanceVocabularies('node', 'page');
71
    $inst_keys = array_keys($instances);
72
    sort($voc_keys);
73
    sort($inst_keys);
74
    $this->assertEqual($voc_keys, $inst_keys, 'Node type page has instances for every vocabulary.');
75

    
76
    // Ensure instance variables are getting through.
77
    foreach ($instances as $instance) {
78
      $this->assertTrue(isset($instance['required']), 'The required setting was preserved during the upgrade path.');
79
      $this->assertTrue($instance['description'], 'The description was preserved during the upgrade path');
80
    }
81

    
82
    // Node type 'story' was not explicitly in $vocabulary->nodes but
83
    // each node of type 'story' was associated to one or more terms.
84
    // Check that the node type 'story' has been associated only to
85
    // the taxonomyextra field.
86
    $instances = $this->instanceVocabularies('node', 'story');
87
    $field_names = array_flip($instances);
88
    $this->assertEqual(count($field_names), 1, 'Only one taxonomy term field instance exists for story nodes');
89
    $this->assertEqual(key($field_names), 'taxonomyextra', 'Only the excess taxonomy term field is used on story nodes');
90

    
91
    // Check that the node type 'poll' has been associated to no taxonomy
92
    // reference field.
93
    $instances = $this->instanceVocabularies('node', 'poll');
94
    $this->assertTrue(empty($instances), 'Node type poll has no taxonomy term reference field instances.');
95

    
96
    // Check that each node of type 'page' and 'story' is associated to all the
97
    // terms except terms whose ID is equal to the node ID or is equal to the
98
    // node ID subtracted from 49.
99
    $nodes = node_load_multiple(array(), array('type' => 'page'));
100
    $nodes += node_load_multiple(array(), array('type' => 'story'));
101
    $terms = db_select('taxonomy_term_data', 'td')
102
      ->fields('td')
103
      ->orderBy('vid')
104
      ->orderBy('tid')
105
      ->execute()
106
      ->fetchAllAssoc('tid');
107
    field_attach_prepare_view('node', $nodes, 'full');
108
    foreach ($nodes as $nid => $node) {
109
      $node->content = field_attach_view('node', $node, 'full');
110
      $render = drupal_render($node->content);
111
      $this->drupalSetContent($render);
112
      $this->verbose($render);
113

    
114
      $vocabulary_seen = array();
115
      foreach ($terms as $tid => $term) {
116
        // In our test database, each node is arbitrary associated with all
117
        // terms except two: one whose tid is ($nid) and one whose tid is
118
        // (49 - $nid).
119
        $should_be_displayed = ($tid != $nid) && ($tid + $nid != 49);
120

    
121
        // Only vocabularies 13 to 24 are properly associated with the node
122
        // type 'page'. All other node types are not associated with any
123
        // vocabulary, but still are associated with terms. Those terms
124
        // will be moved to the taxonomy extra field.
125
        if ($node->type == 'page' && $term->vid >= 13 && $term->vid <= 24) {
126
          $vocabulary = taxonomy_vocabulary_load($term->vid);
127
          $field_class = 'field-name-' . strtr('taxonomy_' . $vocabulary->machine_name, '_', '-');;
128
        }
129
        else {
130
          $field_class = 'field-name-taxonomyextra';
131
        }
132

    
133
        // Odd vocabularies are single, so any additional term will be moved
134
        // to the taxonomy extra field.
135
        if ($should_be_displayed) {
136
          if ($term->vid % 2 == 1 && !empty($vocabulary_seen[$term->vid])) {
137
            $field_class = 'field-name-taxonomyextra';
138
          }
139
          $vocabulary_seen[$term->vid] = TRUE;
140
        }
141

    
142
        $args = array(
143
          '%name' => $term->name,
144
          '@field' => $field_class,
145
          '%nid' => $nid,
146
        );
147

    
148
        // Use link rather than term name because migrated term names can be
149
        // substrings of other term names. e.g. "term 1 of vocabulary 2" is
150
        // found when "term 1 of vocabulary 20" is output.
151
        $term_path = url('taxonomy/term/' . $term->tid);
152
        if (!$should_be_displayed) {
153
          // Look for any link with the term path.
154
          $links = $this->xpath('//a[@href=:term_path]', array(':term_path' => $term_path));
155
          $this->assertFalse($links, format_string('Term %name (@field) is not displayed on node %nid', $args));
156
        }
157
        else {
158
          // Look for a link with the term path inside the correct field.
159
          // We search for "SPACE + class + SPACE" to avoid matching a substring
160
          // of the class.
161
          $links = $this->xpath('//div[contains(concat(" ", normalize-space(@class), " "), :field_class)]//a[@href=:term_path]', array(':field_class' => ' ' . $field_class . ' ', ':term_path' => $term_path));
162
          $this->assertTrue($links, format_string('Term %name (@field) is displayed on node %nid', $args));
163
        }
164
      }
165

    
166
      // nid 1, revision 1 had a bogus record in {term_node} pointing to term
167
      // ID 0. Make sure we ignored this instead of generating a bogus term.
168
      if ($node->nid == 1) {
169
        $link = l($term->name, 'taxonomy/term/0');
170
        $this->assertNoRaw($link, format_string('Bogus term (tid 0) is not displayed on node 1 vid %old_vid.', $args));
171
      }
172

    
173
      // The first 12 nodes have two revisions. For nodes with
174
      // revisions, check that the oldest revision is associated only
175
      // to terms whose ID is equal to the node ID or 49 less the node ID.
176
      $revisions = node_revision_list($node);
177
      if ($node->nid < 13) {
178
        $this->assertEqual(count($revisions), 2, format_string('Node %nid has two revisions.', $args));
179
        $last_rev = end($revisions);
180
        $args['%old_vid'] = $last_rev->vid;
181

    
182
        $node_old = node_load($node->nid, $last_rev->vid);
183
        field_attach_prepare_view('node', array($node_old->nid => $node_old), 'full');
184
        $node_old->content = field_attach_view('node', $node_old, 'full');
185
        $render = drupal_render($node_old->content);
186
        $this->drupalSetContent($render);
187
        $this->verbose($render);
188

    
189
        $term = $terms[$node->nid];
190
        $link = l($term->name, 'taxonomy/term/' . $term->tid);
191
        $this->assertRaw($link, format_string('Term %name (@field) is displayed on node %nid vid %old_vid.', $args));
192
        $term = $terms[49-$node->nid];
193
        $link = l($term->name, 'taxonomy/term/' . $term->tid);
194
        $this->assertRaw($link, format_string('Term %name (@field) is displayed on node %nid %old_vid.', $args));
195
      }
196
      else {
197
        $this->assertEqual(count($revisions), 1, format_string('Node %nid has one revision.', $args));
198
      }
199
    }
200
  }
201
}