ActionTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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_View
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 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_View_Helper_ActionTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_View_Helper_ActionTest::main");
  25. }
  26. /** Zend_View_Helper_Action */
  27. require_once 'Zend/View/Helper/Action.php';
  28. /** Zend_Controller_Front */
  29. require_once 'Zend/Controller/Front.php';
  30. /** Zend_Controller_Request_Http */
  31. require_once 'Zend/Controller/Request/Http.php';
  32. /** Zend_Controller_Response_Http */
  33. require_once 'Zend/Controller/Response/Http.php';
  34. /** Zend_View */
  35. require_once 'Zend/View.php';
  36. /**
  37. * Test class for Zend_View_Helper_Action.
  38. *
  39. * @category Zend
  40. * @package Zend_View
  41. * @subpackage UnitTests
  42. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  43. * @license http://framework.zend.com/license/new-bsd New BSD License
  44. * @group Zend_View
  45. * @group Zend_View_Helper
  46. */
  47. class Zend_View_Helper_ActionTest extends PHPUnit_Framework_TestCase
  48. {
  49. /**
  50. * Runs the test methods of this class.
  51. *
  52. * @return void
  53. */
  54. public static function main()
  55. {
  56. $suite = new PHPUnit_Framework_TestSuite("Zend_View_Helper_ActionTest");
  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. $this->_origServer = $_SERVER;
  68. $_SERVER = array(
  69. 'SCRIPT_FILENAME' => __FILE__,
  70. 'PHP_SELF' => __FILE__,
  71. );
  72. $front = Zend_Controller_Front::getInstance();
  73. $front->resetInstance();
  74. $this->request = new Zend_Controller_Request_Http('http://framework.zend.com/action-foo');
  75. $this->response = new Zend_Controller_Response_Http();
  76. $this->response->headersSentThrowsException = false;
  77. $front->setRequest($this->request)
  78. ->setResponse($this->response)
  79. ->addModuleDirectory(dirname(__FILE__) . '/_files/modules');
  80. $this->view = new Zend_View();
  81. $this->helper = new Zend_View_Helper_Action();
  82. $this->helper->setView($this->view);
  83. }
  84. /**
  85. * Tears down the fixture, for example, close a network connection.
  86. * This method is called after a test is executed.
  87. *
  88. * @return void
  89. */
  90. public function tearDown()
  91. {
  92. unset($this->request, $this->response, $this->helper);
  93. $_SERVER = $this->_origServer;
  94. }
  95. /**
  96. * @return void
  97. */
  98. public function testInitialStateHasClonedObjects()
  99. {
  100. $this->assertNotSame($this->request, $this->helper->request);
  101. $this->assertNotSame($this->response, $this->helper->response);
  102. $dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();
  103. $this->assertNotSame($dispatcher, $this->helper->dispatcher);
  104. }
  105. /**
  106. * @return void
  107. */
  108. public function testInitialStateHasDefaultModuleName()
  109. {
  110. $dispatcher = Zend_Controller_Front::getInstance()->getDispatcher();
  111. $module = $dispatcher->getDefaultModule();
  112. $this->assertEquals($module, $this->helper->defaultModule);
  113. $dispatcher->setDefaultModule('foo');
  114. $helper = new Zend_View_Helper_Action();
  115. $this->assertEquals('foo', $helper->defaultModule);
  116. }
  117. /**
  118. * @return void
  119. */
  120. public function testResetObjectsClearsRequestVars()
  121. {
  122. $this->helper->request->setParam('foo', 'action-bar');
  123. $this->helper->resetObjects();
  124. $this->assertNull($this->helper->request->getParam('foo'));
  125. }
  126. /**
  127. * @return void
  128. */
  129. public function testResetObjectsClearsResponseBody()
  130. {
  131. $this->helper->response->setBody('foobarbaz');
  132. $this->helper->resetObjects();
  133. $body = $this->helper->response->getBody();
  134. $this->assertTrue(empty($body));
  135. }
  136. /**
  137. * @return void
  138. */
  139. public function testResetObjectsClearsResponseHeaders()
  140. {
  141. $this->helper->response->setHeader('X-Foo', 'Bar')
  142. ->setRawHeader('HTTP/1.1');
  143. $this->helper->resetObjects();
  144. $headers = $this->helper->response->getHeaders();
  145. $rawHeaders = $this->helper->response->getRawHeaders();
  146. $this->assertTrue(empty($headers));
  147. $this->assertTrue(empty($rawHeaders));
  148. }
  149. /**
  150. * @return void
  151. */
  152. public function testActionReturnsContentFromDefaultModule()
  153. {
  154. $value = $this->helper->action('bar', 'action-foo');
  155. $this->assertContains('In default module, FooController::barAction()', $value);
  156. }
  157. /**
  158. * @return void
  159. */
  160. public function testActionReturnsContentFromSpecifiedModule()
  161. {
  162. $value = $this->helper->action('bar', 'foo', 'foo');
  163. $this->assertContains('In foo module, Foo_FooController::barAction()', $value);
  164. }
  165. /**
  166. * @return void
  167. */
  168. public function testActionReturnsContentReflectingPassedParams()
  169. {
  170. $value = $this->helper->action('baz', 'action-foo', null, array('bat' => 'This is my message'));
  171. $this->assertNotContains('BOGUS', $value, var_export($this->helper->request->getUserParams(), 1));
  172. $this->assertContains('This is my message', $value);
  173. }
  174. /**
  175. * @return void
  176. */
  177. public function testActionReturnsEmptyStringWhenForwardDetected()
  178. {
  179. $value = $this->helper->action('forward', 'action-foo');
  180. $this->assertEquals('', $value);
  181. }
  182. /**
  183. * @return void
  184. */
  185. public function testActionReturnsEmptyStringWhenRedirectDetected()
  186. {
  187. $value = $this->helper->action('redirect', 'action-foo');
  188. $this->assertEquals('', $value);
  189. }
  190. /**
  191. * @return void
  192. */
  193. public function testConstructorThrowsExceptionWithNoControllerDirsInFrontController()
  194. {
  195. Zend_Controller_Front::getInstance()->resetInstance();
  196. try {
  197. $helper = new Zend_View_Helper_Action();
  198. $this->fail('Empty front controller should cause action helper to throw exception');
  199. } catch (Exception $e) {
  200. }
  201. }
  202. /**
  203. * @return void
  204. */
  205. public function testConstructorThrowsExceptionWithNoRequestInFrontController()
  206. {
  207. $front = Zend_Controller_Front::getInstance();
  208. $front->resetInstance();
  209. $response = new Zend_Controller_Response_Http();
  210. $response->headersSentThrowsException = false;
  211. $front->setResponse($response)
  212. ->addModuleDirectory(dirname(__FILE__) . '/_files/modules');
  213. try {
  214. $helper = new Zend_View_Helper_Action();
  215. $this->fail('No request in front controller should cause action helper to throw exception');
  216. } catch (Exception $e) {
  217. }
  218. }
  219. /**
  220. * @return void
  221. */
  222. public function testConstructorThrowsExceptionWithNoResponseInFrontController()
  223. {
  224. $front = Zend_Controller_Front::getInstance();
  225. $front->resetInstance();
  226. $request = new Zend_Controller_Request_Http('http://framework.zend.com/foo');
  227. $front->setRequest($this->request)
  228. ->addModuleDirectory(dirname(__FILE__) . '/_files/modules');
  229. try {
  230. $helper = new Zend_View_Helper_Action();
  231. $this->fail('No response in front controller should cause action helper to throw exception');
  232. } catch (Exception $e) {
  233. }
  234. }
  235. public function testViewObjectRemainsUnchangedAfterAction()
  236. {
  237. $value = $this->helper->action('bar', 'foo', 'foo');
  238. $this->assertContains('In foo module, Foo_FooController::barAction()', $value);
  239. $this->assertNull($this->view->bar);
  240. }
  241. public function testNestingActionsDoesNotBreakPlaceholderHelpers()
  242. {
  243. $html = $this->helper->action('nest', 'foo', 'foo');
  244. $title = $this->view->headTitle()->toString();
  245. $this->assertContains(' - ', $title, $title);
  246. $this->assertContains('Foo Nest', $title);
  247. $this->assertContains('Nested Stuff', $title);
  248. }
  249. /**
  250. * @group ZF-2716
  251. */
  252. public function testActionWithPartialsUseOfViewRendererReturnsToOriginatingViewState()
  253. {
  254. require_once 'Zend/View/Helper/Partial.php';
  255. $partial = new Zend_View_Helper_Partial();
  256. $this->view->setScriptPath(dirname(__FILE__) . '/_files/modules/default/views/scripts/');
  257. $partial->setView($this->view);
  258. Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->view = $this->view;
  259. $partial->partial('partialActionCall.phtml');
  260. $this->assertSame($this->view, Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->view);
  261. }
  262. /**
  263. * Future ViewRenderer State issues should be included in this test.
  264. *
  265. * @group ZF-2846
  266. */
  267. public function testActionReturnsViewRendererToOriginalState()
  268. {
  269. /* Setup the VR as if we were inside an action controller */
  270. $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer();
  271. $viewRenderer->init();
  272. Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
  273. // make sure noRender is false
  274. $this->assertFalse($viewRenderer->getNoRender());
  275. $value = $this->helper->action('bar', 'action-foo');
  276. $viewRendererPostAction = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  277. // ViewRenderer noRender should still be false
  278. $this->assertFalse($viewRendererPostAction->getNoRender());
  279. $this->assertSame($viewRenderer, $viewRendererPostAction);
  280. }
  281. /**
  282. * Multiple call state issue
  283. *
  284. *
  285. * @group ZF-3456
  286. */
  287. public function testActionCalledWithinActionResetsResponseState()
  288. {
  289. $value = $this->helper->action('bar-one', 'baz', 'foo');
  290. $this->assertRegexp('/Baz-Three-View-Script\s+Baz-Two-View-Script\s+Baz-One-View-Script/s', $value);
  291. }
  292. }
  293. // Call Zend_View_Helper_ActionTest::main() if this source file is executed directly.
  294. if (PHPUnit_MAIN_METHOD == "Zend_View_Helper_ActionTest::main") {
  295. Zend_View_Helper_ActionTest::main();
  296. }