BitmaskedBehaviorTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 testSaveBasic() {
  57. $data = array(
  58. 'comment' => 'test save',
  59. 'statuses' => array(),
  60. );
  61. $entity = $this->Comments->newEntity($data);
  62. $res = $this->Comments->save($entity);
  63. $this->assertTrue((bool)$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->save($entity);
  71. $this->assertTrue((bool)$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. $this->assertEmpty($entity->errors());
  77. $res = $this->Comments->save($entity);
  78. $this->assertTrue((bool)$res);
  79. $res = $this->Comments->find('first', array('conditions' => array('statuses' => $data['statuses'])));
  80. $this->assertTrue(!empty($res));
  81. $expected = BitmaskedComment::STATUS_APPROVED | BitmaskedComment::STATUS_PUBLISHED; // 6
  82. $this->assertEquals($expected, $res['status']);
  83. $expected = $data['statuses'];
  84. $this->assertEquals($expected, $res['statuses']);
  85. // model.field syntax
  86. $res = $this->Comments->find('first', array('conditions' => array('BitmaskedComments.statuses' => $data['statuses'])));
  87. $this->assertTrue((bool)$res->toArray());
  88. // explicit
  89. $activeApprovedAndPublished = BitmaskedComment::STATUS_ACTIVE | BitmaskedComment::STATUS_APPROVED | BitmaskedComment::STATUS_PUBLISHED;
  90. $data = array(
  91. 'comment' => 'another post comment',
  92. 'status' => $activeApprovedAndPublished,
  93. );
  94. $entity = $this->Comments->newEntity($data);
  95. $res = $this->Comments->save($entity);
  96. $this->assertTrue((bool)$res);
  97. $res = $this->Comments->find('first', array('conditions' => array('status' => $activeApprovedAndPublished)));
  98. $this->assertTrue((bool)$res);
  99. $this->assertEquals($activeApprovedAndPublished, $res['status']);
  100. $expected = array(BitmaskedComment::STATUS_ACTIVE, BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED);
  101. $this->assertEquals($expected, $res['statuses']);
  102. }
  103. /**
  104. * Assert that you can manually trigger "notEmpty" rule with null instead of 0 for "not null" db fields
  105. *
  106. * @return void
  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->save($entity);
  115. $this->assertTrue((bool)$res);
  116. $this->assertSame('0', $entity->get('status'));
  117. $this->skipIf(true, '//FIXME');
  118. // Now let's set the default value
  119. $this->Comments->removeBehavior('Bitmasked');
  120. $this->Comments->addBehavior('Tools.Bitmasked', array('mappedField' => 'statuses', 'defaultValue' => ''));
  121. $data = array(
  122. 'comment' => 'test save',
  123. 'statuses' => array(),
  124. );
  125. $entity = $this->Comments->newEntity($data);
  126. $res = $this->Comments->save($entity);
  127. $this->assertFalse($res);
  128. $this->assertSame('', $entity->get('status'));
  129. }
  130. /**
  131. * Assert that it also works with beforeSave event callback.
  132. *
  133. * @return void
  134. */
  135. public function testSaveOnBeforeSave() {
  136. $this->Comments->removeBehavior('Bitmasked');
  137. $this->Comments->addBehavior('Tools.Bitmasked', array('mappedField' => 'statuses', 'on' => 'beforeSave'));
  138. $data = array(
  139. 'comment' => 'test save',
  140. 'statuses' => array(BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED),
  141. );
  142. $entity = $this->Comments->newEntity($data);
  143. $this->assertEmpty($entity->errors());
  144. $res = $this->Comments->save($entity);
  145. $this->assertTrue((bool)$res);
  146. $this->assertSame(BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED, $res['status']);
  147. }
  148. /**
  149. * BitmaskedBehaviorTest::testIs()
  150. *
  151. * @return void
  152. */
  153. public function testIs() {
  154. $res = $this->Comments->isBit(BitmaskedComment::STATUS_PUBLISHED);
  155. $expected = array('BitmaskedComments.status' => 2);
  156. $this->assertEquals($expected, $res);
  157. }
  158. /**
  159. * BitmaskedBehaviorTest::testIsNot()
  160. *
  161. * @return void
  162. */
  163. public function testIsNot() {
  164. $res = $this->Comments->isNotBit(BitmaskedComment::STATUS_PUBLISHED);
  165. $expected = array('NOT' => array('BitmaskedComments.status' => 2));
  166. $this->assertEquals($expected, $res);
  167. }
  168. /**
  169. * BitmaskedBehaviorTest::testContains()
  170. *
  171. * @return void
  172. */
  173. public function testContains() {
  174. $res = $this->Comments->containsBit(BitmaskedComment::STATUS_PUBLISHED);
  175. $expected = array('(BitmaskedComments.status & 2 = 2)');
  176. $this->assertEquals($expected, $res);
  177. $conditions = $res;
  178. $res = $this->Comments->find('all', array('conditions' => $conditions))->toArray();
  179. $this->assertTrue(!empty($res) && count($res) === 3);
  180. // multiple (AND)
  181. $res = $this->Comments->containsBit(array(BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_ACTIVE));
  182. $expected = array('(BitmaskedComments.status & 3 = 3)');
  183. $this->assertEquals($expected, $res);
  184. $conditions = $res;
  185. $res = $this->Comments->find('all', array('conditions' => $conditions))->toArray();
  186. $this->assertTrue(!empty($res) && count($res) === 2);
  187. }
  188. /**
  189. * BitmaskedBehaviorTest::testNotContains()
  190. *
  191. * @return void
  192. */
  193. public function testNotContains() {
  194. $res = $this->Comments->containsNotBit(BitmaskedComment::STATUS_PUBLISHED);
  195. $expected = array('(BitmaskedComments.status & 2 != 2)');
  196. $this->assertEquals($expected, $res);
  197. $conditions = $res;
  198. $res = $this->Comments->find('all', array('conditions' => $conditions))->toArray();
  199. $this->assertTrue(!empty($res) && count($res) === 4);
  200. // multiple (AND)
  201. $res = $this->Comments->containsNotBit(array(BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_ACTIVE));
  202. $expected = array('(BitmaskedComments.status & 3 != 3)');
  203. $this->assertEquals($expected, $res);
  204. $conditions = $res;
  205. $res = $this->Comments->find('all', array('conditions' => $conditions))->toArray();
  206. $this->assertTrue(!empty($res) && count($res) === 5);
  207. }
  208. }