| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- App::uses('SortableBehavior', 'Tools.Model/Behavior');
- App::uses('MyCakeTestCase', 'Tools.TestSuite');
- class SortableBehaviorTest extends MyCakeTestCase {
- public $fixtures = array('plugin.tools.role');
- public $SortableBehavior;
- public $Model;
- public function setUp() {
- parent::setUp();
- $this->SortableBehavior = new SortableBehavior();
- $this->Model = ClassRegistry::init('Role');
- $this->Model->Behaviors->load('Tools.Sortable');
- // Reset order
- $list = $this->_getList();
- $count = count($list);
- foreach ($list as $id => $order) {
- $this->Model->id = $id;
- $this->Model->saveField('sort', $count);
- $count--;
- }
- }
- public function testObject() {
- $this->assertTrue(is_object($this->SortableBehavior));
- $this->assertInstanceOf('SortableBehavior', $this->SortableBehavior);
- }
- /**
- * SortableBehaviorTest::testBasicUp()
- *
- * @return void
- */
- public function testBasicUp() {
- $list = $this->_getList();
- //debug($list);
- $positionBefore = $this->_getPosition(4);
- $this->assertSame(3, $positionBefore);
- $this->Model->moveUp(4);
- $positionAfter = $this->_getPosition(4);
- $this->assertSame(2, $positionAfter);
- $this->Model->moveUp(4);
- $positionAfter = $this->_getPosition(4);
- $this->assertSame(1, $positionAfter);
- $this->Model->moveUp(4);
- $positionAfter = $this->_getPosition(4);
- $this->assertSame(1, $positionAfter);
- }
- /**
- * SortableBehaviorTest::testUp()
- *
- * @return void
- */
- public function testUp() {
- $list = $this->_getList();
- //debug($list);
- $positionBefore = $this->_getPosition(4);
- $this->assertSame(3, $positionBefore);
- $this->Model->moveUp(4, 2);
- $positionAfter = $this->_getPosition(4);
- $this->assertSame(1, $positionAfter);
- }
- /**
- * SortableBehaviorTest::testBasicDown()
- *
- * @return void
- */
- public function testBasicDown() {
- $positionBefore = $this->_getPosition(2);
- $this->assertSame(2, $positionBefore);
- $this->Model->moveDown(2);
- $positionAfter = $this->_getPosition(2);
- $this->assertSame(3, $positionAfter);
- $this->Model->moveDown(2);
- $positionAfter = $this->_getPosition(2);
- $this->assertSame(4, $positionAfter);
- $this->Model->moveDown(2);
- $positionAfter = $this->_getPosition(2);
- $this->assertSame(4, $positionAfter);
- }
- /**
- * SortableBehaviorTest::testDown()
- *
- * @return void
- */
- public function testDown() {
- $positionBefore = $this->_getPosition(2);
- $this->assertSame(2, $positionBefore);
- $this->Model->moveDown(2, 3);
- $positionAfter = $this->_getPosition(2);
- $this->assertSame(4, $positionAfter);
- }
- /**
- * SortableBehaviorTest::testAddNew()
- *
- * @return void
- */
- public function testAddNew() {
- $this->Model->create();
- $data = array(
- 'name' => 'new'
- );
- $this->Model->save($data);
- $id = $this->Model->id;
- $list = $this->_getList();
- $position = $this->_getPosition($id);
- $this->assertSame(count($list), $position);
- }
- /**
- * Get 1-based position in the list.
- *
- * @param mixed $id
- * @param mixed $list
- * @return int Position or null if not found
- */
- protected function _getPosition($id, $list = array()) {
- if (!$list) {
- $list = $this->_getList();
- }
- $count = 0;
- $position = null;
- foreach ($list as $k => $v) {
- $count++;
- if ($id == $k) {
- $position = $count;
- break;
- }
- }
- return $position;
- }
- /**
- * SortableBehaviorTest::_getList()
- *
- * @return array
- */
- protected function _getList() {
- $options = array(
- 'order' => array('sort' => 'DESC'),
- 'fields' => array('id', 'sort')
- );
- return $this->Model->find('list', $options);
- }
- }
|