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. Configure::write('App.namespace', 'TestApp');
  24. $this->Comments = TableRegistry::get('BitmaskedComments');
  25. $this->Comments->addBehavior('Tools.Bitmasked', ['mappedField' => 'statuses']);
  26. }
  27. /**
  28. * @return void
  29. */
  30. public function testEncodeBitmask() {
  31. $res = $this->Comments->encodeBitmask([BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED]);
  32. $expected = BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED;
  33. $this->assertSame($expected, $res);
  34. }
  35. /**
  36. * @return void
  37. */
  38. public function testDecodeBitmask() {
  39. $res = $this->Comments->decodeBitmask(BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED);
  40. $expected = [BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED];
  41. $this->assertSame($expected, $res);
  42. }
  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. * @return void
  53. */
  54. public function testSaveBasic() {
  55. $data = [
  56. 'comment' => 'test save',
  57. 'statuses' => [],
  58. ];
  59. $entity = $this->Comments->newEntity($data);
  60. $res = $this->Comments->save($entity);
  61. $this->assertTrue((bool)$res);
  62. $this->assertSame('0', $entity->get('status'));
  63. $data = [
  64. 'comment' => 'test save',
  65. 'statuses' => [BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED],
  66. ];
  67. $entity = $this->Comments->newEntity($data);
  68. $res = $this->Comments->save($entity);
  69. $this->assertTrue((bool)$res);
  70. $is = $entity->get('status');
  71. $this->assertSame(BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED, $is);
  72. // save + find
  73. $entity = $this->Comments->newEntity($data);
  74. $this->assertEmpty($entity->errors());
  75. $res = $this->Comments->save($entity);
  76. $this->assertTrue((bool)$res);
  77. $res = $this->Comments->find('first', ['conditions' => ['statuses' => $data['statuses']]]);
  78. $this->assertTrue(!empty($res));
  79. $expected = BitmaskedComment::STATUS_APPROVED | BitmaskedComment::STATUS_PUBLISHED; // 6
  80. $this->assertEquals($expected, $res['status']);
  81. $expected = $data['statuses'];
  82. $this->assertEquals($expected, $res['statuses']);
  83. // model.field syntax
  84. $res = $this->Comments->find('first', ['conditions' => ['BitmaskedComments.statuses' => $data['statuses']]]);
  85. $this->assertTrue((bool)$res->toArray());
  86. // explicit
  87. $activeApprovedAndPublished = BitmaskedComment::STATUS_ACTIVE | BitmaskedComment::STATUS_APPROVED | BitmaskedComment::STATUS_PUBLISHED;
  88. $data = [
  89. 'comment' => 'another post comment',
  90. 'status' => $activeApprovedAndPublished,
  91. ];
  92. $entity = $this->Comments->newEntity($data);
  93. $res = $this->Comments->save($entity);
  94. $this->assertTrue((bool)$res);
  95. $res = $this->Comments->find('first', ['conditions' => ['status' => $activeApprovedAndPublished]]);
  96. $this->assertTrue((bool)$res);
  97. $this->assertEquals($activeApprovedAndPublished, $res['status']);
  98. $expected = [BitmaskedComment::STATUS_ACTIVE, BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED];
  99. $this->assertEquals($expected, $res['statuses']);
  100. }
  101. /**
  102. * Assert that you can manually trigger "notEmpty" rule with null instead of 0 for "not null" db fields
  103. *
  104. * @return void
  105. */
  106. public function testSaveWithDefaultValue() {
  107. $data = [
  108. 'comment' => 'test save',
  109. 'statuses' => [],
  110. ];
  111. $entity = $this->Comments->newEntity($data);
  112. $res = $this->Comments->save($entity);
  113. $this->assertTrue((bool)$res);
  114. $this->assertSame('0', $entity->get('status'));
  115. $this->skipIf(true, '//FIXME');
  116. // Now let's set the default value
  117. $this->Comments->removeBehavior('Bitmasked');
  118. $this->Comments->addBehavior('Tools.Bitmasked', ['mappedField' => 'statuses', 'defaultValue' => '']);
  119. $data = [
  120. 'comment' => 'test save',
  121. 'statuses' => [],
  122. ];
  123. $entity = $this->Comments->newEntity($data);
  124. $res = $this->Comments->save($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', ['mappedField' => 'statuses', 'on' => 'beforeSave']);
  136. $data = [
  137. 'comment' => 'test save',
  138. 'statuses' => [BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED],
  139. ];
  140. $entity = $this->Comments->newEntity($data);
  141. $this->assertEmpty($entity->errors());
  142. $res = $this->Comments->save($entity);
  143. $this->assertTrue((bool)$res);
  144. $this->assertSame(BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED, $res['status']);
  145. }
  146. /**
  147. * @return void
  148. */
  149. public function testIs() {
  150. $res = $this->Comments->isBit(BitmaskedComment::STATUS_PUBLISHED);
  151. $expected = ['BitmaskedComments.status' => 2];
  152. $this->assertEquals($expected, $res);
  153. }
  154. /**
  155. * @return void
  156. */
  157. public function testIsNot() {
  158. $res = $this->Comments->isNotBit(BitmaskedComment::STATUS_PUBLISHED);
  159. $expected = ['NOT' => ['BitmaskedComments.status' => 2]];
  160. $this->assertEquals($expected, $res);
  161. }
  162. /**
  163. * @return void
  164. */
  165. public function testContains() {
  166. $config = $this->Comments->connection()->config();
  167. $isPostgres = strpos($config['driver'], 'Postgres') !== false;
  168. $res = $this->Comments->containsBit(BitmaskedComment::STATUS_PUBLISHED);
  169. $expected = ['(BitmaskedComments.status & 2 = 2)'];
  170. if ($isPostgres) {
  171. $expected = ['("BitmaskedComments"."status" & 2 = 2)'];
  172. }
  173. $this->assertEquals($expected, $res);
  174. $conditions = $res;
  175. $res = $this->Comments->find('all', ['conditions' => $conditions])->toArray();
  176. $this->assertTrue(!empty($res) && count($res) === 3);
  177. // multiple (AND)
  178. $res = $this->Comments->containsBit([BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_ACTIVE]);
  179. $expected = ['(BitmaskedComments.status & 3 = 3)'];
  180. if ($isPostgres) {
  181. $expected = ['("BitmaskedComments"."status" & 3 = 3)'];
  182. }
  183. $this->assertEquals($expected, $res);
  184. $conditions = $res;
  185. $res = $this->Comments->find('all', ['conditions' => $conditions])->toArray();
  186. $this->assertTrue(!empty($res) && count($res) === 2);
  187. }
  188. /**
  189. * @return void
  190. */
  191. public function testNotContains() {
  192. $config = $this->Comments->connection()->config();
  193. $isPostgres = strpos($config['driver'], 'Postgres') !== false;
  194. $res = $this->Comments->containsNotBit(BitmaskedComment::STATUS_PUBLISHED);
  195. $expected = ['(BitmaskedComments.status & 2 != 2)'];
  196. if ($isPostgres) {
  197. $expected = ['("BitmaskedComments"."status" & 2 != 2)'];
  198. }
  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. if ($isPostgres) {
  207. $expected = ['("BitmaskedComments"."status" & 3 != 3)'];
  208. }
  209. $this->assertEquals($expected, $res);
  210. $conditions = $res;
  211. $res = $this->Comments->find('all', ['conditions' => $conditions])->toArray();
  212. $this->assertTrue(!empty($res) && count($res) === 5);
  213. }
  214. }