From bc6b8e5dfdc8a4272f819a722d39c8319cf56871 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Fri, 23 Apr 2010 00:04:15 -0400 Subject: [PATCH] Fixing parameters from leaking into the script tag when calling JsHelper::submit(). Added test cases and refactored JsHelper::link(). Fixes #613 --- cake/libs/view/helpers/js.php | 8 ++-- .../tests/cases/libs/view/helpers/js.test.php | 37 +++++++++++++++++++ 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/cake/libs/view/helpers/js.php b/cake/libs/view/helpers/js.php index 5ad10191d..f8caa21e1 100644 --- a/cake/libs/view/helpers/js.php +++ b/cake/libs/view/helpers/js.php @@ -319,10 +319,7 @@ class JsHelper extends AppHelper { $event = $this->event('click', $requestString, $options); } if (isset($options['buffer']) && $options['buffer'] == false) { - $opts = array(); - if (isset($options['safe'])) { - $opts['safe'] = $options['safe']; - } + $opts = array_intersect_key(array('safe' => null), $options); $out .= $this->Html->scriptBlock($event, $opts); } return $out; @@ -397,7 +394,8 @@ class JsHelper extends AppHelper { $event = $this->event('click', $requestString, $options); } if (isset($options['buffer']) && $options['buffer'] == false) { - $out .= $this->Html->scriptBlock($event, $options); + $opts = array_intersect_key(array('safe' => null), $options); + $out .= $this->Html->scriptBlock($event, $opts); } return $out; } diff --git a/cake/tests/cases/libs/view/helpers/js.test.php b/cake/tests/cases/libs/view/helpers/js.test.php index 292bbb19e..0b6f573a0 100644 --- a/cake/tests/cases/libs/view/helpers/js.test.php +++ b/cake/tests/cases/libs/view/helpers/js.test.php @@ -456,6 +456,43 @@ CODE; $this->assertTags($result, $expected); } +/** + * test that no buffer works with submit() and that parameters are leaking into the script tag. + * + * @return void + */ + function testSubmitWithNoBuffer() { + $this->_useMock(); + $options = array('update' => '#content', 'id' => 'test-submit', 'buffer' => false, 'safe' => false); + $this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'serialize-code', array('serializeform', '*')); + $this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'serialize-code', array('serializeForm', '*')); + $this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'ajax-code', array('request', '*')); + $this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'event-handler', array('event', '*')); + + $this->Js->TestJsEngine->expectAt(0, 'dispatchMethod', array('get', '*')); + $this->Js->TestJsEngine->expectAt(1, 'dispatchMethod', array(new PatternExpectation('/serializeForm/i'), '*')); + $this->Js->TestJsEngine->expectAt(2, 'dispatchMethod', array('request', '*')); + + $params = array( + 'update' => $options['update'], 'buffer' => false, 'safe' => false, 'data' => 'serialize-code', + 'method' => 'post', 'dataExpression' => true + ); + $this->Js->TestJsEngine->expectAt(3, 'dispatchMethod', array( + 'event', array('click', "ajax-code", $params) + )); + + $result = $this->Js->submit('Save', $options); + $expected = array( + 'div' => array('class' => 'submit'), + 'input' => array('type' => 'submit', 'id' => $options['id'], 'value' => 'Save'), + '/div', + 'script' => array('type' => 'text/javascript'), + 'event-handler', + '/script' + ); + $this->assertTags($result, $expected); + } + /** * Test that Object::Object() is not breaking json output in JsHelper *