PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/tests/cases/libs/view/helpers/ajax.test.php

https://github.com/klevo/wildflower
PHP | 918 lines | 623 code | 68 blank | 227 comment | 3 complexity | f61043e822abc94cecd9a5e181fd8298 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * AjaxHelperTest file
  5. *
  6. * Long description for file
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) Tests <https://trac.cakephp.org/wiki/Developement/TestSuite>
  11. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  12. *
  13. * Licensed under The Open Group Test Suite License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  17. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  18. * @package cake
  19. * @subpackage cake.tests.cases.libs.view.helpers
  20. * @since CakePHP(tm) v 1.2.0.4206
  21. * @version $Revision$
  22. * @modifiedby $LastChangedBy$
  23. * @lastmodified $Date$
  24. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  25. */
  26. if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
  27. define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
  28. }
  29. uses(
  30. 'view' . DS . 'helpers' . DS . 'app_helper',
  31. 'controller' . DS . 'controller',
  32. 'model' . DS . 'model',
  33. 'view' . DS . 'helper',
  34. 'view' . DS . 'helpers'.DS.'ajax',
  35. 'view' . DS . 'helpers' . DS . 'html',
  36. 'view' . DS . 'helpers' . DS . 'form',
  37. 'view' . DS . 'helpers' . DS . 'javascript'
  38. );
  39. /**
  40. * AjaxTestController class
  41. *
  42. * @package cake
  43. * @subpackage cake.tests.cases.libs.view.helpers
  44. */
  45. class AjaxTestController extends Controller {
  46. /**
  47. * name property
  48. *
  49. * @var string 'AjaxTest'
  50. * @access public
  51. */
  52. var $name = 'AjaxTest';
  53. /**
  54. * uses property
  55. *
  56. * @var mixed null
  57. * @access public
  58. */
  59. var $uses = null;
  60. }
  61. /**
  62. * PostAjaxTest class
  63. *
  64. * @package cake
  65. * @subpackage cake.tests.cases.libs.view.helpers
  66. */
  67. class PostAjaxTest extends Model {
  68. /**
  69. * primaryKey property
  70. *
  71. * @var string 'id'
  72. * @access public
  73. */
  74. var $primaryKey = 'id';
  75. /**
  76. * useTable property
  77. *
  78. * @var bool false
  79. * @access public
  80. */
  81. var $useTable = false;
  82. /**
  83. * schema method
  84. *
  85. * @access public
  86. * @return void
  87. */
  88. function schema() {
  89. return array(
  90. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  91. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  92. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  93. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  94. );
  95. }
  96. }
  97. /**
  98. * TestAjaxHelper class
  99. *
  100. * @package cake
  101. * @subpackage cake.tests.cases.libs.view.helpers
  102. */
  103. class TestAjaxHelper extends AjaxHelper {
  104. /**
  105. * stop method
  106. *
  107. * @access public
  108. * @return void
  109. */
  110. function _stop() {
  111. }
  112. }
  113. /**
  114. * TestJavascriptHelper class
  115. *
  116. * @package cake
  117. * @subpackage cake.tests.cases.libs.view.helpers
  118. */
  119. class TestJavascriptHelper extends JavascriptHelper {
  120. /**
  121. * codeBlocks property
  122. *
  123. * @var mixed
  124. * @access public
  125. */
  126. var $codeBlocks;
  127. /**
  128. * codeBlock method
  129. *
  130. * @param mixed $parameter
  131. * @access public
  132. * @return void
  133. */
  134. function codeBlock($parameter) {
  135. if (empty($this->codeBlocks)) {
  136. $this->codeBlocks = array();
  137. }
  138. $this->codeBlocks[] = $parameter;
  139. }
  140. }
  141. /**
  142. * AjaxTest class
  143. *
  144. * @package cake
  145. * @subpackage cake.tests.cases.libs.view.helpers
  146. */
  147. class AjaxHelperTest extends CakeTestCase {
  148. /**
  149. * Regexp for CDATA start block
  150. *
  151. * @var string
  152. */
  153. var $cDataStart = 'preg:/^\/\/<!\[CDATA\[[\n\r]*/';
  154. /**
  155. * Regexp for CDATA end block
  156. *
  157. * @var string
  158. */
  159. var $cDataEnd = 'preg:/[^\]]*\]\]\>[\s\r\n]*/';
  160. /**
  161. * setUp method
  162. *
  163. * @access public
  164. * @return void
  165. */
  166. function setUp() {
  167. Router::reload();
  168. $this->Ajax =& new TestAjaxHelper();
  169. $this->Ajax->Html =& new HtmlHelper();
  170. $this->Ajax->Form =& new FormHelper();
  171. $this->Ajax->Javascript =& new JavascriptHelper();
  172. $this->Ajax->Form->Html =& $this->Ajax->Html;
  173. $view =& new View(new AjaxTestController());
  174. ClassRegistry::addObject('view', $view);
  175. ClassRegistry::addObject('PostAjaxTest', new PostAjaxTest());
  176. }
  177. /**
  178. * tearDown method
  179. *
  180. * @access public
  181. * @return void
  182. */
  183. function tearDown() {
  184. unset($this->Ajax);
  185. ClassRegistry::flush();
  186. }
  187. /**
  188. * testEvalScripts method
  189. *
  190. * @access public
  191. * @return void
  192. */
  193. function testEvalScripts() {
  194. $result = $this->Ajax->link('Test Link', 'http://www.cakephp.org', array('id' => 'link1', 'update' => 'content', 'evalScripts' => false));
  195. $expected = array(
  196. 'a' => array('id' => 'link1', 'onclick' => ' event.returnValue = false; return false;', 'href' => 'http://www.cakephp.org'),
  197. 'Test Link',
  198. '/a',
  199. array('script' => array('type' => 'text/javascript')),
  200. $this->cDataStart,
  201. "Event.observe('link1', 'click', function(event) { new Ajax.Updater('content','http://www.cakephp.org', {asynchronous:true, evalScripts:false, requestHeaders:['X-Update', 'content']}) }, false);",
  202. $this->cDataEnd,
  203. '/script'
  204. );
  205. $this->assertTags($result, $expected);
  206. $result = $this->Ajax->link('Test Link', 'http://www.cakephp.org', array('id' => 'link1', 'update' => 'content'));
  207. $expected = array(
  208. 'a' => array('id' => 'link1', 'onclick' => ' event.returnValue = false; return false;', 'href' => 'http://www.cakephp.org'),
  209. 'Test Link',
  210. '/a',
  211. array('script' => array('type' => 'text/javascript')),
  212. $this->cDataStart,
  213. "Event.observe('link1', 'click', function(event) { new Ajax.Updater('content','http://www.cakephp.org', {asynchronous:true, evalScripts:true, requestHeaders:['X-Update', 'content']}) }, false);",
  214. $this->cDataEnd,
  215. '/script'
  216. );
  217. $this->assertTags($result, $expected);
  218. }
  219. /**
  220. * testAutoComplete method
  221. *
  222. * @access public
  223. * @return void
  224. */
  225. function testAutoComplete() {
  226. $result = $this->Ajax->autoComplete('PostAjaxTest.title' , '/posts', array('minChars' => 2));
  227. $this->assertPattern('/^<input[^<>]+name="data\[PostAjaxTest\]\[title\]"[^<>]+autocomplete="off"[^<>]+\/>/', $result);
  228. $this->assertPattern('/<div[^<>]+id="PostAjaxTestTitle_autoComplete"[^<>]*><\/div>/', $result);
  229. $this->assertPattern('/<div[^<>]+class="auto_complete"[^<>]*><\/div>/', $result);
  230. $this->assertPattern('/<\/div>\s+<script type="text\/javascript">\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*' . str_replace('/', '\\/', preg_quote('new Ajax.Autocompleter(\'PostAjaxTestTitle\', \'PostAjaxTestTitle_autoComplete\', \'/posts\',')) . '/', $result);
  231. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Autocompleter(\'PostAjaxTestTitle\', \'PostAjaxTestTitle_autoComplete\', \'/posts\', {minChars:2});')) . '/', $result);
  232. $this->assertPattern('/<\/script>$/', $result);
  233. $result = $this->Ajax->autoComplete('PostAjaxTest.title' , '/posts', array('paramName' => 'parameter'));
  234. $this->assertPattern('/^<input[^<>]+name="data\[PostAjaxTest\]\[title\]"[^<>]+autocomplete="off"[^<>]+\/>/', $result);
  235. $this->assertPattern('/<div[^<>]+id="PostAjaxTestTitle_autoComplete"[^<>]*><\/div>/', $result);
  236. $this->assertPattern('/<div[^<>]+class="auto_complete"[^<>]*><\/div>/', $result);
  237. $this->assertPattern('/<\/div>\s+<script type="text\/javascript">\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*' . str_replace('/', '\\/', preg_quote('new Ajax.Autocompleter(\'PostAjaxTestTitle\', \'PostAjaxTestTitle_autoComplete\', \'/posts\',')) . '/', $result);
  238. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Autocompleter(\'PostAjaxTestTitle\', \'PostAjaxTestTitle_autoComplete\', \'/posts\', {paramName:\'parameter\'});')) . '/', $result);
  239. $this->assertPattern('/<\/script>$/', $result);
  240. $result = $this->Ajax->autoComplete('PostAjaxTest.title' , '/posts', array('paramName' => 'parameter', 'updateElement' => 'elementUpdated', 'afterUpdateElement' => 'function (input, element) { alert("updated"); }'));
  241. $this->assertPattern('/^<input[^<>]+name="data\[PostAjaxTest\]\[title\]"[^<>]+autocomplete="off"[^<>]+\/>/', $result);
  242. $this->assertPattern('/<div[^<>]+id="PostAjaxTestTitle_autoComplete"[^<>]*><\/div>/', $result);
  243. $this->assertPattern('/<div[^<>]+class="auto_complete"[^<>]*><\/div>/', $result);
  244. $this->assertPattern('/<\/div>\s+<script type="text\/javascript">\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*' . str_replace('/', '\\/', preg_quote('new Ajax.Autocompleter(\'PostAjaxTestTitle\', \'PostAjaxTestTitle_autoComplete\', \'/posts\',')) . '/', $result);
  245. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Autocompleter(\'PostAjaxTestTitle\', \'PostAjaxTestTitle_autoComplete\', \'/posts\', {paramName:\'parameter\', updateElement:elementUpdated, afterUpdateElement:function (input, element) { alert("updated"); }});')) . '/', $result);
  246. $this->assertPattern('/<\/script>$/', $result);
  247. $result = $this->Ajax->autoComplete('PostAjaxTest.title' , '/posts', array('callback' => 'function (input, queryString) { alert("requesting"); }'));
  248. $this->assertPattern('/^<input[^<>]+name="data\[PostAjaxTest\]\[title\]"[^<>]+autocomplete="off"[^<>]+\/>/', $result);
  249. $this->assertPattern('/<div[^<>]+id="PostAjaxTestTitle_autoComplete"[^<>]*><\/div>/', $result);
  250. $this->assertPattern('/<div[^<>]+class="auto_complete"[^<>]*><\/div>/', $result);
  251. $this->assertPattern('/<\/div>\s+<script type="text\/javascript">\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*' . str_replace('/', '\\/', preg_quote('new Ajax.Autocompleter(\'PostAjaxTestTitle\', \'PostAjaxTestTitle_autoComplete\', \'/posts\',')) . '/', $result);
  252. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Autocompleter(\'PostAjaxTestTitle\', \'PostAjaxTestTitle_autoComplete\', \'/posts\', {callback:function (input, queryString) { alert("requesting"); }});')) . '/', $result);
  253. $this->assertPattern('/<\/script>$/', $result);
  254. $result = $this->Ajax->autoComplete("PostAjaxText.title", "/post", array("parameters" => "'key=value&key2=value2'"));
  255. $this->assertPattern('/{parameters:\'key=value&key2=value2\'}/', $result);
  256. $result = $this->Ajax->autoComplete("PostAjaxText.title", "/post", array("with" => "'key=value&key2=value2'"));
  257. $this->assertPattern('/{parameters:\'key=value&key2=value2\'}/', $result);
  258. }
  259. /**
  260. * testAsynchronous method
  261. *
  262. * @access public
  263. * @return void
  264. */
  265. function testAsynchronous() {
  266. $result = $this->Ajax->link('Test Link', '/', array('id' => 'link1', 'update' => 'content', 'type' => 'synchronous'));
  267. $expected = array(
  268. 'a' => array('id' => 'link1', 'onclick' => ' event.returnValue = false; return false;', 'href' => '/'),
  269. 'Test Link',
  270. '/a',
  271. array('script' => array('type' => 'text/javascript')),
  272. $this->cDataStart,
  273. "Event.observe('link1', 'click', function(event) { new Ajax.Updater('content','/', {asynchronous:false, evalScripts:true, requestHeaders:['X-Update', 'content']}) }, false);",
  274. $this->cDataEnd,
  275. '/script'
  276. );
  277. $this->assertTags($result, $expected);
  278. }
  279. /**
  280. * testDraggable method
  281. *
  282. * @access public
  283. * @return void
  284. */
  285. function testDraggable() {
  286. $result = $this->Ajax->drag('id', array('handle' => 'other_id'));
  287. $expected = array(
  288. array('script' => array('type' => 'text/javascript')),
  289. $this->cDataStart,
  290. "new Draggable('id', {handle:'other_id'});",
  291. $this->cDataEnd,
  292. '/script'
  293. );
  294. $this->assertTags($result, $expected);
  295. $result = $this->Ajax->drag('id', array('onDrag' => 'doDrag', 'onEnd' => 'doEnd'));
  296. $this->assertPattern('/onDrag:doDrag/', $result);
  297. $this->assertPattern('/onEnd:doEnd/', $result);
  298. }
  299. /**
  300. * testDroppable method
  301. *
  302. * @access public
  303. * @return void
  304. */
  305. function testDroppable() {
  306. $result = $this->Ajax->drop('droppable', array('accept' => 'crap'));
  307. $expected = array(
  308. array('script' => array('type' => 'text/javascript')),
  309. $this->cDataStart,
  310. "Droppables.add('droppable', {accept:'crap'});",
  311. $this->cDataEnd,
  312. '/script'
  313. );
  314. $this->assertTags($result, $expected);
  315. $result = $this->Ajax->dropRemote('droppable', array('accept' => 'crap'), array('url' => '/posts'));
  316. $expected = array(
  317. array('script' => array('type' => 'text/javascript')),
  318. $this->cDataStart,
  319. "Droppables.add('droppable', {accept:'crap', onDrop:function(element, droppable, event) {new Ajax.Request('/posts', {asynchronous:true, evalScripts:true})}});",
  320. $this->cDataEnd,
  321. '/script'
  322. );
  323. $this->assertTags($result, $expected);
  324. $result = $this->Ajax->dropRemote('droppable', array('accept' => array('crap1', 'crap2')), array('url' => '/posts'));
  325. $expected = array(
  326. array('script' => array('type' => 'text/javascript')),
  327. $this->cDataStart,
  328. "Droppables.add('droppable', {accept:[\"crap1\",\"crap2\"], onDrop:function(element, droppable, event) {new Ajax.Request('/posts', {asynchronous:true, evalScripts:true})}});",
  329. $this->cDataEnd,
  330. '/script'
  331. );
  332. $this->assertTags($result, $expected);
  333. $result = $this->Ajax->dropRemote('droppable', array('accept' => 'crap'), array('url' => '/posts', 'with' => '{drag_id:element.id,drop_id:dropon.id,event:event.whatever_you_want}'));
  334. $expected = array(
  335. array('script' => array('type' => 'text/javascript')),
  336. $this->cDataStart,
  337. "Droppables.add('droppable', {accept:'crap', onDrop:function(element, droppable, event) {new Ajax.Request('/posts', {asynchronous:true, evalScripts:true, parameters:{drag_id:element.id,drop_id:dropon.id,event:event.whatever_you_want}})}});",
  338. $this->cDataEnd,
  339. '/script'
  340. );
  341. $this->assertTags($result, $expected);
  342. }
  343. /**
  344. * testForm method
  345. *
  346. * @access public
  347. * @return void
  348. */
  349. function testForm() {
  350. $result = $this->Ajax->form('showForm', 'post', array('model' => 'Form', 'url' => array('action' => 'showForm', 'controller' => 'forms'), 'update' => 'form_box'));
  351. $this->assertNoPattern('/model=/', $result);
  352. $result = $this->Ajax->form('showForm', 'post', array('name'=> 'SomeFormName', 'id' => 'MyFormID', 'url' => array('action' => 'showForm', 'controller' => 'forms'), 'update' => 'form_box'));
  353. $this->assertPattern('/id="MyFormID"/', $result);
  354. $this->assertPattern('/name="SomeFormName"/', $result);
  355. }
  356. /**
  357. * testSortable method
  358. *
  359. * @access public
  360. * @return void
  361. */
  362. function testSortable() {
  363. $result = $this->Ajax->sortable('ull', array('constraint' => false, 'ghosting' => true));
  364. $expected = array(
  365. array('script' => array('type' => 'text/javascript')),
  366. $this->cDataStart,
  367. "Sortable.create('ull', {constraint:false, ghosting:true});",
  368. $this->cDataEnd,
  369. '/script'
  370. );
  371. $this->assertTags($result, $expected);
  372. $result = $this->Ajax->sortable('ull', array('constraint' => 'false', 'ghosting' => 'true'));
  373. $expected = array(
  374. array('script' => array('type' => 'text/javascript')),
  375. $this->cDataStart,
  376. "Sortable.create('ull', {constraint:false, ghosting:true});",
  377. $this->cDataEnd,
  378. '/script'
  379. );
  380. $this->assertTags($result, $expected);
  381. $result = $this->Ajax->sortable('ull', array('constraint'=>'false', 'ghosting'=>'true', 'update' => 'myId'));
  382. $expected = array(
  383. array('script' => array('type' => 'text/javascript')),
  384. $this->cDataStart,
  385. "Sortable.create('ull', {constraint:false, ghosting:true, update:'myId'});",
  386. $this->cDataEnd,
  387. '/script'
  388. );
  389. $this->assertTags($result, $expected);
  390. $result = $this->Ajax->sortable('faqs', array('url'=>'http://www.cakephp.org',
  391. 'update' => 'faqs',
  392. 'tag' => 'tbody',
  393. 'handle' => 'grip',
  394. 'before' => "Element.hide('message')",
  395. 'complete' => "Element.show('message');"
  396. ));
  397. $expected = 'Sortable.create(\'faqs\', {update:\'faqs\', tag:\'tbody\', handle:\'grip\', onUpdate:function(sortable) {Element.hide(\'message\'); new Ajax.Updater(\'faqs\',\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, onComplete:function(request, json) {Element.show(\'message\');}, parameters:Sortable.serialize(\'faqs\'), requestHeaders:[\'X-Update\', \'faqs\']})}});';
  398. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*' . str_replace('/', '\\/', preg_quote($expected)) . '\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  399. $result = $this->Ajax->sortable('div', array('overlap' => 'foo'));
  400. $expected = array(
  401. array('script' => array('type' => 'text/javascript')),
  402. $this->cDataStart,
  403. "Sortable.create('div', {overlap:'foo'});",
  404. $this->cDataEnd,
  405. '/script'
  406. );
  407. $this->assertTags($result, $expected);
  408. $result = $this->Ajax->sortable('div', array('block' => false));
  409. $expected = "Sortable.create('div', {});";
  410. $this->assertEqual($result, $expected);
  411. $result = $this->Ajax->sortable('div', array('block' => false, 'scroll' => 'someID'));
  412. $expected = "Sortable.create('div', {scroll:'someID'});";
  413. $this->assertEqual($result, $expected);
  414. $result = $this->Ajax->sortable('div', array('block' => false, 'scroll' => 'window'));
  415. $expected = "Sortable.create('div', {scroll:window});";
  416. $this->assertEqual($result, $expected);
  417. $result = $this->Ajax->sortable('div', array('block' => false, 'scroll' => "$('someElement')"));
  418. $expected = "Sortable.create('div', {scroll:$('someElement')});";
  419. $this->assertEqual($result, $expected);
  420. }
  421. /**
  422. * testSubmitWithIndicator method
  423. *
  424. * @access public
  425. * @return void
  426. */
  427. function testSubmitWithIndicator() {
  428. $result = $this->Ajax->submit('Add', array('div' => false, 'url' => "http://www.cakephp.org", 'indicator' => 'loading', 'loading' => "doSomething()", 'complete' => 'doSomethingElse() '));
  429. $this->assertPattern('/onLoading:function\(request\) {doSomething\(\);\s+Element.show\(\'loading\'\);}/', $result);
  430. $this->assertPattern('/onComplete:function\(request, json\) {doSomethingElse\(\) ;\s+Element.hide\(\'loading\'\);}/', $result);
  431. }
  432. /**
  433. * testLink method
  434. *
  435. * @access public
  436. * @return void
  437. */
  438. function testLink() {
  439. $result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads');
  440. $this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  441. $this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
  442. $this->assertPattern('/^<a[^<>]+id="link\d+"[^<>]*>/', $result);
  443. $this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
  444. $this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
  445. $this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
  446. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  447. $this->assertPattern('/Event.observe\(\'link\d+\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  448. $this->assertPattern('/function\(event\)\s*{\s*new Ajax\.Request\(\'http:\/\/www.cakephp.org\/downloads\',\s*{asynchronous:true, evalScripts:true}\)\s*},\s*false\);/', $result);
  449. $result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads', array('confirm' => 'Are you sure & positive?'));
  450. $this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  451. $this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
  452. $this->assertPattern('/^<a[^<>]+id="link\d+"[^<>]*>/', $result);
  453. $this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
  454. $this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
  455. $this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
  456. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  457. $this->assertPattern('/Event.observe\(\'link\d+\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  458. $this->assertPattern('/function\(event\)\s*{\s*if \(confirm\(\'Are you sure & positive\?\'\)\) {\s*new Ajax\.Request\(\'http:\/\/www.cakephp.org\/downloads\',\s*{asynchronous:true, evalScripts:true}\);\s*}\s*else\s*{\s*event.returnValue = false;\s*return false;\s*}\s*},\s*false\);/', $result);
  459. $result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads', array('update' => 'myDiv'));
  460. $this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  461. $this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
  462. $this->assertPattern('/^<a[^<>]+id="link\d+"[^<>]*>/', $result);
  463. $this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
  464. $this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
  465. $this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
  466. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  467. $this->assertPattern('/Event.observe\(\'link\d+\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  468. $this->assertPattern('/function\(event\)\s*{\s*new Ajax\.Updater\(\'myDiv\',\s*\'http:\/\/www.cakephp.org\/downloads\',\s*{asynchronous:true, evalScripts:true, requestHeaders:\[\'X-Update\', \'myDiv\'\]}\)\s*},\s*false\);/', $result);
  469. $result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads', array('update' => 'myDiv', 'id' => 'myLink'));
  470. $this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  471. $this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
  472. $this->assertPattern('/^<a[^<>]+id="myLink"[^<>]*>/', $result);
  473. $this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
  474. $this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
  475. $this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
  476. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  477. $this->assertPattern('/Event.observe\(\'myLink\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  478. $this->assertPattern('/function\(event\)\s*{\s*new Ajax\.Updater\(\'myDiv\',\s*\'http:\/\/www.cakephp.org\/downloads\',\s*{asynchronous:true, evalScripts:true, requestHeaders:\[\'X-Update\', \'myDiv\'\]}\)\s*},\s*false\);/', $result);
  479. $result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads', array('update' => 'myDiv', 'id' => 'myLink', 'complete' => 'myComplete();'));
  480. $this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  481. $this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
  482. $this->assertPattern('/^<a[^<>]+id="myLink"[^<>]*>/', $result);
  483. $this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
  484. $this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
  485. $this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
  486. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  487. $this->assertPattern('/Event.observe\(\'myLink\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  488. $this->assertPattern('/function\(event\)\s*{\s*new Ajax\.Updater\(\'myDiv\',\s*\'http:\/\/www.cakephp.org\/downloads\',\s*{asynchronous:true, evalScripts:true, onComplete:function\(request, json\) {myComplete\(\);}, requestHeaders:\[\'X-Update\', \'myDiv\'\]}\)\s*},\s*false\);/', $result);
  489. $result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads', array('update' => 'myDiv', 'id' => 'myLink', 'loading' => 'myLoading();', 'complete' => 'myComplete();'));
  490. $this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  491. $this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
  492. $this->assertPattern('/^<a[^<>]+id="myLink"[^<>]*>/', $result);
  493. $this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
  494. $this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
  495. $this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
  496. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  497. $this->assertPattern('/Event.observe\(\'myLink\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  498. $this->assertPattern('/function\(event\)\s*{\s*new Ajax\.Updater\(\'myDiv\',\s*\'http:\/\/www.cakephp.org\/downloads\',\s*{asynchronous:true, evalScripts:true, onComplete:function\(request, json\) {myComplete\(\);}, onLoading:function\(request\) {myLoading\(\);}, requestHeaders:\[\'X-Update\', \'myDiv\'\]}\)\s*},\s*false\);/', $result);
  499. $result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads', array('update' => 'myDiv', 'encoding' => 'utf-8'));
  500. $this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  501. $this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
  502. $this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
  503. $this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
  504. $this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
  505. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  506. $this->assertPattern('/Event.observe\(\'\w+\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  507. $this->assertPattern('/function\(event\)\s*{\s*new Ajax\.Updater\(\'myDiv\',\s*\'http:\/\/www.cakephp.org\/downloads\',\s*{asynchronous:true, evalScripts:true, encoding:\'utf-8\', requestHeaders:\[\'X-Update\', \'myDiv\'\]}\)\s*},\s*false\);/', $result);
  508. $result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads', array('update' => 'myDiv', 'success' => 'success();'));
  509. $this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  510. $this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
  511. $this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
  512. $this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
  513. $this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
  514. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  515. $this->assertPattern('/Event.observe\(\'\w+\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  516. $this->assertPattern('/function\(event\)\s*{\s*new Ajax\.Updater\(\'myDiv\',\s*\'http:\/\/www.cakephp.org\/downloads\',\s*{asynchronous:true, evalScripts:true, onSuccess:function\(request\) {success\(\);}, requestHeaders:\[\'X-Update\', \'myDiv\'\]}\)\s*},\s*false\);/', $result);
  517. $result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads', array('update' => 'myDiv', 'failure' => 'failure();'));
  518. $this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  519. $this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
  520. $this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
  521. $this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
  522. $this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
  523. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  524. $this->assertPattern('/Event.observe\(\'\w+\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  525. $this->assertPattern('/function\(event\)\s*{\s*new Ajax\.Updater\(\'myDiv\',\s*\'http:\/\/www.cakephp.org\/downloads\',\s*{asynchronous:true, evalScripts:true, onFailure:function\(request\) {failure\(\);}, requestHeaders:\[\'X-Update\', \'myDiv\'\]}\)\s*},\s*false\);/', $result);
  526. $result = $this->Ajax->link('Ajax Link', '/test', array('complete' => 'test'));
  527. $this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  528. $this->assertPattern("/Event.observe\('link[0-9]+', [\w\d,'\(\)\s{}]+Ajax\.Request\([\w\d\s,'\(\){}:\/]+onComplete:function\(request, json\) {test}/", $result);
  529. $this->assertNoPattern('/^<a[^<>]+complete="test"[^<>]*>Ajax Link<\/a>/', $result);
  530. $this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
  531. $result = $this->Ajax->link(
  532. 'Ajax Link',
  533. array('controller' => 'posts', 'action' => 'index', '?' => array('one' => '1', 'two' => '2')),
  534. array('update' => 'myDiv', 'id' => 'myLink')
  535. );
  536. $this->assertPattern('#/posts/\?one\=1\&two\=2#', $result);
  537. }
  538. /**
  539. * testRemoteTimer method
  540. *
  541. * @access public
  542. * @return void
  543. */
  544. function testRemoteTimer() {
  545. $result = $this->Ajax->remoteTimer(array('url' => 'http://www.cakephp.org'));
  546. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  547. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  548. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new PeriodicalExecuter\(function\(\) {.+}, 10\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  549. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true})')) . '/', $result);
  550. $result = $this->Ajax->remoteTimer(array('url' => 'http://www.cakephp.org', 'frequency' => 25));
  551. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  552. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  553. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new PeriodicalExecuter\(function\(\) {.+}, 25\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  554. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true})')) . '/', $result);
  555. $result = $this->Ajax->remoteTimer(array('url' => 'http://www.cakephp.org', 'complete' => 'complete();'));
  556. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  557. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  558. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new PeriodicalExecuter\(function\(\) {.+}, 10\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  559. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, onComplete:function(request, json) {complete();}})')) . '/', $result);
  560. $result = $this->Ajax->remoteTimer(array('url' => 'http://www.cakephp.org', 'complete' => 'complete();', 'create' => 'create();'));
  561. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  562. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  563. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new PeriodicalExecuter\(function\(\) {.+}, 10\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  564. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, onComplete:function(request, json) {complete();}, onCreate:function(request, xhr) {create();}})')) . '/', $result);
  565. $result = $this->Ajax->remoteTimer(array('url' => 'http://www.cakephp.org', 'exception' => 'alert(exception);'));
  566. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  567. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  568. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new PeriodicalExecuter\(function\(\) {.+}, 10\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  569. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, onException:function(request, exception) {alert(exception);}})')) . '/', $result);
  570. $result = $this->Ajax->remoteTimer(array('url' => 'http://www.cakephp.org', 'contentType' => 'application/x-www-form-urlencoded'));
  571. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  572. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  573. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new PeriodicalExecuter\(function\(\) {.+}, 10\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  574. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, contentType:\'application/x-www-form-urlencoded\'})')) . '/', $result);
  575. $result = $this->Ajax->remoteTimer(array('url' => 'http://www.cakephp.org', 'method' => 'get', 'encoding' => 'utf-8'));
  576. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  577. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  578. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new PeriodicalExecuter\(function\(\) {.+}, 10\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  579. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, method:\'get\', encoding:\'utf-8\'})')) . '/', $result);
  580. $result = $this->Ajax->remoteTimer(array('url' => 'http://www.cakephp.org', 'postBody' => 'var1=value1'));
  581. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  582. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  583. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new PeriodicalExecuter\(function\(\) {.+}, 10\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  584. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, postBody:\'var1=value1\'})')) . '/', $result);
  585. }
  586. /**
  587. * testObserveField method
  588. *
  589. * @access public
  590. * @return void
  591. */
  592. function testObserveField() {
  593. $result = $this->Ajax->observeField('field', array('url' => 'http://www.cakephp.org'));
  594. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  595. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  596. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new Form.Element.EventObserver\(\'field\', function\(element, value\) {.+}\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  597. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, parameters:Form.Element.serialize(\'field\')})')) . '/', $result);
  598. $result = $this->Ajax->observeField('field', array('url' => 'http://www.cakephp.org', 'frequency' => 15));
  599. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  600. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  601. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new Form.Element.Observer\(\'field\', 15, function\(element, value\) {.+}\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  602. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, parameters:Form.Element.serialize(\'field\')})')) . '/', $result);
  603. $result = $this->Ajax->observeField('field', array('url' => 'http://www.cakephp.org', 'update' => 'divId'));
  604. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  605. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  606. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new Form.Element.EventObserver\(\'field\', function\(element, value\) {.+}\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  607. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Updater(\'divId\',\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, parameters:Form.Element.serialize(\'field\'), requestHeaders:[\'X-Update\', \'divId\']})')) . '/', $result);
  608. $result = $this->Ajax->observeField('field', array('url' => 'http://www.cakephp.org', 'update' => 'divId', 'with' => 'Form.Element.serialize(\'otherField\')'));
  609. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  610. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  611. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new Form.Element.EventObserver\(\'field\', function\(element, value\) {.+}\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  612. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Updater(\'divId\',\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, parameters:Form.Element.serialize(\'otherField\'), requestHeaders:[\'X-Update\', \'divId\']})')) . '/', $result);
  613. }
  614. /**
  615. * testObserveForm method
  616. *
  617. * @access public
  618. * @return void
  619. */
  620. function testObserveForm() {
  621. $result = $this->Ajax->observeForm('form', array('url' => 'http://www.cakephp.org'));
  622. $expected = array(
  623. array('script' => array('type' => 'text/javascript')),
  624. $this->cDataStart,
  625. "new Form.EventObserver('form', function(element, value) {new Ajax.Request('http://www.cakephp.org', {asynchronous:true, evalScripts:true, parameters:Form.serialize('form')})})",
  626. $this->cDataEnd,
  627. '/script'
  628. );
  629. $this->assertTags($result, $expected);
  630. $result = $this->Ajax->observeForm('form', array('url' => 'http://www.cakephp.org', 'frequency' => 15));
  631. $expected = array(
  632. array('script' => array('type' => 'text/javascript')),
  633. $this->cDataStart,
  634. "new Form.Observer('form', 15, function(element, value) {new Ajax.Request('http://www.cakephp.org', {asynchronous:true, evalScripts:true, parameters:Form.serialize('form')})})",
  635. $this->cDataEnd,
  636. '/script'
  637. );
  638. $this->assertTags($result, $expected);
  639. $result = $this->Ajax->observeForm('form', array('url' => 'http://www.cakephp.org', 'update' => 'divId'));
  640. $expected = array(
  641. array('script' => array('type' => 'text/javascript')),
  642. $this->cDataStart,
  643. "new Form.EventObserver('form', function(element, value) {new Ajax.Updater('divId','http://www.cakephp.org', {asynchronous:true, evalScripts:true, parameters:Form.serialize('form'), requestHeaders:['X-Update', 'divId']})}",
  644. $this->cDataEnd,
  645. '/script'
  646. );
  647. $this->assertTags($result, $expected);
  648. $result = $this->Ajax->observeForm('form', array('url' => 'http://www.cakephp.org', 'update' => 'divId', 'with' => "Form.serialize('otherForm')"));
  649. $expected = array(
  650. array('script' => array('type' => 'text/javascript')),
  651. $this->cDataStart,
  652. "new Form.EventObserver('form', function(element, value) {new Ajax.Updater('divId','http://www.cakephp.org', {asynchronous:true, evalScripts:true, parameters:Form.serialize('otherForm'), requestHeaders:['X-Update', 'divId']})}",
  653. $this->cDataEnd,
  654. '/script'
  655. );
  656. $this->assertTags($result, $expected);
  657. }
  658. /**
  659. * testSlider method
  660. *
  661. * @access public
  662. * @return void
  663. */
  664. function testSlider() {
  665. $result = $this->Ajax->slider('sliderId', 'trackId');
  666. $expected = array(
  667. array('script' => array('type' => 'text/javascript')),
  668. $this->cDataStart,
  669. "var sliderId = new Control.Slider('sliderId', 'trackId', {});",
  670. $this->cDataEnd,
  671. '/script'
  672. );
  673. $this->assertTags($result, $expected);
  674. $result = $this->Ajax->slider('sliderId', 'trackId', array('axis' => 'vertical'));
  675. $expected = array(
  676. array('script' => array('type' => 'text/javascript')),
  677. $this->cDataStart,
  678. "var sliderId = new Control.Slider('sliderId', 'trackId', {axis:'vertical'});",
  679. $this->cDataEnd,
  680. '/script'
  681. );
  682. $this->assertTags($result, $expected);
  683. $result = $this->Ajax->slider('sliderId', 'trackId', array('axis' => 'vertical', 'minimum' => 60, 'maximum' => 288, 'alignX' => -28, 'alignY' => -5, 'disabled' => true));
  684. $expected = array(
  685. array('script' => array('type' => 'text/javascript')),
  686. $this->cDataStart,
  687. "var sliderId = new Control.Slider('sliderId', 'trackId', {axis:'vertical', minimum:60, maximum:288, alignX:-28, alignY:-5, disabled:true});",
  688. $this->cDataEnd,
  689. '/script'
  690. );
  691. $this->assertTags($result, $expected);
  692. $result = $this->Ajax->slider('sliderId', 'trackId', array('change' => "alert('changed');"));
  693. $expected = array(
  694. array('script' => array('type' => 'text/javascript')),
  695. $this->cDataStart,
  696. "var sliderId = new Control.Slider('sliderId', 'trackId', {onChange:function(value) {alert('changed');}});",
  697. $this->cDataEnd,
  698. '/script'
  699. );
  700. $this->assertTags($result, $expected);
  701. $result = $this->Ajax->slider('sliderId', 'trackId', array('change' => "alert('changed');", 'slide' => "alert('sliding');"));
  702. $expected = array(
  703. array('script' => array('type' => 'text/javascript')),
  704. $this->cDataStart,
  705. "var sliderId = new Control.Slider('sliderId', 'trackId', {onChange:function(value) {alert('changed');}, onSlide:function(value) {alert('sliding');}});",
  706. $this->cDataEnd,
  707. '/script'
  708. );
  709. $this->assertTags($result, $expected);
  710. $result = $this->Ajax->slider('sliderId', 'trackId', array('values' => array(10, 20, 30)));
  711. $expected = array(
  712. array('script' => array('type' => 'text/javascript')),
  713. $this->cDataStart,
  714. "var sliderId = new Control.Slider('sliderId', 'trackId', {values:[10,20,30]});",
  715. $this->cDataEnd,
  716. '/script'
  717. );
  718. $this->assertTags($result, $expected);
  719. $result = $this->Ajax->slider('sliderId', 'trackId', array('range' => '$R(10, 30)'));
  720. $expected = array(
  721. array('script' => array('type' => 'text/javascript')),
  722. $this->cDataStart,
  723. "var sliderId = new Control.Slider('sliderId', 'trackId', {range:\$R(10, 30)});",
  724. $this->cDataEnd,
  725. '/script'
  726. );
  727. $this->assertTags($result, $expected);
  728. }
  729. /**
  730. * testRemoteFunction method
  731. *
  732. * @access public
  733. * @return void
  734. */
  735. function testRemoteFunction() {
  736. $result = $this->Ajax->remoteFunction(array('complete' => 'testComplete();'));
  737. $expected = "new Ajax.Request('/', {asynchronous:true, evalScripts:true, onComplete:function(request, json) {testComplete();}})";
  738. $this->assertEqual($result, $expected);
  739. $result = $this->Ajax->remoteFunction(array('update' => 'myDiv'));
  740. $expected = "new Ajax.Updater('myDiv','/', {asynchronous:true, evalScripts:true, requestHeaders:['X-Update', 'myDiv']})";
  741. $this->assertEqual($result, $expected);
  742. $result = $this->Ajax->remoteFunction(array('update' => array('div1', 'div2')));
  743. $expected = "new Ajax.Updater(document.createElement('div'),'/', {asynchronous:true, evalScripts:true, requestHeaders:['X-Update', 'div1 div2']})";
  744. $this->assertEqual($result, $expected);
  745. $result = $this->Ajax->remoteFunction(array('update' => 'myDiv', 'confirm' => 'Are you sure?'));
  746. $expected = "if (confirm('Are you sure?')) { new Ajax.Updater('myDiv','/', {asynchronous:true, evalScripts:true, requestHeaders:['X-Update', 'myDiv']}); } else { event.returnValue = false; return false; }";
  747. $this->assertEqual($result, $expected);
  748. }
  749. /**
  750. * testDiv method
  751. *
  752. * @access public
  753. * @return void
  754. */
  755. function testDiv() {
  756. ob_flush();
  757. $oldXUpdate = env('HTTP_X_UPDATE');
  758. $result = $this->Ajax->div('myDiv');
  759. $this->assertTags($result, array('div' => array('id' => 'myDiv')));
  760. $_SERVER['HTTP_X_UPDATE'] = null;
  761. $result = $this->Ajax->divEnd('myDiv');
  762. $this->assertTags($result, '/div');
  763. $_SERVER['HTTP_X_UPDATE'] = 'secondDiv';
  764. $result = $this->Ajax->div('myDiv');
  765. $this->assertTags($result, array('div' => array('id' => 'myDiv')));
  766. $result = $this->Ajax->divEnd('myDiv');
  767. $this->assertTags($result, '/div');
  768. $_SERVER['HTTP_X_UPDATE'] = 'secondDiv myDiv anotherDiv';
  769. $result = $this->Ajax->div('myDiv');
  770. $this->assertTrue(empty($result));
  771. $result = $this->Ajax->divEnd('myDiv');
  772. $this->assertTrue(empty($result));
  773. $_SERVER['HTTP_X_UPDATE'] = $oldXUpdate;
  774. }
  775. /**
  776. * testAfterRender method
  777. *
  778. * @access public
  779. * @return void
  780. */
  781. function testAfterRender() {
  782. $oldXUpdate = env('HTTP_X_UPDATE');
  783. $this->Ajax->Javascript =& new TestJavascriptHelper();
  784. $_SERVER['HTTP_X_UPDATE'] = 'secondDiv myDiv anotherDiv';
  785. $result = $this->Ajax->div('myDiv');
  786. $this->assertTrue(empty($result));
  787. echo 'Contents of myDiv';
  788. $result = $this->Ajax->divEnd('myDiv');
  789. $this->assertTrue(empty($result));
  790. ob_start();
  791. $this->Ajax->afterRender();
  792. $result = array_shift($this->Ajax->Javascript->codeBlocks);
  793. $this->assertPattern('/^\s*' . str_replace('/', '\\/', preg_quote('var __ajaxUpdater__ = {myDiv:"Contents%20of%20myDiv"};')) . '\s*' . str_replace('/', '\\/', preg_quote('for (n in __ajaxUpdater__) { if (typeof __ajaxUpdater__[n] == "string" && $(n)) Element.update($(n), unescape(decodeURIComponent(__ajaxUpdater__[n]))); }')) . '\s*$/s', $result);
  794. $_SERVER['HTTP_X_UPDATE'] = $oldXUpdate;
  795. }
  796. /**
  797. * testEditor method
  798. *
  799. * @access public
  800. * @return void
  801. */
  802. function testEditor() {
  803. $result = $this->Ajax->editor('myDiv', '/');
  804. $expected = array(
  805. array('script' => array('type' => 'text/javascript')),
  806. $this->cDataStart,
  807. "new Ajax.InPlaceEditor('myDiv', '/', {ajaxOptions:{asynchronous:true, evalScripts:true}});",
  808. $this->cDataEnd,
  809. '/script'
  810. );
  811. $this->assertTags($result, $expected);
  812. $result = $this->Ajax->editor('myDiv', '/', array('complete' => 'testComplete();'));
  813. $expected = array(
  814. array('script' => array('type' => 'text/javascript')),
  815. $this->cDataStart,
  816. "new Ajax.InPlaceEditor('myDiv', '/', {ajaxOptions:{asynchronous:true, evalScripts:true, onComplete:function(request, json) {testComplete();}}});",
  817. $this->cDataEnd,
  818. '/script'
  819. );
  820. $this->assertTags($result, $expected);
  821. $result = $this->Ajax->editor('myDiv', '/', array('callback' => 'callback();'));
  822. $expected = array(
  823. array('script' => array('type' => 'text/javascript')),
  824. $this->cDataStart,
  825. "new Ajax.InPlaceEditor('myDiv', '/', {callback:function(form, value) {callback();}, ajaxOptions:{asynchronous:true, evalScripts:true}});",
  826. $this->cDataEnd,
  827. '/script'
  828. );
  829. $this->assertTags($result, $expected);
  830. $result = $this->Ajax->editor('myDiv', '/', array('collection' => array(1 => 'first', 2 => 'second')));
  831. $expected = array(
  832. array('script' => array('type' => 'text/javascript')),
  833. $this->cDataStart,
  834. "new Ajax.InPlaceCollectionEditor('myDiv', '/', {collection:{\"1\":\"first\",\"2\":\"second\"}, ajaxOptions:{asynchronous:true, evalScripts:true}});",
  835. $this->cDataEnd,
  836. '/script'
  837. );
  838. $this->assertTags($result, $expected);
  839. $result = $this->Ajax->editor('myDiv', '/', array('var' => 'myVar'));
  840. $expected = array(
  841. array('script' => array('type' => 'text/javascript')),
  842. $this->cDataStart,
  843. "var myVar = new Ajax.InPlaceEditor('myDiv', '/', {ajaxOptions:{asynchronous:true, evalScripts:true}});",
  844. $this->cDataEnd,
  845. '/script'
  846. );
  847. $this->assertTags($result, $expected);
  848. }
  849. }
  850. ?>