BitmaskedBehaviorTest.php 8.9 KB

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