PageRenderTime 23ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/ZendFramework/tests/Zend/Controller/Action/Helper/AjaxContextTest.php

https://bitbucket.org/Dal-Papa/is-340-publish-base
PHP | 265 lines | 170 code | 38 blank | 57 comment | 12 complexity | eb541fca0e9635df01f9519955e64095 MD5 | raw file
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: AjaxContextTest.php 24593 2012-01-05 20:35:02Z matthew $
  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/Request/Simple.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-2012 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. * @author Fix sponsored by Enrise - www.enrise.com
  173. *
  174. * @group ZF-8444
  175. */
  176. public function testAjaxContextIsRequestDependent()
  177. {
  178. $request = new ZendTest_Controller_Request_SimpleMock_AjaxTest();
  179. $helper = new Zend_Controller_Action_Helper_AjaxContext();
  180. $helper->setActionController(
  181. new Zend_Controller_Action_Helper_AjaxContextTestController(
  182. $request,
  183. $this->response,
  184. array()
  185. )
  186. );
  187. try {
  188. $helper->initContext();
  189. $this->assertTrue(true);
  190. } catch(Exception $e) {
  191. if($e->getMessage() == 'test testAjaxContextIsRequestDependent failed' ) {
  192. $this->fail();
  193. } else {
  194. throw $e;
  195. }
  196. }
  197. }
  198. }
  199. class ZendTest_Controller_Request_SimpleMock_AjaxTest
  200. extends Zend_Controller_Request_Simple
  201. {
  202. public function __call($method, $args) {
  203. if($method == 'isXmlHttpRequest') {
  204. throw new exception('test testAjaxContextIsRequestDependent failed');
  205. }
  206. return parent::__call($method, $args);
  207. }
  208. }
  209. class Zend_Controller_Action_Helper_AjaxContextTestController extends Zend_Controller_Action
  210. {
  211. public $ajaxable = array(
  212. 'foo' => array('xml'),
  213. 'bar' => array('xml', 'json'),
  214. 'baz' => array(),
  215. );
  216. }
  217. class Zend_Controller_Action_Helper_AjaxContextTest_LayoutOverride extends Zend_Layout
  218. {
  219. public static function resetMvcInstance()
  220. {
  221. self::$_mvcInstance = null;
  222. }
  223. }
  224. // Call Zend_Controller_Action_Helper_AjaxContextTest::main() if this source file is executed directly.
  225. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Action_Helper_AjaxContextTest::main") {
  226. Zend_Controller_Action_Helper_AjaxContextTest::main();
  227. }