Line # | Frequency | Source Line |
1 | | <?php
|
2 | | // $Id: taxonomy.test,v 1.10 2008/04/04 14:19:33 rokZlender Exp $
|
3 | |
|
4 | | class TaxonomyVocabularyFunctionsTestCase extends DrupalTestCase {
|
5 | | function getInfo() {
|
6 | 1 | return array('name' => 'Vocabulary functions', 'description' => "Create/Edit/Delete a vocabulary and assert that all fields were properly saved" , 'group' => 'Taxonomy');
|
7 | | }
|
8 | |
|
9 | | function testVocabularyFunctions() {
|
10 | | //preparing data
|
11 | | $vid = 0;
|
12 | | $name = $this->randomName(200);
|
13 | | $description = $this->randomName(200);
|
14 | | $help = $this->randomName(200);
|
15 | | $hierarchy = rand(0,2); // Hierarchy 0,1,2
|
16 | | $multiple = rand(0,1); // multiple 0,1
|
17 | | $required = rand(0,1); // required 0,1
|
18 | | $relations = rand(0,1);
|
19 | | $tags = rand(0,1);
|
20 | | $weight = rand(-9,9);
|
21 | | $module = 'taxonomy';
|
22 | | $nodesList = array_keys(node_get_types());
|
23 | | $maxNodes = rand(1, count($nodesList));
|
24 | | $nodes = array();
|
25 | | for($i = 0; $i < $maxNodes; $i++) {
|
26 | | $nodes[$nodesList[$i]] = $nodesList[$i];
|
27 | | $nodesBak[$nodesList[$i]] = $nodesList[$i];
|
28 | | }
|
29 | | $_t = array('vid', 'name', 'description', 'help', 'relations', 'hierarchy', 'multiple',
|
30 | | 'required', 'tags', 'module', 'weight', 'nodes');
|
31 | | $edit = array();
|
32 | | foreach($_t as $key )
|
33 | | $edit[$key] = $$key;
|
34 | |
|
35 | | // exec save function
|
36 | | taxonomy_save_vocabulary($edit);
|
37 | | //after save we use $nodesBak
|
38 | | ksort($nodesBak);
|
39 | | $edit['nodes'] = $nodesBak;
|
40 | | $vocabularies = taxonomy_get_vocabularies();
|
41 | | foreach($vocabularies as $voc) {
|
42 | | if ($voc->name == $name) {
|
43 | | $vid = $voc->vid;
|
44 | | break;
|
45 | | }
|
46 | | }
|
47 | | $edit['vid'] = $vid;
|
48 | | // get data using function
|
49 | | $getEdit = taxonomy_vocabulary_load($vid);
|
50 | | foreach($getEdit as $key => $value ) {
|
51 | | $this->assertEqual($value, $edit[$key],"Checking value of $key");
|
52 | | }
|
53 | |
|
54 | | // delete vocabulary
|
55 | | // to avoid exception messages we create array with empty fields
|
56 | | $deleteArray = array();
|
57 | | foreach($getEdit as $key => $v)
|
58 | | $deleteArray[$key] = 0;
|
59 | | $deleteArray['vid'] = $vid;
|
60 | | taxonomy_save_vocabulary($deleteArray);
|
61 | | // checking if we deleted voc.
|
62 | | $vocabularies = taxonomy_get_vocabularies();
|
63 | | $vid = 0;
|
64 | | foreach($vocabularies as $voc) {
|
65 | | if ($voc->name == $name) {
|
66 | | $vid = $voc->vid;
|
67 | | break;
|
68 | | }
|
69 | | }
|
70 | | $this->assertEqual($vid, 0, "Deleted vocabulary ($vid)");
|
71 | |
|
72 | | }
|
73 | | }
|
74 | |
|
75 | |
|
76 | | class TaxonomyTermFunctionsTestCase extends DrupalTestCase {
|
77 | | function getInfo() {
|
78 | 1 | return array('name' => 'Term functions', 'description' => "Testing save/update/delete terms" , 'group' => 'Taxonomy');
|
79 | | }
|
80 | |
|
81 | | function testTermsFunctions() {
|
82 | | //preparing data
|
83 | | // vocabulary, hierarchy -> disabled, related terms = on;
|
84 | | $edit = array();
|
85 | | $_t = array('vid', 'name', 'description', 'help', 'relations', 'hierarchy', 'multiple',
|
86 | | 'required', 'tags', 'module', 'weight', 'nodes');
|
87 | | foreach($_t as $key ) {
|
88 | | $edit[$key] = 0;
|
89 | | }
|
90 | | $name = $this->randomName(20);
|
91 | | $relation = 1;
|
92 | | $edit['name'] = $name;
|
93 | | taxonomy_save_vocabulary($edit);
|
94 | |
|
95 | | // create term
|
96 | | $termname = $this->randomName(20);
|
97 | | $termdesc = $this->randomName(200);
|
98 | | $termweight = rand(-9, 9);
|
99 | | $randSyn = rand(0, 9);
|
100 | | $synonyms = array();
|
101 | | for($i = 0; $i < $randSyn; $i++) {
|
102 | | $synonyms[] = $this->randomName(20);
|
103 | | }
|
104 | | $termsyn = implode("\n", $synonyms);
|
105 | | $data = array('name' => $termname, 'description' => $termdesc, 'weight' => $termweight, 'synonyms' => $termsyn, 'vid' => $edit['vid'], 'tid' => 0, 'relations' => 0);
|
106 | | taxonomy_save_term($data);
|
107 | |
|
108 | | // retrieve term and check all fields
|
109 | | $_tArray = taxonomy_get_term_by_name($termname);
|
110 | | $getTerm = $_tArray[0];
|
111 | | $checkField = array('name', 'description', 'weight', 'vid');
|
112 | | foreach($checkField as $v) {
|
113 | | $this->assertEqual($data[$v], $getTerm->$v, "Checking value of the term ($v)");
|
114 | | }
|
115 | | $getSynonyms = taxonomy_get_synonyms($getTerm->tid);
|
116 | | $this->assertEqual(sort($synonyms), sort($getSynonyms), 'Checking synonyms');
|
117 | |
|
118 | | // creating related terms
|
119 | | $relations = array();
|
120 | | $staryTid = $getTerm->tid;
|
121 | | $relations[] = $staryTid;
|
122 | | $termname = $this->randomName(20);
|
123 | | $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => 0, 'vid' => $edit['vid'], 'tid' => 0, 'relations' => array($staryTid));
|
124 | | taxonomy_save_term($data);
|
125 | | $_tArray = taxonomy_get_term_by_name($termname);
|
126 | | $getTerm = $_tArray[0];
|
127 | | $relations[] = $getTerm->tid;
|
128 | |
|
129 | | // Creating another term related to 2 terms above;
|
130 | | $termname = $this->randomName(20);
|
131 | | $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => 0, 'vid' => $edit['vid'], 'tid' => 0, 'relations' => array($staryTid, $getTerm->tid));
|
132 | | taxonomy_save_term($data);
|
133 | | $_tArray = taxonomy_get_term_by_name($termname);
|
134 | | $getTerm = $_tArray[0];
|
135 | |
|
136 | | // check related terms
|
137 | | $related = taxonomy_get_related($getTerm->tid);
|
138 | | foreach($relations as $rTid) {
|
139 | | $this->assertTrue(array_key_exists($rTid, $related), "Checking relations ($rTid)");
|
140 | | }
|
141 | |
|
142 | | // delete vocabulary
|
143 | | $edit['name'] = 0;
|
144 | | taxonomy_save_vocabulary($edit);
|
145 | | }
|
146 | |
|
147 | | function testTermsFunctionsSingleHierarchy() {
|
148 | | //preparing data
|
149 | | // vocabulary hierarchy->single
|
150 | | $edit = array();
|
151 | | $_t = array('vid', 'name', 'description', 'help', 'relations', 'hierarchy', 'multiple',
|
152 | | 'required', 'tags', 'module', 'weight', 'nodes');
|
153 | | foreach($_t as $key ) {
|
154 | | $edit[$key] = 0;
|
155 | | }
|
156 | |
|
157 | | // create vocab
|
158 | | $name = $this->randomName(20);
|
159 | | $edit['hierarchy'] = 1;
|
160 | | $edit['name'] = $name;
|
161 | | taxonomy_save_vocabulary($edit);
|
162 | |
|
163 | | // create 1st term
|
164 | | $termname = $this->randomName(20);
|
165 | | $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $edit['vid'], 'tid' => 0, 'relations' => 0);
|
166 | | taxonomy_save_term($data);
|
167 | | $_tArray = taxonomy_get_term_by_name($termname);
|
168 | | $parent = $_tArray[0];
|
169 | |
|
170 | | // create 2nd term as a child
|
171 | | $termname = $this->randomName(20);
|
172 | | $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $edit['vid'], 'tid' => 0, 'relations' => 0, 'parent' => array($parent->tid));
|
173 | | taxonomy_save_term($data);
|
174 | | $_tArray = taxonomy_get_term_by_name($termname);
|
175 | | $children = $_tArray[0];
|
176 | |
|
177 | | // check hierarchy
|
178 | | $getChildren = taxonomy_get_children($parent->tid);
|
179 | | $getParent = taxonomy_get_parents($children->tid);
|
180 | | $this->assertEqual($parent,$getParent[$parent->tid], 'Checking parents');
|
181 | | $this->assertEqual($children,$getChildren[$children->tid], 'Checking children');
|
182 | |
|
183 | | // delete vocabulary
|
184 | | $edit['name'] = 0;
|
185 | | taxonomy_save_vocabulary($edit);
|
186 | | }
|
187 | |
|
188 | | function testTermsFunctionsMultipleHierarchy() {
|
189 | | //preparing data
|
190 | | // vocabulary hierarchy->single
|
191 | | $edit = array();
|
192 | | $_t = array('vid', 'name', 'description', 'help', 'relations', 'hierarchy', 'multiple',
|
193 | | 'required', 'tags', 'module', 'weight', 'nodes');
|
194 | | foreach($_t as $key )
|
195 | | $edit[$key] = 0;
|
196 | |
|
197 | | $name = $this->randomName(20);
|
198 | | $edit['hierarchy'] = 1;
|
199 | | $edit['name'] = $name;
|
200 | | taxonomy_save_vocabulary($edit);
|
201 | |
|
202 | | // create 1st term
|
203 | | $parent = array();
|
204 | | $termname = $this->randomName(20);
|
205 | | $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $edit['vid'], 'tid' => 0, 'relations' => 0);
|
206 | | taxonomy_save_term($data);
|
207 | | $_tArray = taxonomy_get_term_by_name($termname);
|
208 | | $parent[] = $_tArray[0]->tid;
|
209 | |
|
210 | | // create 2nd term
|
211 | | $termname = $this->randomName(20);
|
212 | | $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $edit['vid'], 'tid' => 0, 'relations' => 0);
|
213 | | taxonomy_save_term($data);
|
214 | | $_tArray = taxonomy_get_term_by_name($termname);
|
215 | | $parent[] = $_tArray[0]->tid;
|
216 | |
|
217 | | // create 3rd term as a child
|
218 | | $termname = $this->randomName(20);
|
219 | | $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $edit['vid'], 'tid' => 0, 'relations' => 0, 'parent' => array($parent));
|
220 | | taxonomy_save_term($data);
|
221 | | $_tArray = taxonomy_get_term_by_name($termname);
|
222 | | $children = $_tArray[0];
|
223 | |
|
224 | | $getParent = taxonomy_get_parents($children->tid);
|
225 | | foreach($parent as $p) {
|
226 | | $this->assertTrue(array_key_exists($p, $getParent), "Checking parents ($p)");
|
227 | | //$this->assertEqual($parent,$getParent[$parent->tid], 'Checking parents');
|
228 | | }
|
229 | |
|
230 | | // delete vocabulary
|
231 | | $edit['name'] = 0;
|
232 | | taxonomy_save_vocabulary($edit);
|
233 | | }
|
234 | |
|
235 | | }
|
236 | |
|
237 | | class TaxonomyTestNodeApiTestCase extends DrupalTestCase {
|
238 | | function getInfo() {
|
239 | 1 | return array('name' => 'Taxonomy nodeapi', 'description' => "Save & edit a node and assert that taxonomy terms are saved/loaded properly." , 'group' => 'Taxonomy');
|
240 | | }
|
241 | |
|
242 | | function testTaxonomyNode() {
|
243 | |
|
244 | | //preparing data
|
245 | | // vocabulary hierarchy->single, multiple -> on
|
246 | | $edit = array();
|
247 | | $_t = array('vid', 'name', 'description', 'help', 'relations', 'hierarchy', 'multiple',
|
248 | | 'required', 'tags', 'module', 'weight', 'nodes');
|
249 | | foreach($_t as $key) {
|
250 | | $edit[$key] = 0;
|
251 | | }
|
252 | |
|
253 | | $name = $this->randomName(20);
|
254 | | $edit['hierarchy'] = 1;
|
255 | | $edit['multiple'] = 1;
|
256 | | $edit['name'] = $name;
|
257 | | $edit['nodes'] = array('article' => 'article');
|
258 | | taxonomy_save_vocabulary($edit);
|
259 | | $vid = $edit['vid']; // we need to persist vid after $edit is unset()
|
260 | |
|
261 | | $parent = array();
|
262 | | $patternArray = array();
|
263 | |
|
264 | | // create 1st term
|
265 | | $termname = $this->randomName(20);
|
266 | | $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $vid, 'tid' => 0, 'relations' => 0);
|
267 | | taxonomy_save_term($data);
|
268 | | $_tArray = taxonomy_get_term_by_name($termname);
|
269 | | $parent[$_tArray[0]->tid] = $_tArray[0]->tid;
|
270 | | $patternArray['term name 1'] = $termname;
|
271 | |
|
272 | | // create 2nd term
|
273 | | $termname = $this->randomName(20);
|
274 | | $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $vid, 'tid' => 0, 'relations' => 0);
|
275 | | taxonomy_save_term($data);
|
276 | | $_tArray = taxonomy_get_term_by_name($termname);
|
277 | | $parent[$_tArray[0]->tid] = $_tArray[0]->tid;
|
278 | | $patternArray['term name 2'] = $termname;
|
279 | |
|
280 | | // create test user and login
|
281 | | $perm = array('access content', 'create article content', 'edit own article content', 'delete own article content');
|
282 | | $account = $this->drupalCreateUser($perm);
|
283 | | $this->drupalLogin($account);
|
284 | |
|
285 | | // why is this printing out the user profile page?
|
286 | | // go to node/add/article
|
287 | | // try to create article
|
288 | | $title = $this->randomName();
|
289 | | $body = $this->randomName(100);
|
290 | | $edit = array('title' => $title, 'body' => $body, "taxonomy[$vid][]" => $parent);
|
291 | |
|
292 | | $this->drupalPost('node/add/article', $edit, t('Save'));
|
293 | |
|
294 | | $patternArray['body text'] = $body;
|
295 | | $patternArray['title'] = $title;
|
296 | |
|
297 | | // $node = array2object(node_load(array('title' => $title)));
|
298 | | $node = node_load(array('title' => $title));
|
299 | |
|
300 | | $this->drupalGet("node/$node->nid");
|
301 | | foreach($patternArray as $name => $termPattern) {
|
302 | | $this->assertText($termPattern, "Checking $name");
|
303 | | }
|
304 | |
|
305 | | // checking database fields
|
306 | | $result = db_query('SELECT tid FROM {term_node} WHERE nid = %d', $node->nid);
|
307 | | while ($nodeRow = db_fetch_array($result)) {
|
308 | | $this->assertTrue(in_array($nodeRow['tid'], $parent), 'Checking database record');
|
309 | | }
|
310 | |
|
311 | | // ok, lets create new terms, and change this node
|
312 | | //pop array
|
313 | | array_pop($parent);
|
314 | |
|
315 | | // create 1st term
|
316 | | $termname = $this->randomName(20);
|
317 | | $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $vid, 'tid' => 0, 'relations' => 0);
|
318 | | taxonomy_save_term($data);
|
319 | | $_tArray = taxonomy_get_term_by_name($termname);
|
320 | | $parent[] = $_tArray[0]->tid;
|
321 | | $patternArray['term name 2'] = $termname;
|
322 | |
|
323 | | // create 2nd term
|
324 | | $termname = $this->randomName(20);
|
325 | | $data = array('name' => $termname, 'description' => '', 'weight' => 0, 'synonyms' => '', 'vid' => $vid, 'tid' => 0, 'relations' => 0);
|
326 | | taxonomy_save_term($data);
|
327 | | $_tArray = taxonomy_get_term_by_name($termname);
|
328 | | $parent[] = $_tArray[0]->tid;
|
329 | | $patternArray['term name 3'] = $termname;
|
330 | |
|
331 | | $edit = array('title' => $title, 'body' => $body, "taxonomy[$vid][]" => $parent);
|
332 | |
|
333 | | $this->drupalPost('node/'. $node->nid .'/edit', $edit, t('Save'));
|
334 | |
|
335 | | // TODO Do a MUCH better check here of the information msg
|
336 | | $patternArray['information message'] = 'been updated';
|
337 | | foreach($patternArray as $name => $termPattern) {
|
338 | | $this->assertText($termPattern, "Checking $name");
|
339 | | }
|
340 | |
|
341 | | // checking database fields
|
342 | | $node = node_load(array('title' => $title));
|
343 | | $result = db_query('SELECT tid FROM {term_node} WHERE vid = %d', $node->vid);
|
344 | | while ($nodeRow = db_fetch_array($result)) {
|
345 | | $this->assertTrue(in_array($nodeRow['tid'], $parent), 'Checking database field');
|
346 | | }
|
347 | |
|
348 | | // delete node through browser
|
349 | | $this->drupalPost('node/'. $node->nid .'/delete', array(), t('Delete'));
|
350 | | // checking after delete
|
351 | | $this->drupalGet("node/".$node->nid);
|
352 | | $this->assertNoText($termname, "Checking if node exists");
|
353 | | // checking database fields
|
354 | | $num_rows = db_result(db_query('SELECT COUNT(*) FROM {term_node} WHERE nid = %d', $node->nid));
|
355 | | $this->assertEqual($num_rows, 0, 'Checking database field after deletion');
|
356 | |
|
357 | | // delete vocabulary
|
358 | | // to avoid exception messages create array with empty fields
|
359 | | $edit = array();
|
360 | | foreach($_t as $key ) {
|
361 | | $edit[$key] = 0;
|
362 | | }
|
363 | | $edit['name'] = 0;
|
364 | | $edit['vid'] = $vid;
|
365 | | taxonomy_save_vocabulary($edit);
|
366 | | }
|
367 | |
|
368 | | }
|
369 | |
|
370 | |
|
371 | | ?>
|