BitmaskedBehaviorTest.php 7.7 KB

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