From 9db411d9e427ff6d7ca10a3f47c4b81a77acbc84 Mon Sep 17 00:00:00 2001 From: Thomas Ploch Date: Mon, 25 Apr 2011 14:34:40 +0200 Subject: [PATCH 1/5] Added HttpSocket::config['request']['uri'] as parameter in HttpSocket::_parseUri() called by HttpSocket::get(). Now creates correct request array for GET if query params are given and the default request should be used. Fixes #1674. No tests modified since behaviour didn't change. --- lib/Cake/Network/Http/HttpSocket.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cake/Network/Http/HttpSocket.php b/lib/Cake/Network/Http/HttpSocket.php index 9cee50af5..f810ef4ed 100644 --- a/lib/Cake/Network/Http/HttpSocket.php +++ b/lib/Cake/Network/Http/HttpSocket.php @@ -405,7 +405,7 @@ class HttpSocket extends CakeSocket { */ public function get($uri = null, $query = array(), $request = array()) { if (!empty($query)) { - $uri = $this->_parseUri($uri); + $uri = $this->_parseUri($uri, $this->config['request']['uri']); if (isset($uri['query'])) { $uri['query'] = array_merge($uri['query'], $query); } else { From 1962e40c9a0a7735c6bfe7bbe120128c55ddfdca Mon Sep 17 00:00:00 2001 From: Juan Basso Date: Tue, 26 Apr 2011 22:10:24 -0400 Subject: [PATCH 2/5] Added tests to get with queryparams and custom configs. Refs #1674. --- .../Case/Network/Http/HttpSocketTest.php | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lib/Cake/tests/Case/Network/Http/HttpSocketTest.php b/lib/Cake/tests/Case/Network/Http/HttpSocketTest.php index d0f46de52..259630c71 100644 --- a/lib/Cake/tests/Case/Network/Http/HttpSocketTest.php +++ b/lib/Cake/tests/Case/Network/Http/HttpSocketTest.php @@ -595,6 +595,34 @@ class HttpSocketTest extends CakeTestCase { $this->assertFalse($this->Socket->connected); } +/** + * testRequestWithConstructor method + * + * @return void + */ + public function testRequestWithConstructor() { + $request = array( + 'request' => array( + 'uri' => array( + 'scheme' => 'http', + 'host' => 'localhost', + 'port' => '5984', + 'user' => null, + 'pass' => null + ) + ) + ); + $http = new MockHttpSocketRequests($request); + + $expected = array('method' => 'GET', 'uri' => '/_test'); + $http->expects($this->at(0))->method('request')->with($expected); + $http->get('/_test'); + + $expected = array('method' => 'GET', 'uri' => 'http://localhost:5984/_test?count=4'); + $http->expects($this->at(0))->method('request')->with($expected); + $http->get('/_test', array('count' => 4)); + } + /** * testRequestWithResource * From df57b15f8943df7c5ed3efb875235725a4f58dd3 Mon Sep 17 00:00:00 2001 From: evilbloodydemon Date: Sun, 17 Apr 2011 12:30:31 +0400 Subject: [PATCH 3/5] Removed undefined variable in App::core, made test pass on Windows. Signed-off-by: mark_story --- lib/Cake/Core/App.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/Cake/Core/App.php b/lib/Cake/Core/App.php index 1ca082b4d..64449f97e 100644 --- a/lib/Cake/Core/App.php +++ b/lib/Cake/Core/App.php @@ -389,9 +389,7 @@ class App { * @return string full path to package */ public static function core($type) { - if ($type) { - return isset($paths[$type]) ? $paths[$type] : array(LIBS . $type . DS); - } + return array(LIBS . str_replace('/', DS, $type) . DS); } /** From 1523ff6874a1fc318c82f0011e161547cb3efd24 Mon Sep 17 00:00:00 2001 From: Juan Basso Date: Tue, 26 Apr 2011 22:19:17 -0400 Subject: [PATCH 4/5] Moved the CakeEmail to Network/Email. Now this class have similar structure of HttpSocket. --- lib/Cake/Controller/Component/EmailComponent.php | 2 +- lib/Cake/Network/{ => Email}/CakeEmail.php | 0 lib/Cake/tests/Case/Network/{ => Email}/CakeEmailTest.php | 2 +- lib/Cake/tests/Case/Network/Email/SmtpTransportTest.php | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) rename lib/Cake/Network/{ => Email}/CakeEmail.php (100%) rename lib/Cake/tests/Case/Network/{ => Email}/CakeEmailTest.php (99%) diff --git a/lib/Cake/Controller/Component/EmailComponent.php b/lib/Cake/Controller/Component/EmailComponent.php index 2311f0d55..de3e1e095 100644 --- a/lib/Cake/Controller/Component/EmailComponent.php +++ b/lib/Cake/Controller/Component/EmailComponent.php @@ -19,7 +19,7 @@ App::uses('Component', 'Controller'); App::uses('Multibyte', 'I18n'); -App::uses('CakeEmail', 'Network'); +App::uses('CakeEmail', 'Network/Email'); /** * EmailComponent diff --git a/lib/Cake/Network/CakeEmail.php b/lib/Cake/Network/Email/CakeEmail.php similarity index 100% rename from lib/Cake/Network/CakeEmail.php rename to lib/Cake/Network/Email/CakeEmail.php diff --git a/lib/Cake/tests/Case/Network/CakeEmailTest.php b/lib/Cake/tests/Case/Network/Email/CakeEmailTest.php similarity index 99% rename from lib/Cake/tests/Case/Network/CakeEmailTest.php rename to lib/Cake/tests/Case/Network/Email/CakeEmailTest.php index d44a602b9..45c7b1443 100644 --- a/lib/Cake/tests/Case/Network/CakeEmailTest.php +++ b/lib/Cake/tests/Case/Network/Email/CakeEmailTest.php @@ -16,7 +16,7 @@ * @since CakePHP(tm) v 2.0.0 * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ -App::uses('CakeEmail', 'Network'); +App::uses('CakeEmail', 'Network/Email'); /** * Help to test CakeEmail diff --git a/lib/Cake/tests/Case/Network/Email/SmtpTransportTest.php b/lib/Cake/tests/Case/Network/Email/SmtpTransportTest.php index 2a7c10469..5b75da480 100644 --- a/lib/Cake/tests/Case/Network/Email/SmtpTransportTest.php +++ b/lib/Cake/tests/Case/Network/Email/SmtpTransportTest.php @@ -16,7 +16,7 @@ * @since CakePHP(tm) v 2.0.0 * @license MIT License (http://www.opensource.org/licenses/mit-license.php) */ -App::uses('CakeEmail', 'Network'); +App::uses('CakeEmail', 'Network/Email'); App::uses('AbstractTransport', 'Network/Email'); App::uses('SmtpTransport', 'Network/Email'); From bea666bb128ebfca37e79366082e24407b3a4334 Mon Sep 17 00:00:00 2001 From: Thomas Ploch Date: Tue, 26 Apr 2011 14:36:20 +0200 Subject: [PATCH 5/5] Removed JsBaseEngineHelper::$useNative. Removed json_encode() emulation from JsBaseEngineHelper::object(). Removed unnessecary call to get_object_vars() in JsBaseEngineHelper::object(). Removed unnessecary tests for equalness of emulation and native json_encode(). Added test for correct encoding of objects to JsHelper test case. Fixes #704 --- lib/Cake/View/Helper/JsBaseEngineHelper.php | 53 +------------ .../tests/Case/View/Helper/JsHelperTest.php | 77 +++---------------- 2 files changed, 13 insertions(+), 117 deletions(-) diff --git a/lib/Cake/View/Helper/JsBaseEngineHelper.php b/lib/Cake/View/Helper/JsBaseEngineHelper.php index ade4ca558..b3f14680f 100644 --- a/lib/Cake/View/Helper/JsBaseEngineHelper.php +++ b/lib/Cake/View/Helper/JsBaseEngineHelper.php @@ -27,13 +27,6 @@ App::uses('AppHelper', 'View/Helper'); * @package cake.view.helpers */ abstract class JsBaseEngineHelper extends AppHelper { -/** - * Determines whether native JSON extension is used for encoding. Set by object constructor. - * - * @var boolean - * @access public - */ - public $useNative = false; /** * The js snippet for the current selection. @@ -76,7 +69,6 @@ abstract class JsBaseEngineHelper extends AppHelper { */ function __construct($View, $settings = array()) { parent::__construct($View, $settings); - $this->useNative = function_exists('json_encode'); } /** @@ -154,50 +146,7 @@ abstract class JsBaseEngineHelper extends AppHelper { ); $options = array_merge($defaultOptions, $options); - if (is_object($data)) { - $data = get_object_vars($data); - } - - $out = $keys = array(); - $numeric = true; - - if ($this->useNative && function_exists('json_encode')) { - $rt = json_encode($data); - } else { - if (is_null($data)) { - return 'null'; - } - if (is_bool($data)) { - return $data ? 'true' : 'false'; - } - if (is_array($data)) { - $keys = array_keys($data); - } - - if (!empty($keys)) { - $numeric = (array_values($keys) === array_keys(array_values($keys))); - } - - foreach ($data as $key => $val) { - if (is_array($val) || is_object($val)) { - $val = $this->object($val); - } else { - $val = $this->value($val); - } - if (!$numeric) { - $val = '"' . $this->value($key, false) . '":' . $val; - } - $out[] = $val; - } - - if (!$numeric) { - $rt = '{' . join(',', $out) . '}'; - } else { - $rt = '[' . join(',', $out) . ']'; - } - } - $rt = $options['prefix'] . $rt . $options['postfix']; - return $rt; + return $options['prefix'] . json_encode($data) . $options['postfix']; } /** diff --git a/lib/Cake/tests/Case/View/Helper/JsHelperTest.php b/lib/Cake/tests/Case/View/Helper/JsHelperTest.php index 69d946054..7aa1bbaff 100644 --- a/lib/Cake/tests/Case/View/Helper/JsHelperTest.php +++ b/lib/Cake/tests/Case/View/Helper/JsHelperTest.php @@ -25,6 +25,12 @@ App::uses('FormHelper', 'View/Helper'); App::uses('View', 'View'); App::uses('ClassRegistry', 'Utility'); +class JsEncodingObject { + protected $_title = 'Old thing'; + + private $__noshow = 'Never ever'; +} + class OptionEngineHelper extends JsBaseEngineHelper { protected $_optionMap = array( 'request' => array( @@ -797,13 +803,18 @@ class JsBaseEngineTest extends CakeTestCase { * @return void */ function testObject() { - $this->JsEngine->useNative = false; $object = array('title' => 'New thing', 'indexes' => array(5, 6, 7, 8)); $result = $this->JsEngine->object($object); $expected = '{"title":"New thing","indexes":[5,6,7,8]}'; $this->assertEqual($result, $expected); + $object = new JsEncodingObject(); + $object->title = 'New thing'; + $object->indexes = array(5,6,7,8); + $result = $this->JsEngine->object($object); + $this->assertEqual($result, $expected); + $result = $this->JsEngine->object(array('default' => 0)); $expected = '{"default":0}'; $this->assertEqual($result, $expected); @@ -842,70 +853,6 @@ class JsBaseEngineTest extends CakeTestCase { $this->assertNoPattern('/.POSTFIX./', $result); } -/** - * test compatibility of JsBaseEngineHelper::object() vs. json_encode() - * - * @return void - */ - function testObjectAgainstJsonEncode() { - $skip = $this->skipIf(!function_exists('json_encode'), 'json_encode() not found, comparison tests skipped. %s'); - if ($skip) { - return; - } - $this->JsEngine->useNative = false; - $data = array(); - $data['mystring'] = "simple string"; - $this->assertEqual(json_encode($data), $this->JsEngine->object($data)); - - $data['mystring'] = "strïng with spécial chârs"; - $this->assertEqual(json_encode($data), $this->JsEngine->object($data)); - - $data['mystring'] = "a two lines\nstring"; - $this->assertEqual(json_encode($data), $this->JsEngine->object($data)); - - $data['mystring'] = "a \t tabbed \t string"; - $this->assertEqual(json_encode($data), $this->JsEngine->object($data)); - - $data['mystring'] = "a \"double-quoted\" string"; - $this->assertEqual(json_encode($data), $this->JsEngine->object($data)); - - $data['mystring'] = 'a \\"double-quoted\\" string'; - $this->assertEqual(json_encode($data), $this->JsEngine->object($data)); - - unset($data['mystring']); - $data[3] = array(1, 2, 3); - $this->assertEqual(json_encode($data), $this->JsEngine->object($data)); - - unset($data[3]); - $data = array('mystring' => null, 'bool' => false, 'array' => array(1, 44, 66)); - $this->assertEqual(json_encode($data), $this->JsEngine->object($data)); - } - -/** - * test that JSON made with JsBaseEngineHelper::object() against json_decode() - * - * @return void - */ - function testObjectAgainstJsonDecode() { - $skip = $this->skipIf(!function_exists('json_encode'), 'json_encode() not found, comparison tests skipped. %s'); - if ($skip) { - return; - } - $this->JsEngine->useNative = false; - - $data = array("simple string"); - $result = $this->JsEngine->object($data); - $this->assertEqual(json_decode($result), $data); - - $data = array('my "string"'); - $result = $this->JsEngine->object($data); - $this->assertEqual(json_decode($result), $data); - - $data = array('my \\"string\\"'); - $result = $this->JsEngine->object($data); - $this->assertEqual(json_decode($result), $data); - } - /** * test Mapping of options. *