BitmaskedBehaviorTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace Tools\Test\TestCase\Model\Behavior;
  3. use Cake\Database\Query;
  4. use Cake\Datasource\ConnectionManager;
  5. use Cake\Event\Event;
  6. use TestApp\Model\Entity\BitmaskedComment;
  7. //use TestApp\Model\Table\BitmaskedCommentsTable;
  8. use Cake\ORM\TableRegistry;
  9. use Cake\TestSuite\TestCase;
  10. use Cake\Core\Configure;
  11. use Tools\Model\Behavior\BitmaskedBehavior;
  12. //App::uses('AppModel', 'Model');
  13. class BitmaskedBehaviorTest extends TestCase {
  14. public $fixtures = array(
  15. 'plugin.tools.bitmasked_comments'
  16. );
  17. public $Comments;
  18. public function setUp() {
  19. parent::setUp();
  20. Configure::write('App.namespace', 'TestApp');
  21. $this->Comments = TableRegistry::get('BitmaskedComments');
  22. $this->Comments->addBehavior('Tools.Bitmasked', array('mappedField' => 'statuses'));
  23. }
  24. /**
  25. * BitmaskedBehaviorTest::testEncodeBitmask()
  26. *
  27. * @return void
  28. */
  29. public function testEncodeBitmask() {
  30. $res = $this->Comments->encodeBitmask(array(BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED));
  31. $expected = BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED;
  32. $this->assertSame($expected, $res);
  33. }
  34. /**
  35. * BitmaskedBehaviorTest::testDecodeBitmask()
  36. *
  37. * @return void
  38. */
  39. public function testDecodeBitmask() {
  40. $res = $this->Comments->decodeBitmask(BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED);
  41. $expected = array(BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED);
  42. $this->assertSame($expected, $res);
  43. }
  44. /**
  45. * BitmaskedBehaviorTest::testFind()
  46. *
  47. * @return void
  48. */
  49. public function testFind() {
  50. $res = $this->Comments->find('all')->toArray();
  51. $this->assertTrue(!empty($res) && is_array($res));
  52. $this->assertTrue(!empty($res[1]['statuses']) && is_array($res[1]['statuses']));
  53. }
  54. /**
  55. * BitmaskedBehaviorTest::testSave()
  56. *
  57. * @return void
  58. */
  59. public function testSave() {
  60. $data = array(
  61. 'comment' => 'test save',
  62. 'statuses' => array(),
  63. );
  64. $entity = $this->Comments->newEntity($data);
  65. $res = $this->Comments->validate($entity);
  66. $this->assertTrue($res);
  67. $this->assertSame('0', $entity->get('status'));
  68. $data = array(
  69. 'comment' => 'test save',
  70. 'statuses' => array(BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED),
  71. );
  72. $entity = $this->Comments->newEntity($data);
  73. $res = $this->Comments->validate($entity);
  74. $this->assertTrue($res);
  75. $is = $entity->get('status');
  76. $this->assertSame(BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED, $is);
  77. // save + find
  78. $entity = $this->Comments->newEntity($data);
  79. $res = $this->Comments->save($entity);
  80. $this->assertTrue((bool)$res);
  81. $res = $this->Comments->find('first', array('conditions' => array('statuses' => $data['statuses'])));
  82. $this->assertTrue(!empty($res));
  83. $expected = BitmaskedComment::STATUS_APPROVED | BitmaskedComment::STATUS_PUBLISHED; // 6
  84. $this->assertEquals($expected, $res['status']);
  85. $expected = $data['statuses'];
  86. $this->assertEquals($expected, $res['statuses']);
  87. // model.field syntax
  88. $res = $this->Comments->find('first', array('conditions' => array('BitmaskedComments.statuses' => $data['statuses'])));
  89. $this->assertTrue((bool)$res->toArray());
  90. // explicit
  91. $activeApprovedAndPublished = BitmaskedComment::STATUS_ACTIVE | BitmaskedComment::STATUS_APPROVED | BitmaskedComment::STATUS_PUBLISHED;
  92. $data = array(
  93. 'comment' => 'another post comment',
  94. 'status' => $activeApprovedAndPublished,
  95. );
  96. $entity = $this->Comments->newEntity($data);
  97. $res = $this->Comments->save($entity);
  98. $this->assertTrue((bool)$res);
  99. $res = $this->Comments->find('first', array('conditions' => array('status' => $activeApprovedAndPublished)));
  100. $this->assertTrue((bool)$res);
  101. $this->assertEquals($activeApprovedAndPublished, $res['status']);
  102. $expected = array(BitmaskedComment::STATUS_ACTIVE, BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED);
  103. $this->assertEquals($expected, $res['statuses']);
  104. }
  105. /**
  106. * Assert that you can manually trigger "notEmpty" rule with null instead of 0 for "not null" db fields
  107. */
  108. public function testSaveWithDefaultValue() {
  109. $data = array(
  110. 'comment' => 'test save',
  111. 'statuses' => array(),
  112. );
  113. $entity = $this->Comments->newEntity($data);
  114. $res = $this->Comments->validate($entity);
  115. $this->assertTrue($res);
  116. $this->assertSame('0', $entity->get('status'));
  117. // Now let's set the default value
  118. $this->Comments->removeBehavior('Bitmasked');
  119. $this->Comments->addBehavior('Tools.Bitmasked', array('mappedField' => 'statuses', 'defaultValue' => ''));
  120. $data = array(
  121. 'comment' => 'test save',
  122. 'statuses' => array(),
  123. );
  124. $entity = $this->Comments->newEntity($data);
  125. $res = $this->Comments->validate($entity);
  126. $this->assertFalse($res);
  127. $this->assertSame('', $entity->get('status'));
  128. }
  129. /**
  130. * Assert that it also works with beforeSave event callback.
  131. */
  132. public function testSaveOnBeforeSave() {
  133. $this->Comments->removeBehavior('Bitmasked');
  134. $this->Comments->addBehavior('Tools.Bitmasked', array('mappedField' => 'statuses', 'on' => 'beforeSave'));
  135. $data = array(
  136. 'comment' => 'test save',
  137. 'statuses' => array(BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED),
  138. );
  139. $entity = $this->Comments->newEntity($data);
  140. $res = $this->Comments->save($entity);
  141. $this->assertTrue((bool)$res);
  142. $this->assertSame(BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED, $res['status']);
  143. }
  144. public function testIs() {
  145. $res = $this->Comments->isBit(BitmaskedComment::STATUS_PUBLISHED);
  146. $expected = array('BitmaskedComments.status' => 2);
  147. $this->assertEquals($expected, $res);
  148. }
  149. public function testIsNot() {
  150. $res = $this->Comments->isNotBit(BitmaskedComment::STATUS_PUBLISHED);
  151. $expected = array('NOT' => array('BitmaskedComments.status' => 2));
  152. $this->assertEquals($expected, $res);
  153. }
  154. public function testContains() {
  155. $res = $this->Comments->containsBit(BitmaskedComment::STATUS_PUBLISHED);
  156. $expected = array('(BitmaskedComments.status & 2 = 2)');
  157. $this->assertEquals($expected, $res);
  158. $conditions = $res;
  159. $res = $this->Comments->find('all', array('conditions' => $conditions))->toArray();
  160. $this->assertTrue(!empty($res) && count($res) === 3);
  161. // multiple (AND)
  162. $res = $this->Comments->containsBit(array(BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_ACTIVE));
  163. $expected = array('(BitmaskedComments.status & 3 = 3)');
  164. $this->assertEquals($expected, $res);
  165. $conditions = $res;
  166. $res = $this->Comments->find('all', array('conditions' => $conditions))->toArray();
  167. $this->assertTrue(!empty($res) && count($res) === 2);
  168. }
  169. public function testNotContains() {
  170. $res = $this->Comments->containsNotBit(BitmaskedComment::STATUS_PUBLISHED);
  171. $expected = array('(BitmaskedComments.status & 2 != 2)');
  172. $this->assertEquals($expected, $res);
  173. $conditions = $res;
  174. $res = $this->Comments->find('all', array('conditions' => $conditions))->toArray();
  175. $this->assertTrue(!empty($res) && count($res) === 4);
  176. // multiple (AND)
  177. $res = $this->Comments->containsNotBit(array(BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_ACTIVE));
  178. $expected = array('(BitmaskedComments.status & 3 != 3)');
  179. $this->assertEquals($expected, $res);
  180. $conditions = $res;
  181. $res = $this->Comments->find('all', array('conditions' => $conditions))->toArray();
  182. $this->assertTrue(!empty($res) && count($res) === 5);
  183. }
  184. }