AutoCompleteTest.php 9.9 KB

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