AjaxContextTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. // Call Zend_Controller_Action_Helper_AjaxContextTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. require_once dirname(__FILE__) . '/../../../../TestHelper.php';
  5. define("PHPUnit_MAIN_METHOD", "Zend_Controller_Action_Helper_AjaxContextTest::main");
  6. }
  7. require_once "PHPUnit/Framework/TestCase.php";
  8. require_once "PHPUnit/Framework/TestSuite.php";
  9. require_once 'Zend/Controller/Action/Helper/AjaxContext.php';
  10. require_once 'Zend/Controller/Action.php';
  11. require_once 'Zend/Controller/Action/HelperBroker.php';
  12. require_once 'Zend/Controller/Front.php';
  13. require_once 'Zend/Controller/Request/Http.php';
  14. require_once 'Zend/Controller/Response/Cli.php';
  15. require_once 'Zend/Layout.php';
  16. require_once 'Zend/View.php';
  17. /**
  18. * Test class for Zend_Controller_Action_Helper_AjaxContext.
  19. */
  20. class Zend_Controller_Action_Helper_AjaxContextTest extends PHPUnit_Framework_TestCase
  21. {
  22. /**
  23. * Runs the test methods of this class.
  24. *
  25. * @access public
  26. * @static
  27. */
  28. public static function main()
  29. {
  30. require_once "PHPUnit/TextUI/TestRunner.php";
  31. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Action_Helper_AjaxContextTest");
  32. $result = PHPUnit_TextUI_TestRunner::run($suite);
  33. }
  34. /**
  35. * Sets up the fixture, for example, open a network connection.
  36. * This method is called before a test is executed.
  37. *
  38. * @return void
  39. */
  40. public function setUp()
  41. {
  42. if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
  43. unset($_SERVER['HTTP_X_REQUESTED_WITH']);
  44. }
  45. Zend_Controller_Action_Helper_AjaxContextTest_LayoutOverride::$_mvcInstance = null;
  46. Zend_Controller_Action_HelperBroker::resetHelpers();
  47. $this->front = Zend_Controller_Front::getInstance();
  48. $this->front->resetInstance();
  49. $this->front->addModuleDirectory(dirname(__FILE__) . '/../../_files/modules');
  50. $this->layout = Zend_Layout::startMvc();
  51. $this->helper = new Zend_Controller_Action_Helper_AjaxContext();
  52. $this->request = new Zend_Controller_Request_Http();
  53. $this->response = new Zend_Controller_Response_Cli();
  54. $this->front->setRequest($this->request)->setResponse($this->response);
  55. $this->view = new Zend_VIew();
  56. $this->view->addHelperPath(dirname(__FILE__) . '/../../../../../library/Zend/View/Helper/');
  57. $this->viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  58. $this->viewRenderer->setView($this->view);
  59. $this->controller = new Zend_Controller_Action_Helper_AjaxContextTestController(
  60. $this->request,
  61. $this->response,
  62. array()
  63. );
  64. $this->helper->setActionController($this->controller);
  65. }
  66. /**
  67. * Tears down the fixture, for example, close a network connection.
  68. * This method is called after a test is executed.
  69. *
  70. * @return void
  71. */
  72. public function tearDown()
  73. {
  74. if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
  75. unset($_SERVER['HTTP_X_REQUESTED_WITH']);
  76. }
  77. }
  78. public function testDefaultContextsIncludesHtml()
  79. {
  80. $contexts = $this->helper->getContexts();
  81. $this->assertTrue(isset($contexts['html']));
  82. $this->assertEquals('ajax.phtml', $this->helper->getSuffix('html'));
  83. $header = $this->helper->getHeaders('html');
  84. $this->assertTrue(empty($header));
  85. }
  86. public function checkNothingIsDone()
  87. {
  88. $this->assertEquals('phtml', $this->viewRenderer->getViewSuffix());
  89. $headers = $this->response->getHeaders();
  90. $this->assertTrue(empty($headers));
  91. }
  92. public function testInitContextFailsOnNonXhrRequests()
  93. {
  94. $this->request->setParam('format', 'xml')
  95. ->setActionName('foo');
  96. $this->helper->initContext();
  97. $this->checkNothingIsDone();
  98. }
  99. public function testInitContextFailsWithNoAjaxableActions()
  100. {
  101. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  102. $this->assertTrue($this->request->isXmlHttpRequest());
  103. $this->controller->contexts = $this->controller->ajaxable;
  104. unset($this->controller->ajaxable);
  105. $this->request->setParam('format', 'xml')
  106. ->setActionName('foo');
  107. $this->helper->initContext();
  108. $this->checkNothingIsDone();
  109. }
  110. public function testInitContextSwitchesContextWithXhrRequests()
  111. {
  112. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  113. $this->assertTrue($this->request->isXmlHttpRequest());
  114. $this->request->setParam('format', 'xml')
  115. ->setActionName('foo');
  116. $this->helper->initContext();
  117. $this->assertEquals('xml.phtml', $this->viewRenderer->getViewSuffix());
  118. $headers = $this->response->getHeaders();
  119. $found = false;
  120. foreach ($headers as $header) {
  121. if ('Content-Type' == $header['name']) {
  122. $found = true;
  123. $value = $header['value'];
  124. }
  125. }
  126. $this->assertTrue($found);
  127. $this->assertEquals('application/xml', $value);
  128. $this->assertFalse($this->layout->isEnabled());
  129. }
  130. public function testGetCurrentContextResetToNullWhenSubsequentInitContextFailsXhrTest()
  131. {
  132. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
  133. $this->assertTrue($this->request->isXmlHttpRequest());
  134. $this->assertNull($this->helper->getCurrentContext());
  135. $this->request->setParam('format', 'xml')
  136. ->setActionName('foo');
  137. $this->helper->initContext();
  138. $this->assertEquals('xml', $this->helper->getCurrentContext());
  139. unset($_SERVER['HTTP_X_REQUESTED_WITH']);
  140. $this->request->setParam('format', 'foo')
  141. ->setActionName('bogus');
  142. $this->helper->initContext();
  143. $this->assertNull($this->helper->getCurrentContext());
  144. }
  145. }
  146. class Zend_Controller_Action_Helper_AjaxContextTestController extends Zend_Controller_Action
  147. {
  148. public $ajaxable = array(
  149. 'foo' => array('xml'),
  150. 'bar' => array('xml', 'json'),
  151. 'baz' => array(),
  152. );
  153. }
  154. class Zend_Controller_Action_Helper_AjaxContextTest_LayoutOverride extends Zend_Layout
  155. {
  156. public static $_mvcInstance;
  157. }
  158. // Call Zend_Controller_Action_Helper_AjaxContextTest::main() if this source file is executed directly.
  159. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Action_Helper_AjaxContextTest::main") {
  160. Zend_Controller_Action_Helper_AjaxContextTest::main();
  161. }