Fixing content type ordering when content_type is not defined.
[akelos.git] / test / unit / lib / AkActiveRecord / AkActsAsNestedSet.php
blobe3eecae654bb8e2a5f36e6486179f2fa558cd24c
1 <?php
3 defined('AK_TEST_DATABASE_ON') ? null : define('AK_TEST_DATABASE_ON', true);
4 require_once(dirname(__FILE__).'/../../../fixtures/config/config.php');
6 if(!defined('AK_ACTIVE_RECORD_PROTECT_GET_RECURSION')){
7 define('AK_ACTIVE_RECORD_PROTECT_GET_RECURSION', false);
10 class test_AkActiveRecord_actsAsNestedSet extends AkUnitTest
13 function test_start()
15 $this->installAndIncludeModels(array(
16 'NestedCategory'=>'id,lft int,rgt int,parent_id,description,department string(25)'
17 ));
20 function Test_of_actsAsNestedSet_instatiation()
22 $Categories =& new NestedCategory();
23 $this->assertEqual($Categories->actsLike(), 'active record,nested set');
25 $this->assertEqual($Categories->nested_set->_parent_column_name,'parent_id');
26 $this->assertEqual($Categories->nested_set->_left_column_name,'lft');
27 $this->assertEqual($Categories->nested_set->_right_column_name,'rgt');
29 $Categories =& new NestedCategory();
31 $this->assertErrorPattern('/columns are required/',$Categories->actsAs('nested_set', array('parent_column'=>'not_available')));
33 $this->assertEqual($Categories->actsLike(), 'active record');
37 function Test_of_Test_of_init()
39 $Categories =& new NestedCategory();
40 $Categories->nested_set->init(array('scope'=>array('category_id = ? AND completed = 0',$Categories->getId()),'custom_attribute'=>'This is not allowed here'));
42 $this->assertEqual($Categories->nested_set->getScopeCondition(), array ( 0 => 'category_id = ? AND completed = 0', 1 => null));
43 $this->assertTrue(empty($Categories->nested_set->custom_attribute));
47 function Test_of__ensureIsActiveRecordInstance()
49 $Categories =& new NestedCategory();
50 $Object =& new AkObject();
51 $this->assertErrorPattern('/is not an active record/',$Categories->nested_set->_ensureIsActiveRecordInstance(&$Object));
54 function Test_of_getType()
56 $Categories =& new NestedCategory();
57 $this->assertEqual($Categories->nested_set->getType(), 'nested set');
61 function Test_of_getScopeCondition_and_setScopeCondition()
63 $Categories =& new NestedCategory();
64 $this->assertEqual($Categories->nested_set->getScopeCondition(), ($Categories->_db->type() == 'postgre') ? 'true' : '1');
65 $Categories->nested_set->setScopeCondition('true');
66 $this->assertEqual($Categories->nested_set->getScopeCondition(), 'true');
69 function Test_of_getters_and_setters()
71 $Categories =& new NestedCategory();
73 $Categories->nested_set->setLeftColumnName('column_name');
74 $this->assertEqual($Categories->nested_set->getLeftColumnName(), 'column_name');
76 $Categories->nested_set->setRightColumnName('column_name');
77 $this->assertEqual($Categories->nested_set->getRightColumnName(), 'column_name');
79 $Categories->nested_set->setParentColumnName('column_name');
80 $this->assertEqual($Categories->nested_set->getParentColumnName(), 'column_name');
84 /**/
86 // New tests for Better Nested Set implementation
88 function getLocation($Location)
90 if(is_array($Location)){
91 return array_values($this->Location->collect($Location,'id','name'));
92 }else{
93 return $Location->get('name');
97 function test_include_locations()
99 $this->installAndIncludeModels(array('Location'));
100 $this->Location =& new Location();
104 function test_getRoot()
106 $this->Europe =& $this->Location->create('name->','Europe');
108 $this->assertEqual('Europe',$this->getLocation($this->Location->nested_set->getRoot()));
109 $this->assertTrue($this->Europe->nested_set->isRoot());
111 $this->Spain =& $this->Location->create('name->','Spain');
113 $this->Europe->nested_set->addChild($this->Spain);
115 $this->assertFalse($this->Spain->nested_set->isRoot());
117 $this->assertEqual('Europe',$this->getLocation($this->Spain->nested_set->getRoot()));
120 function test_getRoots()
122 $this->Oceania =& $this->Location->create('name->','Oceania');
123 $Roots = $this->Oceania->nested_set->getRoots();
125 $this->assertEqual('Europe',$Roots[0]->name);
126 $this->assertEqual('Oceania',$Roots[1]->name);
128 $this->Australia =& $this->Location->create('name->','Australia');
129 $this->Oceania->nested_set->addChild($this->Australia);
131 $Roots = $this->Oceania->nested_set->getRoots();
133 $this->assertEqual('Europe',$Roots[0]->name);
134 $this->assertEqual('Oceania',$Roots[1]->name);
138 function test_getAncestors()
140 $this->Valencia =& $this->Location->create('name->','Valencia');
141 $this->Spain->nested_set->addChild($this->Valencia);
143 $this->Carlet =& $this->Location->create('name->','Carlet');
144 $this->Valencia->nested_set->addChild($this->Carlet);
147 $this->assertEqual(array('Europe','Spain','Valencia'), $this->getLocation($this->Carlet->nested_set->getAncestors()));
148 $this->assertEqual(array('Europe'), $this->getLocation($this->Spain->nested_set->getAncestors()));
152 function test_getSelfAndAncestors()
154 $this->assertEqual(array('Europe','Spain','Valencia','Carlet'), array_values($this->Location->collect($this->Carlet->nested_set->getSelfAndAncestors(),'id','name')));
156 $this->assertEqual(array('Europe','Spain'), array_values($this->Location->collect($this->Spain->nested_set->getSelfAndAncestors(),'id','name')));
160 function test_getSiblings()
162 $this->Gandia =& $this->Location->create('name->','Gandia');
163 $this->Alcudia =& $this->Location->create('name->','Alcudia');
164 $this->Daimus =& $this->Location->create('name->','Daimus');
166 $this->Valencia->nested_set->addChild($this->Gandia);
167 $this->Valencia->nested_set->addChild($this->Alcudia);
168 $this->Valencia->nested_set->addChild($this->Daimus);
170 $this->assertEqual(array('Gandia','Alcudia','Daimus'), array_values($this->Location->collect($this->Carlet->nested_set->getSiblings(),'id','name')));
172 $this->Barcelona =& $this->Location->create('name->','Barcelona');
173 $this->Spain->nested_set->addChild($this->Barcelona);
175 $this->assertEqual(array('Valencia'), array_values($this->Location->collect($this->Barcelona->nested_set->getSiblings(),'id','name')));
180 function test_getSelfAndSiblings()
182 $this->assertEqual(array('Carlet','Gandia','Alcudia','Daimus'), $this->getLocation($this->Carlet->nested_set->getSelfAndSiblings()));
184 $this->assertEqual(array('Carlet','Gandia','Alcudia','Daimus'), $this->getLocation($this->Alcudia->nested_set->getSelfAndSiblings()));
186 $this->assertEqual(array('Valencia','Barcelona'),$this->getLocation($this->Barcelona->nested_set->getSelfAndSiblings()));
191 function test_getLevel()
193 $this->assertIdentical(0,$this->Europe->nested_set->getLevel());
194 $this->assertIdentical(0,$this->Oceania->nested_set->getLevel());
195 $this->assertIdentical(1,$this->Spain->nested_set->getLevel());
196 $this->assertIdentical(2,$this->Barcelona->nested_set->getLevel());
197 $this->assertIdentical(3,$this->Carlet->nested_set->getLevel());
200 function test_countChildren()
202 $this->Europe->reload();
203 $this->Oceania->reload();
204 $this->Spain->reload();
205 $this->Barcelona->reload();
206 $this->Valencia->reload();
208 $this->assertIdentical(7,$this->Europe->nested_set->countChildren());
209 $this->assertIdentical(1,$this->Oceania->nested_set->countChildren());
210 $this->assertIdentical(6,$this->Spain->nested_set->countChildren());
211 $this->assertIdentical(0,$this->Barcelona->nested_set->countChildren());
212 $this->assertIdentical(4,$this->Valencia->nested_set->countChildren());
215 function test_getAllChildren()
217 $this->assertEqual(array('Carlet','Gandia','Alcudia','Daimus'), $this->getLocation($this->Valencia->nested_set->getAllChildren()));
218 $this->assertEqual(array('Valencia','Carlet','Gandia','Alcudia','Daimus','Barcelona'), $this->getLocation($this->Spain->nested_set->getAllChildren()));
219 $this->assertEqual(array('Spain','Valencia','Carlet','Gandia','Alcudia','Daimus','Barcelona'), $this->getLocation($this->Europe->nested_set->getAllChildren()));
223 function test_getAllChildren_excuding_some()
225 $this->assertEqual(array('Spain','Barcelona'), $this->getLocation($this->Europe->nested_set->getAllChildren($this->Valencia)));
226 $this->assertEqual(array('Spain','Barcelona'), $this->getLocation($this->Europe->nested_set->getAllChildren($this->Valencia->id)));
227 $this->assertEqual(array('Spain','Barcelona'), $this->getLocation($this->Europe->nested_set->getAllChildren(array($this->Valencia))));
228 $this->assertEqual(array('Spain','Barcelona'), $this->getLocation($this->Europe->nested_set->getAllChildren(array($this->Valencia->id))));
229 $this->assertEqual(array('Alcudia','Daimus'), $this->getLocation($this->Valencia->nested_set->getAllChildren($this->Carlet,$this->Gandia)));
230 $this->assertEqual(array('Alcudia','Daimus'), $this->getLocation($this->Valencia->nested_set->getAllChildren($this->Carlet->id,$this->Gandia->id)));
231 $this->assertEqual(array('Alcudia','Daimus'), $this->getLocation($this->Valencia->nested_set->getAllChildren(array($this->Carlet,$this->Gandia))));
232 $this->assertEqual(array('Alcudia','Daimus'), $this->getLocation($this->Valencia->nested_set->getAllChildren(array($this->Carlet->id,$this->Gandia->id))));
233 $this->assertEqual(array('Alcudia','Daimus'), $this->getLocation($this->Valencia->nested_set->getAllChildren(array($this->Carlet->id,$this->Gandia))));
237 function test_getFullSet()
239 $this->assertEqual(array('Europe','Spain','Barcelona'), $this->getLocation($this->Europe->nested_set->getFullSet($this->Valencia)));
240 $this->assertEqual(array('Valencia','Carlet','Gandia','Alcudia','Daimus'), $this->getLocation($this->Valencia->nested_set->getFullSet()));
243 function test_moveToLeftOf()
245 $this->Alcudia->nested_set->moveToLeftOf($this->Gandia);
246 $this->assertEqual(array('Carlet','Alcudia','Gandia','Daimus'), $this->getLocation($this->Valencia->nested_set->getAllChildren()));
248 $this->Carlet->reload();
249 $this->Spain->reload();
250 $this->Valencia->reload();
252 $this->Carlet->nested_set->moveToLeftOf($this->Spain->id);
253 $this->assertEqual(array('Alcudia','Gandia','Daimus'), $this->getLocation($this->Valencia->nested_set->getAllChildren()));
254 $this->assertEqual(array('Carlet'), $this->getLocation($this->Spain->nested_set->getSiblings()));
258 function test_moveToRightOf()
260 $this->Alcudia->reload();
261 $this->Gandia->reload();
262 $this->Valencia->reload();
264 $this->Alcudia->nested_set->moveToRightOf($this->Gandia);
265 $this->assertEqual(array('Gandia','Alcudia','Daimus'), $this->getLocation($this->Valencia->nested_set->getAllChildren()));
267 $this->Carlet->reload();
268 $this->Alcudia->reload();
270 $this->Carlet->nested_set->moveToRightOf($this->Alcudia->id);
272 $this->Valencia->reload();
273 $this->Spain->reload();
275 $this->assertEqual(array('Gandia','Alcudia','Carlet','Daimus'), $this->getLocation($this->Valencia->nested_set->getAllChildren()));
276 $this->assertEqual(array('Spain'), $this->getLocation($this->Spain->nested_set->getSelfAndSiblings()));
280 function test_moveToChildOf()
282 $this->Oceania->reload();
283 $this->Spain->nested_set->moveToChildOf($this->Oceania);
284 $this->Spain->reload();
285 $this->assertEqual(array('Australia'), $this->getLocation($this->Spain->nested_set->getSiblings()));
287 $this->Europe->reload();
288 $this->Spain->nested_set->moveToChildOf($this->Europe);
289 $this->Spain->reload();
290 $this->assertEqual(array('Spain'), $this->getLocation($this->Spain->nested_set->getSelfAndSiblings()));
292 $this->Europe->reload();
293 $this->Oceania->reload();
294 $this->World =& $this->Location->create('name->','World');
295 $this->Oceania->nested_set->moveToChildOf($this->World);
296 $this->World->reload();
297 $this->Europe->nested_set->moveToChildOf($this->World);
298 $this->Europe->reload();
299 $this->Oceania->reload();
300 $this->assertEqual('World',$this->getLocation($this->Europe->nested_set->getRoot()));
301 $this->assertEqual('World',$this->getLocation($this->Oceania->nested_set->getRoot()));
302 $this->assertEqual(array('Europe','Oceania'), $this->getLocation($this->Europe->nested_set->getSelfAndSiblings()));
306 function test_of_countChildren()
308 $this->Spain->reload();
309 $this->Oceania->reload();
310 $this->World->reload();
311 $this->assertEqual(6, $this->Spain->nested_set->countChildren());
312 $this->assertEqual(1, $this->Oceania->nested_set->countChildren());
313 $this->assertEqual(10, $this->World->nested_set->countChildren());
317 function test_of_getParent()
319 $this->assertEqual('World',$this->getLocation($this->Europe->nested_set->getParent()));
320 $this->assertEqual('Europe',$this->getLocation($this->Spain->nested_set->getParent()));
321 $this->assertEqual(false,$this->World->nested_set->getParent());
326 function test_of_getParents()
328 $this->Valencia->reload();
329 $this->assertEqual(array('World','Europe','Spain'),$this->getLocation($this->Valencia->nested_set->getParents()));
330 $this->assertEqual(false,$this->World->nested_set->getParents());
334 function Test_of_isChild()
336 $this->assertTrue($this->Carlet->nested_set->isChild());
337 $this->assertFalse($this->World->nested_set->isChild());
338 $this->assertTrue($this->Valencia->nested_set->isChild());
342 function test_deletions_with_children()
344 $this->assertEqual(6, $this->Spain->nested_set->countChildren());
345 $this->Valencia->destroy();
346 $this->Spain->reload();
347 $this->assertEqual(1, $this->Spain->nested_set->countChildren());
350 function test_deletions_without_children()
352 $this->Barcelona->reload();
353 $this->Barcelona->destroy();
354 $this->Spain->reload();
355 $this->assertEqual(0, $this->Spain->nested_set->countChildren());
362 /**/
363 function _resetTable()
365 $this->_deleteTestingModelDatabases();
366 $this->_createNewTestingModelDatabase('NestedCategory');
369 function _getNestedSetList($Categories = null, $breadcrumb = false)
371 if(!isset($Categories)){
372 $Categories = new NestedCategory();
373 $Categories = $Categories->find('all',array('conditions'=>$Categories->nested_set->getScopeCondition(),'order'=>' lft ASC '));
375 $list = array();
376 foreach ($Categories as $Category){
377 $bread_crumb = '';
378 if($Parents = $Category->nested_set->getParents()){
379 foreach ($Parents as $Parent){
380 $bread_crumb .= $Parent->description.' > ';
383 if($breadcrumb){
384 $list[] = $bread_crumb."(".$Category->id.")".$Category->description;//
385 }else{
386 $list[$Category->parent_id][$Category->id] = $Category->lft.' &lt;- '.$Category->description.' -&gt;'.$Category->rgt;// getAttributes();
389 return $list;
391 /**/
395 ak_test('test_AkActiveRecord_actsAsNestedSet',true);