2
0

AjaxLinkTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category ZendX
  16. * @package ZendX_JQuery
  17. * @subpackage View
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. require_once "jQueryTestCase.php";
  23. require_once "ZendX/JQuery/View/Helper/AjaxLink.php";
  24. class ZendX_JQuery_View_AjaxLinkTest extends ZendX_JQuery_View_jQueryTestCase
  25. {
  26. public function testShouldBeCallable() {
  27. $link = $this->view->ajaxLink("Link to Inject", "inject.html");
  28. $this->assertContains('Link to Inject', $link);
  29. $this->assertContains('class="ajaxLink', $link);
  30. $this->assertNotContains('inject.html', $link);
  31. $this->assertNotContains('$.get', $link);
  32. $render = $this->jquery->__toString();
  33. $this->assertContains('inject.html', $render);
  34. $this->assertContains('$.get', $render);
  35. }
  36. public function testShouldBeCallableInline() {
  37. $link = $this->view->ajaxLink("Link to Inject", "inject.html", array('inline' => true));
  38. $this->assertContains('Link to Inject', $link);
  39. $this->assertNotContains('class="ajaxLink', $link);
  40. $this->assertContains('inject.html', $link);
  41. $this->assertContains('$.get', $link);
  42. $render = $this->jquery->__toString();
  43. $this->assertNotContains('inject.html', $render);
  44. $this->assertNotContains('$.get', $render);
  45. }
  46. public function testShouldAllowSendingParamsWithPost() {
  47. $link = $this->view->ajaxLink("Link to Inject2", "inject.php", array('update' => '#test', 'class' => 'someClass'), array('key' => 'value'));
  48. $this->assertContains('Link to Inject2', $link);
  49. $this->assertContains('class="someClass ajaxLink', $link);
  50. $this->assertNotContains('inject.php', $link);
  51. $render = $this->jquery->__toString();
  52. $this->assertContains('inject.php', $render);
  53. $this->assertContains('$.post', $render);
  54. $this->assertContains('#test', $render);
  55. $this->assertContains('{"key":"value"}', $render);
  56. }
  57. public function testShouldAllowSendingParamsWithGet() {
  58. $link = $this->view->ajaxLink("Link to Inject3", "inject123.php", array('method' => 'get', 'update' => '#test'), array('key' => 'value'));
  59. $this->assertContains('Link to Inject3', $link);
  60. $this->assertContains('class="ajaxLink', $link);
  61. $this->assertNotContains('inject123.php', $link);
  62. $render = $this->jquery->__toString();
  63. $this->assertContains('inject123.php', $render);
  64. $this->assertContains('$.get', $render);
  65. $this->assertContains('#test', $render);
  66. $this->assertContains('{"key":"value"}', $render);
  67. }
  68. public function testShouldAllowSpecifyingDataType() {
  69. $link = $this->view->ajaxLink("JSON Response with Callback", "inject.php", array('complete' => 'jsonCallback(data);', 'dataType' => 'json'), array('name' => 'Ludwig von Mises', 'email' => 'mises@vienna.at'));
  70. $this->assertContains('JSON Response with Callback', $link);
  71. $this->assertContains('class="ajaxLink', $link);
  72. $this->assertNotContains('inject.php', $link);
  73. $this->assertNotContains('{"name":"Ludwig von Mises","email":"mises@vienna.at"}', $link);
  74. $render = $this->jquery->__toString();
  75. $this->assertContains('inject.php', $render);
  76. $this->assertContains('function(data, textStatus) { jsonCallback(data); }', $render);
  77. $this->assertContains('"json");', $render);
  78. $this->assertContains('{"name":"Ludwig von Mises","email":"mises@vienna.at"}', $render);
  79. }
  80. public function testShouldWorkInNoConflictMode() {
  81. ZendX_JQuery_View_Helper_JQuery::enableNoConflictMode();
  82. $link = $this->view->ajaxLink("Link to Inject", "inject.html", array('update' => '#test', 'inline' => true, 'beforeSend' => 'hide'));
  83. $this->assertContains('$j.get', $link);
  84. $this->assertContains('$j("#test")', $link);
  85. $this->assertContains('$j(this).hide', $link);
  86. }
  87. public function testShouldAllowSwitchUpdateDataFunc() {
  88. $link = $this->view->ajaxLink("Link to Inject", "inject.html", array('update' => '#test', 'inline' => true, 'dataType' => 'text'));
  89. $this->assertContains('("#test").text(data);', $link);
  90. }
  91. static public function dataBeforeSendEffects()
  92. {
  93. return array(
  94. array('hide', 'hide();'),
  95. array('hideslow', 'hide("slow");'),
  96. array('hidefast', 'hide("fast");'),
  97. array('fadeout', 'fadeOut();'),
  98. array('fadeoutslow', 'fadeOut("slow");'),
  99. array('fadeoutfast', 'fadeOut("fast");'),
  100. array('slideup', 'slideUp(1000);'),
  101. );
  102. }
  103. /**
  104. * @dataProvider dataBeforeSendEffects
  105. * @param string $effect
  106. * @param string $js
  107. */
  108. public function testShouldAllowUsingBeforeSendEffects($effect, $js)
  109. {
  110. $link = $this->view->ajaxLink("Link to Inject", "inject.html", array('update' => '#test', 'inline' => true, 'beforeSend' => $effect));
  111. $this->assertContains(sprintf('$(this).%s', $js), $link);
  112. ZendX_JQuery_View_Helper_JQuery::enableNoConflictMode();
  113. $link = $this->view->ajaxLink("Link to Inject", "inject.html", array('update' => '#test', 'inline' => true, 'beforeSend' => $effect));
  114. $this->assertContains(sprintf('$j(this).%s', $js), $link);
  115. }
  116. static public function dataCompleteEffects()
  117. {
  118. return array(
  119. array('show', 'show();'),
  120. array('showslow', 'show("slow");'),
  121. array('showfast', 'show("fast");'),
  122. array('shownormal', 'show("normal");'),
  123. array('fadein', 'fadeIn("normal");'),
  124. array('fadeinslow', 'fadeIn("slow");'),
  125. array('fadeinfast', 'fadeIn("fast");'),
  126. array('slidedown', 'slideDown("normal");'),
  127. array('slidedownslow', 'slideDown("slow");'),
  128. array('slidedownfast', 'slideDown("fast");'),
  129. );
  130. }
  131. /**
  132. * @dataProvider dataCompleteEffects
  133. * @param <type> $effect
  134. * @param <type> $js
  135. */
  136. public function testShouldAllowUsingCompleteEffects($effect, $js)
  137. {
  138. $link = $this->view->ajaxLink("Link to Inject", "inject.html", array('update' => '#test', 'inline' => true, 'complete' => $effect));
  139. $this->assertContains(sprintf('$("#test").%s', $js), $link);
  140. ZendX_JQuery_View_Helper_JQuery::enableNoConflictMode();
  141. $link = $this->view->ajaxLink("Link to Inject", "inject.html", array('update' => '#test', 'inline' => true, 'complete' => $effect));
  142. $this->assertContains(sprintf('$j("#test").%s', $js), $link);
  143. }
  144. public function testOptionsArrayAllowsForSettingAttributes() {
  145. $view = $this->getView();
  146. $html = $view->ajaxLink("Label1", "/some/url", array(
  147. 'id' => 'ajaxLink1',
  148. 'title' => 'Label1',
  149. 'noscript' => true,
  150. 'attribs' => array('class' => 'test', 'target' => '_blank')
  151. ));
  152. $this->assertContains('id="ajaxLink1"', $html);
  153. $this->assertContains('title="Label1"', $html);
  154. $this->assertContains('href="/some/url"', $html);
  155. $this->assertNotContains('href="#"', $html);
  156. $this->assertContains('class="test"', $html);
  157. $this->assertContains('target="_blank"', $html);
  158. }
  159. public function testSpecifyingIdDoesNotCreateAutomaticCallbackAndClassAttribute() {
  160. $view = $this->getView();
  161. $html = $view->ajaxLink("Label1", "/some/url", array(
  162. 'id' => "someId"
  163. ));
  164. $this->assertNotContains('class=', $html);
  165. $this->assertContains('id="someId"', $html);
  166. }
  167. /**
  168. * @group ZF-5041
  169. */
  170. public function testXhtmlDoctypeDoesNotMakeAnchorInvalidHtml() {
  171. $view = $this->getView();
  172. $view->doctype('XHTML1_STRICT');
  173. $html = $view->ajaxLink("Label1", "/some/url", array('id' => "someId"));
  174. $this->assertNotContains("/>Label1</a>", $html);
  175. $this->assertContains(">Label1</a>", $html);
  176. }
  177. /** @group ZF-9926 */
  178. public function testDoNotUseSingleQuotesInJsAsItBreaksInlineLinks()
  179. {
  180. $view = $this->getView();
  181. $html = $view->ajaxLink('Label1', '/some/url', array(
  182. 'method' => 'post',
  183. 'dataType' => 'json',
  184. 'noscript' => true,
  185. 'beforeSend' => 'if(!confirm("Are you sure?")) {return false;}$("#progress-bar").show();',
  186. 'complete' => '$("#progress-bar").hide();',
  187. 'inline' => true
  188. ));
  189. $this->assertContains('$.post("/some/url"', $html);
  190. $this->assertNotContains("'/some/url'", $html);
  191. $this->assertNotContains("'json'", $html);
  192. }
  193. /** @group ZF-9926 */
  194. public function testSingleQuotesAreEscapedInJsInlineLinks()
  195. {
  196. $view = $this->getView();
  197. $html = $view->ajaxLink('Label1', '/some/url', array(
  198. 'method' => 'post',
  199. 'dataType' => 'json',
  200. 'noscript' => true,
  201. 'beforeSend' => "if(!confirm('Are you sure?')) {return false;}$('#progress-bar').show();",
  202. 'complete' => '$("#progress-bar").hide();',
  203. 'inline' => true
  204. ));
  205. $this->assertContains('&#39;Are you sure?&#39;', $html);
  206. $this->assertContains('&#39;#progress-bar&#39;', $html);
  207. $this->assertContains('"#progress-bar"', $html);
  208. }
  209. }