SortableBehaviorTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. App::uses('SortableBehavior', 'Tools.Model/Behavior');
  3. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  4. class SortableBehaviorTest extends MyCakeTestCase {
  5. public $fixtures = array('plugin.tools.role');
  6. public $SortableBehavior;
  7. public $Model;
  8. public function setUp() {
  9. parent::setUp();
  10. $this->SortableBehavior = new SortableBehavior();
  11. $this->Model = ClassRegistry::init('Role');
  12. $this->Model->Behaviors->load('Tools.Sortable');
  13. // Reset order
  14. $list = $this->_getList();
  15. $count = count($list);
  16. foreach ($list as $id => $order) {
  17. $this->Model->id = $id;
  18. $this->Model->saveField('sort', $count);
  19. $count--;
  20. }
  21. }
  22. public function testObject() {
  23. $this->assertTrue(is_object($this->SortableBehavior));
  24. $this->assertInstanceOf('SortableBehavior', $this->SortableBehavior);
  25. }
  26. /**
  27. * SortableBehaviorTest::testBasicUp()
  28. *
  29. * @return void
  30. */
  31. public function testBasicUp() {
  32. $list = $this->_getList();
  33. //debug($list);
  34. $positionBefore = $this->_getPosition(4);
  35. $this->assertSame(3, $positionBefore);
  36. $this->Model->moveUp(4);
  37. $positionAfter = $this->_getPosition(4);
  38. $this->assertSame(2, $positionAfter);
  39. $this->Model->moveUp(4);
  40. $positionAfter = $this->_getPosition(4);
  41. $this->assertSame(1, $positionAfter);
  42. $this->Model->moveUp(4);
  43. $positionAfter = $this->_getPosition(4);
  44. $this->assertSame(1, $positionAfter);
  45. }
  46. /**
  47. * SortableBehaviorTest::testUp()
  48. *
  49. * @return void
  50. */
  51. public function testUp() {
  52. $list = $this->_getList();
  53. //debug($list);
  54. $positionBefore = $this->_getPosition(4);
  55. $this->assertSame(3, $positionBefore);
  56. $this->Model->moveUp(4, 2);
  57. $positionAfter = $this->_getPosition(4);
  58. $this->assertSame(1, $positionAfter);
  59. }
  60. /**
  61. * SortableBehaviorTest::testBasicDown()
  62. *
  63. * @return void
  64. */
  65. public function testBasicDown() {
  66. $positionBefore = $this->_getPosition(2);
  67. $this->assertSame(2, $positionBefore);
  68. $this->Model->moveDown(2);
  69. $positionAfter = $this->_getPosition(2);
  70. $this->assertSame(3, $positionAfter);
  71. $this->Model->moveDown(2);
  72. $positionAfter = $this->_getPosition(2);
  73. $this->assertSame(4, $positionAfter);
  74. $this->Model->moveDown(2);
  75. $positionAfter = $this->_getPosition(2);
  76. $this->assertSame(4, $positionAfter);
  77. }
  78. /**
  79. * SortableBehaviorTest::testDown()
  80. *
  81. * @return void
  82. */
  83. public function testDown() {
  84. $positionBefore = $this->_getPosition(2);
  85. $this->assertSame(2, $positionBefore);
  86. $this->Model->moveDown(2, 3);
  87. $positionAfter = $this->_getPosition(2);
  88. $this->assertSame(4, $positionAfter);
  89. }
  90. /**
  91. * SortableBehaviorTest::testAddNew()
  92. *
  93. * @return void
  94. */
  95. public function testAddNew() {
  96. $this->Model->create();
  97. $data = array(
  98. 'name' => 'new'
  99. );
  100. $this->Model->save($data);
  101. $id = $this->Model->id;
  102. $list = $this->_getList();
  103. $position = $this->_getPosition($id);
  104. $this->assertSame(count($list), $position);
  105. }
  106. /**
  107. * Get 1-based position in the list.
  108. *
  109. * @param mixed $id
  110. * @param mixed $list
  111. * @return int Position or null if not found
  112. */
  113. protected function _getPosition($id, $list = array()) {
  114. if (!$list) {
  115. $list = $this->_getList();
  116. }
  117. $count = 0;
  118. $position = null;
  119. foreach ($list as $k => $v) {
  120. $count++;
  121. if ($id == $k) {
  122. $position = $count;
  123. break;
  124. }
  125. }
  126. return $position;
  127. }
  128. /**
  129. * SortableBehaviorTest::_getList()
  130. *
  131. * @return array
  132. */
  133. protected function _getList() {
  134. $options = array(
  135. 'order' => array('sort' => 'DESC'),
  136. 'fields' => array('id', 'sort')
  137. );
  138. return $this->Model->find('list', $options);
  139. }
  140. }