BitmaskedBehaviorTest.php 7.5 KB

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