BitmaskedBehaviorTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 $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. $options = [
  84. 'bits' => [],
  85. 'type' => 'contain',
  86. ];
  87. $res = $this->Comments->find('bits', $options)->toArray();
  88. $this->assertCount(1, $res);
  89. $this->assertSame([], $res[0]->statuses);
  90. $options = [
  91. 'bits' => [BitmaskedComment::STATUS_APPROVED],
  92. 'type' => 'contain',
  93. ];
  94. $res = $this->Comments->find('bits', $options)->toArray();
  95. $this->assertCount(3, $res);
  96. $options = [
  97. 'bits' => [BitmaskedComment::STATUS_APPROVED, BitmaskedComment::STATUS_PUBLISHED],
  98. 'type' => 'contain',
  99. ];
  100. $res = $this->Comments->find('bits', $options)->toArray();
  101. $this->assertCount(5, $res);
  102. }
  103. /**
  104. * @return void
  105. */
  106. public function testFindBitmaskedContainAnd() {
  107. $options = [
  108. 'bits' => [],
  109. 'type' => 'contain',
  110. 'containMode' => 'and',
  111. ];
  112. $res = $this->Comments->find('bits', $options)->toArray();
  113. $this->assertCount(1, $res);
  114. $this->assertSame([], $res[0]->statuses);
  115. $options = [
  116. 'bits' => [BitmaskedComment::STATUS_APPROVED],
  117. 'type' => 'contain',
  118. 'containMode' => 'and',
  119. ];
  120. $res = $this->Comments->find('bits', $options)->toArray();
  121. $this->assertCount(3, $res);
  122. $options = [
  123. 'bits' => [BitmaskedComment::STATUS_APPROVED, BitmaskedComment::STATUS_PUBLISHED],
  124. 'type' => 'contain',
  125. 'containMode' => 'and',
  126. ];
  127. $res = $this->Comments->find('bits', $options)->toArray();
  128. $this->assertCount(1, $res);
  129. }
  130. /**
  131. * @return void
  132. */
  133. public function testSaveBasic() {
  134. $data = [
  135. 'comment' => 'test save',
  136. 'statuses' => [],
  137. ];
  138. $entity = $this->Comments->newEntity($data);
  139. $res = $this->Comments->save($entity);
  140. $this->assertTrue((bool)$res);
  141. $this->assertSame(0, $entity->get('status'));
  142. $data = [
  143. 'comment' => 'test save',
  144. 'statuses' => [BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED],
  145. ];
  146. $entity = $this->Comments->newEntity($data);
  147. $res = $this->Comments->save($entity);
  148. $this->assertTrue((bool)$res);
  149. $is = $entity->get('status');
  150. $this->assertSame(BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED, $is);
  151. // save + find
  152. $entity = $this->Comments->newEntity($data);
  153. $this->assertEmpty($entity->getErrors());
  154. $res = $this->Comments->save($entity);
  155. $this->assertTrue((bool)$res);
  156. $res = $this->Comments->find()->where(['statuses IN' => $data['statuses']])->first();
  157. $this->assertTrue(!empty($res));
  158. $expected = BitmaskedComment::STATUS_APPROVED | BitmaskedComment::STATUS_PUBLISHED; // 6
  159. $this->assertEquals($expected, $res['status']);
  160. $expected = $data['statuses'];
  161. $this->assertEquals($expected, $res['statuses']);
  162. // model.field syntax
  163. $res = $this->Comments->find()->where(['BitmaskedComments.statuses IN' => $data['statuses']])->first();
  164. $this->assertTrue((bool)$res);
  165. // explicit
  166. $activeApprovedAndPublished = BitmaskedComment::STATUS_ACTIVE | BitmaskedComment::STATUS_APPROVED | BitmaskedComment::STATUS_PUBLISHED;
  167. $data = [
  168. 'comment' => 'another post comment',
  169. 'status' => $activeApprovedAndPublished,
  170. ];
  171. $entity = $this->Comments->newEntity($data);
  172. $res = $this->Comments->save($entity);
  173. $this->assertTrue((bool)$res);
  174. $res = $this->Comments->find()->where(['status' => $activeApprovedAndPublished])->first();
  175. $this->assertTrue((bool)$res);
  176. $this->assertEquals($activeApprovedAndPublished, $res['status']);
  177. $expected = [BitmaskedComment::STATUS_ACTIVE, BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED];
  178. $this->assertEquals($expected, $res['statuses']);
  179. }
  180. /**
  181. * Assert that you can manually trigger "notEmpty" rule with null instead of 0 for "not null" db fields
  182. *
  183. * @return void
  184. */
  185. public function testSaveWithDefaultValue() {
  186. $data = [
  187. 'comment' => 'test save',
  188. 'statuses' => [],
  189. ];
  190. $entity = $this->Comments->newEntity($data);
  191. $res = $this->Comments->save($entity);
  192. $this->assertTrue((bool)$res);
  193. $this->assertSame(0, $entity->get('status'));
  194. $this->skipIf(true, '//FIXME');
  195. // Now let's set the default value
  196. $this->Comments->removeBehavior('Bitmasked');
  197. $this->Comments->addBehavior('Tools.Bitmasked', ['mappedField' => 'statuses', 'defaultValue' => '']);
  198. $data = [
  199. 'comment' => 'test save',
  200. 'statuses' => [],
  201. ];
  202. $entity = $this->Comments->newEntity($data);
  203. $res = $this->Comments->save($entity);
  204. $this->assertFalse($res);
  205. $this->assertSame('', $entity->get('status'));
  206. }
  207. /**
  208. * Assert that it also works with beforeSave event callback.
  209. *
  210. * @return void
  211. */
  212. public function testSaveOnBeforeSave() {
  213. $this->Comments->removeBehavior('Bitmasked');
  214. $this->Comments->addBehavior('Tools.Bitmasked', ['mappedField' => 'statuses', 'on' => 'beforeSave']);
  215. $data = [
  216. 'comment' => 'test save',
  217. 'statuses' => [BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED],
  218. ];
  219. $entity = $this->Comments->newEntity($data);
  220. $this->assertEmpty($entity->getErrors());
  221. $res = $this->Comments->save($entity);
  222. $this->assertTrue((bool)$res);
  223. $this->assertSame(BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED, $res['status']);
  224. }
  225. /**
  226. * @return void
  227. */
  228. public function testIs() {
  229. $res = $this->Comments->isBit(BitmaskedComment::STATUS_PUBLISHED);
  230. $expected = ['BitmaskedComments.status' => 2];
  231. $this->assertEquals($expected, $res);
  232. }
  233. /**
  234. * @return void
  235. */
  236. public function testIsNot() {
  237. $res = $this->Comments->isNotBit(BitmaskedComment::STATUS_PUBLISHED);
  238. $expected = ['NOT' => ['BitmaskedComments.status' => 2]];
  239. $this->assertEquals($expected, $res);
  240. }
  241. /**
  242. * @return void
  243. */
  244. public function testContains() {
  245. $config = $this->Comments->getConnection()->config();
  246. $isPostgres = strpos($config['driver'], 'Postgres') !== false;
  247. $res = $this->Comments->containsBit(BitmaskedComment::STATUS_PUBLISHED);
  248. $expected = ['(BitmaskedComments.status & 2 = 2)'];
  249. if ($isPostgres) {
  250. $expected = ['("BitmaskedComments"."status" & 2 = 2)'];
  251. }
  252. $this->assertEquals($expected, $res);
  253. $conditions = $res;
  254. $res = $this->Comments->find('all', ['conditions' => $conditions])->toArray();
  255. $this->assertTrue(!empty($res) && count($res) === 3);
  256. // multiple (AND)
  257. $res = $this->Comments->containsBit([BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_ACTIVE]);
  258. $expected = ['(BitmaskedComments.status & 3 = 3)'];
  259. if ($isPostgres) {
  260. $expected = ['("BitmaskedComments"."status" & 3 = 3)'];
  261. }
  262. $this->assertEquals($expected, $res);
  263. $conditions = $res;
  264. $res = $this->Comments->find('all', ['conditions' => $conditions])->toArray();
  265. $this->assertTrue(!empty($res) && count($res) === 2);
  266. }
  267. /**
  268. * @return void
  269. */
  270. public function testNotContains() {
  271. $config = $this->Comments->getConnection()->config();
  272. $isPostgres = strpos($config['driver'], 'Postgres') !== false;
  273. $res = $this->Comments->containsNotBit(BitmaskedComment::STATUS_PUBLISHED);
  274. $expected = ['(BitmaskedComments.status & 2 != 2)'];
  275. if ($isPostgres) {
  276. $expected = ['("BitmaskedComments"."status" & 2 != 2)'];
  277. }
  278. $this->assertEquals($expected, $res);
  279. $conditions = $res;
  280. $res = $this->Comments->find('all', ['conditions' => $conditions])->toArray();
  281. $this->assertTrue(!empty($res) && count($res) === 4);
  282. // multiple (AND)
  283. $res = $this->Comments->containsNotBit([BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_ACTIVE]);
  284. $expected = ['(BitmaskedComments.status & 3 != 3)'];
  285. if ($isPostgres) {
  286. $expected = ['("BitmaskedComments"."status" & 3 != 3)'];
  287. }
  288. $this->assertEquals($expected, $res);
  289. $conditions = $res;
  290. $res = $this->Comments->find('all', ['conditions' => $conditions])->toArray();
  291. $this->assertTrue(!empty($res) && count($res) === 5);
  292. }
  293. }