BitmaskedBehaviorTest.php 8.3 KB

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