AjaxContextTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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_AjaxContextTest::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_AjaxContextTest::main");
  26. }
  27. require_once 'Zend/Controller/Action/Helper/AjaxContext.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. require_once 'Zend/View.php';
  35. /**
  36. * Test class for Zend_Controller_Action_Helper_AjaxContext.
  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_AjaxContextTest 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_AjaxContextTest");
  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. if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
  69. unset($_SERVER['HTTP_X_REQUESTED_WITH']);
  70. }
  71. Zend_Controller_Action_Helper_AjaxContextTest_LayoutOverride::resetMvcInstance();
  72. Zend_Controller_Action_HelperBroker::resetHelpers();
  73. $this->front = Zend_Controller_Front::getInstance();
  74. $this->front->resetInstance();
  75. $this->front->addModuleDirectory(dirname(__FILE__) . '/../../_files/modules');
  76. $this->layout = Zend_Layout::startMvc();
  77. $this->helper = new Zend_Controller_Action_Helper_AjaxContext();
  78. $this->request = new Zend_Controller_Request_Http();
  79. $this->response = new Zend_Controller_Response_Cli();
  80. $this->front->setRequest($this->request)->setResponse($this->response);
  81. $this->view = new Zend_VIew();
  82. $this->view->addHelperPath(dirname(__FILE__) . '/../../../../../library/Zend/View/Helper/');
  83. $this->viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  84. $this->viewRenderer->setView($this->view);
  85. $this->controller = new Zend_Controller_Action_Helper_AjaxContextTestController(
  86. $this->request,
  87. $this->response,
  88. array()
  89. );
  90. $this->helper->setActionController($this->controller);
  91. }
  92. /**
  93. * Tears down the fixture, for example, close a network connection.
  94. * This method is called after a test is executed.
  95. *
  96. * @return void
  97. */
  98. public function tearDown()
  99. {
  100. if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
  101. unset($_SERVER['HTTP_X_REQUESTED_WITH']);
  102. }
  103. }
  104. public function testDefaultContextsIncludesHtml()
  105. {
  106. $contexts = $this->helper->getContexts();
  107. $this->assertTrue(isset($contexts['html']));
  108. $this->assertEquals('ajax.phtml', $this->helper->getSuffix('html'));
  109. $header = $this->helper->getHeaders('html');
  110. $this->assertTrue(empty($header));
  111. }
  112. public function checkNothingIsDone()
  113. {
  114. $this->assertEquals('phtml', $this->viewRenderer->getViewSuffix());
  115. $headers = $this->response->getHeaders();
  116. $this->assertTrue(empty($headers));
  117. }
  118. public function testInitContextFailsOnNonXhrRequests()
  119. {
  120. $this->request->setParam('format', 'xml')
  121. ->setActionName('foo');
  122. $this->helper->initContext();
  123. $this->checkNothingIsDone();
  124. }
  125. public function testInitContextFailsWithNoAjaxableActions()
  126. {
  127. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  128. $this->assertTrue($this->request->isXmlHttpRequest());
  129. $this->controller->contexts = $this->controller->ajaxable;
  130. unset($this->controller->ajaxable);
  131. $this->request->setParam('format', 'xml')
  132. ->setActionName('foo');
  133. $this->helper->initContext();
  134. $this->checkNothingIsDone();
  135. }
  136. public function testInitContextSwitchesContextWithXhrRequests()
  137. {
  138. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  139. $this->assertTrue($this->request->isXmlHttpRequest());
  140. $this->request->setParam('format', 'xml')
  141. ->setActionName('foo');
  142. $this->helper->initContext();
  143. $this->assertEquals('xml.phtml', $this->viewRenderer->getViewSuffix());
  144. $headers = $this->response->getHeaders();
  145. $found = false;
  146. foreach ($headers as $header) {
  147. if ('Content-Type' == $header['name']) {
  148. $found = true;
  149. $value = $header['value'];
  150. }
  151. }
  152. $this->assertTrue($found);
  153. $this->assertEquals('application/xml', $value);
  154. $this->assertFalse($this->layout->isEnabled());
  155. }
  156. public function testGetCurrentContextResetToNullWhenSubsequentInitContextFailsXhrTest()
  157. {
  158. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  159. $this->assertTrue($this->request->isXmlHttpRequest());
  160. $this->assertNull($this->helper->getCurrentContext());
  161. $this->request->setParam('format', 'xml')
  162. ->setActionName('foo');
  163. $this->helper->initContext();
  164. $this->assertEquals('xml', $this->helper->getCurrentContext());
  165. unset($_SERVER['HTTP_X_REQUESTED_WITH']);
  166. $this->request->setParam('format', 'foo')
  167. ->setActionName('bogus');
  168. $this->helper->initContext();
  169. $this->assertNull($this->helper->getCurrentContext());
  170. }
  171. }
  172. class Zend_Controller_Action_Helper_AjaxContextTestController extends Zend_Controller_Action
  173. {
  174. public $ajaxable = array(
  175. 'foo' => array('xml'),
  176. 'bar' => array('xml', 'json'),
  177. 'baz' => array(),
  178. );
  179. }
  180. class Zend_Controller_Action_Helper_AjaxContextTest_LayoutOverride extends Zend_Layout
  181. {
  182. public static function resetMvcInstance()
  183. {
  184. self::$_mvcInstance = null;
  185. }
  186. }
  187. // Call Zend_Controller_Action_Helper_AjaxContextTest::main() if this source file is executed directly.
  188. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Action_Helper_AjaxContextTest::main") {
  189. Zend_Controller_Action_Helper_AjaxContextTest::main();
  190. }