BitmaskedBehaviorTest.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. debug($this->Comment->validate);
  43. debug($this->Comment->data); ob_flush();
  44. $res = $this->Comment->validates();
  45. $this->assertTrue($res);
  46. $is = $this->Comment->data['BitmaskedComment']['status'];
  47. $this->assertSame('0', $is);
  48. $data = [
  49. 'comment' => 'test save',
  50. 'statuses' => [BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED],
  51. ];
  52. $this->Comment->create();
  53. $this->Comment->set($data);
  54. $res = $this->Comment->validates();
  55. $this->assertTrue($res);
  56. $is = $this->Comment->data['BitmaskedComment']['status'];
  57. $this->assertSame(BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED, $is);
  58. // save + find
  59. $this->Comment->create();
  60. $res = $this->Comment->save($data);
  61. $this->assertTrue(!empty($res));
  62. $res = $this->Comment->find('first', ['conditions' => ['statuses' => $data['statuses']]]);
  63. $this->assertTrue(!empty($res));
  64. $expected = BitmaskedComment::STATUS_APPROVED | BitmaskedComment::STATUS_PUBLISHED; // 6
  65. $this->assertEquals($expected, $res['BitmaskedComment']['status']);
  66. $expected = $data['statuses'];
  67. $this->assertEquals($expected, $res['BitmaskedComment']['statuses']);
  68. // model.field syntax
  69. $res = $this->Comment->find('first', ['conditions' => ['BitmaskedComment.statuses' => $data['statuses']]]);
  70. $this->assertTrue(!empty($res));
  71. // explitit
  72. $activeApprovedAndPublished = BitmaskedComment::STATUS_ACTIVE | BitmaskedComment::STATUS_APPROVED | BitmaskedComment::STATUS_PUBLISHED;
  73. $data = [
  74. 'comment' => 'another post comment',
  75. 'status' => $activeApprovedAndPublished,
  76. ];
  77. $this->Comment->create();
  78. $res = $this->Comment->save($data);
  79. $this->assertTrue(!empty($res));
  80. $res = $this->Comment->find('first', ['conditions' => ['status' => $activeApprovedAndPublished]]);
  81. $this->assertTrue(!empty($res));
  82. $this->assertEquals($activeApprovedAndPublished, $res['BitmaskedComment']['status']);
  83. $expected = [BitmaskedComment::STATUS_ACTIVE, BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED];
  84. $this->assertEquals($expected, $res['BitmaskedComment']['statuses']);
  85. }
  86. /**
  87. * Assert that you can manually trigger "notEmpty" rule with null instead of 0 for "not null" db fields
  88. */
  89. public function testSaveWithDefaultValue() {
  90. $this->Comment->Behaviors->load('Tools.Bitmasked', ['mappedField' => 'statuses', 'defaultValue' => '']);
  91. $data = [
  92. 'comment' => 'test save',
  93. 'statuses' => [],
  94. ];
  95. $this->Comment->create();
  96. $this->Comment->set($data);
  97. $res = $this->Comment->validates();
  98. //debug($this->Comment->data);
  99. $this->assertFalse($res);
  100. $is = $this->Comment->data['BitmaskedComment']['status'];
  101. $this->assertSame('', $is);
  102. }
  103. public function testIs() {
  104. $res = $this->Comment->isBit(BitmaskedComment::STATUS_PUBLISHED);
  105. $expected = ['BitmaskedComment.status' => 2];
  106. $this->assertEquals($expected, $res);
  107. }
  108. public function testIsNot() {
  109. $res = $this->Comment->isNotBit(BitmaskedComment::STATUS_PUBLISHED);
  110. $expected = ['NOT' => ['BitmaskedComment.status' => 2]];
  111. $this->assertEquals($expected, $res);
  112. }
  113. public function testContains() {
  114. $res = $this->Comment->containsBit(BitmaskedComment::STATUS_PUBLISHED);
  115. $expected = ['(BitmaskedComment.status & ? = ?)' => [2, 2]];
  116. $this->assertEquals($expected, $res);
  117. $conditions = $res;
  118. $res = $this->Comment->find('all', ['conditions' => $conditions]);
  119. $this->assertTrue(!empty($res) && count($res) === 3);
  120. // multiple (AND)
  121. $res = $this->Comment->containsBit([BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_ACTIVE]);
  122. $expected = ['(BitmaskedComment.status & ? = ?)' => [3, 3]];
  123. $this->assertEquals($expected, $res);
  124. $conditions = $res;
  125. $res = $this->Comment->find('all', ['conditions' => $conditions]);
  126. $this->assertTrue(!empty($res) && count($res) === 2);
  127. }
  128. public function testNotContains() {
  129. $res = $this->Comment->containsNotBit(BitmaskedComment::STATUS_PUBLISHED);
  130. $expected = ['(BitmaskedComment.status & ? != ?)' => [2, 2]];
  131. $this->assertEquals($expected, $res);
  132. $conditions = $res;
  133. $res = $this->Comment->find('all', ['conditions' => $conditions]);
  134. $this->assertTrue(!empty($res) && count($res) === 4);
  135. // multiple (AND)
  136. $res = $this->Comment->containsNotBit([BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_ACTIVE]);
  137. $expected = ['(BitmaskedComment.status & ? != ?)' => [3, 3]];
  138. $this->assertEquals($expected, $res);
  139. $conditions = $res;
  140. $res = $this->Comment->find('all', ['conditions' => $conditions]);
  141. $this->assertTrue(!empty($res) && count($res) === 5);
  142. }
  143. }