BitmaskedBehaviorTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. App::uses('BitmaskedBehavior', 'Tools.Model/Behavior');
  3. App::uses('AppModel', 'Model');
  4. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  5. App::uses('MyModel', 'Tools.Model');
  6. class BitmaskedBehaviorTest extends MyCakeTestCase {
  7. public $fixtures = [
  8. 'plugin.tools.bitmasked_comment'
  9. ];
  10. public $Comment;
  11. public function setUp() {
  12. parent::setUp();
  13. App::build([
  14. 'Model' => [CakePlugin::path('Tools') . 'Test' . DS . 'test_app' . DS . 'Model' . DS],
  15. ], App::RESET);
  16. $this->Comment = ClassRegistry::init('BitmaskedComment');
  17. $this->Comment->Behaviors->load('Tools.Bitmasked', ['mappedField' => 'statuses']);
  18. }
  19. public function testEncodeBitmask() {
  20. $res = $this->Comment->encodeBitmask([BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED]);
  21. $expected = BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED;
  22. $this->assertSame($expected, $res);
  23. }
  24. public function testDecodeBitmask() {
  25. $res = $this->Comment->decodeBitmask(BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED);
  26. $expected = [BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED];
  27. $this->assertSame($expected, $res);
  28. }
  29. public function testFind() {
  30. $res = $this->Comment->find('all');
  31. $this->assertTrue(!empty($res) && is_array($res));
  32. $this->assertTrue(!empty($res[1]['BitmaskedComment']['statuses']) && is_array($res[1]['BitmaskedComment']['statuses']));
  33. //debug($res[count($res)-1]);
  34. }
  35. public function testSave() {
  36. $data = [
  37. 'comment' => 'test save',
  38. 'statuses' => [],
  39. ];
  40. $this->Comment->create();
  41. $this->Comment->set($data);
  42. $res = $this->Comment->validates();
  43. $this->assertTrue($res);
  44. $is = $this->Comment->data['BitmaskedComment']['status'];
  45. $this->assertSame('0', $is);
  46. $data = [
  47. 'comment' => 'test save',
  48. 'statuses' => [BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED],
  49. ];
  50. $this->Comment->create();
  51. $this->Comment->set($data);
  52. $res = $this->Comment->validates();
  53. $this->assertTrue($res);
  54. $is = $this->Comment->data['BitmaskedComment']['status'];
  55. $this->assertSame(BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED, $is);
  56. // save + find
  57. $this->Comment->create();
  58. $res = $this->Comment->save($data);
  59. $this->assertTrue(!empty($res));
  60. $res = $this->Comment->find('first', ['conditions' => ['statuses' => $data['statuses']]]);
  61. $this->assertTrue(!empty($res));
  62. $expected = BitmaskedComment::STATUS_APPROVED | BitmaskedComment::STATUS_PUBLISHED; // 6
  63. $this->assertEquals($expected, $res['BitmaskedComment']['status']);
  64. $expected = $data['statuses'];
  65. $this->assertEquals($expected, $res['BitmaskedComment']['statuses']);
  66. // model.field syntax
  67. $res = $this->Comment->find('first', ['conditions' => ['BitmaskedComment.statuses' => $data['statuses']]]);
  68. $this->assertTrue(!empty($res));
  69. // explitit
  70. $activeApprovedAndPublished = BitmaskedComment::STATUS_ACTIVE | BitmaskedComment::STATUS_APPROVED | BitmaskedComment::STATUS_PUBLISHED;
  71. $data = [
  72. 'comment' => 'another post comment',
  73. 'status' => $activeApprovedAndPublished,
  74. ];
  75. $this->Comment->create();
  76. $res = $this->Comment->save($data);
  77. $this->assertTrue(!empty($res));
  78. $res = $this->Comment->find('first', ['conditions' => ['status' => $activeApprovedAndPublished]]);
  79. $this->assertTrue(!empty($res));
  80. $this->assertEquals($activeApprovedAndPublished, $res['BitmaskedComment']['status']);
  81. $expected = [BitmaskedComment::STATUS_ACTIVE, BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED];
  82. $this->assertEquals($expected, $res['BitmaskedComment']['statuses']);
  83. }
  84. /**
  85. * Assert that you can manually trigger "notEmpty" rule with null instead of 0 for "not null" db fields
  86. */
  87. public function testSaveWithDefaultValue() {
  88. $this->Comment->Behaviors->unload('Bitmasked');
  89. $this->Comment->Behaviors->load('Tools.Bitmasked', ['mappedField' => 'statuses', 'defaultValue' => '']);
  90. $data = [
  91. 'comment' => 'test save',
  92. 'statuses' => [],
  93. ];
  94. $this->Comment->create();
  95. $this->Comment->set($data);
  96. $res = $this->Comment->validates();
  97. //debug($this->Comment->data);
  98. $this->assertFalse($res);
  99. $is = $this->Comment->data['BitmaskedComment']['status'];
  100. $this->assertSame('', $is);
  101. }
  102. public function testIs() {
  103. $res = $this->Comment->isBit(BitmaskedComment::STATUS_PUBLISHED);
  104. $expected = ['BitmaskedComment.status' => 2];
  105. $this->assertEquals($expected, $res);
  106. }
  107. public function testIsNot() {
  108. $res = $this->Comment->isNotBit(BitmaskedComment::STATUS_PUBLISHED);
  109. $expected = ['NOT' => ['BitmaskedComment.status' => 2]];
  110. $this->assertEquals($expected, $res);
  111. }
  112. public function testContains() {
  113. $res = $this->Comment->containsBit(BitmaskedComment::STATUS_PUBLISHED);
  114. $expected = ['(BitmaskedComment.status & ? = ?)' => [2, 2]];
  115. $this->assertEquals($expected, $res);
  116. $conditions = $res;
  117. $res = $this->Comment->find('all', ['conditions' => $conditions]);
  118. $this->assertTrue(!empty($res) && count($res) === 3);
  119. // multiple (AND)
  120. $res = $this->Comment->containsBit([BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_ACTIVE]);
  121. $expected = ['(BitmaskedComment.status & ? = ?)' => [3, 3]];
  122. $this->assertEquals($expected, $res);
  123. $conditions = $res;
  124. $res = $this->Comment->find('all', ['conditions' => $conditions]);
  125. $this->assertTrue(!empty($res) && count($res) === 2);
  126. }
  127. public function testNotContains() {
  128. $res = $this->Comment->containsNotBit(BitmaskedComment::STATUS_PUBLISHED);
  129. $expected = ['(BitmaskedComment.status & ? != ?)' => [2, 2]];
  130. $this->assertEquals($expected, $res);
  131. $conditions = $res;
  132. $res = $this->Comment->find('all', ['conditions' => $conditions]);
  133. $this->assertTrue(!empty($res) && count($res) === 4);
  134. // multiple (AND)
  135. $res = $this->Comment->containsNotBit([BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_ACTIVE]);
  136. $expected = ['(BitmaskedComment.status & ? != ?)' => [3, 3]];
  137. $this->assertEquals($expected, $res);
  138. $conditions = $res;
  139. $res = $this->Comment->find('all', ['conditions' => $conditions]);
  140. $this->assertTrue(!empty($res) && count($res) === 5);
  141. }
  142. public function testSaveMultiFields() {
  143. $this->Comment->Behaviors->unload('Bitmasked');
  144. $this->Comment->Behaviors->load('Tools.Bitmasked', [
  145. ['mappedField' => 'types', 'field' => 'type'],
  146. ['mappedField' => 'statuses', 'field' => 'status'],
  147. ]);
  148. $data = [
  149. 'comment' => 'test save',
  150. 'types' => [
  151. BitmaskedComment::TYPE_COMPLAINT,
  152. BitmaskedComment::TYPE_RFC,
  153. ],
  154. 'statuses' => [
  155. BitmaskedComment::STATUS_ACTIVE,
  156. BitmaskedComment::STATUS_APPROVED,
  157. ],
  158. ];
  159. $this->Comment->create();
  160. $result = $this->Comment->save($data);
  161. $expectedStatus = BitmaskedComment::STATUS_ACTIVE | BitmaskedComment::STATUS_APPROVED;
  162. $this->assertEquals($expectedStatus, $result['BitmaskedComment']['status']);
  163. $expectedType = BitmaskedComment::TYPE_COMPLAINT | BitmaskedComment::TYPE_RFC;
  164. $this->assertEquals($expectedType, $result['BitmaskedComment']['type']);
  165. }
  166. }