Fixing #138
[akelos.git] / test / unit / lib / AkActiveRecord / AkActsAsList.php
blob043b83204bf38defb22cf6f4c6ab98058bb26a39
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 AkActiveRecord_actsAsListTestCase extends AkUnitTest
13 function test_AkActiveRecord_actsAsList()
15 $this->installAndIncludeModels(array(
16 'TodoItem'=>'id, position int, task text, due_time datetime, created_at, expires datetime, updated_at,new_position int'
17 ));
20 function Test_of_actsAsList_instatiation()
22 $TodoItems =& new TodoItem();
23 $this->assertEqual($TodoItems->actsLike(), 'active record,list');
24 $this->assertFalse(empty($TodoItems->list->column));
25 $this->assertTrue(empty($TodoItems->list->scope));
27 $TodoItems =& new TodoItem();
28 $this->assertErrorPattern('/not_available/',$TodoItems->actsAs('list', array('column'=>'not_available')));
30 $this->assertEqual($TodoItems->actsLike(), 'active record');
34 function Test_of_Test_of___construct()
36 $TodoItems =& new TodoItem();
37 $TodoItems->actsAs('list',
38 array(
39 'column'=>'new_position', // Redefining the default column
40 'scope'=>array('todo_list_id = ? AND completed = 0',$TodoItems->getId()),
41 'custom_attribute'=>'This is not allowed here'));
42 $this->assertEqual($TodoItems->list->column, 'new_position');
43 $this->assertEqual($TodoItems->list->scope, array ( 0 => 'todo_list_id = ? AND completed = 0', 1 => null));
44 $this->assertTrue(empty($TodoItems->list->custom_attribute));
47 function Test_of__ensureIsActiveRecordInstance()
49 $TodoItems =& new TodoItem();
50 $Object =& new AkObject();
51 $this->assertErrorPattern('/is not an active record/',$TodoItems->list->_ensureIsActiveRecordInstance(&$Object));
54 function Test_of_getType()
56 $TodoItems =& new TodoItem();
57 $this->assertEqual($TodoItems->list->getType(), 'list');
60 function Test_of_getScopeCondition_and_setScopeCondition()
62 $TodoItems =& new TodoItem();
63 $this->assertEqual($TodoItems->list->getScopeCondition(), ($TodoItems->_db->type() == 'postgre') ? 'true' : '1');
64 $TodoItems->list->setScopeCondition('true');
65 $this->assertEqual($TodoItems->list->getScopeCondition(), 'true');
68 function Test_of_getBottomItem_1()
70 $TodoItems =& new TodoItem();
71 $this->assertFalse($TodoItems->list->getBottomItem());
74 function Test_of_getBottomPosition_1()
76 $TodoItems =& new TodoItem();
77 $this->assertIdentical($TodoItems->list->getBottomPosition(), 0);
80 function Test_of__addToBottom_1()
82 $TodoItems =& new TodoItem();
83 $TodoItems->List->_addToBottom();
84 $this->assertIdentical($TodoItems->position, 1);
85 $this->assertIdentical($TodoItems->List->_ActiveRecordInstance->position, 1);
88 function Test_of_beforeCreate()
90 $TodoItems =& new TodoItem();
91 $position = $TodoItems->getAttribute('position');
92 $TodoItems->List->beforeCreate($TodoItems);
93 $this->assertIdentical($TodoItems->getAttribute('position'),$position+1);
97 function Test_of_getBottomItem_2()
99 $TodoItems =& new TodoItem('task->','Email Hilario with new product specs','due_time->',Ak::getDate(Ak::time()+(60*60*24*7)));
100 $this->assertPattern('/list/',$TodoItems->actsLike());
101 $this->assertTrue($TodoItems->isNewRecord());
103 $this->assertTrue($TodoItems->save());
105 $this->assertTrue($getBottomItem = $TodoItems->List->getBottomItem());
106 $this->assertEqual($getBottomItem->toString(), $TodoItems->toString());
108 $TodoItems =& new TodoItem('task->','Book COMDEX trip','due_time->',Ak::getDate(Ak::time()+(60*60*24*3)));
109 $this->assertTrue($TodoItems->isNewRecord());
110 $this->assertTrue($TodoItems->save());
111 $this->assertTrue($getBottomItem = $TodoItems->List->getBottomItem());
113 $this->assertEqual($getBottomItem->toString(), $TodoItems->toString());
115 $TodoItems =& new TodoItem(1);
116 $this->assertTrue($getBottomItem = $TodoItems->List->getBottomItem(2));
117 $this->assertEqual($getBottomItem->toString(), $TodoItems->toString());
121 function Test_of_getBottomPosition_2()
123 $TodoItems =& new TodoItem();
124 $this->assertEqual($TodoItems->list->getBottomPosition(), 2);
126 $TodoItem = $TodoItems->find(2);
127 $this->assertEqual($TodoItem->list->getBottomPosition(), 2);
130 function Test_of__addToBottom_2()
132 $TodoItems =& new TodoItem();
133 $TodoItems->list->_addToBottom();
134 $this->assertIdentical($TodoItems->position, 3);
135 $this->assertIdentical($TodoItems->list->_ActiveRecordInstance->position, 3);
139 function Test_of_isInList()
141 $TodoItems =& new TodoItem();
142 $this->assertFalse($TodoItems->list->isInList());
144 $TodoItems =& new TodoItem(1);
145 $this->assertTrue($TodoItems->list->isInList());
149 function Test_of_populate_todo_list()
151 for ($i = 0; $i <= 30; $i++){
152 $attributes = array('task'=>'Task number '.($i+3),'due_time'=>Ak::getDate(Ak::time()+(60*60*24*$i)));
153 $TodoTask =& new TodoItem($attributes);
154 $this->assertTrue($TodoTask->save());
155 $this->assertTrue(($TodoTask->task == $attributes['task']) && $TodoTask->due_time == $attributes['due_time']);
160 function Test_of_decrementPositionsOnLowerItems()
162 $TodoItems =& new TodoItem();
163 $TodoItems->transactionStart();
164 $this->assertFalse($TodoItems->list->decrementPositionsOnLowerItems());
166 $todo_list = $this->_getTodoList();
167 $this->assertEqual($todo_list[10] , 'Task number 10');
168 $TodoItems =& new TodoItem(10);
170 $this->assertTrue($TodoItems->list->decrementPositionsOnLowerItems());
171 $todo_list = $this->_getTodoList();
173 $this->assertEqual($todo_list[10] , 'Task number 11');
174 $this->assertFalse(in_array('Task number 10',$todo_list));
176 $TodoItems->transactionFail();
177 $TodoItems->transactionComplete();
179 $todo_list = $this->_getTodoList();
180 $this->assertEqual($todo_list[10] , 'Task number 10','Test failed because a database transaction was not performed correctly');
183 function Test_of_removeFromList()
185 $TodoItems =& new TodoItem(10);
187 $TodoItems->transactionStart();
188 $this->assertTrue($TodoItems->list->removeFromList());
189 $this->assertFalse($TodoItems->list->isInList());
190 $TodoItems->transactionFail();
191 $TodoItems->transactionComplete();
192 $todo_list = $this->_getTodoList();
193 $this->assertEqual($todo_list[10] , 'Task number 10','Test failed because a database transaction was not performed correctly');
197 function Test_of_afterDestroy_and_beforeDestroy()
199 $TodoItems =& new TodoItem(10);
201 $TodoItems->transactionStart();
203 $TodoItems->destroy();
204 $this->assertFalse($TodoItems->list->isInList());
206 $todo_list = $this->_getTodoList();
207 $this->assertEqual($todo_list[10] , 'Task number 11');
209 $this->assertEqual($todo_list[14] , 'Task number 15');
211 $TodoItems->destroy(array(15,16));
213 $todo_list = $this->_getTodoList();
214 $this->assertEqual($todo_list[13] , 'Task number 14');
215 $this->assertEqual($todo_list[14] , 'Task number 17');
216 $this->assertEqual($todo_list[15] , 'Task number 18');
218 $TodoItems->transactionFail();
219 $TodoItems->transactionComplete();
220 $todo_list = $this->_getTodoList();
221 $this->assertEqual($todo_list[10] , 'Task number 10','Test failed because a database transaction was not performed correctly');
225 function Test_of_getLowerItem()
227 $TodoItems =& new TodoItem();
228 $this->assertFalse($TodoItems->list->getLowerItem());
229 $TodoItem = $TodoItems->find(10);
231 $LowerItem = $TodoItem->list->getLowerItem();
232 $this->assertEqual($LowerItem->task, 'Task number 11');
234 $TodoItem = $TodoItems->find(33);
235 $this->assertFalse($TodoItem->list->getLowerItem());
238 $TodoItems =& new TodoItem();
239 $TodoItems->transactionStart();
240 $this->assertTrue($TodoItems->deleteAll());
242 $this->assertFalse($TodoItems->list->getLowerItem());
244 $TodoItems->transactionFail();
245 $TodoItems->transactionComplete();
248 function Test_of_decrementPosition()
250 $TodoItems =& new TodoItem(10);
251 $TodoItems->transactionStart();
253 $this->assertTrue($TodoItems->list->decrementPosition());
255 $TodoItems->transactionFail();
256 $TodoItems->transactionComplete();
259 function Test_of_incrementPosition()
261 $TodoItems =& new TodoItem(10);
262 $TodoItems->transactionStart();
264 $this->assertTrue($TodoItems->list->incrementPosition());
266 $TodoItems->transactionFail();
267 $TodoItems->transactionComplete();
270 function Test_of_moveLower()
272 $TodoItems =& new TodoItem();
273 $this->assertFalse($TodoItems->list->moveLower());
275 $todo_list = $this->_getTodoList();
276 $this->assertEqual($todo_list[10] , 'Task number 10');
277 $this->assertEqual($todo_list[11] , 'Task number 11');
279 $TodoItems =& new TodoItem(10);
280 $TodoItems->transactionStart();
282 $this->assertTrue($TodoItems->list->moveLower());
284 $todo_list = $this->_getTodoList();
285 $this->assertEqual($todo_list[10] , 'Task number 11');
286 $this->assertEqual($todo_list[11] , 'Task number 10');
288 $TodoItems =& new TodoItem(33);
289 $this->assertFalse($TodoItems->list->moveLower());
291 $TodoItems->transactionFail();
292 $TodoItems->transactionComplete();
296 function Test_of_getHigherItem()
298 $TodoItems =& new TodoItem();
299 $this->assertFalse($TodoItems->list->getHigherItem());
301 $TodoItem = $TodoItems->find(10);
302 $HigherItem = $TodoItem->list->getHigherItem();
303 $this->assertEqual($HigherItem->task, 'Task number 9');
305 $TodoItem = $TodoItems->find(1);
306 $this->assertFalse($TodoItem->list->getHigherItem());
309 $TodoItems =& new TodoItem();
310 $TodoItems->transactionStart();
311 $this->assertTrue($TodoItems->deleteAll());
313 $this->assertFalse($TodoItems->list->getHigherItem());
315 $TodoItems->transactionFail();
316 $TodoItems->transactionComplete();
320 function Test_of_moveHigher()
322 $TodoItems =& new TodoItem();
323 $this->assertFalse($TodoItems->list->moveHigher());
325 $todo_list = $this->_getTodoList();
326 $this->assertEqual($todo_list[9] , 'Task number 9');
327 $this->assertEqual($todo_list[10] , 'Task number 10');
329 $TodoItems =& new TodoItem(10);
330 $TodoItems->transactionStart();
332 $this->assertTrue($TodoItems->list->moveHigher());
334 $todo_list = $this->_getTodoList();
335 $this->assertEqual($todo_list[9] , 'Task number 10');
336 $this->assertEqual($todo_list[10] , 'Task number 9');
338 $TodoItems =& new TodoItem(1);
339 $this->assertFalse($TodoItems->list->moveHigher());
341 $TodoItems->transactionFail();
342 $TodoItems->transactionComplete();
345 function Test_of_assumeBottomPosition()
347 $todo_list = $this->_getTodoList();
348 $this->assertEqual($todo_list[10] , 'Task number 10');
350 $TodoItems =& new TodoItem(10);
351 $TodoItems->transactionStart();
353 $this->assertTrue($TodoItems->list->assumeBottomPosition());
355 $todo_list = $this->_getTodoList();
356 $this->assertEqual($todo_list[34] , 'Task number 10');
358 $TodoItems->transactionFail();
359 $TodoItems->transactionComplete();
362 function Test_of_moveToBottom()
364 $todo_list = $this->_getTodoList();
365 $this->assertEqual($todo_list[10] , 'Task number 10');
367 $TodoItems =& new TodoItem();
368 $this->assertFalse($TodoItems->list->moveToBottom());
370 $TodoItems =& new TodoItem(10);
371 $TodoItems->transactionStart();
373 $this->assertTrue($TodoItems->list->moveToBottom());
375 $todo_list = $this->_getTodoList();
376 $this->assertEqual($todo_list[10] , 'Task number 11');
377 $this->assertEqual($todo_list[32] , 'Task number 33');
378 $this->assertEqual($todo_list[33] , 'Task number 10');
380 $TodoItems->transactionFail();
381 $TodoItems->transactionComplete();
383 $TodoItems =& new TodoItem(33);
384 $TodoItems->transactionStart();
386 $this->assertTrue($TodoItems->list->moveToBottom());
388 $todo_list = $this->_getTodoList();
389 $this->assertEqual($todo_list[33] , 'Task number 33');
391 $TodoItems->transactionFail();
392 $TodoItems->transactionComplete();
395 function Test_of_incrementPositionsOnHigherItems()
397 $TodoItems =& new TodoItem();
398 $TodoItems->transactionStart();
399 $this->assertFalse($TodoItems->list->incrementPositionsOnHigherItems());
401 $todo_list = $this->_getTodoList();
402 $this->assertEqual($todo_list[10] , 'Task number 10');
403 $TodoItems =& new TodoItem(10);
404 $this->assertTrue($TodoItems->list->incrementPositionsOnHigherItems());
405 $todo_list = $this->_getTodoList();
406 $this->assertEqual($todo_list[9] , 'Task number 8');
407 $this->assertEqual($todo_list[10] , 'Task number 10'); // Task 9&10 are on position 10, so this is ambigious; last one returned by find wins
408 $TodoItems->transactionFail();
409 $TodoItems->transactionComplete();
413 function Test_of_assumeTopPosition()
415 $TodoItems =& new TodoItem();
416 $TodoItems->transactionStart();
418 $todo_list = $this->_getTodoList();
419 $this->assertEqual($todo_list[10] , 'Task number 10');
420 $TodoItems =& new TodoItem(10);
421 $this->assertTrue($TodoItems->list->assumeTopPosition());
422 $todo_list = $this->_getTodoList();
423 $this->assertEqual($todo_list[1] , 'Task number 10');
424 $TodoItems->transactionFail();
425 $TodoItems->transactionComplete();
428 function Test_of_moveToTop()
430 $todo_list = $this->_getTodoList();
431 $this->assertEqual($todo_list[10] , 'Task number 10');
433 $TodoItems =& new TodoItem();
434 $this->assertFalse($TodoItems->list->moveToTop());
436 $TodoItems =& new TodoItem(10);
437 $TodoItems->transactionStart();
439 $this->assertTrue($TodoItems->list->moveToTop());
441 $todo_list = $this->_getTodoList();
442 $this->assertEqual($todo_list[10] , 'Task number 9');
443 $this->assertEqual($todo_list[2] , 'Email Hilario with new product specs');
444 $this->assertEqual($todo_list[1] , 'Task number 10');
446 $TodoItems->transactionFail();
447 $TodoItems->transactionComplete();
449 $TodoItems =& new TodoItem(1);
450 $TodoItems->transactionStart();
452 $this->assertTrue($TodoItems->list->moveToTop());
454 $todo_list = $this->_getTodoList();
455 $this->assertEqual($todo_list[1] , 'Email Hilario with new product specs');
457 $TodoItems->transactionFail();
458 $TodoItems->transactionComplete();
461 function Test_of_isFirst()
463 $TodoItems =& new TodoItem(1);
465 $this->assertTrue($TodoItems->list->isFirst());
467 $TodoItems =& new TodoItem(2);
468 $this->assertFalse($TodoItems->list->isFirst());
470 $TodoItems =& new TodoItem();
471 $this->assertFalse($TodoItems->list->isFirst());
475 function Test_of_isLast()
477 $TodoItems =& new TodoItem(33);
478 $this->assertTrue($TodoItems->list->isLast());
480 $TodoItems =& new TodoItem(1);
481 $this->assertFalse($TodoItems->list->isLast());
483 $TodoItems =& new TodoItem();
484 $this->assertFalse($TodoItems->list->isLast());
488 function Test_of_incrementPositionsOnLowerItems()
490 $TodoItems =& new TodoItem();
491 $TodoItems->transactionStart();
493 $todo_list = $this->_getTodoList();
494 $this->assertEqual($todo_list[10] , 'Task number 10');
495 $TodoItems =& new TodoItem(10);
496 $this->assertTrue($TodoItems->list->incrementPositionsOnLowerItems(10));
497 $todo_list = $this->_getTodoList();
498 $this->assertEqual($todo_list[9] , 'Task number 9');
499 $this->assertEqual($todo_list[11] , 'Task number 10');
500 $this->assertEqual($todo_list[34] , 'Task number 33');
501 $TodoItems->transactionFail();
502 $TodoItems->transactionComplete();
505 function Test_of_insertAtPosition()
507 $TodoItems =& new TodoItem(10);
508 $TodoItems->transactionStart();
510 $TodoItems->list->insertAtPosition(1);
511 $todo_list = $this->_getTodoList();
512 $this->assertEqual($todo_list[1] , 'Task number 10');
515 $TodoItems =& new TodoItem('task->','ship new InmoEasy version');
516 $TodoItems->list->insertAtPosition(1);
517 $todo_list = $this->_getTodoList();
518 $this->assertEqual($todo_list[1] , 'ship new InmoEasy version');
520 $TodoItems =& new TodoItem(10);
521 $TodoItems->list->insertAtPosition(10);
522 $todo_list = $this->_getTodoList();
523 $this->assertEqual($todo_list[10] , 'Task number 10');
525 $TodoItems =& new TodoItem(33);
526 $TodoItems->list->insertAtPosition(40);
527 $todo_list = $this->_getTodoList();
528 $this->assertEqual($todo_list[40] , 'Task number 33');
530 $TodoItems->transactionFail();
531 $TodoItems->transactionComplete();
533 function Test_of_insertAt(){} //Alias for insertAtPosition but with default value to 1
536 function Test_of_incrementPositionsOnAllItems()
538 $TodoItems =& new TodoItem();
539 $TodoItems->transactionStart();
541 $this->assertTrue($TodoItems->list->incrementPositionsOnAllItems());
543 $todo_list = $this->_getTodoList();
544 $this->assertEqual($todo_list[2] , 'Email Hilario with new product specs');
545 $this->assertEqual($todo_list[34] , 'Task number 33');
547 $TodoItems->transactionFail();
548 $TodoItems->transactionComplete();
551 function Test_of_addToListTop(){} // same as incrementPositionsOnAllItems()
553 function Test_of_decrementPositionsOnHigherItems()
555 $TodoItems =& new TodoItem();
556 $TodoItems->transactionStart();
558 $this->assertTrue($TodoItems->list->decrementPositionsOnHigherItems(10));
560 $todo_list = $this->_getTodoList();
562 $this->assertEqual($todo_list[0] , 'Email Hilario with new product specs');
563 $this->assertEqual($todo_list[9] , 'Task number 10');
565 $TodoItems->transactionFail();
566 $TodoItems->transactionComplete();
569 function _getTodoList($use_id_as_index = false)
571 $TodoItems = new TodoItem();
572 $TodoItems = $TodoItems->find('all',array('order'=>'id ASC'));
573 $list = array();
574 foreach ($TodoItems as $TodoItem){
575 if($use_id_as_index){
576 $list[$TodoItem->id] = $TodoItem->position.') '.$TodoItem->task;
577 }else{
578 $list[$TodoItem->position] = $TodoItem->task;
581 return $list;
584 function test_should_move_up_the_item_with_the_same_position_as_the_inserted()
586 $this->installAndIncludeModels(array('TodoList', 'TodoTask'));
588 $ListA =& new TodoList(array('name' => 'A'));
589 $this->assertTrue($ListA->save());
591 $ListA->task->create(array('details' => 1));
593 $ListB =& new TodoList(array('name' => 'B'));
594 $this->assertTrue($ListB->save());
595 $ListB->task->create(array('details' => 2));
596 $TodoTask =& $ListB->task->create(array('details' => 3));
598 $Task1 =& $TodoTask->find('first',array('details'=>1));
600 $Task1->list->removeFromList();
601 $this->assertTrue($Task1->save());
602 $Task1->todo_list->assign($ListB);
603 $this->assertTrue($Task1->save());
604 $Task1->list->insertAt(2);
606 $ListB =& $ListB->find('first',array('name'=>'B'), array('include'=>'tasks'));
608 foreach (array_keys($ListB->tasks) as $k){
609 $this->assertEqual($ListB->tasks[$k]->get('position'), $k+1);
612 /**//**//**/
616 ak_test('AkActiveRecord_actsAsListTestCase',true);