LayoutTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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_Layout
  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_Layout_LayoutTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Layout_LayoutTest::main");
  25. }
  26. require_once 'Zend/Layout.php';
  27. require_once 'Zend/Layout/Controller/Plugin/Layout.php';
  28. require_once 'Zend/Layout/Controller/Action/Helper/Layout.php';
  29. require_once 'Zend/Controller/Front.php';
  30. require_once 'Zend/Controller/Action/HelperBroker.php';
  31. require_once 'Zend/Filter/Inflector.php';
  32. require_once 'Zend/View/Interface.php';
  33. require_once 'Zend/View.php';
  34. /**
  35. * Test class for Zend_Layout.
  36. *
  37. * @category Zend
  38. * @package Zend_Layout
  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_Layout
  43. */
  44. class Zend_Layout_LayoutTest extends PHPUnit_Framework_TestCase
  45. {
  46. /**
  47. * Runs the test methods of this class.
  48. *
  49. * @return void
  50. */
  51. public static function main()
  52. {
  53. $suite = new PHPUnit_Framework_TestSuite("Zend_Layout_LayoutTest");
  54. $result = PHPUnit_TextUI_TestRunner::run($suite);
  55. }
  56. /**
  57. * Sets up the fixture, for example, open a network connection.
  58. * This method is called before a test is executed.
  59. *
  60. * @return void
  61. */
  62. public function setUp()
  63. {
  64. Zend_Layout_LayoutTest_Override::resetMvcInstance();
  65. Zend_Controller_Front::getInstance()->resetInstance();
  66. if (Zend_Controller_Action_HelperBroker::hasHelper('Layout')) {
  67. Zend_Controller_Action_HelperBroker::removeHelper('Layout');
  68. }
  69. if (Zend_Controller_Action_HelperBroker::hasHelper('viewRenderer')) {
  70. Zend_Controller_Action_HelperBroker::removeHelper('viewRenderer');
  71. }
  72. }
  73. /**
  74. * Tears down the fixture, for example, close a network connection.
  75. * This method is called after a test is executed.
  76. *
  77. * @return void
  78. */
  79. public function tearDown()
  80. {
  81. Zend_Layout::resetMvcInstance();
  82. }
  83. public function testDefaultLayoutStatusAtInitialization()
  84. {
  85. $layout = new Zend_Layout();
  86. $this->assertEquals('layout', $layout->getLayout());
  87. $this->assertEquals('content', $layout->getContentKey());
  88. $this->assertTrue($layout->isEnabled());
  89. $this->assertTrue($layout->inflectorEnabled());
  90. $this->assertNull($layout->getLayoutPath());
  91. $this->assertFalse($layout->getMvcEnabled());
  92. }
  93. public function testDefaultLayoutStatusAtInitializationWhenInitMvcFlagPassed()
  94. {
  95. $layout = new Zend_Layout(null, true);
  96. $this->assertEquals('layout', $layout->getLayout());
  97. $this->assertEquals('content', $layout->getContentKey());
  98. $this->assertTrue($layout->isEnabled());
  99. $this->assertTrue($layout->inflectorEnabled());
  100. $this->assertNull($layout->getLayoutPath());
  101. $this->assertTrue($layout->getMvcEnabled());
  102. }
  103. /**
  104. * @return void
  105. */
  106. public function testSetConfigModifiesAttributes()
  107. {
  108. $layout = new Zend_Layout();
  109. require_once 'Zend/Config.php';
  110. $config = new Zend_Config(array(
  111. 'layout' => 'foo',
  112. 'contentKey' => 'foo',
  113. 'layoutPath' => dirname(__FILE__),
  114. 'mvcEnabled' => false,
  115. ));
  116. $layout->setConfig($config);
  117. $this->assertEquals('foo', $layout->getLayout());
  118. $this->assertEquals('foo', $layout->getContentKey());
  119. $this->assertEquals(dirname(__FILE__), $layout->getLayoutPath());
  120. $this->assertFalse($layout->getMvcEnabled());
  121. }
  122. /**
  123. * @return void
  124. */
  125. public function testSetOptionsWithConfigObjectModifiesAttributes()
  126. {
  127. $layout = new Zend_Layout();
  128. require_once 'Zend/Config.php';
  129. $config = new Zend_Config(array(
  130. 'layout' => 'foo',
  131. 'contentKey' => 'foo',
  132. 'layoutPath' => dirname(__FILE__),
  133. 'mvcEnabled' => false,
  134. ));
  135. $layout->setOptions($config);
  136. $this->assertEquals('foo', $layout->getLayout());
  137. $this->assertEquals('foo', $layout->getContentKey());
  138. $this->assertEquals(dirname(__FILE__), $layout->getLayoutPath());
  139. $this->assertFalse($layout->getMvcEnabled());
  140. }
  141. /**
  142. * @return void
  143. */
  144. public function testLayoutAccessorsModifyAndRetrieveLayoutValue()
  145. {
  146. $layout = new Zend_Layout();
  147. $layout->setLayout('foo');
  148. $this->assertEquals('foo', $layout->getLayout());
  149. }
  150. /**
  151. * @return void
  152. */
  153. public function testSetLayoutEnablesLayouts()
  154. {
  155. $layout = new Zend_Layout();
  156. $layout->disableLayout();
  157. $this->assertFalse($layout->isEnabled());
  158. $layout->setLayout('foo');
  159. $this->assertTrue($layout->isEnabled());
  160. }
  161. /**
  162. * @return void
  163. */
  164. public function testDisableLayoutDisablesLayouts()
  165. {
  166. $layout = new Zend_Layout();
  167. $this->assertTrue($layout->isEnabled());
  168. $layout->disableLayout();
  169. $this->assertFalse($layout->isEnabled());
  170. }
  171. /**
  172. * @return void
  173. */
  174. public function testEnableLayoutEnablesLayouts()
  175. {
  176. $layout = new Zend_Layout();
  177. $this->assertTrue($layout->isEnabled());
  178. $layout->disableLayout();
  179. $this->assertFalse($layout->isEnabled());
  180. $layout->enableLayout();
  181. $this->assertTrue($layout->isEnabled());
  182. }
  183. /**
  184. * @return void
  185. */
  186. public function testLayoutPathAccessorsWork()
  187. {
  188. $layout = new Zend_Layout();
  189. $layout->setLayoutPath(dirname(__FILE__));
  190. $this->assertEquals(dirname(__FILE__), $layout->getLayoutPath());
  191. }
  192. /**
  193. * @return void
  194. */
  195. public function testContentKeyAccessorsWork()
  196. {
  197. $layout = new Zend_Layout();
  198. $layout->setContentKey('foo');
  199. $this->assertEquals('foo', $layout->getContentKey());
  200. }
  201. /**
  202. * @return void
  203. */
  204. public function testMvcEnabledFlagFalseAfterStandardInstantiation()
  205. {
  206. $layout = new Zend_Layout();
  207. $this->assertFalse($layout->getMvcEnabled());
  208. }
  209. /**
  210. * @return void
  211. */
  212. public function testMvcEnabledFlagTrueWhenInstantiatedViaStartMvcMethod()
  213. {
  214. $layout = Zend_Layout::startMvc();
  215. $this->assertTrue($layout->getMvcEnabled());
  216. }
  217. /**
  218. * @return void
  219. */
  220. public function testGetViewRetrievesViewWhenNoneSet()
  221. {
  222. $layout = new Zend_Layout();
  223. $view = $layout->getView();
  224. $this->assertTrue($view instanceof Zend_View_Interface);
  225. }
  226. /**
  227. * @return void
  228. */
  229. public function testGetViewRetrievesViewFromViewRenderer()
  230. {
  231. $layout = new Zend_Layout();
  232. $view = $layout->getView();
  233. $vr = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
  234. $this->assertSame($vr->view, $view);
  235. }
  236. /**
  237. * @return void
  238. */
  239. public function testViewAccessorsAllowSettingView()
  240. {
  241. $layout = new Zend_Layout();
  242. $view = new Zend_View();
  243. $layout->setView($view);
  244. $received = $layout->getView();
  245. $this->assertSame($view, $received);
  246. }
  247. /**
  248. * @return void
  249. */
  250. public function testInflectorAccessorsWork()
  251. {
  252. $layout = new Zend_Layout();
  253. $inflector = new Zend_Filter_Inflector();
  254. $layout->setInflector($inflector);
  255. $this->assertSame($inflector, $layout->getInflector());
  256. }
  257. /**
  258. * @return void
  259. */
  260. public function testPluginClassAccessorsSetState()
  261. {
  262. $layout = new Zend_Layout();
  263. $layout->setPluginClass('Foo_Bar');
  264. $this->assertEquals('Foo_Bar', $layout->getPluginClass());
  265. }
  266. /**
  267. * @return void
  268. */
  269. public function testPluginClassPassedToStartMvcIsUsed()
  270. {
  271. $layout = Zend_Layout::startMvc(array('pluginClass' => 'Zend_Layout_LayoutTest_Controller_Plugin_Layout'));
  272. $this->assertTrue(Zend_Controller_Front::getInstance()->hasPlugin('Zend_Layout_LayoutTest_Controller_Plugin_Layout'));
  273. }
  274. /**
  275. * @return void
  276. */
  277. public function testHelperClassAccessorsSetState()
  278. {
  279. $layout = new Zend_Layout();
  280. $layout->setHelperClass('Foo_Bar');
  281. $this->assertEquals('Foo_Bar', $layout->getHelperClass());
  282. }
  283. /**
  284. * @return void
  285. */
  286. public function testHelperClassPassedToStartMvcIsUsed()
  287. {
  288. $layout = Zend_Layout::startMvc(array('helperClass' => 'Zend_Layout_LayoutTest_Controller_Action_Helper_Layout'));
  289. $this->assertTrue(Zend_Controller_Action_HelperBroker::hasHelper('layout'));
  290. $helper = Zend_Controller_Action_HelperBroker::getStaticHelper('layout');
  291. $this->assertTrue($helper instanceof Zend_Layout_LayoutTest_Controller_Action_Helper_Layout);
  292. }
  293. /**
  294. * @return void
  295. */
  296. public function testEnableInflector()
  297. {
  298. $layout = new Zend_Layout();
  299. $layout->disableInflector();
  300. $this->assertFalse($layout->inflectorEnabled());
  301. $layout->enableInflector();
  302. $this->assertTrue($layout->inflectorEnabled());
  303. }
  304. /**
  305. * @return void
  306. */
  307. public function testDisableInflector()
  308. {
  309. $layout = new Zend_Layout();
  310. $layout->disableInflector();
  311. $this->assertFalse($layout->inflectorEnabled());
  312. }
  313. /**
  314. * @return void
  315. */
  316. public function testOverloadingAccessorsWork()
  317. {
  318. $layout = new Zend_Layout();
  319. $layout->foo = 'bar';
  320. $this->assertTrue(isset($layout->foo));
  321. $this->assertEquals('bar', $layout->foo);
  322. unset($layout->foo);
  323. $this->assertFalse(isset($layout->foo));
  324. }
  325. /**
  326. * @return void
  327. */
  328. public function testAssignWithKeyValuePairPopulatesPropertyAccessibleViaOverloading()
  329. {
  330. $layout = new Zend_Layout();
  331. $layout->assign('foo', 'bar');
  332. $this->assertEquals('bar', $layout->foo);
  333. }
  334. /**
  335. * @return void
  336. */
  337. public function testAssignWithArrayPopulatesPropertiesAccessibleViaOverloading()
  338. {
  339. $layout = new Zend_Layout();
  340. $layout->assign(array(
  341. 'foo' => 'bar',
  342. 'bar' => 'baz'
  343. ));
  344. $this->assertEquals('bar', $layout->foo);
  345. $this->assertEquals('baz', $layout->bar);
  346. }
  347. /**
  348. * @return void
  349. */
  350. public function testRenderWithNoInflection()
  351. {
  352. $layout = new Zend_Layout();
  353. $view = new Zend_View();
  354. $layout->setLayoutPath(dirname(__FILE__) . '/_files/layouts')
  355. ->disableInflector()
  356. ->setLayout('layout.phtml')
  357. ->setView($view);
  358. $layout->message = 'Rendered layout';
  359. $received = $layout->render();
  360. $this->assertContains('Testing layouts:', $received);
  361. $this->assertContains($layout->message, $received);
  362. }
  363. public function testRenderWithDefaultInflection()
  364. {
  365. $layout = new Zend_Layout();
  366. $view = new Zend_View();
  367. $layout->setLayoutPath(dirname(__FILE__) . '/_files/layouts')
  368. ->setView($view);
  369. $layout->message = 'Rendered layout';
  370. $received = $layout->render();
  371. $this->assertContains('Testing layouts:', $received);
  372. $this->assertContains($layout->message, $received);
  373. }
  374. public function testRenderWithCustomInflection()
  375. {
  376. $layout = new Zend_Layout();
  377. $view = new Zend_View();
  378. $layout->setLayoutPath(dirname(__FILE__) . '/_files/layouts')
  379. ->setView($view);
  380. $inflector = $layout->getInflector();
  381. $inflector->setTarget('test/:script.:suffix')
  382. ->setStaticRule('suffix', 'php');
  383. $layout->message = 'Rendered layout';
  384. $received = $layout->render();
  385. $this->assertContains('Testing layouts with custom inflection:', $received);
  386. $this->assertContains($layout->message, $received);
  387. }
  388. public function testGetMvcInstanceReturnsNullWhenStartMvcHasNotBeenCalled()
  389. {
  390. $this->assertNull(Zend_Layout::getMvcInstance());
  391. }
  392. public function testGetMvcInstanceReturnsLayoutInstanceWhenStartMvcHasBeenCalled()
  393. {
  394. $layout = Zend_Layout::startMvc();
  395. $received = Zend_Layout::getMvcInstance();
  396. $this->assertSame($layout, $received);
  397. }
  398. public function testSubsequentCallsToStartMvcWithOptionsSetState()
  399. {
  400. $layout = Zend_Layout::startMvc();
  401. $this->assertTrue($layout->getMvcSuccessfulActionOnly());
  402. $this->assertEquals('content', $layout->getContentKey());
  403. Zend_Layout::startMvc(array(
  404. 'mvcSuccessfulActionOnly' => false,
  405. 'contentKey' => 'foobar'
  406. ));
  407. $this->assertFalse($layout->getMvcSuccessfulActionOnly());
  408. $this->assertEquals('foobar', $layout->getContentKey());
  409. }
  410. public function testGetViewSuffixRetrievesDefaultValue()
  411. {
  412. $layout = new Zend_Layout();
  413. $this->assertEquals('phtml', $layout->getViewSuffix());
  414. }
  415. public function testViewSuffixAccessorsWork()
  416. {
  417. $layout = new Zend_Layout();
  418. $layout->setViewSuffix('php');
  419. $this->assertEquals('php', $layout->getViewSuffix());
  420. }
  421. public function testSettingViewSuffixChangesInflectorSuffix()
  422. {
  423. $layout = new Zend_Layout();
  424. $inflector = $layout->getInflector();
  425. $rules = $inflector->getRules();
  426. $this->assertTrue(isset($rules['suffix']));
  427. $this->assertEquals($layout->getViewSuffix(), $rules['suffix']);
  428. $layout->setViewSuffix('php');
  429. $this->assertEquals($layout->getViewSuffix(), $rules['suffix']);
  430. }
  431. public function testGetInflectorTargetRetrievesDefaultValue()
  432. {
  433. $layout = new Zend_Layout();
  434. $this->assertEquals(':script.:suffix', $layout->getInflectorTarget());
  435. }
  436. public function testInflectorTargetAccessorsWork()
  437. {
  438. $layout = new Zend_Layout();
  439. $layout->setInflectorTarget(':script-foo.:suffix');
  440. $this->assertEquals(':script-foo.:suffix', $layout->getInflectorTarget());
  441. }
  442. public function testSettingInflectorTargetChangesInflectorSuffix()
  443. {
  444. $layout = new Zend_Layout();
  445. $inflector = $layout->getInflector();
  446. $target = $inflector->getTarget();
  447. $this->assertEquals($layout->getInflectorTarget(), $inflector->getTarget());
  448. $layout->setInflectorTarget('php');
  449. $this->assertEquals($layout->getInflectorTarget(), $inflector->getTarget());
  450. }
  451. public function testLayoutWithViewBasePath()
  452. {
  453. $layout = new Zend_Layout(array(
  454. 'viewBasePath' => dirname(__FILE__) . '/_files/layouts-basepath/')
  455. );
  456. $this->assertEquals('layout inside basePath', $layout->render());
  457. $layout->setLayout('layout2');
  458. $this->assertEquals('foobar-helper-output', $layout->render());
  459. }
  460. public function testResettingMvcInstanceUnregistersHelperAndPlugin()
  461. {
  462. $this->testGetMvcInstanceReturnsLayoutInstanceWhenStartMvcHasBeenCalled();
  463. Zend_Layout::resetMvcInstance();
  464. $front = Zend_Controller_Front::getInstance();
  465. $this->assertFalse($front->hasPlugin('Zend_Layout_Controller_Plugin_Layout'), 'Plugin not unregistered');
  466. $this->assertFalse(Zend_Controller_Action_HelperBroker::hasHelper('Layout'), 'Helper not unregistered');
  467. }
  468. public function testResettingMvcInstanceRemovesMvcSingleton()
  469. {
  470. $this->testGetMvcInstanceReturnsLayoutInstanceWhenStartMvcHasBeenCalled();
  471. Zend_Layout::resetMvcInstance();
  472. $this->assertNull(Zend_Layout::getMvcInstance());
  473. }
  474. public function testMinimalViewObjectWorks()
  475. {
  476. require_once dirname(__FILE__) . '/_files/MinimalCustomView.php';
  477. $layout = new Zend_Layout(array(
  478. 'view' => new Zend_Layout_Test_MinimalCustomView(),
  479. 'ViewScriptPath' => 'some/path'
  480. ));
  481. $layout->render();
  482. }
  483. /**
  484. * @group ZF-5152
  485. */
  486. public function testCallingStartMvcTwiceDoesntGenerateAnyUnexpectedBehavior()
  487. {
  488. Zend_Layout::startMvc('/some/path');
  489. $this->assertEquals(Zend_Layout::getMvcInstance()->getLayoutPath(),'/some/path');
  490. Zend_Layout::startMvc('/some/other/path');
  491. $this->assertEquals(Zend_Layout::getMvcInstance()->getLayoutPath(),'/some/other/path');
  492. $this->assertTrue(Zend_Layout::getMvcInstance()->isEnabled());
  493. }
  494. /**
  495. * @group ZF-5891
  496. */
  497. public function testSetLayoutWithDisabledFlag()
  498. {
  499. $layout = new Zend_Layout();
  500. $layout->disableLayout();
  501. $layout->setLayout('foo', false);
  502. $this->assertEquals('foo', $layout->getLayout());
  503. $this->assertFalse($layout->isEnabled());
  504. }
  505. }
  506. /**
  507. * Zend_Layout extension to allow resetting mvcInstance static member
  508. */
  509. class Zend_Layout_LayoutTest_Override extends Zend_Layout
  510. {
  511. public static function resetMvcInstance()
  512. {
  513. self::$_mvcInstance = null;
  514. }
  515. }
  516. class Zend_Layout_LayoutTest_Controller_Plugin_Layout extends Zend_Layout_Controller_Plugin_Layout
  517. {
  518. }
  519. class Zend_Layout_LayoutTest_Controller_Action_Helper_Layout extends Zend_Layout_Controller_Action_Helper_Layout
  520. {
  521. }
  522. // Call Zend_Layout_LayoutTest::main() if this source file is executed directly.
  523. if (PHPUnit_MAIN_METHOD == "Zend_Layout_LayoutTest::main") {
  524. Zend_Layout_LayoutTest::main();
  525. }