| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- <?php
- App::uses('JsonableBehavior', 'Tools.Model/Behavior');
- App::uses('AppModel', 'Model');
- App::uses('MyCakeTestCase', 'Tools.TestSuite');
- class JsonableBehaviorTest extends MyCakeTestCase {
- public $fixtures = [
- 'plugin.tools.jsonable_comment'
- ];
- public $Comment;
- public function setUp() {
- parent::setUp();
- $this->Comment = ClassRegistry::init('JsonableComment');
- $this->Comment->Behaviors->load('Tools.Jsonable', []);
- }
- /** INPUT **/
- public function testBasic() {
- //echo $this->_header(__FUNCTION__);
- // accuracy >= 5
- $data = [
- 'comment' => 'blabla',
- 'url' => 'www.dereuromark.de',
- 'title' => 'some Name',
- 'details' => ['x' => 'y'],
- ];
- $res = $this->Comment->save($data);
- $this->assertTrue((bool)$res);
- $this->assertSame('{"x":"y"}', $res['JsonableComment']['details']);
- }
- public function testFieldsWithList() {
- //echo $this->_header(__FUNCTION__);
- $this->Comment->Behaviors->unload('Jsonable');
- $this->Comment->Behaviors->load('Tools.Jsonable', ['fields' => ['details'], 'input' => 'list']);
- $data = [
- 'comment' => 'blabla',
- 'url' => 'www.dereuromark.de',
- 'title' => 'some Name',
- 'details' => 'z|y|x',
- ];
- $res = $this->Comment->save($data);
- $this->assertTrue((bool)$res);
- $this->assertSame('["z","y","x"]', $res['JsonableComment']['details']);
- // with sort and unique
- $data = [
- 'comment' => 'blabla',
- 'url' => 'www.dereuromark.de',
- 'title' => 'some Name',
- 'details' => 'z|x|y|x',
- ];
- $this->Comment->Behaviors->unload('Jsonable');
- $this->Comment->Behaviors->load('Tools.Jsonable', ['fields' => ['details'], 'input' => 'list', 'sort' => true]);
- $res = $this->Comment->save($data);
- $this->assertTrue((bool)$res);
- $this->assertSame('["x","y","z"]', $res['JsonableComment']['details']);
- }
- public function testFieldsWithParam() {
- //echo $this->_header(__FUNCTION__);
- $this->Comment->Behaviors->unload('Jsonable');
- $this->Comment->Behaviors->load('Tools.Jsonable', ['fields' => ['details'], 'input' => 'param']);
- $data = [
- 'comment' => 'blabla',
- 'url' => 'www.dereuromark.de',
- 'title' => 'some Name',
- 'details' => 'z:vz|y:yz|x:xz',
- ];
- $res = $this->Comment->save($data);
- $this->assertTrue((bool)$res);
- $this->assertSame('{"z":"vz","y":"yz","x":"xz"}', $res['JsonableComment']['details']);
- }
- /** OUTPUT **/
- public function testFieldsOnFind() {
- //echo $this->_header(__FUNCTION__);
- // array
- $this->Comment->Behaviors->unload('Jsonable');
- $this->Comment->Behaviors->load('Tools.Jsonable', ['fields' => ['details']]);
- $data = [
- 'comment' => 'blabla',
- 'url' => 'www.dereuromark.de',
- 'title' => 'param',
- 'details' => ['x' => 'y'],
- ];
- $res = $this->Comment->save($data);
- $this->assertTrue((bool)$res);
- $res = $this->Comment->find('first', ['conditions' => ['title' => 'param']]);
- $this->assertEquals(['x' => 'y'], $res['JsonableComment']['details']);
- // param
- $this->Comment->Behaviors->unload('Jsonable');
- $res = $this->Comment->find('first', ['conditions' => ['title' => 'param']]);
- $this->assertEquals('{"x":"y"}', $res['JsonableComment']['details']);
- $this->Comment->Behaviors->load('Tools.Jsonable', ['output' => 'param', 'fields' => ['details']]);
- $res = $this->Comment->find('first', ['conditions' => ['title' => 'param']]);
- $this->assertEquals('x:y', $res['JsonableComment']['details']);
- // list
- $this->Comment->Behaviors->unload('Jsonable');
- $this->Comment->Behaviors->load('Tools.Jsonable', ['output' => 'list', 'fields' => ['details']]);
- $data = [
- 'comment' => 'blabla',
- 'url' => 'www.dereuromark.de',
- 'title' => 'list',
- 'details' => ['z', 'y', 'x'],
- ];
- $this->Comment->create();
- $res = $this->Comment->save($data);
- $this->assertTrue((bool)$res);
- $this->Comment->Behaviors->unload('Jsonable');
- $res = $this->Comment->find('first', ['conditions' => ['title' => 'list']]);
- $this->assertEquals('["z","y","x"]', $res['JsonableComment']['details']);
- $this->Comment->Behaviors->load('Tools.Jsonable', ['output' => 'list', 'fields' => ['details']]);
- $res = $this->Comment->find('first', ['conditions' => ['title' => 'list']]);
- $this->assertEquals('z|y|x', $res['JsonableComment']['details']);
- // custom separator
- $this->Comment->Behaviors->unload('Jsonable');
- $this->Comment->Behaviors->load('Tools.Jsonable', ['output' => 'list', 'separator' => ', ', 'fields' => ['details']]);
- // find first
- $res = $this->Comment->find('first', ['conditions' => ['title' => 'list']]);
- $this->assertEquals('z, y, x', $res['JsonableComment']['details']);
- // find all
- $res = $this->Comment->find('all', ['order' => ['title' => 'ASC']]);
- $this->assertEquals('z, y, x', $res[0]['JsonableComment']['details']);
- }
- public function testEncodeParams() {
- // $depth param added in 5.5.0
- $this->skipIf(!version_compare(PHP_VERSION, '5.5.0', '>='));
- // Test encode depth = 1
- $this->Comment->Behaviors->unload('Jsonable');
- $this->Comment->Behaviors->load('Tools.Jsonable', ['fields' => ['details'], 'encodeParams' => ['depth' => 1]]);
- $data = [
- 'comment' => 'blabla',
- 'url' => 'www.dereuromark.de',
- 'title' => 'param',
- 'details' => ['x' => ['y' => 'z']],
- ];
- $this->Comment->save($data);
- $res = $this->Comment->find('first', ['conditions' => ['title' => 'param']]);
- $expected = [];
- $this->assertEquals($expected, $res['JsonableComment']['details']);
- // Test encode depth = 2
- $this->Comment->Behaviors->unload('Jsonable');
- $this->Comment->Behaviors->load('Tools.Jsonable', ['fields' => ['details'], 'encodeParams' => ['depth' => 2]]);
- $data = [
- 'comment' => 'blabla',
- 'url' => 'www.dereuromark.de',
- 'title' => 'param',
- 'details' => ['x' => ['y' => 'z']],
- ];
- $this->Comment->save($data);
- $res = $this->Comment->find('first', ['conditions' => ['title' => 'param']]);
- $obj = new stdClass();
- $obj->y = 'z';
- $expected = ['x' => $obj];
- $this->assertEquals($expected, $res['JsonableComment']['details']);
- }
- public function testDecodeParams() {
- $this->Comment->Behaviors->load('Tools.Jsonable', ['fields' => ['details']]);
- $data = [
- 'comment' => 'blabla',
- 'url' => 'www.dereuromark.de',
- 'title' => 'param',
- 'details' => ['x' => ['y' => 'z']],
- ];
- $this->Comment->save($data);
- // Test decode with default params
- $res = $this->Comment->find('first', ['conditions' => ['title' => 'param']]);
- $obj = new stdClass();
- $obj->y = 'z';
- $expected = ['x' => $obj];
- $this->assertEquals($expected, $res['JsonableComment']['details']);
- // Test decode with assoc = true
- $this->Comment->Behaviors->unload('Jsonable');
- $this->Comment->Behaviors->load('Tools.Jsonable', ['fields' => ['details'], 'decodeParams' => ['assoc' => true]]);
- $res = $this->Comment->find('first', ['conditions' => ['title' => 'param']]);
- $expected = ['x' => ['y' => 'z']];
- $this->assertEquals($expected, $res['JsonableComment']['details']);
- // Test decode with assoc = true and depth = 2
- $this->Comment->Behaviors->unload('Jsonable');
- $this->Comment->Behaviors->load('Tools.Jsonable', ['fields' => ['details'], 'decodeParams' => ['assoc' => true, 'depth' => 2]]);
- $res = $this->Comment->find('first', ['conditions' => ['title' => 'param']]);
- $expected = [];
- $this->assertEquals($expected, $res['JsonableComment']['details']);
- // Test decode with assoc = true and depth = 3
- $this->Comment->Behaviors->unload('Jsonable');
- $this->Comment->Behaviors->load('Tools.Jsonable', ['fields' => ['details'], 'decodeParams' => ['assoc' => true, 'depth' => 3]]);
- $res = $this->Comment->find('first', ['conditions' => ['title' => 'param']]);
- $expected = ['x' => ['y' => 'z']];
- $this->assertEquals($expected, $res['JsonableComment']['details']);
- }
- }
|