BitmaskedBehaviorTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. namespace Tools\Test\TestCase\Model\Behavior;
  3. use RuntimeException;
  4. use Shim\TestSuite\TestCase;
  5. use TestApp\Model\Entity\BitmaskedComment;
  6. class BitmaskedBehaviorTest extends TestCase {
  7. /**
  8. * @var array
  9. */
  10. protected array $fixtures = [
  11. 'plugin.Tools.BitmaskedComments',
  12. ];
  13. /**
  14. * @var \Tools\Model\Table\Table|\Tools\Model\Behavior\BitmaskedBehavior
  15. */
  16. protected $Comments;
  17. /**
  18. * @return void
  19. */
  20. public function setUp(): void {
  21. parent::setUp();
  22. $this->Comments = $this->getTableLocator()->get('BitmaskedComments');
  23. $this->Comments->addBehavior('Tools.Bitmasked', ['mappedField' => 'statuses']);
  24. }
  25. /**
  26. * @return void
  27. */
  28. public function testConfig() {
  29. $this->Comments->removeBehavior('Bitmasked');
  30. $this->Comments->addBehavior('Tools.Bitmasked', []);
  31. $bits = $this->Comments->behaviors()->Bitmasked->getConfig('bits');
  32. $expected = BitmaskedComment::statuses();
  33. $this->assertSame($expected, $bits);
  34. }
  35. /**
  36. * @return void
  37. */
  38. public function testFieldMethodMissing() {
  39. $this->Comments->removeBehavior('Bitmasked');
  40. $this->expectException(RuntimeException::class);
  41. $this->expectExceptionMessage('Bits not found for field my_field, expected pluralized static method myFields() on the entity.');
  42. $this->Comments->addBehavior('Tools.Bitmasked', ['field' => 'my_field']);
  43. }
  44. /**
  45. * @return void
  46. */
  47. public function testEncodeBitmask() {
  48. $res = $this->Comments->encodeBitmask([BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED]);
  49. $expected = BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED;
  50. $this->assertSame($expected, $res);
  51. }
  52. /**
  53. * @return void
  54. */
  55. public function testDecodeBitmask() {
  56. $res = $this->Comments->decodeBitmask(BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED);
  57. $expected = [BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED];
  58. $this->assertSame($expected, $res);
  59. }
  60. /**
  61. * @return void
  62. */
  63. public function testFind() {
  64. $res = $this->Comments->find('all')->toArray();
  65. $this->assertTrue(!empty($res) && is_array($res));
  66. $this->assertTrue(!empty($res[1]['statuses']) && is_array($res[1]['statuses']));
  67. }
  68. /**
  69. * @return void
  70. */
  71. public function testFindBitmasked() {
  72. $res = $this->Comments->find('bits', bits: [])->toArray();
  73. $this->assertCount(1, $res);
  74. $this->assertSame([], $res[0]->statuses);
  75. $res = $this->Comments->find('bits', bits: [BitmaskedComment::STATUS_ACTIVE, BitmaskedComment::STATUS_APPROVED])->toArray();
  76. $this->assertCount(1, $res);
  77. $this->assertSame([BitmaskedComment::STATUS_ACTIVE, BitmaskedComment::STATUS_APPROVED], $res[0]->statuses);
  78. }
  79. /**
  80. * @return void
  81. */
  82. public function testFindBitmaskedContain() {
  83. $bits = [];
  84. $options = [
  85. 'type' => 'contain',
  86. ];
  87. $res = $this->Comments->find('bits', bits: $bits, options: $options)->toArray();
  88. $this->assertCount(1, $res);
  89. $this->assertSame([], $res[0]->statuses);
  90. $bits = [BitmaskedComment::STATUS_APPROVED];
  91. $options = [
  92. 'type' => 'contain',
  93. ];
  94. $res = $this->Comments->find('bits', bits: $bits, options: $options)->toArray();
  95. $this->assertCount(3, $res);
  96. $bits = [BitmaskedComment::STATUS_APPROVED, BitmaskedComment::STATUS_PUBLISHED];
  97. $options = [
  98. 'type' => 'contain',
  99. ];
  100. $res = $this->Comments->find('bits', bits: $bits, options: $options)->toArray();
  101. $this->assertCount(5, $res);
  102. }
  103. /**
  104. * @return void
  105. */
  106. public function testFindBitmaskedContainAnd() {
  107. $bits = [];
  108. $options = [
  109. 'type' => 'contain',
  110. 'containMode' => 'and',
  111. ];
  112. $res = $this->Comments->find('bits', bits: $bits, options: $options)->toArray();
  113. $this->assertCount(1, $res);
  114. $this->assertSame([], $res[0]->statuses);
  115. $bits = [BitmaskedComment::STATUS_APPROVED];
  116. $res = $this->Comments->find('bits', bits: $bits, options: $options)->toArray();
  117. $this->assertCount(3, $res);
  118. $bits = [BitmaskedComment::STATUS_APPROVED, BitmaskedComment::STATUS_PUBLISHED];
  119. $res = $this->Comments->find('bits', bits: $bits, options: $options)->toArray();
  120. $this->assertCount(1, $res);
  121. }
  122. /**
  123. * @return void
  124. */
  125. public function testSaveBasic() {
  126. $data = [
  127. 'comment' => 'test save',
  128. 'statuses' => [],
  129. ];
  130. $entity = $this->Comments->newEntity($data);
  131. $res = $this->Comments->save($entity);
  132. $this->assertTrue((bool)$res);
  133. $this->assertSame(0, $entity->get('status'));
  134. $data = [
  135. 'comment' => 'test save',
  136. 'statuses' => [BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED],
  137. ];
  138. $entity = $this->Comments->newEntity($data);
  139. $res = $this->Comments->save($entity);
  140. $this->assertTrue((bool)$res);
  141. $is = $entity->get('status');
  142. $this->assertSame(BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED, $is);
  143. // save + find
  144. $entity = $this->Comments->newEntity($data);
  145. $this->assertEmpty($entity->getErrors());
  146. $res = $this->Comments->save($entity);
  147. $this->assertTrue((bool)$res);
  148. $res = $this->Comments->find()->where(['statuses IN' => $data['statuses']])->first();
  149. $this->assertTrue(!empty($res));
  150. $expected = BitmaskedComment::STATUS_APPROVED | BitmaskedComment::STATUS_PUBLISHED; // 6
  151. $this->assertEquals($expected, $res['status']);
  152. $expected = $data['statuses'];
  153. $this->assertEquals($expected, $res['statuses']);
  154. // model.field syntax
  155. $res = $this->Comments->find()->where(['BitmaskedComments.statuses IN' => $data['statuses']])->first();
  156. $this->assertTrue((bool)$res);
  157. // explicit
  158. $activeApprovedAndPublished = BitmaskedComment::STATUS_ACTIVE | BitmaskedComment::STATUS_APPROVED | BitmaskedComment::STATUS_PUBLISHED;
  159. $data = [
  160. 'comment' => 'another post comment',
  161. 'status' => $activeApprovedAndPublished,
  162. ];
  163. $entity = $this->Comments->newEntity($data);
  164. $res = $this->Comments->save($entity);
  165. $this->assertTrue((bool)$res);
  166. $res = $this->Comments->find()->where(['status' => $activeApprovedAndPublished])->first();
  167. $this->assertTrue((bool)$res);
  168. $this->assertEquals($activeApprovedAndPublished, $res['status']);
  169. $expected = [BitmaskedComment::STATUS_ACTIVE, BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED];
  170. $this->assertEquals($expected, $res['statuses']);
  171. }
  172. /**
  173. * Assert that you can manually trigger "notEmpty" rule with null instead of 0 for "not null" db fields
  174. *
  175. * @return void
  176. */
  177. public function testSaveWithDefaultValue() {
  178. $data = [
  179. 'comment' => 'test save',
  180. 'statuses' => [],
  181. ];
  182. $entity = $this->Comments->newEntity($data);
  183. $res = $this->Comments->save($entity);
  184. $this->assertTrue((bool)$res);
  185. $this->assertSame(0, $entity->get('status'));
  186. $this->skipIf(true, '//FIXME');
  187. // Now let's set the default value
  188. $this->Comments->removeBehavior('Bitmasked');
  189. $this->Comments->addBehavior('Tools.Bitmasked', ['mappedField' => 'statuses', 'defaultValue' => '']);
  190. $data = [
  191. 'comment' => 'test save',
  192. 'statuses' => [],
  193. ];
  194. $entity = $this->Comments->newEntity($data);
  195. $res = $this->Comments->save($entity);
  196. $this->assertFalse($res);
  197. $this->assertSame('', $entity->get('status'));
  198. }
  199. /**
  200. * Assert that it also works with beforeSave event callback.
  201. *
  202. * @return void
  203. */
  204. public function testSaveOnBeforeSave() {
  205. $this->Comments->removeBehavior('Bitmasked');
  206. $this->Comments->addBehavior('Tools.Bitmasked', ['mappedField' => 'statuses', 'on' => 'beforeSave']);
  207. $data = [
  208. 'comment' => 'test save',
  209. 'statuses' => [BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED],
  210. ];
  211. $entity = $this->Comments->newEntity($data);
  212. $this->assertEmpty($entity->getErrors());
  213. $res = $this->Comments->save($entity);
  214. $this->assertTrue((bool)$res);
  215. $this->assertSame(BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED, $res['status']);
  216. }
  217. /**
  218. * @return void
  219. */
  220. public function testIs() {
  221. $res = $this->Comments->isBit(BitmaskedComment::STATUS_PUBLISHED);
  222. $expected = ['BitmaskedComments.status' => 2];
  223. $this->assertEquals($expected, $res);
  224. }
  225. /**
  226. * @return void
  227. */
  228. public function testIsNot() {
  229. $res = $this->Comments->isNotBit(BitmaskedComment::STATUS_PUBLISHED);
  230. $expected = ['NOT' => ['BitmaskedComments.status' => 2]];
  231. $this->assertEquals($expected, $res);
  232. }
  233. /**
  234. * @return void
  235. */
  236. public function testContains() {
  237. $config = $this->Comments->getConnection()->config();
  238. $isPostgres = strpos($config['driver'], 'Postgres') !== false;
  239. $res = $this->Comments->containsBit(BitmaskedComment::STATUS_PUBLISHED);
  240. $expected = ['(BitmaskedComments.status & 2 = 2)'];
  241. if ($isPostgres) {
  242. $expected = ['("BitmaskedComments"."status" & 2 = 2)'];
  243. }
  244. $this->assertEquals($expected, $res);
  245. $conditions = $res;
  246. $res = $this->Comments->find('all', ['conditions' => $conditions])->toArray();
  247. $this->assertTrue(!empty($res) && count($res) === 3);
  248. // multiple (AND)
  249. $res = $this->Comments->containsBit([BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_ACTIVE]);
  250. $expected = ['(BitmaskedComments.status & 3 = 3)'];
  251. if ($isPostgres) {
  252. $expected = ['("BitmaskedComments"."status" & 3 = 3)'];
  253. }
  254. $this->assertEquals($expected, $res);
  255. $conditions = $res;
  256. $res = $this->Comments->find('all', ['conditions' => $conditions])->toArray();
  257. $this->assertTrue(!empty($res) && count($res) === 2);
  258. }
  259. /**
  260. * @return void
  261. */
  262. public function testNotContains() {
  263. $config = $this->Comments->getConnection()->config();
  264. $isPostgres = strpos($config['driver'], 'Postgres') !== false;
  265. $res = $this->Comments->containsNotBit(BitmaskedComment::STATUS_PUBLISHED);
  266. $expected = ['(BitmaskedComments.status & 2 != 2)'];
  267. if ($isPostgres) {
  268. $expected = ['("BitmaskedComments"."status" & 2 != 2)'];
  269. }
  270. $this->assertEquals($expected, $res);
  271. $conditions = $res;
  272. $res = $this->Comments->find('all', ['conditions' => $conditions])->toArray();
  273. $this->assertTrue(!empty($res) && count($res) === 4);
  274. // multiple (AND)
  275. $res = $this->Comments->containsNotBit([BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_ACTIVE]);
  276. $expected = ['(BitmaskedComments.status & 3 != 3)'];
  277. if ($isPostgres) {
  278. $expected = ['("BitmaskedComments"."status" & 3 != 3)'];
  279. }
  280. $this->assertEquals($expected, $res);
  281. $conditions = $res;
  282. $res = $this->Comments->find('all', ['conditions' => $conditions])->toArray();
  283. $this->assertTrue(!empty($res) && count($res) === 5);
  284. }
  285. }