ViewRendererTest.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  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-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_Controller_Action_Helper_ViewRendererTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Controller_Action_Helper_ViewRendererTest::main");
  25. }
  26. require_once 'Zend/Controller/Action/Helper/ViewRenderer.php';
  27. require_once 'Zend/Controller/Front.php';
  28. require_once 'Zend/Controller/Request/Http.php';
  29. require_once 'Zend/Controller/Response/Http.php';
  30. require_once 'Zend/Filter/Inflector.php';
  31. require_once 'Zend/View.php';
  32. require_once dirname(__FILE__) . '/../../_files/modules/foo/controllers/IndexController.php';
  33. require_once dirname(__FILE__) . '/../../_files/modules/bar/controllers/IndexController.php';
  34. /**
  35. * Test class for Zend_Controller_Action_Helper_ViewRenderer.
  36. *
  37. * @category Zend
  38. * @package Zend_Controller
  39. * @subpackage UnitTests
  40. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. * @group Zend_Controller
  43. * @group Zend_Controller_Action
  44. * @group Zend_Controller_Action_Helper
  45. */
  46. class Zend_Controller_Action_Helper_ViewRendererTest extends PHPUnit_Framework_TestCase
  47. {
  48. /**
  49. * Base path to controllers, views
  50. * @var string
  51. */
  52. public $basePath;
  53. /**
  54. * Front controller object
  55. * @var Zend_Controller_Front
  56. */
  57. public $front;
  58. /**
  59. * ViewRenderer helper
  60. * @var Zend_Controller_Action_Helper_ViewRenderer
  61. */
  62. public $helper;
  63. /**
  64. * Request object
  65. * @var Zend_Controller_Request_Http
  66. */
  67. public $request;
  68. /**
  69. * Response object
  70. * @var Zend_Controller_Response_Http
  71. */
  72. public $response;
  73. /**
  74. * Runs the test methods of this class.
  75. *
  76. * @access public
  77. * @static
  78. */
  79. public static function main()
  80. {
  81. $suite = new PHPUnit_Framework_TestSuite("Zend_Controller_Action_Helper_ViewRendererTest");
  82. $result = PHPUnit_TextUI_TestRunner::run($suite);
  83. }
  84. /**
  85. * Sets up the fixture, for example, open a network connection.
  86. * This method is called before a test is executed.
  87. *
  88. * @access protected
  89. */
  90. protected function setUp()
  91. {
  92. $this->basePath = realpath(dirname(__FILE__) . str_repeat(DIRECTORY_SEPARATOR . '..', 2));
  93. $this->request = new Zend_Controller_Request_Http();
  94. $this->response = new Zend_Controller_Response_Http();
  95. $this->front = Zend_Controller_Front::getInstance();
  96. $this->front->resetInstance();
  97. $this->front->addModuleDirectory($this->basePath . DIRECTORY_SEPARATOR . '_files' . DIRECTORY_SEPARATOR . 'modules')
  98. ->setRequest($this->request)
  99. ->setResponse($this->response);
  100. $this->helper = new Zend_Controller_Action_Helper_ViewRenderer();
  101. Zend_Controller_Action_HelperBroker::addHelper($this->helper);
  102. }
  103. /**
  104. * Tears down the fixture, for example, close a network connection.
  105. * This method is called after a test is executed.
  106. *
  107. * @access protected
  108. */
  109. protected function tearDown()
  110. {
  111. Zend_Controller_Action_HelperBroker::resetHelpers();
  112. }
  113. public function testConstructorSetsViewWhenPassed()
  114. {
  115. $view = new Zend_View();
  116. $helper = new Zend_Controller_Action_Helper_ViewRenderer($view);
  117. $this->assertNotNull(isset($helper->view));
  118. $this->assertSame($view, $helper->view);
  119. }
  120. public function testConstructorSetsOptionsWhenPassed()
  121. {
  122. $helper = new Zend_Controller_Action_Helper_ViewRenderer(null, array(
  123. 'neverRender' => true,
  124. 'noRender' => true,
  125. 'noController' => true,
  126. 'viewSuffix' => 'php',
  127. 'scriptAction' => 'foo',
  128. 'responseSegment' => 'baz'
  129. ));
  130. $this->assertTrue($helper->getNeverRender());
  131. $this->assertTrue($helper->getNoRender());
  132. $this->assertTrue($helper->getNoController());
  133. $this->assertEquals('php', $helper->getViewSuffix());
  134. $this->assertEquals('foo', $helper->getScriptAction());
  135. $this->assertEquals('baz', $helper->getResponseSegment());
  136. }
  137. public function testSetView()
  138. {
  139. $view = new Zend_View();
  140. $this->helper->setView($view);
  141. $this->assertSame($view, $this->helper->view);
  142. }
  143. public function testGetFrontController()
  144. {
  145. $this->assertSame($this->front, $this->helper->getFrontController());
  146. }
  147. protected function _checkDefaults($module = 'foo', $count = 1)
  148. {
  149. $this->assertTrue(isset($this->helper->view));
  150. $this->assertTrue($this->helper->view instanceof Zend_View);
  151. $this->assertFalse($this->helper->getNeverRender());
  152. $this->assertFalse($this->helper->getNoRender());
  153. $this->assertNull($this->helper->getResponseSegment());
  154. $this->assertNull($this->helper->getScriptAction());
  155. $scriptPaths = $this->helper->view->getScriptPaths();
  156. $this->assertEquals($count, count($scriptPaths), var_export($scriptPaths, 1));
  157. $this->assertContains($module, $scriptPaths[0]);
  158. $helperPaths = $this->helper->view->getHelperPaths();
  159. $test = ucfirst($module) . '_View_Helper_';
  160. $found = false;
  161. foreach ($helperPaths as $prefix => $paths) {
  162. if ($test == $prefix) {
  163. $found = true;
  164. }
  165. }
  166. $this->assertTrue($found, 'Did not find auto-initialized helper path: ' . var_export($helperPaths, 1));
  167. $filterPaths = $this->helper->view->getFilterPaths();
  168. $test = ucfirst($module) . '_View_Filter_';
  169. $found = false;
  170. foreach ($filterPaths as $prefix => $paths) {
  171. if ($test == $prefix) {
  172. $found = true;
  173. }
  174. }
  175. $this->assertTrue($found, 'Did not find auto-initialized filter path: ' . var_export($filterPaths, 1));
  176. }
  177. public function testInitViewWithDefaults()
  178. {
  179. $this->request->setModuleName('foo')
  180. ->setControllerName('index');
  181. $controller = new Foo_IndexController($this->request, $this->response, array());
  182. $this->helper->setActionController($controller);
  183. $this->helper->initView();
  184. $this->_checkDefaults();
  185. }
  186. public function testInitViewWillNotRegisterSameViewPathTwice()
  187. {
  188. $this->request->setModuleName('foo')
  189. ->setControllerName('index');
  190. $controller = new Foo_IndexController($this->request, $this->response, array());
  191. $this->helper->setActionController($controller);
  192. $this->helper->initView();
  193. $moduleDir = dirname($this->front->getControllerDirectory('foo'));
  194. $this->helper->initView($moduleDir . '/views', 'Foo', array('encoding' => 'ISO-8858-1'));
  195. $this->_checkDefaults();
  196. }
  197. public function testInitViewCanBeCalledAfterPostDispatch()
  198. {
  199. $this->request->setModuleName('foo')
  200. ->setControllerName('index');
  201. $controller = new Foo_IndexController($this->request, $this->response, array());
  202. $this->helper->setActionController($controller);
  203. $this->helper->initView();
  204. $this->helper->setNoRender();
  205. $this->helper->postDispatch();
  206. $this->request->setModuleName('bar')
  207. ->setControllerName('index');
  208. $controller = new Bar_IndexController($this->request, $this->response, array());
  209. $this->helper->setActionController($controller);
  210. $this->helper->initView();
  211. $this->_checkDefaults('bar', 2);
  212. }
  213. public function testPreDispatchWithDefaults()
  214. {
  215. $this->request->setModuleName('foo')
  216. ->setControllerName('index');
  217. $controller = new Foo_IndexController($this->request, $this->response, array());
  218. $this->helper->setActionController($controller);
  219. $this->helper->preDispatch();
  220. $this->_checkDefaults();
  221. }
  222. public function testInitViewWithOptions()
  223. {
  224. $this->request->setModuleName('foo')
  225. ->setControllerName('index');
  226. $controller = new Foo_IndexController($this->request, $this->response, array());
  227. $this->helper->setActionController($controller);
  228. $viewDir = dirname(__FILE__) . str_repeat(DIRECTORY_SEPARATOR . '..', 2) . DIRECTORY_SEPARATOR . 'views';
  229. $this->helper->initView($viewDir, 'Baz_Bat', array(
  230. 'neverRender' => true,
  231. 'noRender' => true,
  232. 'noController' => true,
  233. 'viewSuffix' => 'php',
  234. 'scriptAction' => 'foo',
  235. 'responseSegment' => 'baz'
  236. ));
  237. $this->assertTrue($this->helper->getNeverRender());
  238. $this->assertTrue($this->helper->getNoRender());
  239. $this->assertTrue($this->helper->getNoController());
  240. $this->assertEquals('php', $this->helper->getViewSuffix());
  241. $this->assertEquals('foo', $this->helper->getScriptAction());
  242. $this->assertEquals('baz', $this->helper->getResponseSegment());
  243. $scriptPaths = $this->helper->view->getScriptPaths();
  244. $scriptPath = $scriptPaths[0];
  245. $this->assertContains(
  246. $this->_normalizePath($viewDir),
  247. $this->_normalizePath($scriptPath)
  248. );
  249. $helperPaths = $this->helper->view->getHelperPaths();
  250. $found = false;
  251. foreach ($helperPaths as $prefix => $paths) {
  252. if ('Baz_Bat_Helper_' == $prefix) {
  253. $found = true;
  254. }
  255. }
  256. $this->assertTrue($found, 'Helper prefix not set according to spec: ' . var_export($helperPaths, 1));
  257. $filterPaths = $this->helper->view->getFilterPaths();
  258. $found = false;
  259. foreach ($filterPaths as $prefix => $paths) {
  260. if ('Baz_Bat_Filter_' == $prefix) {
  261. $found = true;
  262. }
  263. }
  264. $this->assertTrue($found, 'Filter prefix not set according to spec' . var_export($filterPaths, 1));
  265. }
  266. public function testNeverRenderFlag()
  267. {
  268. $this->assertFalse($this->helper->getNeverRender());
  269. $this->helper->setNeverRender();
  270. $this->assertTrue($this->helper->getNeverRender());
  271. $this->helper->setNeverRender(false);
  272. $this->assertFalse($this->helper->getNeverRender());
  273. $this->helper->setNeverRender(true);
  274. $this->assertTrue($this->helper->getNeverRender());
  275. }
  276. public function testNeverRenderFlagDisablesRendering()
  277. {
  278. $this->helper->setNeverRender();
  279. $this->request->setModuleName('bar')
  280. ->setControllerName('index')
  281. ->setActionName('test')
  282. ->setDispatched(true);
  283. $controller = new Bar_IndexController($this->request, $this->response, array());
  284. $this->helper->setActionController($controller);
  285. $this->helper->postDispatch();
  286. $content = $this->response->getBody();
  287. $this->assertNotContains('Rendered index/test.phtml', $this->response->getBody());
  288. }
  289. public function testNoRenderFlag()
  290. {
  291. $this->assertFalse($this->helper->getNoRender());
  292. $this->helper->setNoRender();
  293. $this->assertTrue($this->helper->getNoRender());
  294. $this->helper->setNoRender(false);
  295. $this->assertFalse($this->helper->getNoRender());
  296. $this->helper->setNoRender(true);
  297. $this->assertTrue($this->helper->getNoRender());
  298. }
  299. public function testScriptActionProperty()
  300. {
  301. $this->assertNull($this->helper->getScriptAction());
  302. $this->helper->setScriptAction('foo');
  303. $this->assertEquals('foo', $this->helper->getScriptAction());
  304. $this->helper->setScriptAction('foo/bar');
  305. $this->assertEquals('foo/bar', $this->helper->getScriptAction());
  306. }
  307. public function testResponseSegmentProperty()
  308. {
  309. $this->assertNull($this->helper->getResponseSegment());
  310. $this->helper->setResponseSegment('foo');
  311. $this->assertEquals('foo', $this->helper->getResponseSegment());
  312. $this->helper->setResponseSegment('foo/bar');
  313. $this->assertEquals('foo/bar', $this->helper->getResponseSegment());
  314. }
  315. public function testNoControllerFlag()
  316. {
  317. $this->assertFalse($this->helper->getNoController());
  318. $this->helper->setNoController();
  319. $this->assertTrue($this->helper->getNoController());
  320. $this->helper->setNoController(false);
  321. $this->assertFalse($this->helper->getNoController());
  322. $this->helper->setNoController(true);
  323. $this->assertTrue($this->helper->getNoController());
  324. }
  325. public function testNeverControllerFlag()
  326. {
  327. $this->assertFalse($this->helper->getNeverController());
  328. $this->helper->setNeverController();
  329. $this->assertTrue($this->helper->getNeverController());
  330. $this->helper->setNeverController(false);
  331. $this->assertFalse($this->helper->getNeverController());
  332. $this->helper->setNeverController(true);
  333. $this->assertTrue($this->helper->getNeverController());
  334. }
  335. protected function _checkRenderProperties()
  336. {
  337. $this->assertEquals('foo', $this->helper->getScriptAction());
  338. $this->assertEquals('bar', $this->helper->getResponseSegment());
  339. $this->assertTrue($this->helper->getNoController());
  340. }
  341. public function testSetRenderSetsProperties()
  342. {
  343. $this->helper->setRender('foo', 'bar', true);
  344. $this->_checkRenderProperties();
  345. }
  346. public function testPostDispatchRendersAppropriateScript()
  347. {
  348. $this->request->setModuleName('bar')
  349. ->setControllerName('index')
  350. ->setActionName('test')
  351. ->setDispatched(true);
  352. $controller = new Bar_IndexController($this->request, $this->response, array());
  353. $this->helper->setActionController($controller);
  354. $this->helper->postDispatch();
  355. $content = $this->response->getBody();
  356. $this->assertContains('Rendered index/test.phtml in bar module', $content);
  357. }
  358. public function testPostDispatchDoesNothingOnForward()
  359. {
  360. $this->request->setModuleName('bar')
  361. ->setControllerName('index')
  362. ->setActionName('test')
  363. ->setDispatched(false);
  364. $controller = new Bar_IndexController($this->request, $this->response, array());
  365. $this->helper->setActionController($controller);
  366. $this->helper->postDispatch();
  367. $content = $this->response->getBody();
  368. $this->assertNotContains('Rendered index/test.phtml in bar module', $content);
  369. $this->assertTrue(empty($content));
  370. }
  371. public function testPostDispatchDoesNothingOnRedirect()
  372. {
  373. $this->request->setModuleName('bar')
  374. ->setControllerName('index')
  375. ->setActionName('test')
  376. ->setDispatched(true);
  377. $this->response->setHttpResponseCode(302);
  378. $controller = new Bar_IndexController($this->request, $this->response, array());
  379. $this->helper->setActionController($controller);
  380. $this->helper->postDispatch();
  381. $content = $this->response->getBody();
  382. $this->assertNotContains('Rendered index/test.phtml in bar module', $content);
  383. $this->assertTrue(empty($content));
  384. }
  385. public function testPostDispatchDoesNothingWithNoController()
  386. {
  387. $this->request->setModuleName('bar')
  388. ->setControllerName('index')
  389. ->setActionName('test')
  390. ->setDispatched(true);
  391. $this->helper->postDispatch();
  392. $content = $this->response->getBody();
  393. $this->assertNotContains('Rendered index/test.phtml in bar module', $content);
  394. $this->assertTrue(empty($content));
  395. }
  396. public function testPostDispatchDoesNothingWithNeverController()
  397. {
  398. $this->request->setModuleName('bar')
  399. ->setControllerName('index')
  400. ->setActionName('test')
  401. ->setDispatched(true);
  402. $this->helper->setNeverController(true);
  403. $this->helper->postDispatch();
  404. $content = $this->response->getBody();
  405. $this->assertNotContains('Rendered index/test.phtml in bar module', $content);
  406. $this->assertTrue(empty($content));
  407. }
  408. public function testDirectProxiesToSetRender()
  409. {
  410. $this->helper->direct('foo', 'bar', true);
  411. $this->_checkRenderProperties();
  412. }
  413. public function testViewBasePathSpecDefault()
  414. {
  415. $this->assertEquals(':moduleDir/views', $this->helper->getViewBasePathSpec());
  416. }
  417. public function testSettingViewBasePathSpec()
  418. {
  419. $this->helper->setViewBasePathSpec(':moduleDir/views/:controller');
  420. $this->assertEquals(':moduleDir/views/:controller', $this->helper->getViewBasePathSpec());
  421. }
  422. public function testViewScriptPathSpecDefault()
  423. {
  424. $this->assertEquals(':controller/:action.:suffix', $this->helper->getViewScriptPathSpec());
  425. }
  426. public function testSettingViewScriptPathSpec()
  427. {
  428. $this->helper->setViewScriptPathSpec(':moduleDir/views/:controller');
  429. $this->assertEquals(':moduleDir/views/:controller', $this->helper->getViewScriptPathSpec());
  430. }
  431. public function testViewScriptPathNoControllerSpecDefault()
  432. {
  433. $this->assertEquals(':action.:suffix', $this->helper->getViewScriptPathNoControllerSpec());
  434. }
  435. public function testSettingViewScriptPathNoControllerSpec()
  436. {
  437. $this->helper->setViewScriptPathNoControllerSpec(':module/:action.:suffix');
  438. $this->assertEquals(':module/:action.:suffix', $this->helper->getViewScriptPathNoControllerSpec());
  439. }
  440. public function testGetViewScriptWithDefaults()
  441. {
  442. $this->request->setModuleName('bar')
  443. ->setControllerName('index')
  444. ->setActionName('test');
  445. $controller = new Bar_IndexController($this->request, $this->response, array());
  446. $expected = 'index/test.phtml';
  447. $this->assertEquals($expected, $this->helper->getViewScript());
  448. }
  449. public function testGetViewScriptWithSpecifiedAction()
  450. {
  451. $this->request->setModuleName('bar')
  452. ->setControllerName('index')
  453. ->setActionName('test');
  454. $controller = new Bar_IndexController($this->request, $this->response, array());
  455. $expected = 'index/baz.phtml';
  456. $this->assertEquals($expected, $this->helper->getViewScript('baz'));
  457. }
  458. public function testGetViewScriptWithSpecifiedVars()
  459. {
  460. $this->request->setModuleName('bar')
  461. ->setControllerName('index')
  462. ->setActionName('test');
  463. $controller = new Bar_IndexController($this->request, $this->response, array());
  464. $expected = 'baz/bat.php';
  465. $this->assertEquals(
  466. $expected,
  467. $this->helper->getViewScript(
  468. null,
  469. array('controller' => 'baz', 'action' => 'bat', 'suffix' => 'php')
  470. )
  471. );
  472. }
  473. public function testGetViewScriptWithNoControllerSet()
  474. {
  475. $this->request->setModuleName('bar')
  476. ->setControllerName('index')
  477. ->setActionName('test');
  478. $controller = new Bar_IndexController($this->request, $this->response, array());
  479. $this->helper->setNoController();
  480. $expected = 'test.phtml';
  481. $this->assertEquals($expected, $this->helper->getViewScript());
  482. }
  483. public function testRenderScript()
  484. {
  485. $this->request->setModuleName('bar')
  486. ->setControllerName('index')
  487. ->setActionName('test');
  488. $controller = new Bar_IndexController($this->request, $this->response, array());
  489. $this->helper->renderScript('index/test.phtml');
  490. $body = $this->response->getBody();
  491. $this->assertContains('Rendered index/test.phtml in bar module', $body);
  492. }
  493. public function testRenderScriptToNamedResponseSegment()
  494. {
  495. $this->request->setModuleName('bar')
  496. ->setControllerName('index')
  497. ->setActionName('test');
  498. $controller = new Bar_IndexController($this->request, $this->response, array());
  499. $this->helper->renderScript('index/test.phtml', 'foo');
  500. $body = $this->response->getBody('foo');
  501. $this->assertContains('Rendered index/test.phtml in bar module', $body);
  502. }
  503. public function testRenderScriptToPreviouslyNamedResponseSegment()
  504. {
  505. $this->request->setModuleName('bar')
  506. ->setControllerName('index')
  507. ->setActionName('test');
  508. $controller = new Bar_IndexController($this->request, $this->response, array());
  509. $this->helper->setResponseSegment('foo');
  510. $this->helper->renderScript('index/test.phtml');
  511. $body = $this->response->getBody('foo');
  512. $this->assertContains('Rendered index/test.phtml in bar module', $body);
  513. }
  514. public function testRenderWithDefaults()
  515. {
  516. $this->request->setModuleName('bar')
  517. ->setControllerName('index')
  518. ->setActionName('test');
  519. $controller = new Bar_IndexController($this->request, $this->response, array());
  520. $this->helper->render();
  521. $body = $this->response->getBody();
  522. $this->assertContains('Rendered index/test.phtml in bar module', $body);
  523. }
  524. public function testRenderToSpecifiedAction()
  525. {
  526. $this->request->setModuleName('bar')
  527. ->setControllerName('index')
  528. ->setActionName('index');
  529. $controller = new Bar_IndexController($this->request, $this->response, array());
  530. $this->helper->render('test');
  531. $body = $this->response->getBody();
  532. $this->assertContains('Rendered index/test.phtml in bar module', $body);
  533. }
  534. public function testRenderWithNoController()
  535. {
  536. $this->request->setModuleName('bar')
  537. ->setControllerName('index')
  538. ->setActionName('test');
  539. $controller = new Bar_IndexController($this->request, $this->response, array());
  540. $this->helper->render(null, null, true);
  541. $body = $this->response->getBody();
  542. $this->assertContains('Rendered test.phtml in bar module', $body);
  543. }
  544. public function testRenderToNamedSegment()
  545. {
  546. $this->request->setModuleName('bar')
  547. ->setControllerName('index')
  548. ->setActionName('test');
  549. $controller = new Bar_IndexController($this->request, $this->response, array());
  550. $this->helper->render(null, 'foo');
  551. $body = $this->response->getBody('foo');
  552. $this->assertContains('Rendered index/test.phtml in bar module', $body);
  553. }
  554. public function testRenderBySpec()
  555. {
  556. $this->request->setModuleName('bar')
  557. ->setControllerName('index')
  558. ->setActionName('index');
  559. $controller = new Bar_IndexController($this->request, $this->response, array());
  560. $this->helper->renderBySpec('foo', array('controller' => 'test', 'suffix' => 'php'));
  561. $body = $this->response->getBody();
  562. $this->assertContains('Rendered test/foo.php', $body);
  563. }
  564. public function testRenderBySpecToNamedResponseSegment()
  565. {
  566. $this->request->setModuleName('bar')
  567. ->setControllerName('index')
  568. ->setActionName('index');
  569. $controller = new Bar_IndexController($this->request, $this->response, array());
  570. $this->helper->renderBySpec('foo', array('controller' => 'test', 'suffix' => 'php'), 'foo');
  571. $body = $this->response->getBody('foo');
  572. $this->assertContains('Rendered test/foo.php', $body);
  573. }
  574. public function testInitDoesNotInitViewWhenNoViewRendererSet()
  575. {
  576. $this->request->setModuleName('bar')
  577. ->setControllerName('index')
  578. ->setActionName('index');
  579. $this->front->setParam('noViewRenderer', true);
  580. $controller = new Bar_IndexController($this->request, $this->response, array());
  581. $this->assertNull($controller->view);
  582. }
  583. public function testPostDispatchDoesNotRenderViewWhenNoViewRendererSet()
  584. {
  585. $this->request->setModuleName('bar')
  586. ->setControllerName('index')
  587. ->setActionName('index');
  588. $this->front->setParam('noViewRenderer', true);
  589. $controller = new Bar_IndexController($this->request, $this->response, array());
  590. $this->helper->postDispatch();
  591. $body = $this->response->getBody();
  592. $this->assertTrue(empty($body));
  593. }
  594. public function testRenderNormalizationIsCorrect()
  595. {
  596. $this->request->setModuleName('default')
  597. ->setControllerName('foo')
  598. ->setActionName('myBar');
  599. $controller = new Bar_IndexController($this->request, $this->response, array());
  600. $this->helper->setActionController($controller);
  601. $scriptName = $this->helper->getViewScript();
  602. $this->assertEquals('foo/my-bar.phtml', $scriptName);
  603. $this->request->setModuleName('default')
  604. ->setControllerName('foo')
  605. ->setActionName('baz__bat');
  606. $scriptName = $this->helper->getViewScript();
  607. $this->assertEquals('foo/baz-bat.phtml', $scriptName);
  608. $this->request->setModuleName('default')
  609. ->setControllerName('Foo_Bar')
  610. ->setActionName('bar__baz');
  611. $scriptName = $this->helper->getViewScript();
  612. $this->assertEquals('foo/bar/bar-baz.phtml', $scriptName);
  613. }
  614. public function testGetInflectorGetsDefaultInflectorWhenNoneProvided()
  615. {
  616. $inflector = $this->helper->getInflector();
  617. $this->assertTrue($inflector instanceof Zend_Filter_Inflector);
  618. $rules = $inflector->getRules();
  619. $this->assertTrue(isset($rules['module']));
  620. $this->assertTrue(isset($rules['moduleDir']));
  621. $this->assertTrue(isset($rules['controller']));
  622. $this->assertTrue(isset($rules['action']));
  623. $this->assertTrue(isset($rules['suffix']));
  624. }
  625. public function testInflectorAccessorsAllowSwappingInflectors()
  626. {
  627. $inflector = $this->helper->getInflector();
  628. $this->assertTrue($inflector instanceof Zend_Filter_Inflector);
  629. $newInflector = new Zend_Filter_Inflector();
  630. $this->helper->setInflector($newInflector);
  631. $receivedInflector = $this->helper->getInflector();
  632. $this->assertSame($newInflector, $receivedInflector);
  633. $this->assertNotSame($newInflector, $inflector);
  634. }
  635. public function testCustomInflectorCanUseItsOwnTarget()
  636. {
  637. $this->request->setModuleName('bar')
  638. ->setControllerName('index')
  639. ->setActionName('index');
  640. $controller = new Bar_IndexController($this->request, $this->response, array());
  641. $this->helper->view->addBasePath($this->basePath . '/_files/modules/bar/views');
  642. $inflector = new Zend_Filter_Inflector('test.phtml');
  643. $inflector->addFilterRule(':controller', array('Word_CamelCaseToDash'));
  644. $this->helper->setInflector($inflector);
  645. $this->helper->render();
  646. $body = $this->response->getBody();
  647. $this->assertContains('Rendered test.phtml in bar module', $body);
  648. }
  649. public function testCustomInflectorUsesViewRendererTargetWhenPassedInWithReferenceFlag()
  650. {
  651. $this->request->setModuleName('bar')
  652. ->setControllerName('index')
  653. ->setActionName('test');
  654. $controller = new Bar_IndexController($this->request, $this->response, array());
  655. $this->helper->view->addBasePath($this->basePath . '/_files/modules/bar/views');
  656. require_once 'Zend/Filter/PregReplace.php';
  657. require_once 'Zend/Filter/Word/UnderscoreToSeparator.php';
  658. $inflector = new Zend_Filter_Inflector('test.phtml');
  659. $inflector->addRules(array(
  660. ':module' => array('Word_CamelCaseToDash', 'stringToLower'),
  661. ':controller' => array('Word_CamelCaseToDash', new Zend_Filter_Word_UnderscoreToSeparator(DIRECTORY_SEPARATOR), 'StringToLower'),
  662. ':action' => array(
  663. 'Word_CamelCaseToDash',
  664. new Zend_Filter_PregReplace('/[^a-z0-9]+/i', '-'),
  665. 'StringToLower'
  666. ),
  667. ));
  668. $this->helper->setInflector($inflector, true);
  669. $this->helper->render();
  670. $body = $this->response->getBody();
  671. $this->assertContains('Rendered index/test.phtml in bar module', $body);
  672. }
  673. public function testStockInflectorAllowsSubDirectoryViewScripts()
  674. {
  675. $this->request->setModuleName('bar')
  676. ->setControllerName('index')
  677. ->setActionName('layout/admin');
  678. $this->assertEquals('index/layout/admin.phtml', $this->helper->getViewScript());
  679. }
  680. /**
  681. * @see ZF-2443
  682. */
  683. public function testStockInflectorWorksWithViewBaseSpec()
  684. {
  685. $this->request->setModuleName('bar') // bar must exist so the ViewRendere doesnt throw an exception
  686. ->setControllerName('index')
  687. ->setActionName('admin');
  688. $controller = new Bar_IndexController($this->request, $this->response, array());
  689. $this->helper->setActionController($controller);
  690. $this->helper->setViewBasePathSpec(':moduleDir/:module');
  691. $this->helper->initView();
  692. $viewScriptPaths = $this->helper->view->getAllPaths();
  693. $expectedPathRegex = '#modules/bar/bar/scripts/$#';
  694. $this->assertRegExp(
  695. $expectedPathRegex,
  696. $this->_normalizePath($viewScriptPaths['script'][0])
  697. );
  698. $this->assertEquals($this->helper->getViewScript(), 'index/admin.phtml');
  699. }
  700. /**
  701. * @see ZF-2738
  702. */
  703. public function testStockInflectorWorksWithDottedRequestParts()
  704. {
  705. $this->request->setModuleName('foo')
  706. ->setControllerName('car.bar')
  707. ->setActionName('baz');
  708. $controller = new Bar_IndexController($this->request, $this->response, array());
  709. $this->helper->setActionController($controller);
  710. $viewScriptPaths = $this->helper->view->getAllPaths();
  711. $expectedPathRegex = '#modules/foo/views/scripts/$#';
  712. $this->assertRegExp(
  713. $expectedPathRegex,
  714. $this->_normalizePath($viewScriptPaths['script'][0])
  715. );
  716. $this->assertEquals('car-bar/baz.phtml', $this->helper->getViewScript());
  717. }
  718. public function testCorrectViewHelperPathShouldBePropagatedWhenSubControllerInvoked()
  719. {
  720. require_once $this->basePath . '/_files/modules/foo/controllers/Admin/IndexController.php';
  721. $this->request->setModuleName('foo')
  722. ->setControllerName('admin_index')
  723. ->setActionName('use-helper');
  724. $controller = new Foo_Admin_IndexController($this->request, $this->response, array());
  725. $this->helper->render();
  726. $body = $this->response->getBody();
  727. $this->assertContains('fooUseHelper invoked', $body, 'Received ' . $body);
  728. }
  729. /**
  730. * @group ZF-10725
  731. */
  732. public function testThatCharactersStrippedFromActionNameByDispatcherAreAlsoStrippedFromViewScriptName()
  733. {
  734. $this->request->setModuleName('default')
  735. ->setControllerName('foo')
  736. ->setActionName('-myBar-');
  737. $controller = new Bar_IndexController($this->request, $this->response, array());
  738. $this->helper->setActionController($controller);
  739. $scriptName = $this->helper->getViewScript();
  740. $this->assertEquals('foo/my-bar.phtml', $scriptName);
  741. }
  742. public function testCorrectViewHelperPathShouldBePropagatedWhenSubControllerInvokedInDefaultModule()
  743. {
  744. require_once $this->basePath . '/_files/modules/default/controllers/Admin/HelperController.php';
  745. $this->request->setControllerName('admin_helper')
  746. ->setActionName('render');
  747. $controller = new Admin_HelperController($this->request, $this->response, array());
  748. $this->helper->render();
  749. $body = $this->response->getBody();
  750. $this->assertContains('SampleZfHelper invoked', $body, 'Received ' . $body);
  751. }
  752. /**
  753. * @group ZF-11127
  754. */
  755. public function testViewSuffixInstanceNotSharedWhenViewHelperIsCloned()
  756. {
  757. $a = new Zend_Controller_Action_Helper_ViewRenderer();
  758. $a->init();
  759. $a->setViewSuffix('A');
  760. $this->assertEquals('A', $a->getViewSuffix());
  761. $b = clone $a;
  762. $this->assertEquals('A', $b->getViewSuffix());
  763. $b->setViewSuffix('B');
  764. $this->assertEquals('B', $b->getViewSuffix());
  765. $this->assertNotEquals('B', $a->getViewSuffix());
  766. }
  767. /**
  768. * @group ZF-10725
  769. * @dataProvider providerViewScriptNameDoesNotIncludeDisallowedCharacters
  770. */
  771. public function testViewScriptNameDoesNotIncludeDisallowedCharacters($actionName)
  772. {
  773. $this->request->setModuleName('default')
  774. ->setControllerName('foo')
  775. ->setActionName($actionName);
  776. $controller = new Bar_IndexController($this->request, $this->response, array());
  777. $this->helper->setActionController($controller);
  778. $scriptName = $this->helper->getViewScript();
  779. $this->assertEquals('foo/my-bar.phtml', $scriptName);
  780. }
  781. /**
  782. * Data provider for testViewScriptNameDoesNotIncludeDisallowedCharacters
  783. * @group ZF-10725
  784. * @return array
  785. */
  786. public function providerViewScriptNameDoesNotIncludeDisallowedCharacters()
  787. {
  788. return array(
  789. array('myBar-'),
  790. array('-myBar'),
  791. array('-myBar-'),
  792. array('-MyBar-'),
  793. array('MyBar-'),
  794. array('-MyBar')
  795. );
  796. }
  797. /**
  798. * @group GH-440
  799. * @dataProvider providerControllerNameDoesNotIncludeDisallowedCharacters
  800. */
  801. public function testControllerNameDoesNotIncludeDisallowedCharacters($controllerName)
  802. {
  803. $this->request->setControllerName($controllerName)
  804. ->setActionName('index');
  805. $this->helper->setActionController(
  806. new Bar_IndexController(
  807. $this->request, $this->response, array()
  808. )
  809. );
  810. $this->assertEquals(
  811. 'index/index.phtml', $this->helper->getViewScript()
  812. );
  813. }
  814. /**
  815. * Data provider for testControllerNameDoesNotIncludeDisallowedCharacters
  816. * @group GH-440
  817. * @return array
  818. */
  819. public function providerControllerNameDoesNotIncludeDisallowedCharacters()
  820. {
  821. return array(
  822. array('!index'),
  823. array('@index'),
  824. array('-index'),
  825. );
  826. }
  827. /**
  828. * @group GH-440
  829. */
  830. public function testControllerNameFormattingShouldRespectWordCamelCaseToDash()
  831. {
  832. $this->request->setControllerName('MetadataValidation')
  833. ->setActionName('index');
  834. $this->helper->setActionController(
  835. new Bar_IndexController(
  836. $this->request, $this->response, array()
  837. )
  838. );
  839. $this->assertEquals(
  840. 'metadata-validation/index.phtml', $this->helper->getViewScript()
  841. );
  842. }
  843. protected function _normalizePath($path)
  844. {
  845. return str_replace(array('/', '\\'), '/', $path);
  846. }
  847. /**
  848. * @group ZF-10725
  849. */
  850. public function testActionsWithLeadingCapitalLettersShouldNotInvokeTruncatedViewScripts()
  851. {
  852. $this->request->setModuleName('default')
  853. ->setControllerName('Controller')
  854. ->setActionName('Action');
  855. $controller = new Bar_IndexController($this->request, $this->response, array());
  856. $this->helper->setActionController($controller);
  857. $scriptName = $this->helper->getViewScript();
  858. $this->assertEquals('controller/action.phtml', $scriptName);
  859. }
  860. }
  861. // Call Zend_Controller_Action_Helper_ViewRendererTest::main() if this source file is executed directly.
  862. if (PHPUnit_MAIN_METHOD == "Zend_Controller_Action_Helper_ViewRendererTest::main") {
  863. Zend_Controller_Action_Helper_ViewRendererTest::main();
  864. }