BitmaskedBehaviorTest.php 11 KB

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