AjaxContextTest.php 7.4 KB

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