Browse Source

Fix Jsonable return values.

Mark Scherer 10 years ago
parent
commit
54e9998c0e

+ 6 - 2
src/Model/Behavior/JsonableBehavior.php

@@ -58,7 +58,7 @@ class JsonableBehavior extends Behavior {
 			'depth' => 512,
 		],
 		'decodeParams' => [ // params for json_decode
-			'assoc' => false, // useful when working with multidimensional arrays
+			'assoc' => true, // useful when working with multidimensional arrays
 			'depth' => 512,
 			'options' => 0
 		]
@@ -185,6 +185,7 @@ class JsonableBehavior extends Behavior {
 				}
 			}
 		}
+
 		if (is_array($val)) {
 			// Depth param added in PHP 5.5
 			if (version_compare(PHP_VERSION, '5.5.0', '>=')) {
@@ -193,6 +194,7 @@ class JsonableBehavior extends Behavior {
 				$val = json_encode($val, $this->_config['encodeParams']['options']);
 			}
 		}
+
 		return $val;
 	}
 
@@ -209,7 +211,9 @@ class JsonableBehavior extends Behavior {
 		if ($decoded === false) {
 			return false;
 		}
-		$decoded = (array)$decoded;
+		if ($this->_config['decodeParams']['assoc']) {
+			$decoded = (array)$decoded;
+		}
 		if ($this->_config['output'] === 'param') {
 			$decoded = $this->_toParam($decoded);
 		} elseif ($this->_config['output'] === 'list') {

+ 2 - 0
src/Model/Behavior/SluggedBehavior.php

@@ -236,6 +236,8 @@ class SluggedBehavior extends Behavior {
 	 * @return void
 	 */
 	public function _slug(Entity $entity) {
+		extract($this->_config);
+
 		$overwrite = $this->config['overwrite'];
 		if (!$overwrite && !$entity->get($overwriteField)) {
 			$overwrite = true;

+ 12 - 0
tests/TestApp/Model/Table/JsonableCommentsTable.php

@@ -0,0 +1,12 @@
+<?php
+
+namespace TestApp\Model\Table;
+
+use Tools\Model\Table\Table;
+
+class JsonableCommentsTable extends Table {
+
+	public $validate = [
+	];
+
+}

+ 50 - 8
tests/TestCase/Model/Behavior/JsonableBehaviorTest.php

@@ -18,6 +18,8 @@ class JsonableBehaviorTest extends TestCase {
 	public function setUp() {
 		parent::setUp();
 
+		Configure::write('App.namespace', 'TestApp');
+
 		$this->Comments = TableRegistry::get('JsonableComments');
 		$this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details']]);
 	}
@@ -202,11 +204,11 @@ class JsonableBehaviorTest extends TestCase {
 		$expected = [];
 		$this->assertEquals($expected, $res['details']);
 
-		$this->skipIf(true, 'FIXME!');
+		$this->Comments->truncate();
 
 		// Test encode depth = 2
 		$this->Comments->removeBehavior('Jsonable');
-		$this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'encodeParams' => ['depth' => 2]]);
+		$this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'encodeParams' => ['depth' => 2], 'decodeParams' => ['assoc' => false]]);
 
 		$data = [
 			'comment' => 'blabla',
@@ -218,10 +220,49 @@ class JsonableBehaviorTest extends TestCase {
 		$this->Comments->save($entity);
 
 		$res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
-		debug($res); ob_flush();
 		$obj = new \stdClass();
-		$obj->y = 'z';
-		$expected = ['x' => $obj];
+		$obj->x = new \stdClass();
+		$obj->x->y = 'z';
+		$expected = $obj;
+		$this->assertEquals($expected, $res['details']);
+	}
+
+	public function testEncodeParamsAssocFalse() {
+		// Test encode depth = 1
+		$this->Comments->removeBehavior('Jsonable');
+		$this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'encodeParams' => ['depth' => 1], 'decodeParams' => ['assoc' => false]]);
+
+		$data = [
+			'comment' => 'blabla',
+			'url' => 'www.dereuromark.de',
+			'title' => 'param',
+			'details' => ['y' => 'yy'],
+		];
+		$entity = $this->Comments->newEntity($data);
+		$this->Comments->save($entity);
+
+		$res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
+		$obj = new \stdClass();
+		$obj->y = 'yy';
+		$expected = $obj;
+		$this->assertEquals($expected, $res['details']);
+
+		$this->Comments->truncate();
+
+		$this->Comments->removeBehavior('Jsonable');
+		$this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'encodeParams' => ['depth' => 1], 'decodeParams' => ['assoc' => false]]);
+
+		$data = [
+			'comment' => 'blabla',
+			'url' => 'www.dereuromark.de',
+			'title' => 'param',
+			'details' => ['y' => ['yy' => 'yyy']],
+		];
+		$entity = $this->Comments->newEntity($data);
+		$this->Comments->save($entity);
+
+		$res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
+		$expected = null;
 		$this->assertEquals($expected, $res['details']);
 	}
 
@@ -232,7 +273,7 @@ class JsonableBehaviorTest extends TestCase {
 	 */
 	public function testDecodeParams() {
 		$this->Comments->removeBehavior('Jsonable');
-		$this->Comments->addBehavior('Tools.Jsonable', ['output' => 'array', 'fields' => ['details']]);
+		$this->Comments->addBehavior('Tools.Jsonable', ['output' => 'array', 'fields' => ['details'], 'decodeParams' => ['assoc'=> false]]);
 
 		$data = [
 			'comment' => 'blabla',
@@ -246,8 +287,9 @@ class JsonableBehaviorTest extends TestCase {
 		// Test decode with default params
 		$res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first();
 		$obj = new \stdClass();
-		$obj->y = 'z';
-		$expected = ['x' => $obj];
+		$obj->x = new \stdClass();
+		$obj->x->y = 'z';
+		$expected = $obj;
 		$this->assertEquals($expected, $res['details']);
 
 		// Test decode with assoc = true