taxonomy.test 13.3 KB
Newer Older
1
2
3
<?php
// $Id$

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
 * @file
 * Tests for Taxonomy module.
 */

 /**
  * Tests for the taxonomy vocabulary interface.
  */
class TaxonomyVocabularyFunctionalTest extends DrupalWebTestCase {

  function getInfo() {
    return array(
      'name' => t('Taxonomy vocabulary interface'),
      'description' => t('Test the taxonomy vocabulary interface.'),
      'group' => t('Taxonomy'),
    );
  }

  function setUp() {
    parent::setUp();
    $this->admin_user = $this->drupalCreateUser(array('administer taxonomy'));
  }

27
  /**
28
   * Create, edit and delete a vocabulary via the user interface.
29
   */
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  function testVocabularyInterface() {
    // Visit the main taxonomy administration page.
    $this->drupalLogin($this->admin_user);
    $this->drupalGet('admin/content/taxonomy');

    // Create a new vocabulary.
    $this->clickLink(t('Add vocabulary'));
    $edit = array();
    $edit['name'] = $this->randomName();
    $edit['description'] = $this->randomName();
    $edit['help'] = $this->randomName();
    $edit['nodes[article]'] = 'article';
    $edit['tags'] = 1;
    $edit['multiple'] = 1;
    $edit['required'] = 1;
    $edit['weight'] = 0;
    $this->drupalPost(NULL, $edit, t('Save'));
    $this->assertRaw(t('Created new vocabulary %name.', array('%name' => $edit['name']), t('Vocabulary created successfully')));

    // Edit the vocabulary.
    $this->drupalGet('admin/content/taxonomy');
    $this->assertText($edit['name'], t('Vocabulary found in the vocabulary overview listing.'));
    $this->clickLink(t('edit vocabulary'));
    $edit = array();
    $edit['name'] = $this->randomName();
    $this->drupalPost(NULL, $edit, t('Save'));
    $this->assertRaw(t('Updated vocabulary %name.', array('%name' => $edit['name'])));
    $this->drupalGet('admin/content/taxonomy');
    $this->assertText($edit['name'], t('Vocabulary found in the vocabulary overview listing.'));
  }
}


/**
 * Tests for taxonomy vocabulary functions.
 */
class TaxonomyVocabularyUnitTest extends DrupalWebTestCase {

function getInfo() {
69
     return array(
70
71
       'name' => t('Taxonomy vocabularies'),
       'description' => t('Test loading, saving and deleting vocabularies.'),
72
73
74
75
76
77
       'group' => t('Taxonomy'),
     );
   }

  function setUp() {
    parent::setUp('taxonomy');
78
    $admin_user = $this->drupalCreateUser(array('create article content', 'administer taxonomy'));
79
    $this->drupalLogin($admin_user);
80
81
82
83
84
85
86
87
    // Create a new vocabulary.
    $vocabulary = new stdClass();
    $vocabulary->name = $this->randomName();
    $vocabulary->description = $this->randomName();
    $vocabulary->help = '';
    $vocabulary->weight = 0;
    taxonomy_vocabulary_save($vocabulary);
    $this->vocabulary = $vocabulary;
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
  }

  /**
   * Ensure that when an invalid vocabulary vid is loaded, it is possible
   * to load the same vid successfully if it subsequently becomes valid.
   */
  function testTaxonomyVocabularyLoadReturnFalse() {
    // Load a vocabulary that doesn't exist.
    $vocabularies = taxonomy_get_vocabularies();
    $vid = count($vocabularies) + 1;
    $vocabulary = taxonomy_vocabulary_load($vid);
    // This should not return an object because no such vocabulary exists.
    $this->assertTrue(!is_object($vocabulary), t('No object loaded.'));

    // Create a new vocabulary.
103
104
105
106
107
108
    $vocabulary = new stdClass();
    $vocabulary->name = $this->randomName();
    $vocabulary->description = $this->randomName();
    $vocabulary->help = '';
    $vocabulary->weight = 0;
    taxonomy_vocabulary_save($vocabulary);
109
110
111
112
113
114
115

    // Load the vocabulary with the same $vid from earlier.
    // This should return a vocabulary object since it now matches a real vid.
    $vocabulary = taxonomy_vocabulary_load($vid);
    $this->assertTrue(is_object($vocabulary), t('Vocabulary is an object'));
    $this->assertTrue($vocabulary->vid == $vid, t('Valid vocabulary vid is the same as our previously invalid one.'));
  }
116
117
118
119
120

  /**
   * Ensure that the vocabulary static reset works correctly.
   */
  function testTaxonomyVocabularyLoadStaticReset() {
121
122
123
    $original_vocabulary = taxonomy_vocabulary_load($this->vocabulary->vid);
    $this->assertTrue(is_object($original_vocabulary), t('Vocabulary loaded successfully'));
    $this->assertEqual($this->vocabulary->name, $original_vocabulary->name, t('Vocabulary loaded successfully'));
124
125

    // Change the name and description.
126
127
128
129
    $vocabulary = $original_vocabulary;
    $vocabulary->name = $this->randomName();
    $vocabulary->description = $this->randomName();
    taxonomy_vocabulary_save($vocabulary);
130
131
132

    // Load the vocabulary with $reset TRUE.
    $new_vocabulary = taxonomy_vocabulary_load($original_vocabulary->vid, TRUE);
133
134
    $this->assertEqual($new_vocabulary->name, $vocabulary->name);
    $this->assertEqual($new_vocabulary->name, $vocabulary->name);
135
136
137
138
139

    // Delete the vocabulary.
    taxonomy_vocabulary_delete($this->vocabulary->vid);
    $vocabularies = taxonomy_get_vocabularies();
    $this->assertTrue(!isset($vocabularies[$this->vocabulary->vid]), t('The vocabulary was deleted'));
140
  }
141
142
}

143
144
145
146
147
/**
 * Tests for taxonomy term functions.
 */
class TaxonomyTermTestCase extends DrupalWebTestCase {

148
149
  function getInfo() {
    return array(
150
151
      'name' => t('Taxonomy term functions and forms.'),
      'description' => t('Test load, save and delete for taxonomy terms.'),
152
153
154
155
      'group' => t('Taxonomy')
    );
  }

156
157
  function setUp() {
    parent::setUp('taxonomy');
158
    $this->admin_user = $this->drupalCreateUser(array('administer taxonomy', 'bypass node access'));
159

160
161
162
163
164
165
    // Create a vocabulary.
    $vocabulary = new stdClass();
    $vocabulary->name = $this->randomName();
    $vocabulary->description = $this->randomName();
    $vocabulary->help = '';
    $vocabulary->nodes = array('article' => 'article');
166
    taxonomy_vocabulary_save($vocabulary);
167
    $this->vocabulary = $vocabulary;
168
169
170
  }

  /**
171
   * Test related terms.
172
   */
173
174
175
176
177
178
179
180
181
182
183
184
185
  function testTaxonomyTermRelations() {
    // Create two taxonomy terms.
    $term1 = new stdClass();
    $term1->name = $this->randomName();
    $term1->vid = $this->vocabulary->vid;
    $term2 = new stdClass();
    $term2->name = $this->randomName();
    $term2->vid = $this->vocabulary->vid;
    taxonomy_term_save($term1);
    taxonomy_term_save($term2);

    // Edit $term1 and add $term2 as a relationship.
    $this->drupalLogin($this->admin_user);
186
    $edit = array();
187
188
    $edit['relations[]'] = $term2->tid;
    $this->drupalPost('taxonomy/term/' . $term1->tid . '/edit', $edit, t('Save'));
189

190
191
    $related = taxonomy_get_related($term1->tid);
    $this->assertTrue(isset($related[$term2->tid]), t('Related term was found'));
192
  }
193
194

  /**
195
   * Test synonyms.
196
   */
197
198
199
200
201
202
203
  function testTaxonomySynonyms() {
    // Create a taxonomy term with two synonyms.
    $synonym = $this->randomName();
    $term = new stdClass();
    $term->name = $this->randomName();
    $term->vid = $this->vocabulary->vid;
    $term->synonyms = $synonym;
204
    taxonomy_term_save($term);
205

206
207
208
209
    // Fetch the synonyms.
    $synonyms = taxonomy_get_synonyms($term->tid);
    $count = count($synonyms);
    $this->assertEqual($count, 1, t('@count synonyms were found.', array('@count' => $count)));
210

211
212
213
    // Fetch the term using the synonyms.
    $returned_term = taxonomy_get_synonym_root($synonyms[0]);
    $this->assertEqual($term->tid, $returned_term->tid, t('Term ID returned correctly'));
214
  }
215
216

  /**
217
   * Test terms in a single and multiple hierarchy.
218
   */
219
220
221
222
223
224
225
226
227
228
229
230
231
  function testTaxonomyTermHierarchy() {
    // Create two taxonomy terms.
    $term1 = new stdClass();
    $term1->name = $this->randomName();
    $term1->vid = $this->vocabulary->vid;
    $term2 = new stdClass();
    $term2->name = $this->randomName();
    $term2->vid = $this->vocabulary->vid;
    taxonomy_term_save($term1);
    taxonomy_term_save($term2);

    // Edit $term2, setting $term1 as parent.
    $this->drupalLogin($this->admin_user);
232
    $edit = array();
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
    $edit['parent[]'] = $term1->tid;
    $this->drupalPost('taxonomy/term/' . $term2->tid . '/edit', $edit, t('Save'));

    // Check the hierarchy.
    $children = taxonomy_get_children($term1->tid);
    $parents = taxonomy_get_parents($term2->tid);
    $this->assertTrue(isset($children[$term2->tid]), t('Child found correctly.'));
    $this->assertTrue(isset($parents[$term1->tid]), t('Parent found correctly.'));

    // Create a third term and save this as a parent of term2.
    $term3 = new stdClass();
    $term3->name = $this->randomName();
    $term3->vid = $this->vocabulary->vid;
    taxonomy_term_save($term3);
    $term2->parent = array($term1->tid, $term3->tid);
    taxonomy_term_save($term2);
    $parents = taxonomy_get_parents($term2->tid);
    $this->assertTrue(isset($parents[$term1->tid]) && isset($parents[$term3->tid]), t('Both parents found successfully.'));
251
252
  }

253
  /**
254
   * Test that hook_nodeapi_$op implementations work correctly.
255
256
257
258
259
   */
  /*
   * Save & edit a node and assert that taxonomy terms are saved/loaded properly.
   */
  function testTaxonomyNode() {
260
261
262
263
264
265
266
267
268
269
270
    $term1 = new stdClass();
    $term1->name = $this->randomName();
    $term1->vid = $this->vocabulary->vid;
    $term2 = new stdClass();
    $term2->name = $this->randomName();
    $term2->vid = $this->vocabulary->vid;
    taxonomy_term_save($term1);
    taxonomy_term_save($term2);

    $this->drupalLogin($this->admin_user);
    // Post an article.
271
    $edit = array();
272
273
274
    $edit['title'] = $this->randomName();
    $edit['body'] = $this->randomName();
    $edit['taxonomy[' . $this->vocabulary->vid . ']'] = $term1->tid;
275
276
    $this->drupalPost('node/add/article', $edit, t('Save'));

277
278
279
280
    // Check that the term is displayed when the node is viewed.
    $node = node_load(array('title' => $edit['title']));
    $this->drupalGet('node/' . $node->nid);
    $this->assertText($term1->name, t('Term is displayed when viewing the node.'));
281

282
283
    // Edit the node with a different term.
    $edit['taxonomy[' . $this->vocabulary->vid . ']'] = $term2->tid;
284
    $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
285

286
287
    $this->drupalGet('node/' . $node->nid);
    $this->assertText($term2->name, t('Term is displayed when viewing the node.'));
288
289

    // Delete node through browser.
290
    $this->drupalPost('node/' . $node->nid . '/delete', array(), t('Delete'));
291
292
    $this->drupalGet('node/' . $node->nid);
    $this->assertNoText($term2->name, t('Checking if node exists'));
293
    // Checking database fields.
294
295
    $result = db_query('SELECT * FROM {term_node} WHERE nid = :nid', array(':nid' => $node->nid))->fetch();
    $this->assertTrue(empty($result), t('Term/node relationships are no longer in the database table.'));
296
297
  }

298
299
300
301
  /**
   * Test term creation with a free-tagging vocabulary from the node form.
   */
  function testNodeTermCreation() {
302
303
304
    // Enable tags in the vocabulary.
    $this->vocabulary->tags = 1;
    taxonomy_vocabulary_save($this->vocabulary);
305
306
307
308
309
    $terms = array(
      $this->randomName(),
      $this->randomName(),
      $this->randomName(),
    );
310
    $this->drupalLogin($this->admin_user);
311
312
313
314
    $edit = array();
    $edit['title'] = $this->randomName();
    // Insert the terms in a comma separated list. Vocabulary 1 is a
    // free-tagging field created by the default profile.
315
    $edit['taxonomy[tags][' . $this->vocabulary->vid .']'] =  implode(', ', $terms);
316
317
318
319
320
321
322
323
    $edit['body'] = $this->randomName();
    $this->drupalPost('node/add/article', $edit, t('Save'));
    $this->assertRaw(t('@type %title has been created.', array('@type' => t('Article'), '%title' => $edit['title'])), t('The node was created successfully'));
    foreach ($terms as $term) {
      $this->assertText($term, t('The term was saved and appears on the node page'));
    }
  }

324
325
326
327
  /**
   * Save and edit a term and assert that the name and description are correct.
   */
  function testTermEdit() {
328
    $this->drupalLogin($this->admin_user);
329
330
331
332
    $edit = array(
      'name' => $this->randomName(12),
      'description' => $this->randomName(100),
    );
333
    // Explicitly set the parents field to 'root', to ensure that
334
335
    // taxonomy_form_term_submit() handles the invalid term ID correctly.
    $edit['parent[]'] = 0;
336

337
338
    // Create the term to edit.
    $this->drupalPost('admin/content/taxonomy/' . $this->vocabulary->vid . '/add', $edit, t('Save'));
339
340
341
342
343

    $term = taxonomy_get_term_by_name($edit['name']);
    $this->assertNotNull($term, t('Term found in database'));

    // Submitting a term takes us to the add page; we need the List page.
344
    $this->drupalGet('admin/content/taxonomy/' . $this->vocabulary->vid . '/list');
345
346
347
348
349
350
351

    // Test edit link as accessed from Taxonomy administration pages.
    // Because Simpletest creates its own database when running tests, we know
    // the first edit link found on the listing page is to our term.
    $this->clickLink(t('edit'));

    // This failed inexplicably with assertText, so used assertRaw. @TODO: Why?
352
    $this->assertText($edit['name'], t('The randomly generated term name is present.'));
353
354
355
356
357
358
359
360
    $this->assertText($edit['description'], t('The randomly generated term description is present.'));

    $edit = array(
      'name' => $this->randomName(14),
      'description' => $this->randomName(102),
    );

    // Edit the term.
361
    $this->drupalPost('taxonomy/term/' . $term[0]->tid . '/edit', $edit, t('Save'));
362
363

    // View the term and check that it is correct.
364
    $this->drupalGet('taxonomy/term/' . $term[0]->tid);
365
366
367
    $this->assertText($edit['name'], t('The randomly generated term name is present.'));
    $this->assertText($edit['description'], t('The randomly generated term description is present.'));
  }
368
}