Skip to content

Commit

Permalink
Applying patch from kleingeist to fix Js->value incorrectly calling H…
Browse files Browse the repository at this point in the history
…elper::value()
  • Loading branch information
markstory committed Dec 7, 2009
1 parent 547ed83 commit 4cefb5f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
13 changes: 13 additions & 0 deletions cake/libs/view/helpers/js.php
Expand Up @@ -162,6 +162,19 @@ function object($data = array(), $options = array()) {
return $this->{$this->__engineName}->object($data, $options);
}

/**
* Overwrite inherited Helper::value()
* See JsBaseEngineHelper::value() for more information on this method.
*
* @param mixed $val A PHP variable to be converted to JSON
* @param boolean $quoteStrings If false, leaves string values unquoted
* @return string a JavaScript-safe/JSON representation of $val
* @access public
**/
function value($val, $quoteString = true) {
return $this->{$this->__engineName}->value($val, $quoteString);
}

/**
* Writes all Javascript generated so far to a code block or
* caches them to a file and returns a linked script.
Expand Down
22 changes: 17 additions & 5 deletions cake/tests/cases/libs/view/helpers/js.test.php
Expand Up @@ -310,7 +310,7 @@ function testLinkWithMock() {
$options = array('id' => 'something', 'htmlAttributes' => array('arbitrary' => 'value', 'batman' => 'robin'));
$result = $this->Js->link('test link', '/posts/view/1', $options);
$expected = array(
'a' => array('id' => $options['id'], 'href' => '/posts/view/1', 'arbitrary' => 'value',
'a' => array('id' => $options['id'], 'href' => '/posts/view/1', 'arbitrary' => 'value',
'batman' => 'robin'),
'test link',
'/a'
Expand Down Expand Up @@ -373,7 +373,7 @@ function testSubmitWithMock() {
$this->Js->TestJsEngine->expectAt(2, 'dispatchMethod', array('request', '*'));

$params = array(
'update' => $options['update'], 'data' => 'serialize-code',
'update' => $options['update'], 'data' => 'serialize-code',
'method' => 'post', 'dataExpression' => true
);
$this->Js->TestJsEngine->expectAt(3, 'dispatchMethod', array(
Expand Down Expand Up @@ -402,13 +402,13 @@ function testSubmitWithMock() {
$this->Js->TestJsEngine->expectAt(6, 'dispatchMethod', array('request', $requestParams));

$params = array(
'update' => '#content', 'data' => 'serialize-code',
'update' => '#content', 'data' => 'serialize-code',
'method' => 'post', 'dataExpression' => true
);
$this->Js->TestJsEngine->expectAt(7, 'dispatchMethod', array(
'event', array('click', "ajax-code", $params)
));

$options = array('update' => '#content', 'id' => 'test-submit', 'url' => '/custom/url');
$result = $this->Js->submit('Save', $options);
$expected = array(
Expand All @@ -430,6 +430,18 @@ function testObjectPassThrough() {
$this->assertEqual($result, $expected);
}

/**
* Test that inherited Helper::value() is overwritten in JsHelper::value()
* and calls JsBaseEngineHelper::value().
*
* @return void
*/
function testValuePassThrough() {
$result = $this->Js->value('string "quote"', true);
$expected = '"string \"quote\""';
$this->assertEqual($result, $expected);
}

/**
* test set()'ing variables to the Javascript buffer and controlling the output var name.
*
Expand All @@ -448,7 +460,7 @@ function testSet() {
$result = $this->Js->getBuffer();
$expected = 'var WICKED = {"loggedIn":true,"height":"tall","color":"purple"};';
$this->assertEqual($result[0], $expected);

$this->Js->set('loggedIn', true);
$this->Js->set(array('height' => 'tall', 'color' => 'purple'));
$this->Js->setVariable = 'Application.variables';
Expand Down

0 comments on commit 4cefb5f

Please sign in to comment.