Skip to content

Commit

Permalink
Adding non buffered tests to Js::link()
Browse files Browse the repository at this point in the history
Implementing Js::submit() adding test cases.
  • Loading branch information
markstory committed Jul 25, 2009
1 parent a3d5a9b commit 76c13fa
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 25 deletions.
77 changes: 66 additions & 11 deletions cake/libs/view/helpers/js.php
Expand Up @@ -39,7 +39,7 @@ class JsHelper extends AppHelper {
*
* @var array
**/
var $helpers = array('Html');
var $helpers = array('Html', 'Form');
/**
* Scripts that are queued for output
*
Expand Down Expand Up @@ -205,9 +205,13 @@ function getBuffer($clear = true) {
* element that is enhanced with Javascript. Options can include
* both those for HtmlHelper::link() and JsBaseEngine::request(), JsBaseEngine::event();
*
* ### Options
* ### Options
*
* - confirm - Generate a confirm() dialog before sending the event.
* - confirm - Generate a confirm() dialog before sending the event.
* - id - use a custom id.
* - htmlAttrs - additional non-standard htmlAttributes. Standard attributes are class, id,
* rel, title, escape, onblur and onfocus.
* - buffer - Disable the buffering and return a script tag in addition to the link.
*
* @param string $title Title for the link.
* @param mixed $url Mixed either a string URL or an cake url array.
Expand All @@ -223,13 +227,15 @@ function link($title, $url = null, $options = array()) {
$this->get('#' . $htmlOptions['id']);
$requestString = '';
if (isset($options['confirm'])) {
$requestString .= 'var _confirm = ' . $this->confirm($options['confirm']);
$requestString .= "if (!_confirm) {\n\treturn false;\n}";
$requestString = $this->confirmReturn($options['confirm']);
unset($options['confirm']);
}
$requestString .= $this->request($url, $options);
if (!empty($requestString)) {
$this->event('click', $requestString, $options);
$event = $this->event('click', $requestString, $options);
}
if (isset($options['buffer']) && $options['buffer'] == false) {
$out .= $this->Html->scriptBlock($event, $options);
}
return $out;
}
Expand All @@ -242,25 +248,51 @@ function link($title, $url = null, $options = array()) {
* @param array $options Array of options to use.
* @return string Completed submit button.
**/
function submit($title, $options = array()) {

function submit($caption = null, $options = array()) {
if (!isset($options['id'])) {
$options['id'] = 'submit-' . intval(mt_rand());
}
$formOptions = array('div');
$htmlOptions = $this->_getHtmlOptions($options, $formOptions);
$out = $this->Form->submit($caption, $htmlOptions);

$this->get('#' . $htmlOptions['id']);
$options['data'] = $this->serializeForm('#' . $htmlOptions);
$requestString = '';
if (isset($options['confirm'])) {
$requestString = $this->confirmReturn($options['confirm']);
unset($options['confirm']);
}
$requestString .= $this->request('', $options);
if (!empty($requestString)) {
$event = $this->event('click', $requestString, $options);
}
if (isset($options['buffer']) && $options['buffer'] == false) {
$out .= $this->Html->scriptBlock($event, $options);
}
return $out;
}
/**
* Parse a set of Options and extract the Html options.
* Extracted Html Options are removed from the $options param.
*
* @param array Options to filter.
* @param array $options Options to filter.
* @param array $additional Array of additional keys to extract and include in the return options array.
* @return array Array of options for non-js.
**/
function _getHtmlOptions(&$options) {
$htmlKeys = array('class', 'id', 'escape', 'onblur', 'onfocus', 'rel', 'title');
function _getHtmlOptions(&$options, $additional = array()) {
$htmlKeys = array_merge(array('class', 'id', 'escape', 'onblur', 'onfocus', 'rel', 'title'), $additional);
$htmlOptions = array();
foreach ($htmlKeys as $key) {
if (isset($options[$key])) {
$htmlOptions[$key] = $options[$key];
}
unset($options[$key]);
}
if (isset($options['htmlAttributes'])) {
$htmlOptions = array_merge($htmlOptions, $options['htmlAttributes']);
unset($options['htmlAttributes']);
}
return $htmlOptions;
}
}
Expand Down Expand Up @@ -340,6 +372,19 @@ function redirect($url = null) {
function confirm($message) {
return 'confirm("' . $this->escape($message) . '");';
}
/**
* Generate a confirm snippet that returns false from the current
* function scope.
*
* @param string $message Message to use in the confirm dialog.
* @access public
* @return string
**/
function confirmReturn($message) {
$out = 'var _confirm = ' . $this->confirm($message);
$out .= "if (!_confirm) {\n\treturn false;\n}";
return $out;
}
/**
* Create a prompt() Javascript function
*
Expand Down Expand Up @@ -744,6 +789,16 @@ function sortable() {
function slider() {
trigger_error(sprintf(__('%s does not have slider() implemented', true), get_class($this)), E_USER_WARNING);
}
/**
* serializeForm
*
* @return string Completed form serialization script
**/
function serializeForm() {
trigger_error(
sprintf(__('%s does not have serializeForm() implemented', true), get_class($this)), E_USER_WARNING
);
}
/**
* Parse an options assoc array into an Javascript object literal.
* Similar to object() but treats any non-integer value as a string,
Expand Down
82 changes: 68 additions & 14 deletions cake/tests/cases/libs/view/helpers/js.test.php
Expand Up @@ -20,7 +20,7 @@
* @since CakePHP(tm) v 1.3
* @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
*/
App::import('Helper', array('Js', 'Html'));
App::import('Helper', array('Js', 'Html', 'Form'));
App::import('Core', array('View', 'ClassRegistry'));

Mock::generate('JsBaseEngineHelper', 'TestJsEngineHelper', array('methodOne'));
Expand Down Expand Up @@ -80,6 +80,8 @@ class JsHelperTestCase extends CakeTestCase {
function startTest() {
$this->Js =& new JsHelper('JsBase');
$this->Js->Html =& new HtmlHelper();
$this->Js->Form =& new FormHelper();
$this->Js->Form->Html =& new HtmlHelper();
$this->Js->JsBaseEngine =& new JsBaseEngineHelper();

$view =& new JsHelperMockView();
Expand All @@ -104,6 +106,8 @@ function _useMock() {
$this->Js =& new JsHelper(array('TestJs'));
$this->Js->TestJsEngine =& new TestJsEngineHelper($this);
$this->Js->Html =& new HtmlHelper();
$this->Js->Form =& new FormHelper();
$this->Js->Form->Html =& new HtmlHelper();
}
/**
* test object construction
Expand All @@ -112,16 +116,16 @@ function _useMock() {
**/
function testConstruction() {
$js =& new JsHelper();
$this->assertEqual($js->helpers, array('Html', 'JqueryEngine'));
$this->assertEqual($js->helpers, array('Html', 'Form', 'JqueryEngine'));

$js =& new JsHelper(array('mootools'));
$this->assertEqual($js->helpers, array('Html', 'mootoolsEngine'));
$this->assertEqual($js->helpers, array('Html', 'Form', 'mootoolsEngine'));

$js =& new JsHelper('prototype');
$this->assertEqual($js->helpers, array('Html', 'prototypeEngine'));
$this->assertEqual($js->helpers, array('Html', 'Form', 'prototypeEngine'));

$js =& new JsHelper('MyPlugin.Dojo');
$this->assertEqual($js->helpers, array('Html', 'MyPlugin.DojoEngine'));
$this->assertEqual($js->helpers, array('Html', 'Form', 'MyPlugin.DojoEngine'));
}
/**
* test that methods dispatch internally and to the engine class
Expand Down Expand Up @@ -264,7 +268,7 @@ function testLinkWithMock() {
'/a'
);
$this->assertTags($result, $expected);

$options = array(
'confirm' => 'Are you sure?',
'update' => '#content',
Expand All @@ -288,6 +292,52 @@ function testLinkWithMock() {
'/a'
);
$this->assertTags($result, $expected);

$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',
'batman' => 'robin'),
'test link',
'/a'
);
$this->assertTags($result, $expected);
}
/**
* test that link() and no buffering returns an <a> and <script> tags.
*
* @return void
**/
function testLinkWithNoBuffering() {
$this->_useMock();
$this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'ajax code', array('request', '*'));
$this->Js->TestJsEngine->setReturnValue('dispatchMethod', '-event handler-', array('event', '*'));

$options = array('update' => '#content', 'buffer' => false);
$result = $this->Js->link('test link', '/posts/view/1', $options);
$expected = array(
'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
'test link',
'/a',
'script' => array('type' => 'text/javascript'),
$this->cDataStart,
'-event handler-',
$this->cDataEnd,
'/script'
);
$this->assertTags($result, $expected);

$options = array('update' => '#content', 'buffer' => false, 'safe' => false);
$result = $this->Js->link('test link', '/posts/view/1', $options);
$expected = array(
'a' => array('id' => 'preg:/link-\d+/', 'href' => '/posts/view/1'),
'test link',
'/a',
'script' => array('type' => 'text/javascript'),
'-event handler-',
'/script'
);
$this->assertTags($result, $expected);
}
/**
* test submit() with a Mock to check Engine method calls
Expand All @@ -301,21 +351,25 @@ function testSubmitWithMock() {
'id' => 'test-submit'
);
$this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'serialize-code', array('serializeForm', '*'));
$this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'ajax code', array('request', '*'));
$this->Js->TestJsEngine->setReturnValue('dispatchMethod', 'ajax-code', array('request', '*'));

$this->Js->TestJsEngine->expectAt(0, 'dispatchMethod', array('get', '*'));
$this->Js->TestJsEngine->expectAt(1, 'dispatchMethod', array('serializeForm', '*'));
$this->Js->TestJsEngine->expectAt(2, 'dispatchMethod', array('request', '*'));
$params = array(
'update' => $options['update'],
'data' => 'serialize-code'
);
$this->Js->TestJsEngine->expectAt(3, 'dispatchMethod', array(
'event', array('click', "serialize-code\najax-code", $options)
'event', array('click', "ajax-code", $params)
));
$result = $this->Js->submit('Save', $options);
$expected = array(
'div' => array('class' => 'input submit'),
'div' => array('class' => 'submit'),
'input' => array('type' => 'submit', 'id' => $options['id'], 'value' => 'Save'),
'/div'
);
$this->assertTags($result, $expected);
$this->assertTags($result, $expected, true);
}
}

Expand Down Expand Up @@ -401,7 +455,7 @@ function testAlert() {

$result = $this->JsEngine->alert('"Hey"');
$expected = 'alert("\"Hey\"");';
$this->assertEqual($result, $expected);
$this->assertEqual($result, $expected);
}
/**
* test confirm generation
Expand All @@ -415,7 +469,7 @@ function testConfirm() {

$result = $this->JsEngine->confirm('"Are you sure?"');
$expected = 'confirm("\"Are you sure?\"");';
$this->assertEqual($result, $expected);
$this->assertEqual($result, $expected);
}
/**
* test Redirect
Expand Down Expand Up @@ -452,7 +506,7 @@ function testObject() {
'Fall' => array(
'1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
)
),
),
'2006' => array(
'Spring' => array(
'1' => array('id' => 1, 'name' => 'Josh'), '2' => array('id' => 2, 'name' => 'Becky')
Expand Down Expand Up @@ -547,7 +601,7 @@ function testOptionMapping() {

$result = $JsEngine->testMap(array('complete' => 'myFunc', 'type' => 'json', 'update' => '#element'));
$this->assertEqual($result, array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));

$result = $JsEngine->testMap(array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
$this->assertEqual($result, array('success' => 'myFunc', 'dataType' => 'json', 'update' => '#element'));
}
Expand Down

0 comments on commit 76c13fa

Please sign in to comment.