BitmaskedBehaviorTest.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. namespace Tools\Test\TestCase\Model\Behavior;
  3. use App\Model\Entity\BitmaskedComment;
  4. use Cake\ORM\TableRegistry;
  5. use Tools\TestSuite\TestCase;
  6. class BitmaskedBehaviorTest extends TestCase {
  7. /**
  8. * @var array
  9. */
  10. public $fixtures = [
  11. 'plugin.Tools.BitmaskedComments'
  12. ];
  13. /**
  14. * @var \Tools\Model\Table\Table|\Tools\Model\Behavior\BitmaskedBehavior
  15. */
  16. public $Comments;
  17. /**
  18. * @return void
  19. */
  20. public function setUp() {
  21. parent::setUp();
  22. $this->Comments = TableRegistry::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. * @expectedException \RuntimeException
  38. * @expectedExceptionMessage Bits not found for field my_field, expected pluralized static method myFields() on the entity.
  39. */
  40. public function testFieldMethodMissing() {
  41. $this->Comments->removeBehavior('Bitmasked');
  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. }
  97. /**
  98. * @return void
  99. */
  100. public function testSaveBasic() {
  101. $data = [
  102. 'comment' => 'test save',
  103. 'statuses' => [],
  104. ];
  105. $entity = $this->Comments->newEntity($data);
  106. $res = $this->Comments->save($entity);
  107. $this->assertTrue((bool)$res);
  108. $this->assertSame(0, $entity->get('status'));
  109. $data = [
  110. 'comment' => 'test save',
  111. 'statuses' => [BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED],
  112. ];
  113. $entity = $this->Comments->newEntity($data);
  114. $res = $this->Comments->save($entity);
  115. $this->assertTrue((bool)$res);
  116. $is = $entity->get('status');
  117. $this->assertSame(BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED, $is);
  118. // save + find
  119. $entity = $this->Comments->newEntity($data);
  120. $this->assertEmpty($entity->getErrors());
  121. $res = $this->Comments->save($entity);
  122. $this->assertTrue((bool)$res);
  123. $res = $this->Comments->find('first', ['conditions' => ['statuses' => $data['statuses']]]);
  124. $this->assertTrue(!empty($res));
  125. $expected = BitmaskedComment::STATUS_APPROVED | BitmaskedComment::STATUS_PUBLISHED; // 6
  126. $this->assertEquals($expected, $res['status']);
  127. $expected = $data['statuses'];
  128. $this->assertEquals($expected, $res['statuses']);
  129. // model.field syntax
  130. $res = $this->Comments->find('first', ['conditions' => ['BitmaskedComments.statuses' => $data['statuses']]]);
  131. $this->assertTrue((bool)$res->toArray());
  132. // explicit
  133. $activeApprovedAndPublished = BitmaskedComment::STATUS_ACTIVE | BitmaskedComment::STATUS_APPROVED | BitmaskedComment::STATUS_PUBLISHED;
  134. $data = [
  135. 'comment' => 'another post comment',
  136. 'status' => $activeApprovedAndPublished,
  137. ];
  138. $entity = $this->Comments->newEntity($data);
  139. $res = $this->Comments->save($entity);
  140. $this->assertTrue((bool)$res);
  141. $res = $this->Comments->find('first', ['conditions' => ['status' => $activeApprovedAndPublished]]);
  142. $this->assertTrue((bool)$res);
  143. $this->assertEquals($activeApprovedAndPublished, $res['status']);
  144. $expected = [BitmaskedComment::STATUS_ACTIVE, BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED];
  145. $this->assertEquals($expected, $res['statuses']);
  146. }
  147. /**
  148. * Assert that you can manually trigger "notEmpty" rule with null instead of 0 for "not null" db fields
  149. *
  150. * @return void
  151. */
  152. public function testSaveWithDefaultValue() {
  153. $data = [
  154. 'comment' => 'test save',
  155. 'statuses' => [],
  156. ];
  157. $entity = $this->Comments->newEntity($data);
  158. $res = $this->Comments->save($entity);
  159. $this->assertTrue((bool)$res);
  160. $this->assertSame(0, $entity->get('status'));
  161. $this->skipIf(true, '//FIXME');
  162. // Now let's set the default value
  163. $this->Comments->removeBehavior('Bitmasked');
  164. $this->Comments->addBehavior('Tools.Bitmasked', ['mappedField' => 'statuses', 'defaultValue' => '']);
  165. $data = [
  166. 'comment' => 'test save',
  167. 'statuses' => [],
  168. ];
  169. $entity = $this->Comments->newEntity($data);
  170. $res = $this->Comments->save($entity);
  171. $this->assertFalse($res);
  172. $this->assertSame('', $entity->get('status'));
  173. }
  174. /**
  175. * Assert that it also works with beforeSave event callback.
  176. *
  177. * @return void
  178. */
  179. public function testSaveOnBeforeSave() {
  180. $this->Comments->removeBehavior('Bitmasked');
  181. $this->Comments->addBehavior('Tools.Bitmasked', ['mappedField' => 'statuses', 'on' => 'beforeSave']);
  182. $data = [
  183. 'comment' => 'test save',
  184. 'statuses' => [BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED],
  185. ];
  186. $entity = $this->Comments->newEntity($data);
  187. $this->assertEmpty($entity->getErrors());
  188. $res = $this->Comments->save($entity);
  189. $this->assertTrue((bool)$res);
  190. $this->assertSame(BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED, $res['status']);
  191. }
  192. /**
  193. * @return void
  194. */
  195. public function testIs() {
  196. $res = $this->Comments->isBit(BitmaskedComment::STATUS_PUBLISHED);
  197. $expected = ['BitmaskedComments.status' => 2];
  198. $this->assertEquals($expected, $res);
  199. }
  200. /**
  201. * @return void
  202. */
  203. public function testIsNot() {
  204. $res = $this->Comments->isNotBit(BitmaskedComment::STATUS_PUBLISHED);
  205. $expected = ['NOT' => ['BitmaskedComments.status' => 2]];
  206. $this->assertEquals($expected, $res);
  207. }
  208. /**
  209. * @return void
  210. */
  211. public function testContains() {
  212. $config = $this->Comments->getConnection()->config();
  213. $isPostgres = strpos($config['driver'], 'Postgres') !== false;
  214. $res = $this->Comments->containsBit(BitmaskedComment::STATUS_PUBLISHED);
  215. $expected = ['(BitmaskedComments.status & 2 = 2)'];
  216. if ($isPostgres) {
  217. $expected = ['("BitmaskedComments"."status" & 2 = 2)'];
  218. }
  219. $this->assertEquals($expected, $res);
  220. $conditions = $res;
  221. $res = $this->Comments->find('all', ['conditions' => $conditions])->toArray();
  222. $this->assertTrue(!empty($res) && count($res) === 3);
  223. // multiple (AND)
  224. $res = $this->Comments->containsBit([BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_ACTIVE]);
  225. $expected = ['(BitmaskedComments.status & 3 = 3)'];
  226. if ($isPostgres) {
  227. $expected = ['("BitmaskedComments"."status" & 3 = 3)'];
  228. }
  229. $this->assertEquals($expected, $res);
  230. $conditions = $res;
  231. $res = $this->Comments->find('all', ['conditions' => $conditions])->toArray();
  232. $this->assertTrue(!empty($res) && count($res) === 2);
  233. }
  234. /**
  235. * @return void
  236. */
  237. public function testNotContains() {
  238. $config = $this->Comments->getConnection()->config();
  239. $isPostgres = strpos($config['driver'], 'Postgres') !== false;
  240. $res = $this->Comments->containsNotBit(BitmaskedComment::STATUS_PUBLISHED);
  241. $expected = ['(BitmaskedComments.status & 2 != 2)'];
  242. if ($isPostgres) {
  243. $expected = ['("BitmaskedComments"."status" & 2 != 2)'];
  244. }
  245. $this->assertEquals($expected, $res);
  246. $conditions = $res;
  247. $res = $this->Comments->find('all', ['conditions' => $conditions])->toArray();
  248. $this->assertTrue(!empty($res) && count($res) === 4);
  249. // multiple (AND)
  250. $res = $this->Comments->containsNotBit([BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_ACTIVE]);
  251. $expected = ['(BitmaskedComments.status & 3 != 3)'];
  252. if ($isPostgres) {
  253. $expected = ['("BitmaskedComments"."status" & 3 != 3)'];
  254. }
  255. $this->assertEquals($expected, $res);
  256. $conditions = $res;
  257. $res = $this->Comments->find('all', ['conditions' => $conditions])->toArray();
  258. $this->assertTrue(!empty($res) && count($res) === 5);
  259. }
  260. }