| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- <?php
- namespace Tools\Test\TestCase\Model\Behavior;
- use Cake\Database\Query;
- use Cake\Event\Event;
- use TestApp\Model\Entity\BitmaskedComment;
- use Cake\ORM\TableRegistry;
- use Tools\TestSuite\TestCase;
- use Cake\Core\Configure;
- use Tools\Model\Behavior\BitmaskedBehavior;
- class BitmaskedBehaviorTest extends TestCase {
- public $fixtures = array(
- 'plugin.tools.bitmasked_comments'
- );
- public $Comments;
- public function setUp() {
- parent::setUp();
- Configure::write('App.namespace', 'TestApp');
- $this->Comments = TableRegistry::get('BitmaskedComments');
- $this->Comments->addBehavior('Tools.Bitmasked', array('mappedField' => 'statuses'));
- }
- /**
- * BitmaskedBehaviorTest::testEncodeBitmask()
- *
- * @return void
- */
- public function testEncodeBitmask() {
- $res = $this->Comments->encodeBitmask(array(BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED));
- $expected = BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED;
- $this->assertSame($expected, $res);
- }
- /**
- * BitmaskedBehaviorTest::testDecodeBitmask()
- *
- * @return void
- */
- public function testDecodeBitmask() {
- $res = $this->Comments->decodeBitmask(BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED);
- $expected = array(BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED);
- $this->assertSame($expected, $res);
- }
- /**
- * BitmaskedBehaviorTest::testFind()
- *
- * @return void
- */
- public function testFind() {
- $res = $this->Comments->find('all')->toArray();
- $this->assertTrue(!empty($res) && is_array($res));
- $this->assertTrue(!empty($res[1]['statuses']) && is_array($res[1]['statuses']));
- }
- /**
- * BitmaskedBehaviorTest::testSave()
- *
- * @return void
- */
- public function testSave() {
- $data = array(
- 'comment' => 'test save',
- 'statuses' => array(),
- );
- $entity = $this->Comments->newEntity($data);
- $res = $this->Comments->validate($entity);
- $this->assertTrue($res);
- $this->assertSame('0', $entity->get('status'));
- $data = array(
- 'comment' => 'test save',
- 'statuses' => array(BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED),
- );
- $entity = $this->Comments->newEntity($data);
- $res = $this->Comments->validate($entity);
- $this->assertTrue($res);
- $is = $entity->get('status');
- $this->assertSame(BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED, $is);
- // save + find
- $entity = $this->Comments->newEntity($data);
- $res = $this->Comments->save($entity);
- $this->assertTrue((bool)$res);
- $res = $this->Comments->find('first', array('conditions' => array('statuses' => $data['statuses'])));
- $this->assertTrue(!empty($res));
- $expected = BitmaskedComment::STATUS_APPROVED | BitmaskedComment::STATUS_PUBLISHED; // 6
- $this->assertEquals($expected, $res['status']);
- $expected = $data['statuses'];
- $this->assertEquals($expected, $res['statuses']);
- // model.field syntax
- $res = $this->Comments->find('first', array('conditions' => array('BitmaskedComments.statuses' => $data['statuses'])));
- $this->assertTrue((bool)$res->toArray());
- // explicit
- $activeApprovedAndPublished = BitmaskedComment::STATUS_ACTIVE | BitmaskedComment::STATUS_APPROVED | BitmaskedComment::STATUS_PUBLISHED;
- $data = array(
- 'comment' => 'another post comment',
- 'status' => $activeApprovedAndPublished,
- );
- $entity = $this->Comments->newEntity($data);
- $res = $this->Comments->save($entity);
- $this->assertTrue((bool)$res);
- $res = $this->Comments->find('first', array('conditions' => array('status' => $activeApprovedAndPublished)));
- $this->assertTrue((bool)$res);
- $this->assertEquals($activeApprovedAndPublished, $res['status']);
- $expected = array(BitmaskedComment::STATUS_ACTIVE, BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED);
- $this->assertEquals($expected, $res['statuses']);
- }
- /**
- * Assert that you can manually trigger "notEmpty" rule with null instead of 0 for "not null" db fields
- *
- * @return void
- */
- public function testSaveWithDefaultValue() {
- $data = array(
- 'comment' => 'test save',
- 'statuses' => array(),
- );
- $entity = $this->Comments->newEntity($data);
- $res = $this->Comments->validate($entity);
- $this->assertTrue($res);
- $this->assertSame('0', $entity->get('status'));
- // Now let's set the default value
- $this->Comments->removeBehavior('Bitmasked');
- $this->Comments->addBehavior('Tools.Bitmasked', array('mappedField' => 'statuses', 'defaultValue' => ''));
- $data = array(
- 'comment' => 'test save',
- 'statuses' => array(),
- );
- $entity = $this->Comments->newEntity($data);
- $res = $this->Comments->validate($entity);
- $this->assertFalse($res);
- $this->assertSame('', $entity->get('status'));
- }
- /**
- * Assert that it also works with beforeSave event callback.
- *
- * @return void
- */
- public function testSaveOnBeforeSave() {
- $this->Comments->removeBehavior('Bitmasked');
- $this->Comments->addBehavior('Tools.Bitmasked', array('mappedField' => 'statuses', 'on' => 'beforeSave'));
- $data = array(
- 'comment' => 'test save',
- 'statuses' => array(BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_APPROVED),
- );
- $entity = $this->Comments->newEntity($data);
- $res = $this->Comments->save($entity);
- $this->assertTrue((bool)$res);
- $this->assertSame(BitmaskedComment::STATUS_PUBLISHED | BitmaskedComment::STATUS_APPROVED, $res['status']);
- }
- /**
- * BitmaskedBehaviorTest::testIs()
- *
- * @return void
- */
- public function testIs() {
- $res = $this->Comments->isBit(BitmaskedComment::STATUS_PUBLISHED);
- $expected = array('BitmaskedComments.status' => 2);
- $this->assertEquals($expected, $res);
- }
- /**
- * BitmaskedBehaviorTest::testIsNot()
- *
- * @return void
- */
- public function testIsNot() {
- $res = $this->Comments->isNotBit(BitmaskedComment::STATUS_PUBLISHED);
- $expected = array('NOT' => array('BitmaskedComments.status' => 2));
- $this->assertEquals($expected, $res);
- }
- /**
- * BitmaskedBehaviorTest::testContains()
- *
- * @return void
- */
- public function testContains() {
- $res = $this->Comments->containsBit(BitmaskedComment::STATUS_PUBLISHED);
- $expected = array('(BitmaskedComments.status & 2 = 2)');
- $this->assertEquals($expected, $res);
- $conditions = $res;
- $res = $this->Comments->find('all', array('conditions' => $conditions))->toArray();
- $this->assertTrue(!empty($res) && count($res) === 3);
- // multiple (AND)
- $res = $this->Comments->containsBit(array(BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_ACTIVE));
- $expected = array('(BitmaskedComments.status & 3 = 3)');
- $this->assertEquals($expected, $res);
- $conditions = $res;
- $res = $this->Comments->find('all', array('conditions' => $conditions))->toArray();
- $this->assertTrue(!empty($res) && count($res) === 2);
- }
- /**
- * BitmaskedBehaviorTest::testNotContains()
- *
- * @return void
- */
- public function testNotContains() {
- $res = $this->Comments->containsNotBit(BitmaskedComment::STATUS_PUBLISHED);
- $expected = array('(BitmaskedComments.status & 2 != 2)');
- $this->assertEquals($expected, $res);
- $conditions = $res;
- $res = $this->Comments->find('all', array('conditions' => $conditions))->toArray();
- $this->assertTrue(!empty($res) && count($res) === 4);
- // multiple (AND)
- $res = $this->Comments->containsNotBit(array(BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_ACTIVE));
- $expected = array('(BitmaskedComments.status & 3 != 3)');
- $this->assertEquals($expected, $res);
- $conditions = $res;
- $res = $this->Comments->find('all', array('conditions' => $conditions))->toArray();
- $this->assertTrue(!empty($res) && count($res) === 5);
- }
- }
|