BitmaskedBehaviorTest.php 7.4 KB

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