taxonomy.test 18 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
// $Id$

class TaxonomyVocabularyFunctionsTestCase extends DrupalWebTestCase {
  /**
   * Implementation of getInfo().
   */
  function getInfo() {
    return array(
      'name' => t('Vocabulary functions'),
      'description' => t('Create/Edit/Delete vocabulary and assert that all fields were properly saved.'),
      'group' => t('Taxonomy')
    );
  }

  /**
   * Create/Edit/Delete vocabulary and assert that all fields were properly saved.
   */
  function testVocabularyFunctions() {
    //preparing data
    $vid = 0;
    $name = $this->randomName(200);
    $description = $this->randomName(200);
    $help = $this->randomName(200);
    $hierarchy = rand(0,2); // Hierarchy 0,1,2
    $multiple = rand(0,1); // multiple 0,1
    $required = rand(0,1); // required 0,1
    $relations = rand(0,1);
    $tags = rand(0,1);
    $weight = rand(-9,9);
    $module = 'taxonomy';
    $nodesList = array_keys(node_get_types());
    $maxNodes = rand(1, count($nodesList));
    $nodes = array();
    for($i = 0; $i < $maxNodes; $i++) {
      $nodes[$nodesList[$i]] = $nodesList[$i];
      $nodesBak[$nodesList[$i]] = $nodesList[$i];
    }
    $_t = array('vid', 'name', 'description', 'help', 'relations', 'hierarchy', 'multiple',
      'required', 'tags', 'module', 'weight', 'nodes');
    $edit = array();
    foreach($_t as $key)
      $edit[$key] = $$key;

    // Exec save function.
    taxonomy_save_vocabulary($edit);
    // After save we use $nodesBak.
    ksort($nodesBak);
    $edit['nodes'] = $nodesBak;
    $vocabularies = taxonomy_get_vocabularies();
    foreach($vocabularies as $voc) {
      if ($voc->name == $name) {
        $vid = $voc->vid;
        break;
      }
    }
    $edit['vid'] = $vid;
    // Get data using function.
    $getEdit = taxonomy_vocabulary_load($vid);
    foreach($getEdit as $key => $value ) {
      $this->assertEqual($value, $edit[$key], t('Checking value of @key.', array('@key' => $key)));
    }

    // Delete vocabulary to avoid exception messages we create array with empty fields.
    $deleteArray = array();
    foreach($getEdit as $key => $v) {
      $deleteArray[$key] = 0;
    }
    $deleteArray['vid'] = $vid;
    taxonomy_save_vocabulary($deleteArray);
    // Checking if we deleted voc.
    $vocabularies = taxonomy_get_vocabularies();
    $vid = 0;
    foreach($vocabularies as $voc) {
      if ($voc->name == $name) {
        $vid = $voc->vid;
        break;
      }
    }
    $this->assertEqual($vid, 0, t('Deleted vocabulary (@vid)', array('@vid' => $vid)));
  }
}


class TaxonomyTermFunctionsTestCase extends DrupalWebTestCase {
  /**
   * Implementation of getInfo().
   */
  function getInfo() {
    return array(
91
92
      'name' => t('Term no hierarchy'),
      'description' => t('Testing save/update/delete terms without a hierarchy.'),
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
      'group' => t('Taxonomy')
    );
  }

  /**
   * Test term related functions.
   */
  function testTermsFunctions() {
    // Preparing data: vocabulary, hierarchy -> disabled, related terms = on.
    $edit = array();
    $_t = array('vid', 'name', 'description', 'help', 'relations', 'hierarchy', 'multiple',
        'required', 'tags', 'module', 'weight', 'nodes');
    foreach($_t as $key ) {
      $edit[$key] = 0;
    }
    $name = $this->randomName(20);
    $relation = 1;
    $edit['name'] = $name;
    taxonomy_save_vocabulary($edit);

    // Create term.
    $termname = $this->randomName(20);
    $termdesc = $this->randomName(200);
    $termweight = rand(-9, 9);
    $randSyn = rand(0, 9);
    $synonyms = array();
    for($i = 0; $i < $randSyn; $i++) {
      $synonyms[] = $this->randomName(20);
    }
    $termsyn = implode("\n", $synonyms);
    $data = array('name' => $termname, 'description' => $termdesc, 'weight' => $termweight, 'synonyms' => $termsyn, 'vid' => $edit['vid'], 'tid' => 0, 'relations' => 0);
    taxonomy_save_term($data);

    // Retrieve term and check all fields.
    $_tArray = taxonomy_get_term_by_name($termname);
    $getTerm = $_tArray[0];
    $checkField = array('name', 'description', 'weight', 'vid');
    foreach($checkField as $v) {
      $this->assertEqual($data[$v], $getTerm->$v, t('Checking value of the term (@v).', array('@v' => $v)));
    }
    $getSynonyms = taxonomy_get_synonyms($getTerm->tid);
    $this->assertEqual(sort($synonyms), sort($getSynonyms), 'Checking synonyms');

    // Creating related terms.
    $relations = array();
    $staryTid = $getTerm->tid;
    $relations[] = $staryTid;
    $termname = $this->randomName(20);
    $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => 0, 'vid' => $edit['vid'], 'tid' => 0, 'relations' => array($staryTid));
    taxonomy_save_term($data);
    $_tArray = taxonomy_get_term_by_name($termname);
    $getTerm = $_tArray[0];
    $relations[] = $getTerm->tid;

    // Creating another term related to 2 terms above.
    $termname = $this->randomName(20);
    $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => 0, 'vid' => $edit['vid'], 'tid' => 0, 'relations' => array($staryTid, $getTerm->tid));
    taxonomy_save_term($data);
    $_tArray = taxonomy_get_term_by_name($termname);
    $getTerm = $_tArray[0];

    // Check related terms.
    $related = taxonomy_get_related($getTerm->tid);
    foreach($relations as $rTid) {
      $this->assertTrue(array_key_exists($rTid, $related), t('Checking relations (@rTid).', array('@rTid' => $rTid)));
    }

    // Delete vocabulary.
    $edit['name'] = 0;
    taxonomy_save_vocabulary($edit);
  }
164
165
166
167
168
169
170
171
172
173
174
175
176
177
}

class TaxonomyTermSingleTestCase extends DrupalWebTestCase {

  /**
   * Implementation of getInfo().
   */
  function getInfo() {
    return array(
      'name' => t('Term single hierarchy'),
      'description' => t('Testing save/update/delete terms in a single hierarchy.'),
      'group' => t('Taxonomy')
    );
  }
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220

  /**
   * Test single hierarchy terms.
   */
  function testTermsFunctionsSingleHierarchy() {
    // Preparing data: vocabulary hierarchy->single.
    $edit = array();
    $_t = array('vid', 'name', 'description', 'help', 'relations', 'hierarchy', 'multiple',
        'required', 'tags', 'module', 'weight', 'nodes');
    foreach($_t as $key ) {
      $edit[$key] = 0;
    }

    // Create vocab.
    $name = $this->randomName(20);
    $edit['hierarchy'] = 1;
    $edit['name'] = $name;
    taxonomy_save_vocabulary($edit);

    // Create 1st term.
    $termname = $this->randomName(20);
    $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $edit['vid'], 'tid' => 0, 'relations' => 0);
    taxonomy_save_term($data);
    $_tArray = taxonomy_get_term_by_name($termname);
    $parent = $_tArray[0];

    // Create 2nd term as a child.
    $termname = $this->randomName(20);
    $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $edit['vid'], 'tid' => 0, 'relations' => 0, 'parent' => array($parent->tid));
    taxonomy_save_term($data);
    $_tArray = taxonomy_get_term_by_name($termname);
    $children = $_tArray[0];

    // Check hierarchy.
    $getChildren = taxonomy_get_children($parent->tid);
    $getParent = taxonomy_get_parents($children->tid);
    $this->assertEqual($parent,$getParent[$parent->tid], t('Checking parents.'));
    $this->assertEqual($children,$getChildren[$children->tid], t('Checking children.'));

    // Delete vocabulary.
    $edit['name'] = 0;
    taxonomy_save_vocabulary($edit);
  }
221
222
223
224
225
226
227
228
229
230
231
232
233
234
}

class TaxonomyTermMultipleTestCase extends DrupalWebTestCase {

  /**
   * Implementation of getInfo().
   */
  function getInfo() {
    return array(
      'name' => t('Term multiple hierarchy'),
      'description' => t('Testing save/update/delete terms in a multiple hierarchy.'),
      'group' => t('Taxonomy')
    );
  }
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286

  /**
   * Test multiple hierarchy terms.
   */
  function testTermsFunctionsMultipleHierarchy() {
    // Preparing data: vocabulary hierarchy->single.
    $edit = array();
    $_t = array('vid', 'name', 'description', 'help', 'relations', 'hierarchy', 'multiple',
        'required', 'tags', 'module', 'weight', 'nodes');
    foreach($_t as $key )
      $edit[$key] = 0;

    $name = $this->randomName(20);
    $edit['hierarchy'] = 1;
    $edit['name'] = $name;
    taxonomy_save_vocabulary($edit);

    // Create 1st term.
    $parent = array();
    $termname = $this->randomName(20);
    $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $edit['vid'], 'tid' => 0, 'relations' => 0);
    taxonomy_save_term($data);
    $_tArray = taxonomy_get_term_by_name($termname);
    $parent[] = $_tArray[0]->tid;

    // Create 2nd term.
    $termname = $this->randomName(20);
    $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $edit['vid'], 'tid' => 0, 'relations' => 0);
    taxonomy_save_term($data);
    $_tArray = taxonomy_get_term_by_name($termname);
    $parent[] = $_tArray[0]->tid;

    // Create 3rd term as a child.
    $termname = $this->randomName(20);
    $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $edit['vid'], 'tid' => 0, 'relations' => 0, 'parent' => array($parent));
    taxonomy_save_term($data);
    $_tArray = taxonomy_get_term_by_name($termname);
    $children = $_tArray[0];

    $getParent = taxonomy_get_parents($children->tid);
    foreach($parent as $p) {
      $this->assertTrue(array_key_exists($p, $getParent), t('Checking parents (@p)', array('@p' => $p)));
      //$this->assertEqual($parent,$getParent[$parent->tid], 'Checking parents');
    }

    // Delete vocabulary.
    $edit['name'] = 0;
    taxonomy_save_vocabulary($edit);
  }

}

287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
class TaxonomyAdminTestCase extends DrupalWebTestCase {
  /**
   * Implementation of getInfo().
   */
  function getInfo() {
    return array(
      'name' => t('Administration interface'),
      'description' => t('Test the vocabulary administration interface.'),
      'group' => t('Taxonomy'),
    );
  }

  /**
   * Implementation of setUp().
   */
  function setUp() {
    parent::setUp();
    $this->taxonomy_admin = $this->drupalCreateUser(array('administer taxonomy'));
  }

  /**
   * Visit the various admin pages for the default 'Tags' vocabulary.
   */
  function testTaxonomyAdminPages() {
    $this->drupalLogin($this->taxonomy_admin);
    $this->drupalGet('admin/content/taxonomy');
    $this->assertResponse('200');
    $this->assertText(t('Article'), t('Article vocabulary found.'));
    $this->clickLink(t('edit vocabulary'));
    $this->assertResponse('200');
    $this->drupalGet('admin/content/taxonomy');
    $this->clickLink(t('list terms'));
    $this->assertResponse('200');
    $this->clickLink(t('Add term'));
    $this->assertResponse('200');
  }
}

325
326
327
328
329
330
331
332
class TaxonomyTestNodeApiTestCase extends DrupalWebTestCase {
  /**
   * Implementation of getInfo().
   */
  function getInfo() {
    return array(
      'name' => t('Taxonomy nodeapi'),
      'description' => t('Save & edit a node and assert that taxonomy terms are saved/loaded properly.'),
333
      'group' => t('Taxonomy'),
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
    );
  }

  /*
   * Save & edit a node and assert that taxonomy terms are saved/loaded properly.
   */
  function testTaxonomyNode() {
    // Preparing data: vocabulary hierarchy->single, multiple -> on.
    $edit = array();
    $_t = array('vid', 'name', 'description', 'help', 'relations', 'hierarchy', 'multiple',
        'required', 'tags', 'module', 'weight', 'nodes');
    foreach($_t as $key) {
      $edit[$key] = 0;
    }

    $name = $this->randomName(20);
    $edit['hierarchy'] = 1;
    $edit['multiple'] = 1;
    $edit['name'] = $name;
    $edit['nodes'] = array('article' => 'article');
    taxonomy_save_vocabulary($edit);
    $vid = $edit['vid']; // We need to persist vid after $edit is unset().

    $parent = array();
    $patternArray = array();

    // Create 1st term.
    $termname = $this->randomName(20);
    $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $vid, 'tid' => 0, 'relations' => 0);
    taxonomy_save_term($data);
    $_tArray = taxonomy_get_term_by_name($termname);
    $parent[$_tArray[0]->tid] = $_tArray[0]->tid;
    $patternArray['term name 1'] = $termname;

    // Create 2nd term.
    $termname = $this->randomName(20);
    $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $vid, 'tid' => 0, 'relations' => 0);
    taxonomy_save_term($data);
    $_tArray = taxonomy_get_term_by_name($termname);
    $parent[$_tArray[0]->tid] = $_tArray[0]->tid;
    $patternArray['term name 2'] = $termname;

    // Create test user and login.
    $perm = array('access content', 'create article content', 'edit own article content', 'delete own article content');
    $account = $this->drupalCreateUser($perm);
    $this->drupalLogin($account);

    // Why is this printing out the user profile page?
    // Go to node/add/article.
    // Try to create article.
    $title = $this->randomName();
    $body = $this->randomName(100);
    $edit = array('title' => $title, 'body' => $body, "taxonomy[$vid][]" => $parent);

    $this->drupalPost('node/add/article', $edit, t('Save'));

    $patternArray['body text'] = $body;
    $patternArray['title'] = $title;

    $node = node_load(array('title' => $title));

    $this->drupalGet("node/$node->nid");
    foreach($patternArray as $name => $termPattern) {
      $this->assertText($termPattern, "Checking $name");
    }

    // Checking database fields.
    $result = db_query('SELECT tid FROM {term_node} WHERE nid = %d', $node->nid);
    while ($nodeRow = db_fetch_array($result)) {
      $this->assertTrue(in_array($nodeRow['tid'], $parent), 'Checking database record');
    }

    // Ok, lets create new terms, and change this node.
    array_pop($parent);

    // create 1st term
    $termname = $this->randomName(20);
    $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $vid, 'tid' => 0, 'relations' => 0);
    taxonomy_save_term($data);
    $_tArray = taxonomy_get_term_by_name($termname);
    $parent[] = $_tArray[0]->tid;
    $patternArray['term name 2'] = $termname;

    // create 2nd term
    $termname = $this->randomName(20);
    $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $vid, 'tid' => 0, 'relations' => 0);
    taxonomy_save_term($data);
    $_tArray = taxonomy_get_term_by_name($termname);
    $parent[] = $_tArray[0]->tid;
    $patternArray['term name 3'] = $termname;

    $edit = array('title' => $title, 'body' => $body, "taxonomy[$vid][]" => $parent);

427
    $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442

    // TODO Do a MUCH better check here of the information msg.
    $patternArray['information message'] = 'been updated';
    foreach($patternArray as $name => $termPattern) {
      $this->assertText($termPattern, t('Checking @name.', array('@name' => $name)));
    }

    // Checking database fields.
    $node = node_load(array('title' => $title));
    $result = db_query('SELECT tid FROM {term_node} WHERE vid = %d', $node->vid);
    while ($nodeRow = db_fetch_array($result)) {
      $this->assertTrue(in_array($nodeRow['tid'], $parent), t('Checking database field.'));
    }

    // Delete node through browser.
443
    $this->drupalPost('node/' . $node->nid . '/delete', array(), t('Delete'));
444
    // Checking after delete.
445
    $this->drupalGet("node/" . $node->nid);
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
    $this->assertNoText($termname, t('Checking if node exists'));
    // Checking database fields.
    $num_rows = db_result(db_query('SELECT COUNT(*) FROM {term_node} WHERE nid = %d', $node->nid));
    $this->assertEqual($num_rows, 0, t('Checking database field after deletion'));

    // Delete vocabulary to avoid exception messages create array with empty fields.
    $edit = array();
    foreach($_t as $key ) {
      $edit[$key] = 0;
    }
    $edit['name'] = 0;
    $edit['vid'] = $vid;
    taxonomy_save_vocabulary($edit);
  }
}
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528

/**
 * Test term edit form to ensure terms can be edited from administration page
 * and that term name and description are saved.
 */
class TermEditTestCase extends DrupalWebTestCase {
  /**
   * Implementation of getInfo().
   */
  function getInfo() {
    return array(
      'name'  => 'Term edit test',
      'description'  => t('Ensure terms can be edited from administration page
       and that term name and description are saved.'),
      'group' => t('Taxonomy'));
  }
  
  /**
   * Implementation of setUp().
   */
  function setUp() {
    parent::setUp();
    // Prepare a user to do the tests.
    $web_user = $this->drupalCreateUser(array('administer taxonomy'));
    $this->drupalLogin($web_user);
  }

  /**
   * Save and edit a term and assert that the name and description are correct.
   */
  function testTermEdit() {
    $edit = array(
      'name' => $this->randomName(12),
      'description' => $this->randomName(100),
    );

    // Create the term to edit (adding to the default 'Tags' vocabulary).
    $this->drupalPost('admin/content/taxonomy/1/add/term', $edit, t('Save'));

    $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.
    $this->clickLink(t('List'));

    // 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?
    $this->assertRaw($edit['name'], t('The randomly generated term name is present.'));
    $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.
    $this->drupalPost('admin/content/taxonomy/edit/term/'. $term[0]->tid, $edit, t('Save'));

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