ActionTest.php 11 KB

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