AutoCompleteTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 Zend
  16. * @package Zend_Controller
  17. * @subpackage UnitTests
  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. // Call Zend_Controller_Action_Helper_AutoCompleteTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  25. define("PHPUnit_MAIN_METHOD", "Zend_Controller_Action_Helper_AutoCompleteTest::main");
  26. }
  27. require_once 'Zend/Controller/Action/Helper/AutoCompleteDojo.php';
  28. require_once 'Zend/Controller/Action/Helper/AutoCompleteScriptaculous.php';
  29. require_once 'Zend/Controller/Action.php';
  30. require_once 'Zend/Controller/Action/HelperBroker.php';
  31. require_once 'Zend/Controller/Front.php';
  32. require_once 'Zend/Controller/Request/Http.php';
  33. require_once 'Zend/Controller/Response/Cli.php';
  34. require_once 'Zend/Layout.php';
  35. /**
  36. * Test class for Zend_Controller_Action_Helper_AutoComplete.
  37. *
  38. * @category Zend
  39. * @package Zend_Controller
  40. * @subpackage UnitTests
  41. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  42. * @license http://framework.zend.com/license/new-bsd New BSD License
  43. * @group Zend_Controller
  44. * @group Zend_Controller_Action
  45. * @group Zend_Controller_Action_Helper
  46. */
  47. class Zend_Controller_Action_Helper_AutoCompleteTest extends PHPUnit_Framework_TestCase
  48. {
  49. /**
  50. * Runs the test methods of this class.
  51. *
  52. * @access public
  53. * @static
  54. */
  55. public static function main()
  56. {
  57. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Action_Helper_AutoCompleteTest");
  58. $result = PHPUnit_TextUI_TestRunner::run($suite);
  59. }
  60. /**
  61. * Sets up the fixture, for example, open a network connection.
  62. * This method is called before a test is executed.
  63. *
  64. * @return void
  65. */
  66. public function setUp()
  67. {
  68. Zend_Controller_Action_Helper_AutoCompleteTest_LayoutOverride::resetMvcInstance();
  69. Zend_Controller_Action_HelperBroker::resetHelpers();
  70. Zend_Controller_Action_HelperBroker::setPluginLoader(null);
  71. $this->request = new Zend_Controller_Request_Http();
  72. $this->response = new Zend_Controller_Response_Cli();
  73. $this->front = Zend_Controller_Front::getInstance();
  74. $this->front->resetInstance();
  75. $this->front->setRequest($this->request)->setResponse($this->response);
  76. $this->viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  77. $this->layout = Zend_Layout::startMvc();
  78. }
  79. /**
  80. * Tears down the fixture, for example, close a network connection.
  81. * This method is called after a test is executed.
  82. *
  83. * @return void
  84. */
  85. public function tearDown()
  86. {
  87. }
  88. public function testConcreteImplementationsDeriveFromAutoCompleteBaseClass()
  89. {
  90. $dojo = new Zend_Controller_Action_Helper_AutoCompleteDojo();
  91. $this->assertTrue($dojo instanceof Zend_Controller_Action_Helper_AutoComplete_Abstract);
  92. $scriptaculous = new Zend_Controller_Action_Helper_AutoCompleteScriptaculous();
  93. $this->assertTrue($scriptaculous instanceof Zend_Controller_Action_Helper_AutoComplete_Abstract);
  94. }
  95. public function testEncodeJsonProxiesToJsonActionHelper()
  96. {
  97. $dojo = new Zend_Controller_Action_Helper_AutoCompleteDojo();
  98. $data = array('foo', 'bar', 'baz');
  99. $encoded = $dojo->prepareAutoCompletion($data);
  100. $decoded = Zend_Json::decode($encoded);
  101. $test = array();
  102. foreach ($decoded['items'] as $item) {
  103. $test[] = $item['name'];
  104. }
  105. $this->assertSame($data, $test);
  106. $this->assertFalse($this->layout->isEnabled());
  107. $headers = $this->response->getHeaders();
  108. $found = false;
  109. foreach ($headers as $header) {
  110. if ('Content-Type' == $header['name']) {
  111. if ('application/json' == $header['value']) {
  112. $found = true;
  113. }
  114. break;
  115. }
  116. }
  117. $this->assertTrue($found, "JSON content-type header not found");
  118. }
  119. public function testDojoHelperEncodesToJson()
  120. {
  121. $dojo = new Zend_Controller_Action_Helper_AutoCompleteDojo();
  122. $data = array('foo', 'bar', 'baz');
  123. $encoded = $dojo->direct($data, false);
  124. $decoded = Zend_Json::decode($encoded);
  125. $this->assertContains('items', array_keys($decoded));
  126. $this->assertContains('identifier', array_keys($decoded));
  127. $this->assertEquals('name', $decoded['identifier']);
  128. $test = array();
  129. foreach ($decoded['items'] as $item) {
  130. $test[] = $item['label'];
  131. }
  132. $this->assertEquals($data, $test);
  133. }
  134. public function testDojoHelperSendsResponseByDefault()
  135. {
  136. $dojo = new Zend_Controller_Action_Helper_AutoCompleteDojo();
  137. $dojo->suppressExit = true;
  138. $data = array('foo', 'bar', 'baz');
  139. $encoded = $dojo->direct($data);
  140. $decoded = Zend_Json::decode($encoded);
  141. $test = array();
  142. foreach ($decoded['items'] as $item) {
  143. $test[] = $item['name'];
  144. }
  145. $this->assertSame($data, $test);
  146. $body = $this->response->getBody();
  147. $this->assertSame($encoded, $body);
  148. }
  149. public function testDojoHelperDisablesLayoutsAndViewRendererByDefault()
  150. {
  151. $dojo = new Zend_Controller_Action_Helper_AutoCompleteDojo();
  152. $dojo->suppressExit = true;
  153. $data = array('foo', 'bar', 'baz');
  154. $encoded = $dojo->direct($data);
  155. $this->assertFalse($this->layout->isEnabled());
  156. $this->assertTrue($this->viewRenderer->getNoRender());
  157. }
  158. public function testDojoHelperCanEnableLayoutsAndViewRenderer()
  159. {
  160. $dojo = new Zend_Controller_Action_Helper_AutoCompleteDojo();
  161. $dojo->suppressExit = true;
  162. $data = array('foo', 'bar', 'baz');
  163. $encoded = $dojo->direct($data, false, true);
  164. $this->assertTrue($this->layout->isEnabled());
  165. $this->assertFalse($this->viewRenderer->getNoRender());
  166. }
  167. /**
  168. * @group ZF-9126
  169. */
  170. public function testDojoHelperEncodesUnicodeChars()
  171. {
  172. $dojo = new Zend_Controller_Action_Helper_AutoCompleteDojo();
  173. $dojo->suppressExit = true;
  174. $data = array ('garçon', 'schließen', 'Helgi Þormar Þorbjörnsson');
  175. $encoded = $dojo->direct($data);
  176. $body = $this->response->getBody();
  177. $decoded = Zend_Json::decode($encoded);
  178. $test = array ();
  179. foreach ($decoded['items'] as $item) {
  180. $test[] = $item['name'];
  181. }
  182. $this->assertSame($data, $test);
  183. $this->assertSame($encoded, $body);
  184. }
  185. public function testScriptaculousHelperThrowsExceptionOnInvalidDataFormat()
  186. {
  187. $scriptaculous = new Zend_Controller_Action_Helper_AutoCompleteScriptaculous();
  188. $data = new stdClass;
  189. $data->foo = 'bar';
  190. $data->bar = 'baz';
  191. try {
  192. $encoded = $scriptaculous->encodeJson($data);
  193. $this->fail('Objects should be considered invalid');
  194. } catch (Zend_Controller_Action_Exception $e) {
  195. $this->assertContains('Invalid data', $e->getMessage());
  196. }
  197. }
  198. public function testScriptaculousHelperCreatesHtmlMarkup()
  199. {
  200. $scriptaculous = new Zend_Controller_Action_Helper_AutoCompleteScriptaculous();
  201. $scriptaculous->suppressExit = true;
  202. $data = array('foo', 'bar', 'baz');
  203. $formatted = $scriptaculous->direct($data);
  204. $this->assertContains('<ul>', $formatted);
  205. foreach ($data as $value) {
  206. $this->assertContains('<li>' . $value . '</li>', $formatted);
  207. }
  208. $this->assertContains('</ul>', $formatted);
  209. }
  210. public function testScriptaculousHelperSendsResponseByDefault()
  211. {
  212. $scriptaculous = new Zend_Controller_Action_Helper_AutoCompleteScriptaculous();
  213. $scriptaculous->suppressExit = true;
  214. $data = array('foo', 'bar', 'baz');
  215. $encoded = $scriptaculous->direct($data);
  216. $body = $this->response->getBody();
  217. $this->assertSame($encoded, $body);
  218. }
  219. public function testScriptaculousHelperDisablesLayoutsAndViewRendererByDefault()
  220. {
  221. $scriptaculous = new Zend_Controller_Action_Helper_AutoCompleteScriptaculous();
  222. $scriptaculous->suppressExit = true;
  223. $data = array('foo', 'bar', 'baz');
  224. $encoded = $scriptaculous->direct($data);
  225. $this->assertFalse($this->layout->isEnabled());
  226. $this->assertTrue($this->viewRenderer->getNoRender());
  227. }
  228. public function testScriptaculousHelperCanEnableLayoutsAndViewRenderer()
  229. {
  230. $scriptaculous = new Zend_Controller_Action_Helper_AutoCompleteScriptaculous();
  231. $scriptaculous->suppressExit = true;
  232. $data = array('foo', 'bar', 'baz');
  233. $encoded = $scriptaculous->direct($data, false, true);
  234. $this->assertTrue($this->layout->isEnabled());
  235. $this->assertFalse($this->viewRenderer->getNoRender());
  236. }
  237. }
  238. class Zend_Controller_Action_Helper_AutoCompleteTest_LayoutOverride extends Zend_Layout
  239. {
  240. public static function resetMvcInstance()
  241. {
  242. self::$_mvcInstance = null;
  243. }
  244. }
  245. // Call Zend_Controller_Action_Helper_AutoCompleteTest::main() if this source file is executed directly.
  246. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Action_Helper_AutoCompleteTest::main") {
  247. Zend_Controller_Action_Helper_AutoCompleteTest::main();
  248. }