Projet

Général

Profil

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

root / drupal7 / sites / all / modules / taxonomy_menu / taxonomy_menu.test @ 87dbc3bf

1
<?php
2

    
3
/**
4
 * @file
5
 * Tests for taxonomy_menu.module.
6
 */
7

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

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

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

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

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

    
62
    return $term;
63
  }
64
}
65

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

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

    
79
  function setUp() {
80
    parent::setUp('taxonomy_menu');
81
    $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'));
82
    $this->drupalLogin($admin_user);
83
    $this->vocabulary = $this->createVocabulary();
84
    $this->term = $this->createTerm($this->vocabulary);
85
  }
86

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

    
92
    $this->assertResponse(200);
93

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

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

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

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

    
108
  }
109

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

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

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

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

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

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

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

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

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

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

    
180

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

    
186
    $this->assertResponse(200);
187

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

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

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

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

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

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

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

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

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