PageRenderTime 62ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/tfe/maitre-d
PHP | 737 lines | 453 code | 64 blank | 220 comment | 2 complexity | a3f3fa026c8aec7c198ac73960dc0a0b MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /* SVN FILE: $Id: ajax.test.php 7296 2008-06-27 09:09:03Z gwoo $ */
  3. /**
  4. * Short description for 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-2008, Cake Software Foundation, Inc.
  12. * 1785 E. Sahara Avenue, Suite 490-204
  13. * Las Vegas, Nevada 89104
  14. *
  15. * Licensed under The Open Group Test Suite License
  16. * Redistributions of files must retain the above copyright notice.
  17. *
  18. * @filesource
  19. * @copyright Copyright 2005-2008, Cake Software Foundation, Inc.
  20. * @link https://trac.cakephp.org/wiki/Developement/TestSuite CakePHP(tm) Tests
  21. * @package cake.tests
  22. * @subpackage cake.tests.cases.libs.view.helpers
  23. * @since CakePHP(tm) v 1.2.0.4206
  24. * @version $Revision: 7296 $
  25. * @modifiedby $LastChangedBy: gwoo $
  26. * @lastmodified $Date: 2008-06-27 02:09:03 -0700 (Fri, 27 Jun 2008) $
  27. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  28. */
  29. if (!defined('CAKEPHP_UNIT_TEST_EXECUTION')) {
  30. define('CAKEPHP_UNIT_TEST_EXECUTION', 1);
  31. }
  32. uses('view'.DS.'helpers'.DS.'app_helper', 'controller'.DS.'controller', 'model'.DS.'model', 'view'.DS.'helper', 'view'.DS.'helpers'.DS.'ajax',
  33. 'view'.DS.'helpers'.DS.'html', 'view'.DS.'helpers'.DS.'form', 'view'.DS.'helpers'.DS.'javascript');
  34. /**
  35. * AjaxTestController class
  36. *
  37. * @package cake
  38. * @subpackage cake.tests.cases.libs.view.helpers
  39. */
  40. class AjaxTestController extends Controller {
  41. /**
  42. * name property
  43. *
  44. * @var string 'AjaxTest'
  45. * @access public
  46. */
  47. var $name = 'AjaxTest';
  48. /**
  49. * uses property
  50. *
  51. * @var mixed null
  52. * @access public
  53. */
  54. var $uses = null;
  55. }
  56. /**
  57. * PostAjaxTest class
  58. *
  59. * @package cake
  60. * @subpackage cake.tests.cases.libs.view.helpers
  61. */
  62. class PostAjaxTest extends Model {
  63. /**
  64. * primaryKey property
  65. *
  66. * @var string 'id'
  67. * @access public
  68. */
  69. var $primaryKey = 'id';
  70. /**
  71. * useTable property
  72. *
  73. * @var bool false
  74. * @access public
  75. */
  76. var $useTable = false;
  77. /**
  78. * schema method
  79. *
  80. * @access public
  81. * @return void
  82. */
  83. function schema() {
  84. return array(
  85. 'id' => array('type' => 'integer', 'null' => '', 'default' => '', 'length' => '8'),
  86. 'name' => array('type' => 'string', 'null' => '', 'default' => '', 'length' => '255'),
  87. 'created' => array('type' => 'date', 'null' => '1', 'default' => '', 'length' => ''),
  88. 'updated' => array('type' => 'datetime', 'null' => '1', 'default' => '', 'length' => null)
  89. );
  90. }
  91. }
  92. /**
  93. * TestAjaxHelper class
  94. *
  95. * @package cake
  96. * @subpackage cake.tests.cases.libs.view.helpers
  97. */
  98. class TestAjaxHelper extends AjaxHelper {
  99. /**
  100. * stop method
  101. *
  102. * @access public
  103. * @return void
  104. */
  105. function _stop() {
  106. }
  107. }
  108. /**
  109. * TestJavascriptHelper class
  110. *
  111. * @package cake
  112. * @subpackage cake.tests.cases.libs.view.helpers
  113. */
  114. class TestJavascriptHelper extends JavascriptHelper {
  115. /**
  116. * codeBlocks property
  117. *
  118. * @var mixed
  119. * @access public
  120. */
  121. var $codeBlocks;
  122. /**
  123. * codeBlock method
  124. *
  125. * @param mixed $parameter
  126. * @access public
  127. * @return void
  128. */
  129. function codeBlock($parameter) {
  130. if (empty($this->codeBlocks)) {
  131. $this->codeBlocks = array();
  132. }
  133. $this->codeBlocks[] = $parameter;
  134. }
  135. }
  136. /**
  137. * Short description for class.
  138. *
  139. * @package cake.tests
  140. * @subpackage cake.tests.cases.libs.view.helpers
  141. */
  142. class AjaxTest extends CakeTestCase {
  143. /**
  144. * setUp method
  145. *
  146. * @access public
  147. * @return void
  148. */
  149. function setUp() {
  150. Router::reload();
  151. $this->Ajax =& new TestAjaxHelper();
  152. $this->Ajax->Html =& new HtmlHelper();
  153. $this->Ajax->Form =& new FormHelper();
  154. $this->Ajax->Javascript =& new JavascriptHelper();
  155. $this->Ajax->Form->Html =& $this->Ajax->Html;
  156. $view =& new View(new AjaxTestController());
  157. ClassRegistry::addObject('view', $view);
  158. ClassRegistry::addObject('PostAjaxTest', new PostAjaxTest());
  159. }
  160. /**
  161. * testEvalScripts method
  162. *
  163. * @access public
  164. * @return void
  165. */
  166. function testEvalScripts() {
  167. $result = $this->Ajax->link('Test Link', 'http://www.cakephp.org', array('id' => 'link1', 'update' => 'content', 'evalScripts' => false));
  168. $this->assertPattern('/^<a\s+[^<>]+>Test Link<\/a><script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*' . str_replace('/', '\\/', preg_quote('Event.observe(\'link1\', \'click\', function(event) { new Ajax.Updater(\'content\',\'http://www.cakephp.org\', {asynchronous:true, evalScripts:false, requestHeaders:[\'X-Update\', \'content\']}) }, false);')) . '\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  169. $this->assertPattern('/^<a\s+[^<>]*href="http:\/\/www.cakephp.org"[^<>]*>/', $result);
  170. $this->assertPattern('/^<a\s+[^<>]*id="link1"[^<>]*>/', $result);
  171. $this->assertPattern('/^<a\s+[^<>]*onclick="\s*' . str_replace('/', '\\/', preg_quote('event.returnValue = false; return false;')) . '\s*"[^<>]*>/', $result);
  172. $result = $this->Ajax->link('Test Link', 'http://www.cakephp.org', array('id' => 'link1', 'update' => 'content'));
  173. $this->assertPattern('/^<a\s+[^<>]+>Test Link<\/a><script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*' . str_replace('/', '\\/', preg_quote('Event.observe(\'link1\', \'click\', function(event) { new Ajax.Updater(\'content\',\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, requestHeaders:[\'X-Update\', \'content\']}) }, false);')) . '\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  174. $this->assertPattern('/^<a\s+[^<>]*href="http:\/\/www.cakephp.org"[^<>]*>/', $result);
  175. $this->assertPattern('/^<a\s+[^<>]*id="link1"[^<>]*>/', $result);
  176. $this->assertPattern('/^<a\s+[^<>]*onclick="\s*' . str_replace('/', '\\/', preg_quote('event.returnValue = false; return false;')) . '\s*"[^<>]*>/', $result);
  177. }
  178. /**
  179. * testAutoComplete method
  180. *
  181. * @access public
  182. * @return void
  183. */
  184. function testAutoComplete() {
  185. $result = $this->Ajax->autoComplete('PostAjaxTest.title' , '/posts', array('minChars' => 2));
  186. $this->assertPattern('/^<input[^<>]+name="data\[PostAjaxTest\]\[title\]"[^<>]+autocomplete="off"[^<>]+\/>/', $result);
  187. $this->assertPattern('/<div[^<>]+id="PostAjaxTestTitle_autoComplete"[^<>]*><\/div>/', $result);
  188. $this->assertPattern('/<div[^<>]+class="auto_complete"[^<>]*><\/div>/', $result);
  189. $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);
  190. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Autocompleter(\'PostAjaxTestTitle\', \'PostAjaxTestTitle_autoComplete\', \'/posts\', {minChars:2});')) . '/', $result);
  191. $this->assertPattern('/<\/script>$/', $result);
  192. $result = $this->Ajax->autoComplete('PostAjaxTest.title' , '/posts', array('paramName' => 'parameter'));
  193. $this->assertPattern('/^<input[^<>]+name="data\[PostAjaxTest\]\[title\]"[^<>]+autocomplete="off"[^<>]+\/>/', $result);
  194. $this->assertPattern('/<div[^<>]+id="PostAjaxTestTitle_autoComplete"[^<>]*><\/div>/', $result);
  195. $this->assertPattern('/<div[^<>]+class="auto_complete"[^<>]*><\/div>/', $result);
  196. $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);
  197. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Autocompleter(\'PostAjaxTestTitle\', \'PostAjaxTestTitle_autoComplete\', \'/posts\', {paramName:\'parameter\'});')) . '/', $result);
  198. $this->assertPattern('/<\/script>$/', $result);
  199. $result = $this->Ajax->autoComplete('PostAjaxTest.title' , '/posts', array('paramName' => 'parameter', 'updateElement' => 'elementUpdated', 'afterUpdateElement' => 'function (input, element) { alert("updated"); }'));
  200. $this->assertPattern('/^<input[^<>]+name="data\[PostAjaxTest\]\[title\]"[^<>]+autocomplete="off"[^<>]+\/>/', $result);
  201. $this->assertPattern('/<div[^<>]+id="PostAjaxTestTitle_autoComplete"[^<>]*><\/div>/', $result);
  202. $this->assertPattern('/<div[^<>]+class="auto_complete"[^<>]*><\/div>/', $result);
  203. $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);
  204. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Autocompleter(\'PostAjaxTestTitle\', \'PostAjaxTestTitle_autoComplete\', \'/posts\', {paramName:\'parameter\', updateElement:elementUpdated, afterUpdateElement:function (input, element) { alert("updated"); }});')) . '/', $result);
  205. $this->assertPattern('/<\/script>$/', $result);
  206. $result = $this->Ajax->autoComplete('PostAjaxTest.title' , '/posts', array('callback' => 'function (input, queryString) { alert("requesting"); }'));
  207. $this->assertPattern('/^<input[^<>]+name="data\[PostAjaxTest\]\[title\]"[^<>]+autocomplete="off"[^<>]+\/>/', $result);
  208. $this->assertPattern('/<div[^<>]+id="PostAjaxTestTitle_autoComplete"[^<>]*><\/div>/', $result);
  209. $this->assertPattern('/<div[^<>]+class="auto_complete"[^<>]*><\/div>/', $result);
  210. $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);
  211. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Autocompleter(\'PostAjaxTestTitle\', \'PostAjaxTestTitle_autoComplete\', \'/posts\', {callback:function (input, queryString) { alert("requesting"); }});')) . '/', $result);
  212. $this->assertPattern('/<\/script>$/', $result);
  213. }
  214. /**
  215. * testAsynchronous method
  216. *
  217. * @access public
  218. * @return void
  219. */
  220. function testAsynchronous() {
  221. $result = $this->Ajax->link('Test Link', '/', array('id' => 'link1', 'update' => 'content', 'type' => 'synchronous'));
  222. $this->assertPattern('/^<a\s+[^<>]+>Test Link<\/a><script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*' . str_replace('/', '\\/', preg_quote('Event.observe(\'link1\', \'click\', function(event) { new Ajax.Updater(\'content\',\'/\', {asynchronous:false, evalScripts:true, requestHeaders:[\'X-Update\', \'content\']}) }, false);')) . '\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  223. $this->assertPattern('/^<a\s+[^<>]*href="\/"[^<>]*>/', $result);
  224. $this->assertPattern('/^<a\s+[^<>]*id="link1"[^<>]*>/', $result);
  225. $this->assertPattern('/^<a\s+[^<>]*onclick="\s*' . str_replace('/', '\\/', preg_quote('event.returnValue = false; return false;')) . '\s*"[^<>]*>/', $result);
  226. }
  227. /**
  228. * testDraggable method
  229. *
  230. * @access public
  231. * @return void
  232. */
  233. function testDraggable() {
  234. $result = $this->Ajax->drag('id', array('handle' => 'other_id'));
  235. $expected = 'new Draggable(\'id\', {handle:\'other_id\'});';
  236. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*' . str_replace('/', '\\/', preg_quote($expected)) . '\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  237. }
  238. /**
  239. * testDroppable method
  240. *
  241. * @access public
  242. * @return void
  243. */
  244. function testDroppable() {
  245. $result = $this->Ajax->drop('droppable', array('accept' => 'crap'));
  246. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  247. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  248. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*Droppables.add\(\'droppable\', {accept:\'crap\'}\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  249. $result = $this->Ajax->dropRemote('droppable', array('accept' => 'crap'), array('url' => '/posts'));
  250. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  251. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  252. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*Droppables.add\(\'droppable\', {accept:\'crap\', onDrop:function\(element, droppable, event\) {.+}\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  253. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'/posts\', {asynchronous:true, evalScripts:true})')) . '/', $result);
  254. $result = $this->Ajax->dropRemote('droppable', array('accept' => array('crap1', 'crap2')), array('url' => '/posts'));
  255. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  256. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  257. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*Droppables.add\(\'droppable\', {accept:\["crap1","crap2"\], onDrop:function\(element, droppable, event\) {.+}\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  258. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'/posts\', {asynchronous:true, evalScripts:true})')) . '/', $result);
  259. $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}'));
  260. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  261. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  262. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*Droppables.add\(\'droppable\', {accept:\'crap\', onDrop:function\(element, droppable, event\) {.+}\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  263. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'/posts\', {asynchronous:true, evalScripts:true, parameters:{drag_id:element.id,drop_id:dropon.id,event:event.whatever_you_want}})')) . '/', $result);
  264. }
  265. /**
  266. * testForm method
  267. *
  268. * @access public
  269. * @return void
  270. */
  271. function testForm() {
  272. $result = $this->Ajax->form('showForm', 'post', array('model' => 'Form', 'url' => array('action' => 'showForm', 'controller' => 'forms'), 'update' => 'form_box'));
  273. $this->assertNoPattern('/model=/', $result);
  274. $result = $this->Ajax->form('showForm', 'post', array('name'=> 'SomeFormName', 'id' => 'MyFormID', 'url' => array('action' => 'showForm', 'controller' => 'forms'), 'update' => 'form_box'));
  275. $this->assertPattern('/id="MyFormID"/', $result);
  276. $this->assertPattern('/name="SomeFormName"/', $result);
  277. }
  278. /**
  279. * testSortable method
  280. *
  281. * @access public
  282. * @return void
  283. */
  284. function testSortable() {
  285. $result = $this->Ajax->sortable('ull', array('constraint' => false, 'ghosting' => true));
  286. $expected = 'Sortable.create(\'ull\', {constraint:false, ghosting:true});';
  287. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*' . str_replace('/', '\\/', preg_quote($expected)) . '\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  288. $result = $this->Ajax->sortable('ull', array('constraint' => 'false', 'ghosting' => 'true'));
  289. $expected = 'Sortable.create(\'ull\', {constraint:false, ghosting:true});';
  290. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*' . str_replace('/', '\\/', preg_quote($expected)) . '\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  291. $result = $this->Ajax->sortable('ull', array('constraint'=>'false', 'ghosting'=>'true', 'update' => 'myId'));
  292. $expected = 'Sortable.create(\'ull\', {constraint:false, ghosting:true, update:\'myId\'});';
  293. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*' . str_replace('/', '\\/', preg_quote($expected)) . '\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  294. $result = $this->Ajax->sortable('faqs', array('url'=>'http://www.cakephp.org',
  295. 'update' => 'faqs',
  296. 'tag' => 'tbody',
  297. 'handle' => 'grip',
  298. 'before' => 'Element.hide(\'message\')',
  299. 'complete' => 'Element.show(\'message\');'));
  300. $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\']})}});';
  301. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*' . str_replace('/', '\\/', preg_quote($expected)) . '\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  302. $result = $this->Ajax->sortable('div', array('overlap' => 'foo'));
  303. $expected = "Sortable.create('div', {overlap:'foo'});";
  304. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote($expected)) . '/', $result);
  305. $result = $this->Ajax->sortable('div', array('block' => false));
  306. $expected = "Sortable.create('div', {});";
  307. $this->assertEqual($result, $expected);
  308. $result = $this->Ajax->sortable('div', array('block' => false, 'scroll' => 'someID'));
  309. $expected = "Sortable.create('div', {scroll:'someID'});";
  310. $this->assertEqual($result, $expected);
  311. $result = $this->Ajax->sortable('div', array('block' => false, 'scroll' => 'window'));
  312. $expected = "Sortable.create('div', {scroll:window});";
  313. $this->assertEqual($result, $expected);
  314. $result = $this->Ajax->sortable('div', array('block' => false, 'scroll' => '$("someElement")'));
  315. $expected = "Sortable.create('div', {scroll:$(\"someElement\")});";
  316. $this->assertEqual($result, $expected);
  317. }
  318. /**
  319. * testSubmitWithIndicator method
  320. *
  321. * @access public
  322. * @return void
  323. */
  324. function testSubmitWithIndicator() {
  325. $result = $this->Ajax->submit('Add', array('div' => false, 'url' => "http://www.cakephp.org", 'indicator' => 'loading', 'loading' => "doSomething()", 'complete' => 'doSomethingElse() '));
  326. $this->assertPattern('/onLoading:function\(request\) {doSomething\(\);\s+Element.show\(\'loading\'\);}/', $result);
  327. $this->assertPattern('/onComplete:function\(request, json\) {doSomethingElse\(\) ;\s+Element.hide\(\'loading\'\);}/', $result);
  328. }
  329. /**
  330. * testLink method
  331. *
  332. * @access public
  333. * @return void
  334. */
  335. function testLink() {
  336. $result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads');
  337. $this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  338. $this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
  339. $this->assertPattern('/^<a[^<>]+id="link\d+"[^<>]*>/', $result);
  340. $this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
  341. $this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
  342. $this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
  343. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  344. $this->assertPattern('/Event.observe\(\'link\d+\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  345. $this->assertPattern('/function\(event\)\s*{\s*new Ajax\.Request\(\'http:\/\/www.cakephp.org\/downloads\',\s*{asynchronous:true, evalScripts:true}\)\s*},\s*false\);/', $result);
  346. $result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads', array('confirm' => 'Are you sure & positive?'));
  347. $this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  348. $this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
  349. $this->assertPattern('/^<a[^<>]+id="link\d+"[^<>]*>/', $result);
  350. $this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
  351. $this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
  352. $this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
  353. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  354. $this->assertPattern('/Event.observe\(\'link\d+\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  355. $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);
  356. $result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads', array('update' => 'myDiv'));
  357. $this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  358. $this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
  359. $this->assertPattern('/^<a[^<>]+id="link\d+"[^<>]*>/', $result);
  360. $this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
  361. $this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
  362. $this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
  363. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  364. $this->assertPattern('/Event.observe\(\'link\d+\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  365. $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);
  366. $result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads', array('update' => 'myDiv', 'id' => 'myLink'));
  367. $this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  368. $this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
  369. $this->assertPattern('/^<a[^<>]+id="myLink"[^<>]*>/', $result);
  370. $this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
  371. $this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
  372. $this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
  373. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  374. $this->assertPattern('/Event.observe\(\'myLink\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  375. $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);
  376. $result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads', array('update' => 'myDiv', 'id' => 'myLink', 'complete' => 'myComplete();'));
  377. $this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  378. $this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
  379. $this->assertPattern('/^<a[^<>]+id="myLink"[^<>]*>/', $result);
  380. $this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
  381. $this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
  382. $this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
  383. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  384. $this->assertPattern('/Event.observe\(\'myLink\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  385. $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);
  386. $result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads', array('update' => 'myDiv', 'id' => 'myLink', 'loading' => 'myLoading();', 'complete' => 'myComplete();'));
  387. $this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  388. $this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
  389. $this->assertPattern('/^<a[^<>]+id="myLink"[^<>]*>/', $result);
  390. $this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
  391. $this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
  392. $this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
  393. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  394. $this->assertPattern('/Event.observe\(\'myLink\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  395. $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);
  396. $result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads', array('update' => 'myDiv', 'encoding' => 'utf-8'));
  397. $this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  398. $this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
  399. $this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
  400. $this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
  401. $this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
  402. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  403. $this->assertPattern('/Event.observe\(\'\w+\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  404. $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);
  405. $result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads', array('update' => 'myDiv', 'success' => 'success();'));
  406. $this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  407. $this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
  408. $this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
  409. $this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
  410. $this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
  411. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  412. $this->assertPattern('/Event.observe\(\'\w+\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  413. $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);
  414. $result = $this->Ajax->link('Ajax Link', 'http://www.cakephp.org/downloads', array('update' => 'myDiv', 'failure' => 'failure();'));
  415. $this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  416. $this->assertPattern('/^<a[^<>]+href="http:\/\/www.cakephp.org\/downloads"[^<>]*>/', $result);
  417. $this->assertPattern('/^<a[^<>]+onclick="\s*event.returnValue = false;\s*return false;"[^<>]*>/', $result);
  418. $this->assertPattern('/<script[^<>]+type="text\/javascript"[^<>]*>/', $result);
  419. $this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
  420. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  421. $this->assertPattern('/Event.observe\(\'\w+\',\s*\'click\',\s*function\(event\)\s*{.+},\s*false\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  422. $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);
  423. $result = $this->Ajax->link('Ajax Link', '/test', array('complete' => 'test'));
  424. $this->assertPattern('/^<a[^<>]+>Ajax Link<\/a><script [^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*[^<>]+\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  425. $this->assertPattern("/Event.observe\('link[0-9]+', [\w\d,'\(\)\s{}]+Ajax\.Request\([\w\d\s,'\(\){}:\/]+onComplete:function\(request, json\) {test}/", $result);
  426. $this->assertNoPattern('/^<a[^<>]+complete="test"[^<>]*>Ajax Link<\/a>/', $result);
  427. $this->assertNoPattern('/^<a\s+[^<>]*url="[^"]*"[^<>]*>/', $result);
  428. }
  429. /**
  430. * testRemoteTimer method
  431. *
  432. * @access public
  433. * @return void
  434. */
  435. function testRemoteTimer() {
  436. $result = $this->Ajax->remoteTimer(array('url' => 'http://www.cakephp.org'));
  437. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  438. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  439. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new PeriodicalExecuter\(function\(\) {.+}, 10\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  440. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true})')) . '/', $result);
  441. $result = $this->Ajax->remoteTimer(array('url' => 'http://www.cakephp.org', 'frequency' => 25));
  442. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  443. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  444. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new PeriodicalExecuter\(function\(\) {.+}, 25\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  445. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true})')) . '/', $result);
  446. $result = $this->Ajax->remoteTimer(array('url' => 'http://www.cakephp.org', 'complete' => 'complete();'));
  447. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  448. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  449. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new PeriodicalExecuter\(function\(\) {.+}, 10\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  450. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, onComplete:function(request, json) {complete();}})')) . '/', $result);
  451. $result = $this->Ajax->remoteTimer(array('url' => 'http://www.cakephp.org', 'complete' => 'complete();', 'create' => 'create();'));
  452. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  453. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  454. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new PeriodicalExecuter\(function\(\) {.+}, 10\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  455. $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);
  456. $result = $this->Ajax->remoteTimer(array('url' => 'http://www.cakephp.org', 'exception' => 'alert(exception);'));
  457. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  458. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  459. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new PeriodicalExecuter\(function\(\) {.+}, 10\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  460. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, onException:function(request, exception) {alert(exception);}})')) . '/', $result);
  461. $result = $this->Ajax->remoteTimer(array('url' => 'http://www.cakephp.org', 'contentType' => 'application/x-www-form-urlencoded'));
  462. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  463. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  464. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new PeriodicalExecuter\(function\(\) {.+}, 10\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  465. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, contentType:\'application/x-www-form-urlencoded\'})')) . '/', $result);
  466. $result = $this->Ajax->remoteTimer(array('url' => 'http://www.cakephp.org', 'method' => 'get', 'encoding' => 'utf-8'));
  467. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  468. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  469. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new PeriodicalExecuter\(function\(\) {.+}, 10\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  470. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, method:\'get\', encoding:\'utf-8\'})')) . '/', $result);
  471. $result = $this->Ajax->remoteTimer(array('url' => 'http://www.cakephp.org', 'postBody' => 'var1=value1'));
  472. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  473. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  474. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new PeriodicalExecuter\(function\(\) {.+}, 10\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  475. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, postBody:\'var1=value1\'})')) . '/', $result);
  476. }
  477. /**
  478. * testObserveField method
  479. *
  480. * @access public
  481. * @return void
  482. */
  483. function testObserveField() {
  484. $result = $this->Ajax->observeField('field', array('url' => 'http://www.cakephp.org'));
  485. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  486. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  487. $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);
  488. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, parameters:Form.Element.serialize(\'field\')})')) . '/', $result);
  489. $result = $this->Ajax->observeField('field', array('url' => 'http://www.cakephp.org', 'frequency' => 15));
  490. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  491. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  492. $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);
  493. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, parameters:Form.Element.serialize(\'field\')})')) . '/', $result);
  494. $result = $this->Ajax->observeField('field', array('url' => 'http://www.cakephp.org', 'update' => 'divId'));
  495. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  496. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  497. $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);
  498. $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);
  499. $result = $this->Ajax->observeField('field', array('url' => 'http://www.cakephp.org', 'update' => 'divId', 'with' => 'Form.Element.serialize(\'otherField\')'));
  500. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  501. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  502. $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);
  503. $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);
  504. }
  505. /**
  506. * testObserveForm method
  507. *
  508. * @access public
  509. * @return void
  510. */
  511. function testObserveForm() {
  512. $result = $this->Ajax->observeForm('form', array('url' => 'http://www.cakephp.org'));
  513. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  514. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  515. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new Form.EventObserver\(\'form\', function\(element, value\) {.+}\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  516. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, parameters:Form.serialize(\'form\')})')) . '/', $result);
  517. $result = $this->Ajax->observeForm('form', array('url' => 'http://www.cakephp.org', 'frequency' => 15));
  518. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  519. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  520. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new Form.Observer\(\'form\', 15, function\(element, value\) {.+}\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  521. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Request(\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, parameters:Form.serialize(\'form\')})')) . '/', $result);
  522. $result = $this->Ajax->observeForm('form', array('url' => 'http://www.cakephp.org', 'update' => 'divId'));
  523. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  524. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  525. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new Form.EventObserver\(\'form\', function\(element, value\) {.+}\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  526. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Updater(\'divId\',\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, parameters:Form.serialize(\'form\'), requestHeaders:[\'X-Update\', \'divId\']})')) . '/', $result);
  527. $result = $this->Ajax->observeForm('form', array('url' => 'http://www.cakephp.org', 'update' => 'divId', 'with' => 'Form.serialize(\'otherForm\')'));
  528. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  529. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  530. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*new Form.EventObserver\(\'form\', function\(element, value\) {.+}\)\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  531. $this->assertPattern('/' . str_replace('/', '\\/', preg_quote('new Ajax.Updater(\'divId\',\'http://www.cakephp.org\', {asynchronous:true, evalScripts:true, parameters:Form.serialize(\'otherForm\'), requestHeaders:[\'X-Update\', \'divId\']})')) . '/', $result);
  532. }
  533. /**
  534. * testSlider method
  535. *
  536. * @access public
  537. * @return void
  538. */
  539. function testSlider() {
  540. $result = $this->Ajax->slider('sliderId', 'trackId');
  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*var sliderId = new Control.Slider\(\'sliderId\', \'trackId\', {}\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  544. $result = $this->Ajax->slider('sliderId', 'trackId', array('axis' => 'vertical'));
  545. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  546. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  547. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*var sliderId = new Control.Slider\(\'sliderId\', \'trackId\', {axis:\'vertical\'}\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  548. $result = $this->Ajax->slider('sliderId', 'trackId', array('axis' => 'vertical', 'minimum' => 60, 'maximum' => 288, 'alignX' => -28, 'alignY' => -5, 'disabled' => true));
  549. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  550. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  551. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*var sliderId = new Control.Slider\(\'sliderId\', \'trackId\', {axis:\'vertical\', minimum:60, maximum:288, alignX:-28, alignY:-5, disabled:true}\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  552. $result = $this->Ajax->slider('sliderId', 'trackId', array('change' => 'alert(\'changed\');'));
  553. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  554. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  555. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*var sliderId = new Control.Slider\(\'sliderId\', \'trackId\', {onChange:function\(value\) {alert\(\'changed\'\);}}\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  556. $result = $this->Ajax->slider('sliderId', 'trackId', array('change' => 'alert(\'changed\');', 'slide' => 'alert(\'sliding\');'));
  557. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  558. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  559. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*var sliderId = new Control.Slider\(\'sliderId\', \'trackId\', {onChange:function\(value\) {alert\(\'changed\'\);}, onSlide:function\(value\) {alert\(\'sliding\'\);}}\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  560. $result = $this->Ajax->slider('sliderId', 'trackId', array('values' => array(10, 20, 30)));
  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*var sliderId = new Control.Slider\(\'sliderId\', \'trackId\', {values:\[10,20,30\]}\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  564. $result = $this->Ajax->slider('sliderId', 'trackId', array('range' => '$R(10, 30)'));
  565. $this->assertPattern('/^<script[^<>]+type="text\/javascript"[^<>]*>.+<\/script>$/s', $result);
  566. $this->assertNoPattern('/<script[^<>]+[^type]=[^<>]*>/', $result);
  567. $this->assertPattern('/^<script[^<>]+>\s*' . str_replace('/', '\\/', preg_quote('//<![CDATA[')) . '\s*var sliderId = new Control.Slider\(\'sliderId\', \'trackId\', {range:\$R\(10, 30\)}\);\s*' . str_replace('/', '\\/', preg_quote('//]]>')) . '\s*<\/script>$/', $result);
  568. }
  569. /**
  570. * testRemoteFunction method
  571. *
  572. * @access public
  573. * @return void
  574. */
  575. function testRemoteFunction() {
  576. $result = $this->Ajax->remoteFunction(array('complete' => 'testComplete();'));
  577. $expected = 'new Ajax.Request(\'/\', {asynchronous:true, evalScripts:true, onComplete:function(request, json) {testComplete();}})';
  578. $this->assertEqual($result, $expected);
  579. $result = $this->Ajax->remoteFunction(array('update' => 'myDiv'));
  580. $expected = 'new Ajax.Updater(\'myDiv\',\'/\', {asynchronous:true, evalScripts:true, requestHeaders:[\'X-Update\', \'myDiv\']})';
  581. $this->assertEqual($result, $expected);
  582. $result = $this->Ajax->remoteFunction(array('update' => array('div1', 'div2')));
  583. $expected = 'new Ajax.Updater(document.createElement(\'div\'),\'/\', {asynchronous:true, evalScripts:true, requestHeaders:[\'X-Update\', \'div1 div2\']})';
  584. $this->assertEqual($result, $expected);
  585. $result = $this->Ajax->remoteFunction(array('update' => 'myDiv', 'confirm' => 'Are you sure?'));
  586. $expected = 'if (confirm(\'Are you sure?\')) { new Ajax.Updater(\'myDiv\',\'/\', {asynchronous:true, evalScripts:true, requestHeaders:[\'X-Update\', \'myDiv\']}); } else { event.returnValue = false; return false; }';
  587. }
  588. /**
  589. * testDiv method
  590. *
  591. * @access public
  592. * @return void
  593. */
  594. function testDiv() {
  595. $oldXUpdate = env('HTTP_X_UPDATE');
  596. $result = $this->Ajax->div('myDiv');
  597. $this->assertTags($result, array('div' => array('id' => 'myDiv')));
  598. $_SERVER['HTTP_X_UPDATE'] = null;
  599. $result = $this->Ajax->divEnd('myDiv');
  600. $this->assertTags($result, '/div');
  601. $_SERVER['HTTP_X_UPDATE'] = 'secondDiv';
  602. $result = $this->Ajax->div('myDiv');
  603. $this->assertTags($result, array('div' => array('id' => 'myDiv')));
  604. $result = $this->Ajax->divEnd('myDiv');
  605. $this->assertTags($result, '/div');
  606. $_SERVER['HTTP_X_UPDATE'] = 'secondDiv myDiv anotherDiv';
  607. $result = $this->Ajax->div('myDiv');
  608. $this->assertTrue(empty($result));
  609. $result = $this->Ajax->divEnd('myDiv');
  610. $this->assertTrue(empty($result));
  611. $_SERVER['HTTP_X_UPDATE'] = $oldXUpdate;
  612. }
  613. /**
  614. * testAfterRender method
  615. *
  616. * @access public
  617. * @return void
  618. */
  619. function testAfterRender() {
  620. $oldXUpdate = env('HTTP_X_UPDATE');
  621. $this->Ajax->Javascript =& new TestJavascriptHelper();
  622. $_SERVER['HTTP_X_UPDATE'] = 'secondDiv myDiv anotherDiv';
  623. $result = $this->Ajax->div('myDiv');
  624. $this->assertTrue(empty($result));
  625. echo 'Contents of myDiv';
  626. $result = $this->Ajax->divEnd('myDiv');
  627. $this->assertTrue(empty($result));
  628. ob_start();
  629. $this->Ajax->afterRender();
  630. $result = array_shift($this->Ajax->Javascript->codeBlocks);
  631. $this->ass

Large files files are truncated, but you can click here to view the full file