BitmaskedBehaviorTest.php 8.6 KB

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