Projet

Général

Profil

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

root / drupal7 / sites / all / modules / taxonomy_menu / taxonomy_menu.test @ 6d8023f2

1
<?php
2
/**
3
 * @file
4
 * Tests for taxonomy_menu.module.
5
 */
6

    
7
/**
8
 * Class with common helper methods.
9
 */
10
class TaxonomyMenuWebTestCase extends DrupalWebTestCase {
11

    
12
  /**
13
   * Returns a new vocabulary with random properties.
14
   */
15
  function createVocabulary() {
16
    // Create a vocabulary.
17
    $vocabulary = new stdClass();
18
    $vocabulary->name = $this->randomName();
19
    $vocabulary->description = $this->randomName();
20
    $vocabulary->machine_name = drupal_strtolower($this->randomName());
21
    $vocabulary->help = '';
22
    $vocabulary->nodes = array('article' => 'article');
23
    $vocabulary->weight = mt_rand(0, 10);
24
    taxonomy_vocabulary_save($vocabulary);
25
    return $vocabulary;
26
  }
27

    
28
  /**
29
   * Returns a new term with random properties in vocabulary $vid.
30
   */
31
  function createTerm($vocabulary) {
32
    $term = $this->createNewTerm($vocabulary);
33
    taxonomy_term_save($term);
34
    return $term;
35
  }
36

    
37
  /**
38
   * Get the first available text format.
39
   */
40
  function getTextFormat() {
41
    static $format = NULL;
42
    if ($format === NULL) {
43
      $format = db_query_range('SELECT format FROM {filter_format}', 0, 1)->fetchField();
44
    }
45
    return $format;
46
  }
47

    
48
  /**
49
   * Create a new term record.
50
   */
51
  function createNewTerm($vocabulary, $name = NULL, $parent = NULL) {
52
    $term = new stdClass();
53
    $term->name = ($name === NULL) ? $this->randomName() : $name;
54
    $term->description = $this->randomName();
55
    $term->format = $this->getTextFormat();
56
    $term->vid = $vocabulary->vid;
57
    if ($parent !== NULL) {
58
      $term->parent = array($parent->tid);
59
    }
60

    
61
    return $term;
62
  }
63
}
64

    
65
/**
66
 * Tests for taxonomy vocabulary functions.
67
 */
68
class TaxonomyMenuUnitTest extends TaxonomyMenuWebTestCase {
69

    
70
  public static function getInfo() {
71
    return array(
72
      'name' => 'CRUD',
73
      'description' => 'Test CRUD functions',
74
      'group' => 'Taxonomy menu',
75
    );
76
  }
77

    
78
  function setUp() {
79
    parent::setUp('taxonomy_menu');
80
    $admin_user = $this->drupalCreateUser(array('access administration pages', 'administer blocks', 'administer taxonomy', 'administer menu', 'administer nodes', 'create article content', 'create page content', 'edit any article content', 'edit any page content', 'delete any article content', 'delete any page content', 'access content overview'));
81
    $this->drupalLogin($admin_user);
82
    $this->vocabulary = $this->createVocabulary();
83
    $this->term = $this->createTerm($this->vocabulary);
84
  }
85

    
86
  function testTaxonomyMenuCRUD() {
87
    $edit = array();
88
    $edit['taxonomy_menu[vocab_parent]'] = 'main-menu:0';
89
    $this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/edit', $edit, 'Save');
90

    
91
    $this->assertResponse(200);
92

    
93
    $term = $this->term;
94
    $this->assertLink($term->name);
95

    
96
    $term = $this->term;
97
    taxonomy_term_delete($term->tid);
98
    $this->drupalGet('<front>');
99

    
100
    $this->assertNoLink($term->name);
101

    
102
    $node = $this->drupalCreateNode();
103
    $this->drupalPost("node/$node->nid/delete", array(), t('Delete'));
104
    $this->drupalGet("node/$node->nid");
105
    $this->assertResponse(404);
106

    
107
  }
108

    
109
  function testTaxonomyMenuCustomMenu() {
110
    // Create a custom menu.
111
    $edit = array();
112
    $custom_name = $this->randomName(16);
113
    $machine_name = drupal_substr(hash('sha256', $custom_name), 0, MENU_MAX_MENU_NAME_LENGTH_UI);
114
    $edit['title'] = $custom_name;
115
    $edit['menu_name'] = $machine_name;
116
    $this->drupalPost('admin/structure/menu/add', $edit, 'Save');
117

    
118
    // Move the menu block to a region.
119
    $edit = array();
120
    $edit['blocks[menu_menu-' . $machine_name . '][region]'] = 'sidebar_first';
121
    $this->drupalPost('admin/structure/block', $edit, 'Save blocks');
122
    $this->assertResponse(200);
123

    
124
    $edit = array();
125
    $edit['taxonomy_menu[vocab_parent]'] = 'menu-' . $machine_name . ':0';
126
    $this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/edit', $edit, 'Save');
127

    
128
    $term = $this->term;
129
    $this->drupalGet('<front>');
130
    $this->assertLink($term->name);
131

    
132
    $term = $this->term;
133
    taxonomy_term_delete($term->tid);
134
    $this->drupalGet('<front>');
135

    
136
    $this->assertNoLink($term->name);
137
  }
138
}
139

    
140
/**
141
 * Tests for features requiring a taxonomy hierarchy.
142
 */
143
class TaxonomyMenuHierarchyTest extends TaxonomyMenuWebTestCase {
144

    
145
  var $forest = array(
146
    "term1" => array(
147
      "term1_1" => TRUE,
148
      "term1_2" => array(
149
        "term1_2_1" => TRUE,
150
        "term1_2_2" => TRUE,
151
      ),
152
    ),
153
    "term2" => array(
154
      "term2_1" => TRUE,
155
      "term2_2" => TRUE,
156
    ),
157
    "term3" => TRUE
158
  );
159

    
160
  public static function getInfo() {
161
    return array(
162
      'name' => 'Hierarchy',
163
      'description' => 'Test functions related to taxonomy hierarchy',
164
      'group' => 'Taxonomy menu',
165
    );
166
  }
167

    
168
  function setUp() {
169
    parent::setUp('taxonomy_menu');
170
    $admin_user = $this->drupalCreateUser(array('access administration pages', 'create article content', 'administer blocks', 'administer taxonomy', 'administer menu'));
171
    $this->drupalLogin($admin_user);
172
    $this->vocabulary = $this->createVocabulary();
173
    $this->terms = array();
174
    foreach ($this->forest as $name => $children) {
175
      $this->terms[] = new TaxonomyMenuTreeNode($this, NULL, $name, $children);
176
    }
177
  }
178

    
179

    
180
  function testTaxonomyMenuHierarchy() {
181
    $edit = array();
182
    $edit['taxonomy_menu[vocab_parent]'] = 'navigation:0';
183
    $this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/edit', $edit, 'Save');
184

    
185
    $this->assertResponse(200);
186

    
187
    // By default, auto expand is on : we must find the whole hierarchy.
188
    foreach ($this->terms as $term)  {
189
      $this->assertLink($term->name); // 1st level
190
      foreach ($term->children as $child) {
191
        $this->assertLink($child->name); // 2nd level
192
        foreach ($child->children as $granchild) {
193
          $this->assertLink($granchild->name); // 3 level
194
          // No sub level.
195
        }
196
      }
197
    }
198

    
199
    // Set auto expand to off.
200
    $edit = array();
201
    $edit['taxonomy_menu[options][expanded]'] = FALSE;
202
    // $edit['taxonomy_menu[options][rebuild]'] = '1'; // Rebuild menu on submit.
203
    $this->drupalPost('admin/structure/taxonomy/' . $this->vocabulary->machine_name . '/edit', $edit, 'Save');
204
    $this->assertResponse(200);
205
    // $this->drupalGet('admin/structure/taxonomy/'.  $this->vocabulary->machine_name . '/edit');
206

    
207
    // We should have links to the first level of the hierarchy only.
208
    $this->drupalGet('<front>');
209
    foreach ($this->terms as $term)  {
210
      $this->assertLink($term->name);
211
      foreach ($term->children as $child) {
212
        $this->assertNoLink($child->name);
213
      }
214
    }
215

    
216
    // Move to term1_2 : we should have links to
217
    // - 1st level
218
    // - siblings of term1_2
219
    // - children of term1_2
220
    $this->clickLink("term1");
221
    $this->clickLink("term1_2");
222

    
223
    foreach ($this->terms as $term)  {
224
      $this->assertLink($term->name); // 1st level
225
      foreach ($term->children as $child) {
226
        // second level
227
        if ($term->name != "term1") {
228
          $this->assertNoLink($child->name);
229
        }
230
        else {
231
          // We must have a link AND the children.
232
          $this->assertLink($child->name);
233
          if ($child->name == "term1_2") {
234
            foreach ($child->children as $grandchild) {
235
              $this->assertLink($grandchild->name);
236
            }
237
          }
238
        }
239
      }
240
    }
241
  }
242
}
243

    
244
/**
245
 * Helper class to build the tree and keep data on hand.
246
 */
247
class TaxonomyMenuTreeNode {
248
  function __construct(&$testcase, $parent, $name, $children) {
249
    $this->name = $name;
250
    $this->children = array();
251
    $this->parent = $parent;
252

    
253
    $this->term = $testcase->createNewTerm($testcase->vocabulary, $name, $parent ?$parent->term : NULL);
254

    
255
    taxonomy_term_save($this->term);
256
    if (is_array($children)) {
257
      foreach ($children as $name => $grandchildren) {
258
        $this->children[$name] = new TaxonomyMenuTreeNode($testcase, $this, $name, $grandchildren);
259
      }
260
    }
261
  }
262

    
263
  function display($level = "") {
264
    foreach ($this->children as $child) {
265
      $child->display($level . "  ");
266
    }
267
  }
268
}