PageRenderTime 61ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

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

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