|
@@ -8,14 +8,13 @@ App::uses('MyCakeTestCase', 'Tools.TestSuite');
|
|
|
class JsonableBehaviorTest extends MyCakeTestCase {
|
|
class JsonableBehaviorTest extends MyCakeTestCase {
|
|
|
|
|
|
|
|
public $fixtures = array(
|
|
public $fixtures = array(
|
|
|
- 'core.comment'
|
|
|
|
|
|
|
+ 'plugin.tools.jsonable_comment'
|
|
|
);
|
|
);
|
|
|
|
|
|
|
|
public $Comment;
|
|
public $Comment;
|
|
|
|
|
|
|
|
public function setUp() {
|
|
public function setUp() {
|
|
|
- //$this->Comment = ClassRegistry::init('Comment');
|
|
|
|
|
- $this->Comment = new JsonableBehaviorTestModel();
|
|
|
|
|
|
|
+ $this->Comment = ClassRegistry::init('JsonableComment');
|
|
|
$this->Comment->Behaviors->load('Tools.Jsonable', array());
|
|
$this->Comment->Behaviors->load('Tools.Jsonable', array());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -27,15 +26,13 @@ class JsonableBehaviorTest extends MyCakeTestCase {
|
|
|
$data = array(
|
|
$data = array(
|
|
|
'comment' => 'blabla',
|
|
'comment' => 'blabla',
|
|
|
'url' => 'www.dereuromark.de',
|
|
'url' => 'www.dereuromark.de',
|
|
|
- 'name' => 'some Name',
|
|
|
|
|
|
|
+ 'title' => 'some Name',
|
|
|
'details' => array('x'=>'y'),
|
|
'details' => array('x'=>'y'),
|
|
|
);
|
|
);
|
|
|
$res = $this->Comment->save($data);
|
|
$res = $this->Comment->save($data);
|
|
|
- $this->assertTrue($res);
|
|
|
|
|
|
|
+ $this->assertTrue((bool)$res);
|
|
|
|
|
|
|
|
- $res = $this->Comment->data;
|
|
|
|
|
- debug($res); ob_flush();
|
|
|
|
|
- $this->assertSame($res['JsonableBehaviorTestModel']['details'], '{"x":"y"}');
|
|
|
|
|
|
|
+ $this->assertSame($res['JsonableComment']['details'], '{"x":"y"}');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function testFieldsWithList() {
|
|
public function testFieldsWithList() {
|
|
@@ -46,32 +43,28 @@ class JsonableBehaviorTest extends MyCakeTestCase {
|
|
|
$data = array(
|
|
$data = array(
|
|
|
'comment' => 'blabla',
|
|
'comment' => 'blabla',
|
|
|
'url' => 'www.dereuromark.de',
|
|
'url' => 'www.dereuromark.de',
|
|
|
- 'name' => 'some Name',
|
|
|
|
|
|
|
+ 'title' => 'some Name',
|
|
|
'details' => 'z|y|x',
|
|
'details' => 'z|y|x',
|
|
|
);
|
|
);
|
|
|
$res = $this->Comment->save($data);
|
|
$res = $this->Comment->save($data);
|
|
|
- $this->assertTrue($res);
|
|
|
|
|
|
|
+ $this->assertTrue((bool)$res);
|
|
|
|
|
|
|
|
- $res = $this->Comment->data;
|
|
|
|
|
- debug($res);
|
|
|
|
|
- $this->assertSame($res['JsonableBehaviorTestModel']['details'], '["z","y","x"]');
|
|
|
|
|
|
|
+ $this->assertSame($res['JsonableComment']['details'], '["z","y","x"]');
|
|
|
|
|
|
|
|
# with sort and unique
|
|
# with sort and unique
|
|
|
$data = array(
|
|
$data = array(
|
|
|
'comment' => 'blabla',
|
|
'comment' => 'blabla',
|
|
|
'url' => 'www.dereuromark.de',
|
|
'url' => 'www.dereuromark.de',
|
|
|
- 'name' => 'some Name',
|
|
|
|
|
|
|
+ 'title' => 'some Name',
|
|
|
'details' => 'z|x|y|x',
|
|
'details' => 'z|x|y|x',
|
|
|
);
|
|
);
|
|
|
$this->Comment->Behaviors->unload('Jsonable');
|
|
$this->Comment->Behaviors->unload('Jsonable');
|
|
|
$this->Comment->Behaviors->load('Tools.Jsonable', array('fields'=>array('details'), 'input'=>'list', 'sort'=>true));
|
|
$this->Comment->Behaviors->load('Tools.Jsonable', array('fields'=>array('details'), 'input'=>'list', 'sort'=>true));
|
|
|
|
|
|
|
|
$res = $this->Comment->save($data);
|
|
$res = $this->Comment->save($data);
|
|
|
- $this->assertTrue($res);
|
|
|
|
|
|
|
+ $this->assertTrue((bool)$res);
|
|
|
|
|
|
|
|
- $res = $this->Comment->data;
|
|
|
|
|
- debug($res);
|
|
|
|
|
- $this->assertSame($res['JsonableBehaviorTestModel']['details'], '["x","y","z"]');
|
|
|
|
|
|
|
+ $this->assertSame($res['JsonableComment']['details'], '["x","y","z"]');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function testFieldsWithParam() {
|
|
public function testFieldsWithParam() {
|
|
@@ -82,15 +75,13 @@ class JsonableBehaviorTest extends MyCakeTestCase {
|
|
|
$data = array(
|
|
$data = array(
|
|
|
'comment' => 'blabla',
|
|
'comment' => 'blabla',
|
|
|
'url' => 'www.dereuromark.de',
|
|
'url' => 'www.dereuromark.de',
|
|
|
- 'name' => 'some Name',
|
|
|
|
|
|
|
+ 'title' => 'some Name',
|
|
|
'details' => 'z:vz|y:yz|x:xz',
|
|
'details' => 'z:vz|y:yz|x:xz',
|
|
|
);
|
|
);
|
|
|
$res = $this->Comment->save($data);
|
|
$res = $this->Comment->save($data);
|
|
|
- $this->assertTrue($res);
|
|
|
|
|
|
|
+ $this->assertTrue((bool)$res);
|
|
|
|
|
|
|
|
- $res = $this->Comment->data;
|
|
|
|
|
- debug($res);
|
|
|
|
|
- $this->assertSame($res['JsonableBehaviorTestModel']['details'], '{"z":"vz","y":"yz","x":"xz"}');
|
|
|
|
|
|
|
+ $this->assertSame($res['JsonableComment']['details'], '{"z":"vz","y":"yz","x":"xz"}');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@@ -99,90 +90,70 @@ class JsonableBehaviorTest extends MyCakeTestCase {
|
|
|
|
|
|
|
|
public function testFieldsOnFind() {
|
|
public function testFieldsOnFind() {
|
|
|
echo $this->_header(__FUNCTION__);
|
|
echo $this->_header(__FUNCTION__);
|
|
|
|
|
+
|
|
|
|
|
+ // array
|
|
|
$this->Comment->Behaviors->unload('Jsonable');
|
|
$this->Comment->Behaviors->unload('Jsonable');
|
|
|
$this->Comment->Behaviors->load('Tools.Jsonable', array('fields'=>array('details')));
|
|
$this->Comment->Behaviors->load('Tools.Jsonable', array('fields'=>array('details')));
|
|
|
|
|
|
|
|
- $res = $this->Comment->find('first', array());
|
|
|
|
|
-
|
|
|
|
|
- $this->assertEquals($res['JsonableBehaviorTestModel']['details'], array('x'=>'y'));
|
|
|
|
|
- pr($res);
|
|
|
|
|
|
|
+ $data = array(
|
|
|
|
|
+ 'comment' => 'blabla',
|
|
|
|
|
+ 'url' => 'www.dereuromark.de',
|
|
|
|
|
+ 'title' => 'param',
|
|
|
|
|
+ 'details' => array('x'=>'y'),
|
|
|
|
|
+ );
|
|
|
|
|
+ $res = $this->Comment->save($data);
|
|
|
|
|
+ $this->assertTrue((bool)$res);
|
|
|
|
|
|
|
|
|
|
+ $res = $this->Comment->find('first', array('conditions' => array('title' => 'param')));
|
|
|
|
|
+ $this->assertEquals($res['JsonableComment']['details'], array('x'=>'y'));
|
|
|
|
|
|
|
|
|
|
+ // param
|
|
|
$this->Comment->Behaviors->unload('Jsonable');
|
|
$this->Comment->Behaviors->unload('Jsonable');
|
|
|
- $this->Comment->Behaviors->load('Tools.Jsonable', array('output'=>'param', 'fields'=>array('details')));
|
|
|
|
|
-
|
|
|
|
|
- $res = $this->Comment->find('first', array());
|
|
|
|
|
- pr($res);
|
|
|
|
|
- $this->assertEquals($res['JsonableBehaviorTestModel']['details'], 'x:y');
|
|
|
|
|
|
|
|
|
|
|
|
+ $res = $this->Comment->find('first', array('conditions' => array('title' => 'param')));
|
|
|
|
|
+ $this->assertEquals($res['JsonableComment']['details'], '{"x":"y"}');
|
|
|
|
|
|
|
|
|
|
+ $this->Comment->Behaviors->load('Tools.Jsonable', array('output'=>'param', 'fields'=>array('details')));
|
|
|
|
|
|
|
|
|
|
+ $res = $this->Comment->find('first', array('conditions' => array('title' => 'param')));
|
|
|
|
|
+ $this->assertEquals($res['JsonableComment']['details'], 'x:y');
|
|
|
|
|
|
|
|
|
|
+ // list
|
|
|
$this->Comment->Behaviors->unload('Jsonable');
|
|
$this->Comment->Behaviors->unload('Jsonable');
|
|
|
$this->Comment->Behaviors->load('Tools.Jsonable', array('output'=>'list', 'fields'=>array('details')));
|
|
$this->Comment->Behaviors->load('Tools.Jsonable', array('output'=>'list', 'fields'=>array('details')));
|
|
|
|
|
|
|
|
$data = array(
|
|
$data = array(
|
|
|
'comment' => 'blabla',
|
|
'comment' => 'blabla',
|
|
|
'url' => 'www.dereuromark.de',
|
|
'url' => 'www.dereuromark.de',
|
|
|
- 'name' => 'some Name',
|
|
|
|
|
- 'details' => '["z","y","x"]',
|
|
|
|
|
|
|
+ 'title' => 'list',
|
|
|
|
|
+ 'details' => array('z', 'y', 'x'),
|
|
|
);
|
|
);
|
|
|
- $res = $this->Comment->find('first', array(), $data);
|
|
|
|
|
- pr($res);
|
|
|
|
|
- $this->assertEquals($res['JsonableBehaviorTestModel']['details'], 'z|y|x');
|
|
|
|
|
-
|
|
|
|
|
- echo BR.BR;
|
|
|
|
|
-
|
|
|
|
|
|
|
+ $this->Comment->create();
|
|
|
|
|
+ $res = $this->Comment->save($data);
|
|
|
|
|
+ $this->assertTrue((bool)$res);
|
|
|
|
|
|
|
|
$this->Comment->Behaviors->unload('Jsonable');
|
|
$this->Comment->Behaviors->unload('Jsonable');
|
|
|
- $this->Comment->Behaviors->load('Tools.Jsonable', array('output'=>'list', 'separator'=>', ', 'fields'=>array('details')));
|
|
|
|
|
-
|
|
|
|
|
- $data = array(
|
|
|
|
|
- 'comment' => 'blabla',
|
|
|
|
|
- 'url' => 'www.dereuromark.de',
|
|
|
|
|
- 'name' => 'some Name',
|
|
|
|
|
- 'details' => '["z","y","x"]',
|
|
|
|
|
- );
|
|
|
|
|
- $res = $this->Comment->find('first', array(), $data);
|
|
|
|
|
- pr($res);
|
|
|
|
|
- $this->assertEquals($res['JsonableBehaviorTestModel']['details'], 'z, y, x');
|
|
|
|
|
|
|
|
|
|
|
|
+ $res = $this->Comment->find('first', array('conditions' => array('title' => 'list')));
|
|
|
|
|
+ $this->assertEquals($res['JsonableComment']['details'], '["z","y","x"]');
|
|
|
|
|
|
|
|
- echo BR.BR;
|
|
|
|
|
-
|
|
|
|
|
- $res = $this->Comment->find('all', array(), $data);
|
|
|
|
|
- pr($res);
|
|
|
|
|
- $this->assertEquals($res[0]['JsonableBehaviorTestModel']['details'], 'z, y, x');
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ $this->Comment->Behaviors->load('Tools.Jsonable', array('output'=>'list', 'fields'=>array('details')));
|
|
|
|
|
|
|
|
-/*** other files ***/
|
|
|
|
|
|
|
+ $res = $this->Comment->find('first', array('conditions' => array('title' => 'list')));
|
|
|
|
|
+ $this->assertEquals($res['JsonableComment']['details'], 'z|y|x');
|
|
|
|
|
|
|
|
-class JsonableBehaviorTestModel extends AppModel {
|
|
|
|
|
|
|
+ // custom separator
|
|
|
|
|
+ $this->Comment->Behaviors->unload('Jsonable');
|
|
|
|
|
+ $this->Comment->Behaviors->load('Tools.Jsonable', array('output'=>'list', 'separator'=>', ', 'fields'=>array('details')));
|
|
|
|
|
|
|
|
- public $useTable = false;
|
|
|
|
|
- public $displayField = 'title';
|
|
|
|
|
|
|
+ // find first
|
|
|
|
|
+ $res = $this->Comment->find('first', array('conditions' => array('title' => 'list')));
|
|
|
|
|
+ $this->assertEquals($res['JsonableComment']['details'], 'z, y, x');
|
|
|
|
|
|
|
|
- public function find($type = null, $options = array(), $customData = null) {
|
|
|
|
|
- $data = array(
|
|
|
|
|
- 'comment' => 'blabla',
|
|
|
|
|
- 'url' => 'www.dereuromark.de',
|
|
|
|
|
- 'name' => 'some Name',
|
|
|
|
|
- 'details' => '{"x":"y"}',
|
|
|
|
|
- );
|
|
|
|
|
- if ($customData !== null) {
|
|
|
|
|
- $data = $customData;
|
|
|
|
|
- }
|
|
|
|
|
- if ($type === 'count') {
|
|
|
|
|
- $results = array(0=>array(0=>array('count'=>2)));
|
|
|
|
|
- } else {
|
|
|
|
|
- $results = array(0=>array($this->alias=>$data));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- $results = $this->_filterResults($results);
|
|
|
|
|
- if ($type === 'first') {
|
|
|
|
|
- $results = $this->_findFirst('after', null, $results);
|
|
|
|
|
- }
|
|
|
|
|
- return $results;
|
|
|
|
|
|
|
+ // find all
|
|
|
|
|
+ $res = $this->Comment->find('all', array('order' => array('title' => 'ASC')));
|
|
|
|
|
+ $this->assertEquals($res[0]['JsonableComment']['details'], 'z, y, x');
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
+
|