AjaxContextTest.php 7.6 KB

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