BitmaskedBehaviorTest.php 6.5 KB

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