ViewTest.php 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  12. * @since 1.2.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View;
  16. use Cake\Cache\Cache;
  17. use Cake\Controller\Controller;
  18. use Cake\Core\App;
  19. use Cake\Core\Configure;
  20. use Cake\Core\Plugin;
  21. use Cake\Event\Event;
  22. use Cake\Event\EventListener;
  23. use Cake\Network\Request;
  24. use Cake\Routing\Router;
  25. use Cake\TestSuite\TestCase;
  26. use Cake\View\Helper;
  27. use Cake\View\View;
  28. /**
  29. * ViewPostsController class
  30. *
  31. */
  32. class ViewPostsController extends Controller {
  33. /**
  34. * name property
  35. *
  36. * @var string
  37. */
  38. public $name = 'Posts';
  39. /**
  40. * uses property
  41. *
  42. * @var mixed
  43. */
  44. public $uses = null;
  45. /**
  46. * index method
  47. *
  48. * @return void
  49. */
  50. public function index() {
  51. $this->set(array(
  52. 'testData' => 'Some test data',
  53. 'test2' => 'more data',
  54. 'test3' => 'even more data',
  55. ));
  56. }
  57. /**
  58. * nocache_tags_with_element method
  59. *
  60. * @return void
  61. */
  62. public function nocache_multiple_element() {
  63. $this->set('foo', 'this is foo var');
  64. $this->set('bar', 'this is bar var');
  65. }
  66. }
  67. /**
  68. * ThemePostsController class
  69. *
  70. */
  71. class ThemePostsController extends Controller {
  72. public $theme = null;
  73. /**
  74. * index method
  75. *
  76. * @return void
  77. */
  78. public function index() {
  79. $this->set('testData', 'Some test data');
  80. $test2 = 'more data';
  81. $test3 = 'even more data';
  82. $this->set(compact('test2', 'test3'));
  83. }
  84. }
  85. /**
  86. * TestThemeView class
  87. *
  88. */
  89. class TestThemeView extends View {
  90. /**
  91. * getViewFileName method
  92. *
  93. * @param string $name Controller action to find template filename for
  94. * @return string Template filename
  95. */
  96. public function getViewFileName($name = null) {
  97. return $this->_getViewFileName($name);
  98. }
  99. /**
  100. * getLayoutFileName method
  101. *
  102. * @param string $name The name of the layout to find.
  103. * @return string Filename for layout file (.ctp).
  104. */
  105. public function getLayoutFileName($name = null) {
  106. return $this->_getLayoutFileName($name);
  107. }
  108. }
  109. /**
  110. * TestView class
  111. *
  112. */
  113. class TestView extends View {
  114. /**
  115. * getViewFileName method
  116. *
  117. * @param string $name Controller action to find template filename for
  118. * @return string Template filename
  119. */
  120. public function getViewFileName($name = null) {
  121. return $this->_getViewFileName($name);
  122. }
  123. /**
  124. * getLayoutFileName method
  125. *
  126. * @param string $name The name of the layout to find.
  127. * @return string Filename for layout file (.ctp).
  128. */
  129. public function getLayoutFileName($name = null) {
  130. return $this->_getLayoutFileName($name);
  131. }
  132. /**
  133. * paths method
  134. *
  135. * @param string $plugin Optional plugin name to scan for view files.
  136. * @param bool $cached Set to true to force a refresh of view paths.
  137. * @return array paths
  138. */
  139. public function paths($plugin = null, $cached = true) {
  140. return $this->_paths($plugin, $cached);
  141. }
  142. /**
  143. * Test only function to return instance scripts.
  144. *
  145. * @return array Scripts
  146. */
  147. public function scripts() {
  148. return $this->_scripts;
  149. }
  150. }
  151. /**
  152. * TestBeforeAfterHelper class
  153. *
  154. */
  155. class TestBeforeAfterHelper extends Helper {
  156. /**
  157. * property property
  158. *
  159. * @var string
  160. */
  161. public $property = '';
  162. /**
  163. * beforeLayout method
  164. *
  165. * @param string $viewFile View file name.
  166. * @return void
  167. */
  168. public function beforeLayout($viewFile) {
  169. $this->property = 'Valuation';
  170. }
  171. /**
  172. * afterLayout method
  173. *
  174. * @param string $layoutFile Layout file name.
  175. * @return void
  176. */
  177. public function afterLayout($layoutFile) {
  178. $this->_View->append('content', 'modified in the afterlife');
  179. }
  180. }
  181. /**
  182. * Class TestObjectWithToString
  183. *
  184. * An object with the magic method __toString() for testing with view blocks.
  185. */
  186. class TestObjectWithToString {
  187. /**
  188. * Return string value.
  189. *
  190. * @return string
  191. */
  192. public function __toString() {
  193. return "I'm ObjectWithToString";
  194. }
  195. }
  196. /**
  197. * Class TestObjectWithoutToString
  198. *
  199. * An object without the magic method __toString() for testing with view blocks.
  200. */
  201. class TestObjectWithoutToString {
  202. }
  203. /**
  204. * Class TestViewEventListener
  205. *
  206. * An event listener to test cakePHP events
  207. */
  208. class TestViewEventListener implements EventListener {
  209. /**
  210. * type of view before rendering has occurred
  211. *
  212. * @var string
  213. */
  214. public $beforeRenderViewType;
  215. /**
  216. * type of view after rendering has occurred
  217. *
  218. * @var string
  219. */
  220. public $afterRenderViewType;
  221. /**
  222. * implementedEvents method
  223. *
  224. * @return array
  225. */
  226. public function implementedEvents() {
  227. return array(
  228. 'View.beforeRender' => 'beforeRender',
  229. 'View.afterRender' => 'afterRender'
  230. );
  231. }
  232. /**
  233. * beforeRender method
  234. *
  235. * @param \Cake\Event\Event $event the event being sent
  236. * @return void
  237. */
  238. public function beforeRender(Event $event) {
  239. $this->beforeRenderViewType = $event->subject()->getCurrentType();
  240. }
  241. /**
  242. * afterRender method
  243. *
  244. * @param \Cake\Event\Event $event the event being sent
  245. * @return void
  246. */
  247. public function afterRender(Event $event) {
  248. $this->afterRenderViewType = $event->subject()->getCurrentType();
  249. }
  250. }
  251. /**
  252. * ViewTest class
  253. *
  254. */
  255. class ViewTest extends TestCase {
  256. /**
  257. * Fixtures used in this test.
  258. *
  259. * @var array
  260. */
  261. public $fixtures = array('core.user', 'core.post');
  262. /**
  263. * setUp method
  264. *
  265. * @return void
  266. */
  267. public function setUp() {
  268. parent::setUp();
  269. $request = new Request();
  270. $this->Controller = new Controller($request);
  271. $this->PostsController = new ViewPostsController($request);
  272. $this->PostsController->viewPath = 'Posts';
  273. $this->PostsController->index();
  274. $this->View = $this->PostsController->createView();
  275. $themeRequest = new Request('posts/index');
  276. $this->ThemeController = new Controller($themeRequest);
  277. $this->ThemePostsController = new ThemePostsController($themeRequest);
  278. $this->ThemePostsController->viewPath = 'Posts';
  279. $this->ThemePostsController->index();
  280. $this->ThemeView = $this->ThemePostsController->createView();
  281. App::objects('Plugin', null, false);
  282. Plugin::load(['TestPlugin', 'PluginJs', 'TestTheme', 'Company/TestPluginThree']);
  283. Configure::write('debug', true);
  284. }
  285. /**
  286. * tearDown method
  287. *
  288. * @return void
  289. */
  290. public function tearDown() {
  291. parent::tearDown();
  292. Plugin::unload();
  293. unset($this->View);
  294. unset($this->PostsController);
  295. unset($this->Controller);
  296. unset($this->ThemeView);
  297. unset($this->ThemePostsController);
  298. unset($this->ThemeController);
  299. }
  300. /**
  301. * Test getViewFileName method
  302. *
  303. * @return void
  304. */
  305. public function testGetTemplate() {
  306. $request = $this->getMock('Cake\Network\Request');
  307. $response = $this->getMock('Cake\Network\Response');
  308. $viewOptions = [
  309. 'plugin' => null,
  310. 'name' => 'Pages',
  311. 'viewPath' => 'Pages'
  312. ];
  313. $request->action = 'display';
  314. $request->params['pass'] = array('home');
  315. $ThemeView = new TestThemeView(null, null, null, $viewOptions);
  316. $ThemeView->theme = 'TestTheme';
  317. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Pages' . DS . 'home.ctp';
  318. $result = $ThemeView->getViewFileName('home');
  319. $this->assertPathEquals($expected, $result);
  320. $expected = Plugin::path('TestTheme') . 'src' . DS . 'Template' . DS . 'Posts' . DS . 'index.ctp';
  321. $result = $ThemeView->getViewFileName('/Posts/index');
  322. $this->assertPathEquals($expected, $result);
  323. $expected = Plugin::path('TestTheme') . 'src' . DS . 'Template' . DS . 'Layout' . DS . 'default.ctp';
  324. $result = $ThemeView->getLayoutFileName();
  325. $this->assertPathEquals($expected, $result);
  326. $ThemeView->layoutPath = 'rss';
  327. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Layout' . DS . 'rss' . DS . 'default.ctp';
  328. $result = $ThemeView->getLayoutFileName();
  329. $this->assertPathEquals($expected, $result);
  330. $ThemeView->layoutPath = 'Email' . DS . 'html';
  331. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Layout' . DS . 'Email' . DS . 'html' . DS . 'default.ctp';
  332. $result = $ThemeView->getLayoutFileName();
  333. $this->assertPathEquals($expected, $result);
  334. $ThemeView = new TestThemeView(null, null, null, $viewOptions);
  335. $ThemeView->theme = 'Company/TestPluginThree';
  336. $expected = Plugin::path('Company/TestPluginThree') . 'src' . DS . 'Template' . DS . 'Layout' . DS . 'default.ctp';
  337. $result = $ThemeView->getLayoutFileName();
  338. $this->assertPathEquals($expected, $result);
  339. }
  340. /**
  341. * Test getLayoutFileName method on plugin
  342. *
  343. * @return void
  344. */
  345. public function testPluginGetTemplate() {
  346. $viewOptions = ['plugin' => 'TestPlugin',
  347. 'name' => 'TestPlugin',
  348. 'viewPath' => 'Tests',
  349. 'view' => 'index'
  350. ];
  351. $View = new TestView(null, null, null, $viewOptions);
  352. $expected = Plugin::path('TestPlugin') . 'src' . DS . 'Template' . DS . 'Tests' . DS . 'index.ctp';
  353. $result = $View->getViewFileName('index');
  354. $this->assertEquals($expected, $result);
  355. $expected = Plugin::path('TestPlugin') . 'src' . DS . 'Template' . DS . 'Layout' . DS . 'default.ctp';
  356. $result = $View->getLayoutFileName();
  357. $this->assertEquals($expected, $result);
  358. }
  359. /**
  360. * Test getViewFileName method on plugin
  361. *
  362. * @return void
  363. */
  364. public function testPluginThemedGetTemplate() {
  365. $viewOptions = ['plugin' => 'TestPlugin',
  366. 'name' => 'TestPlugin',
  367. 'viewPath' => 'Tests',
  368. 'view' => 'index',
  369. 'theme' => 'TestTheme'
  370. ];
  371. $ThemeView = new TestThemeView(null, null, null, $viewOptions);
  372. $themePath = Plugin::path('TestTheme') . 'src' . DS . 'Template' . DS;
  373. $expected = $themePath . 'Plugin' . DS . 'TestPlugin' . DS . 'Tests' . DS . 'index.ctp';
  374. $result = $ThemeView->getViewFileName('index');
  375. $this->assertPathEquals($expected, $result);
  376. $expected = $themePath . 'Plugin' . DS . 'TestPlugin' . DS . 'Layout' . DS . 'plugin_default.ctp';
  377. $result = $ThemeView->getLayoutFileName('plugin_default');
  378. $this->assertPathEquals($expected, $result);
  379. $expected = $themePath . 'Layout' . DS . 'default.ctp';
  380. $result = $ThemeView->getLayoutFileName('default');
  381. $this->assertPathEquals($expected, $result);
  382. }
  383. /**
  384. * Test that plugin/$plugin_name is only appended to the paths it should be.
  385. *
  386. * @return void
  387. */
  388. public function testPluginPathGeneration() {
  389. $viewOptions = ['plugin' => 'TestPlugin',
  390. 'name' => 'TestPlugin',
  391. 'viewPath' => 'Tests',
  392. 'view' => 'index'
  393. ];
  394. $View = new TestView(null, null, null, $viewOptions);
  395. $paths = $View->paths();
  396. $expected = array_merge(App::path('Template'), App::core('Template'));
  397. $this->assertEquals($expected, $paths);
  398. $paths = $View->paths('TestPlugin');
  399. $pluginPath = Plugin::path('TestPlugin');
  400. $expected = array(
  401. TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Plugin' . DS . 'TestPlugin' . DS,
  402. $pluginPath . 'src' . DS . 'Template' . DS,
  403. TEST_APP . 'TestApp' . DS . 'Template' . DS,
  404. CAKE . 'Template' . DS,
  405. );
  406. $this->assertPathEquals($expected, $paths);
  407. }
  408. /**
  409. * Test that CamelCase'd plugins still find their view files.
  410. *
  411. * @return void
  412. */
  413. public function testCamelCasePluginGetTemplate() {
  414. $viewOptions = ['plugin' => 'TestPlugin',
  415. 'name' => 'TestPlugin',
  416. 'viewPath' => 'Tests',
  417. 'view' => 'index'
  418. ];
  419. $View = new TestView(null, null, null, $viewOptions);
  420. $pluginPath = Plugin::path('TestPlugin');
  421. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'src' . DS .
  422. 'Template' . DS . 'Tests' . DS . 'index.ctp';
  423. $result = $View->getViewFileName('index');
  424. $this->assertPathEquals($expected, $result);
  425. $expected = $pluginPath . 'src' . DS . 'Template' . DS . 'Layout' . DS . 'default.ctp';
  426. $result = $View->getLayoutFileName();
  427. $this->assertPathEquals($expected, $result);
  428. }
  429. /**
  430. * Test getViewFileName method
  431. *
  432. * @return void
  433. */
  434. public function testGetViewFileNames() {
  435. $viewOptions = ['plugin' => null,
  436. 'name' => 'Pages',
  437. 'viewPath' => 'Pages'
  438. ];
  439. $request = $this->getMock('Cake\Network\Request');
  440. $response = $this->getMock('Cake\Network\Response');
  441. $View = new TestView(null, null, null, $viewOptions);
  442. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Pages' . DS . 'home.ctp';
  443. $result = $View->getViewFileName('home');
  444. $this->assertPathEquals($expected, $result);
  445. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Posts' . DS . 'index.ctp';
  446. $result = $View->getViewFileName('/Posts/index');
  447. $this->assertPathEquals($expected, $result);
  448. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Posts' . DS . 'index.ctp';
  449. $result = $View->getViewFileName('../Posts/index');
  450. $this->assertPathEquals($expected, $result);
  451. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Pages' . DS . 'page.home.ctp';
  452. $result = $View->getViewFileName('page.home');
  453. $this->assertPathEquals($expected, $result, 'Should not ruin files with dots.');
  454. Plugin::load('TestPlugin');
  455. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Pages' . DS . 'home.ctp';
  456. $result = $View->getViewFileName('TestPlugin.home');
  457. $this->assertPathEquals($expected, $result, 'Plugin is missing the view, cascade to app.');
  458. $View->viewPath = 'Tests';
  459. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'src' . DS .
  460. 'Template' . DS . 'Tests' . DS . 'index.ctp';
  461. $result = $View->getViewFileName('TestPlugin.index');
  462. $this->assertPathEquals($expected, $result);
  463. }
  464. /**
  465. * Test getting layout filenames
  466. *
  467. * @return void
  468. */
  469. public function testGetLayoutFileName() {
  470. $viewOptions = ['plugin' => null,
  471. 'name' => 'Pages',
  472. 'viewPath' => 'Pages',
  473. 'action' => 'display'
  474. ];
  475. $View = new TestView(null, null, null, $viewOptions);
  476. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Layout' . DS . 'default.ctp';
  477. $result = $View->getLayoutFileName();
  478. $this->assertPathEquals($expected, $result);
  479. $View->layoutPath = 'rss';
  480. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Layout' . DS . 'rss' . DS . 'default.ctp';
  481. $result = $View->getLayoutFileName();
  482. $this->assertPathEquals($expected, $result);
  483. $View->layoutPath = 'Email' . DS . 'html';
  484. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Layout' . DS . 'Email' . DS . 'html' . DS . 'default.ctp';
  485. $result = $View->getLayoutFileName();
  486. $this->assertPathEquals($expected, $result);
  487. }
  488. /**
  489. * Test getting layout filenames for plugins.
  490. *
  491. * @return void
  492. */
  493. public function testGetLayoutFileNamePlugin() {
  494. $viewOptions = ['plugin' => null,
  495. 'name' => 'Pages',
  496. 'viewPath' => 'Pages',
  497. 'action' => 'display'
  498. ];
  499. $View = new TestView(null, null, null, $viewOptions);
  500. Plugin::load('TestPlugin');
  501. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'src' . DS .
  502. 'Template' . DS . 'Layout' . DS . 'default.ctp';
  503. $result = $View->getLayoutFileName('TestPlugin.default');
  504. $this->assertPathEquals($expected, $result);
  505. $View->plugin = 'TestPlugin';
  506. $expected = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS . 'src' . DS .
  507. 'Template' . DS . 'Layout' . DS . 'default.ctp';
  508. $result = $View->getLayoutFileName('default');
  509. $this->assertPathEquals($expected, $result);
  510. }
  511. /**
  512. * Test for missing views
  513. *
  514. * @expectedException \Cake\View\Error\MissingViewException
  515. * @return void
  516. */
  517. public function testMissingView() {
  518. $viewOptions = ['plugin' => null,
  519. 'name' => 'Pages',
  520. 'viewPath' => 'Pages'
  521. ];
  522. $request = $this->getMock('Cake\Network\Request');
  523. $response = $this->getMock('Cake\Network\Response');
  524. $View = new TestView($request, $response, null, $viewOptions);
  525. $View->getViewFileName('does_not_exist');
  526. }
  527. /**
  528. * Test for missing layouts
  529. *
  530. * @expectedException \Cake\View\Error\MissingLayoutException
  531. * @return void
  532. */
  533. public function testMissingLayout() {
  534. $viewOptions = ['plugin' => null,
  535. 'name' => 'Pages',
  536. 'viewPath' => 'Pages',
  537. 'layout' => 'whatever'
  538. ];
  539. $View = new TestView(null, null, null, $viewOptions);
  540. $View->getLayoutFileName();
  541. }
  542. /**
  543. * Test viewVars method
  544. *
  545. * @return void
  546. */
  547. public function testViewVars() {
  548. $this->assertEquals(array('testData' => 'Some test data', 'test2' => 'more data', 'test3' => 'even more data'), $this->View->viewVars);
  549. }
  550. /**
  551. * Test generation of UUIDs method
  552. *
  553. * @return void
  554. */
  555. public function testUUIDGeneration() {
  556. Router::connect('/:controller', ['action' => 'index']);
  557. $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
  558. $this->assertEquals('form5988016017', $result);
  559. $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
  560. $this->assertEquals('formc3dc6be854', $result);
  561. $result = $this->View->uuid('form', array('controller' => 'posts', 'action' => 'index'));
  562. $this->assertEquals('form28f92cc87f', $result);
  563. }
  564. /**
  565. * Test elementExists method
  566. *
  567. * @return void
  568. */
  569. public function testElementExists() {
  570. $result = $this->View->elementExists('test_element');
  571. $this->assertTrue($result);
  572. $result = $this->View->elementExists('TestPlugin.plugin_element');
  573. $this->assertTrue($result);
  574. $result = $this->View->elementExists('non_existent_element');
  575. $this->assertFalse($result);
  576. $result = $this->View->elementExists('TestPlugin.element');
  577. $this->assertFalse($result);
  578. $this->View->plugin = 'TestPlugin';
  579. $result = $this->View->elementExists('test_plugin_element');
  580. $this->assertTrue($result);
  581. }
  582. /**
  583. * Test element method
  584. *
  585. * @return void
  586. */
  587. public function testElement() {
  588. $result = $this->View->element('test_element');
  589. $this->assertEquals('this is the test element', $result);
  590. $result = $this->View->element('TestPlugin.plugin_element');
  591. $this->assertEquals('this is the plugin element using params[plugin]', $result);
  592. $this->View->plugin = 'TestPlugin';
  593. $result = $this->View->element('test_plugin_element');
  594. $this->assertEquals('this is the test set using View::$plugin plugin element', $result);
  595. }
  596. /**
  597. * Test elementInexistent method
  598. *
  599. * @expectedException Cake\View\Error\MissingElementException
  600. * @return void
  601. */
  602. public function testElementInexistent() {
  603. $this->View->element('non_existent_element');
  604. }
  605. /**
  606. * Test elementInexistent3 method
  607. *
  608. * @expectedException Cake\View\Error\MissingElementException
  609. * @return void
  610. */
  611. public function testElementInexistent3() {
  612. $this->View->element('test_plugin.plugin_element');
  613. }
  614. /**
  615. * Test that elements can have callbacks
  616. *
  617. * @return void
  618. */
  619. public function testElementCallbacks() {
  620. $count = 0;
  621. $callback = function ($event, $file) use (&$count) {
  622. $count++;
  623. };
  624. $events = $this->View->eventManager();
  625. $events->attach($callback, 'View.beforeRender');
  626. $events->attach($callback, 'View.afterRender');
  627. $this->View->element('test_element', array(), array('callbacks' => true));
  628. $this->assertEquals(2, $count);
  629. }
  630. /**
  631. * Test that additional element viewVars don't get overwritten with helpers.
  632. *
  633. * @return void
  634. */
  635. public function testElementParamsDontOverwriteHelpers() {
  636. $Controller = new ViewPostsController();
  637. $Controller->helpers = array('Form');
  638. $View = $Controller->createView();
  639. $result = $View->element('type_check', array('form' => 'string'), array('callbacks' => true));
  640. $this->assertEquals('string', $result);
  641. $View->set('form', 'string');
  642. $result = $View->element('type_check', array(), array('callbacks' => true));
  643. $this->assertEquals('string', $result);
  644. }
  645. /**
  646. * Test elementCacheHelperNoCache method
  647. *
  648. * @return void
  649. */
  650. public function testElementCacheHelperNoCache() {
  651. $Controller = new ViewPostsController();
  652. $View = $Controller->createView();
  653. $View->loadHelpers();
  654. $result = $View->element('test_element', array('ram' => 'val', 'test' => array('foo', 'bar')));
  655. $this->assertEquals('this is the test element', $result);
  656. }
  657. /**
  658. * Test elementCache method
  659. *
  660. * @return void
  661. */
  662. public function testElementCache() {
  663. Cache::drop('test_view');
  664. Cache::config('test_view', [
  665. 'engine' => 'File',
  666. 'duration' => '+1 day',
  667. 'path' => CACHE . 'views/',
  668. 'prefix' => ''
  669. ]);
  670. Cache::clear(true, 'test_view');
  671. $View = $this->PostsController->createView();
  672. $View->elementCache = 'test_view';
  673. $result = $View->element('test_element', array(), array('cache' => true));
  674. $expected = 'this is the test element';
  675. $this->assertEquals($expected, $result);
  676. $result = Cache::read('element__test_element_cache_callbacks', 'test_view');
  677. $this->assertEquals($expected, $result);
  678. $result = $View->element('test_element', array('param' => 'one', 'foo' => 'two'), array('cache' => true));
  679. $this->assertEquals($expected, $result);
  680. $result = Cache::read('element__test_element_cache_callbacks_param_foo', 'test_view');
  681. $this->assertEquals($expected, $result);
  682. $View->element('test_element', array(
  683. 'param' => 'one',
  684. 'foo' => 'two'
  685. ), array(
  686. 'cache' => array('key' => 'custom_key')
  687. ));
  688. $result = Cache::read('element_custom_key', 'test_view');
  689. $this->assertEquals($expected, $result);
  690. $View->elementCache = 'default';
  691. $View->element('test_element', array(
  692. 'param' => 'one',
  693. 'foo' => 'two'
  694. ), array(
  695. 'cache' => array('config' => 'test_view'),
  696. ));
  697. $result = Cache::read('element__test_element_cache_callbacks_param_foo', 'test_view');
  698. $this->assertEquals($expected, $result);
  699. Cache::clear(true, 'test_view');
  700. Cache::drop('test_view');
  701. }
  702. /**
  703. * Test element events
  704. *
  705. * @return void
  706. */
  707. public function testViewEvent() {
  708. $View = $this->PostsController->createView();
  709. $View->autoLayout = false;
  710. $listener = new TestViewEventListener();
  711. $View->eventManager()->attach($listener);
  712. $View->render('index');
  713. $this->assertEquals(View::TYPE_VIEW, $listener->beforeRenderViewType);
  714. $this->assertEquals(View::TYPE_VIEW, $listener->afterRenderViewType);
  715. $this->assertEquals($View->getCurrentType(), View::TYPE_VIEW);
  716. $View->element('test_element', array(), array('callbacks' => true));
  717. $this->assertEquals($View->getCurrentType(), View::TYPE_VIEW);
  718. $this->assertEquals(View::TYPE_ELEMENT, $listener->beforeRenderViewType);
  719. $this->assertEquals(View::TYPE_ELEMENT, $listener->afterRenderViewType);
  720. }
  721. /**
  722. * Test __get allowing access to helpers.
  723. *
  724. * @return void
  725. */
  726. public function testMagicGetAndAddHelper() {
  727. $View = new View();
  728. $View->addHelper('Html');
  729. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html);
  730. }
  731. /**
  732. * Test loadHelpers method
  733. *
  734. * @return void
  735. */
  736. public function testLoadHelpers() {
  737. $View = new View();
  738. $View->helpers = array('Html', 'Form');
  739. $View->loadHelpers();
  740. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html, 'Object type is wrong.');
  741. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $View->Form, 'Object type is wrong.');
  742. }
  743. /**
  744. * Test lazy loading helpers
  745. *
  746. * @return void
  747. */
  748. public function testLazyLoadHelpers() {
  749. $View = new View();
  750. $View->helpers = array();
  751. $this->assertInstanceOf('Cake\View\Helper\HtmlHelper', $View->Html, 'Object type is wrong.');
  752. $this->assertInstanceOf('Cake\View\Helper\FormHelper', $View->Form, 'Object type is wrong.');
  753. }
  754. /**
  755. * Test the correct triggering of helper callbacks
  756. *
  757. * @return void
  758. */
  759. public function testHelperCallbackTriggering() {
  760. $View = $this->PostsController->createView();
  761. $manager = $this->getMock('Cake\Event\EventManager');
  762. $View->eventManager($manager);
  763. $manager->expects($this->at(0))->method('dispatch')
  764. ->with(
  765. $this->logicalAnd(
  766. $this->isInstanceOf('Cake\Event\Event'),
  767. $this->attributeEqualTo('_name', 'View.beforeRender'),
  768. $this->attributeEqualTo('_subject', $View)
  769. )
  770. );
  771. $manager->expects($this->at(1))->method('dispatch')
  772. ->with(
  773. $this->logicalAnd(
  774. $this->isInstanceOf('Cake\Event\Event'),
  775. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  776. $this->attributeEqualTo('_subject', $View)
  777. )
  778. );
  779. $manager->expects($this->at(2))->method('dispatch')
  780. ->with(
  781. $this->logicalAnd(
  782. $this->isInstanceOf('Cake\Event\Event'),
  783. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  784. $this->attributeEqualTo('_subject', $View)
  785. )
  786. );
  787. $manager->expects($this->at(3))->method('dispatch')
  788. ->with(
  789. $this->logicalAnd(
  790. $this->isInstanceOf('Cake\Event\Event'),
  791. $this->attributeEqualTo('_name', 'View.afterRender'),
  792. $this->attributeEqualTo('_subject', $View)
  793. )
  794. );
  795. $manager->expects($this->at(4))->method('dispatch')
  796. ->with(
  797. $this->logicalAnd(
  798. $this->isInstanceOf('Cake\Event\Event'),
  799. $this->attributeEqualTo('_name', 'View.beforeLayout'),
  800. $this->attributeEqualTo('_subject', $View)
  801. )
  802. );
  803. $manager->expects($this->at(5))->method('dispatch')
  804. ->with(
  805. $this->logicalAnd(
  806. $this->isInstanceOf('Cake\Event\Event'),
  807. $this->attributeEqualTo('_name', 'View.beforeRenderFile'),
  808. $this->attributeEqualTo('_subject', $View)
  809. )
  810. );
  811. $manager->expects($this->at(6))->method('dispatch')
  812. ->with(
  813. $this->logicalAnd(
  814. $this->isInstanceOf('Cake\Event\Event'),
  815. $this->attributeEqualTo('_name', 'View.afterRenderFile'),
  816. $this->attributeEqualTo('_subject', $View)
  817. )
  818. );
  819. $manager->expects($this->at(7))->method('dispatch')
  820. ->with(
  821. $this->logicalAnd(
  822. $this->isInstanceOf('Cake\Event\Event'),
  823. $this->attributeEqualTo('_name', 'View.afterLayout'),
  824. $this->attributeEqualTo('_subject', $View)
  825. )
  826. );
  827. $View->render('index');
  828. }
  829. /**
  830. * Test beforeLayout method
  831. *
  832. * @return void
  833. */
  834. public function testBeforeLayout() {
  835. $this->PostsController->helpers = array(
  836. 'Session',
  837. 'TestBeforeAfter' => array('className' => __NAMESPACE__ . '\TestBeforeAfterHelper'),
  838. 'Html'
  839. );
  840. $View = $this->PostsController->createView();
  841. $View->render('index');
  842. $this->assertEquals('Valuation', $View->helpers()->TestBeforeAfter->property);
  843. }
  844. /**
  845. * Test afterLayout method
  846. *
  847. * @return void
  848. */
  849. public function testAfterLayout() {
  850. $this->PostsController->helpers = array(
  851. 'Session',
  852. 'TestBeforeAfter' => array('className' => __NAMESPACE__ . '\TestBeforeAfterHelper'),
  853. 'Html'
  854. );
  855. $this->PostsController->set('variable', 'values');
  856. $View = $this->PostsController->createView();
  857. $content = 'This is my view output';
  858. $result = $View->renderLayout($content, 'default');
  859. $this->assertRegExp('/modified in the afterlife/', $result);
  860. $this->assertRegExp('/This is my view output/', $result);
  861. }
  862. /**
  863. * Test renderLoadHelper method
  864. *
  865. * @return void
  866. */
  867. public function testRenderLoadHelper() {
  868. $this->PostsController->helpers = array('Session', 'Html', 'Form', 'Number');
  869. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  870. $result = $View->render('index', false);
  871. $this->assertEquals('posts index', $result);
  872. $attached = $View->helpers()->loaded();
  873. $this->assertEquals(array('Session', 'Html', 'Form', 'Number'), $attached);
  874. $this->PostsController->helpers = array('Html', 'Form', 'Number', 'TestPlugin.PluggedHelper');
  875. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  876. $result = $View->render('index', false);
  877. $this->assertEquals('posts index', $result);
  878. $attached = $View->helpers()->loaded();
  879. $expected = array('Html', 'Form', 'Number', 'PluggedHelper');
  880. $this->assertEquals($expected, $attached, 'Attached helpers are wrong.');
  881. }
  882. /**
  883. * Test render method
  884. *
  885. * @return void
  886. */
  887. public function testRender() {
  888. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  889. $result = $View->render('index');
  890. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result);
  891. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  892. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  893. $this->assertTrue(isset($View->viewVars['content_for_layout']), 'content_for_layout should be a view var');
  894. $this->assertTrue(isset($View->viewVars['scripts_for_layout']), 'scripts_for_layout should be a view var');
  895. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  896. $result = $View->render(false, 'ajax2');
  897. $this->assertRegExp('/Ajax\!/', $result);
  898. $this->assertNull($View->render(false, 'ajax2'));
  899. $this->PostsController->helpers = array('Session', 'Cache', 'Html');
  900. $this->PostsController->constructClasses();
  901. $this->PostsController->cacheAction = array('index' => 3600);
  902. $this->PostsController->request->params['action'] = 'index';
  903. Configure::write('Cache.check', true);
  904. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  905. $result = $View->render('index');
  906. $this->assertRegExp("/<meta http-equiv=\"Content-Type\" content=\"text\/html; charset=utf-8\" \/>\s*<title>/", $result);
  907. $this->assertRegExp("/<div id=\"content\">\s*posts index\s*<\/div>/", $result);
  908. }
  909. /**
  910. * Test that View::$view works
  911. *
  912. * @return void
  913. */
  914. public function testRenderUsingViewProperty() {
  915. $this->PostsController->view = 'cache_form';
  916. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  917. $this->assertEquals('cache_form', $View->view);
  918. $result = $View->render();
  919. $this->assertRegExp('/Add User/', $result);
  920. }
  921. /**
  922. * Test render()ing a file in a subdir from a custom viewPath
  923. * in a plugin.
  924. *
  925. * @return void
  926. */
  927. public function testGetViewFileNameSubdirWithPluginAndViewPath() {
  928. $this->PostsController->plugin = 'TestPlugin';
  929. $this->PostsController->name = 'Posts';
  930. $this->PostsController->viewPath = 'Element';
  931. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  932. $pluginPath = TEST_APP . 'Plugin' . DS . 'TestPlugin' . DS;
  933. $result = $View->getViewFileName('sub_dir/sub_element');
  934. $expected = $pluginPath . 'src' . DS . 'Template' . DS . 'Element' . DS . 'sub_dir' . DS . 'sub_element.ctp';
  935. $this->assertPathEquals($expected, $result);
  936. }
  937. /**
  938. * Test that view vars can replace the local helper variables
  939. * and not overwrite the $this->Helper references
  940. *
  941. * @return void
  942. */
  943. public function testViewVarOverwritingLocalHelperVar() {
  944. $Controller = new ViewPostsController();
  945. $Controller->helpers = array('Session', 'Html');
  946. $Controller->set('html', 'I am some test html');
  947. $View = $Controller->createView();
  948. $result = $View->render('helper_overwrite', false);
  949. $this->assertRegExp('/I am some test html/', $result);
  950. $this->assertRegExp('/Test link/', $result);
  951. }
  952. /**
  953. * Test getViewFileName method
  954. *
  955. * @return void
  956. */
  957. public function testViewFileName() {
  958. $View = $this->PostsController->createView('Cake\Test\TestCase\View\TestView');
  959. $result = $View->getViewFileName('index');
  960. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  961. $result = $View->getViewFileName('TestPlugin.index');
  962. $this->assertRegExp('/Posts(\/|\\\)index.ctp/', $result);
  963. $result = $View->getViewFileName('/Pages/home');
  964. $this->assertRegExp('/Pages(\/|\\\)home.ctp/', $result);
  965. $result = $View->getViewFileName('../Element/test_element');
  966. $this->assertRegExp('/Element(\/|\\\)test_element.ctp/', $result);
  967. $expected = TEST_APP . 'TestApp' . DS . 'Template' . DS . 'Posts' . DS . 'index.ctp';
  968. $result = $View->getViewFileName('../Posts/index');
  969. $this->assertPathEquals($expected, $result);
  970. }
  971. /**
  972. * Test renderCache method
  973. *
  974. * @return void
  975. */
  976. public function testRenderCache() {
  977. $this->skipIf(!is_writable(CACHE . 'views/'), 'CACHE/views dir is not writable, cannot test renderCache.');
  978. $view = 'test_view';
  979. $View = $this->PostsController->createView();
  980. $path = CACHE . 'views/view_cache_' . $view;
  981. $cacheText = '<!--cachetime:' . time() . '-->some cacheText';
  982. $f = fopen($path, 'w+');
  983. fwrite($f, $cacheText);
  984. fclose($f);
  985. $result = $View->renderCache($path, '+1 second');
  986. $this->assertFalse($result);
  987. if (file_exists($path)) {
  988. unlink($path);
  989. }
  990. $cacheText = '<!--cachetime:' . (time() + 10) . '-->some cacheText';
  991. $f = fopen($path, 'w+');
  992. fwrite($f, $cacheText);
  993. fclose($f);
  994. $result = $View->renderCache($path, '+1 second');
  995. $this->assertRegExp('/^some cacheText/', $result);
  996. if (file_exists($path)) {
  997. unlink($path);
  998. }
  999. }
  1000. /**
  1001. * Test that render() will remove the cake:nocache tags when only the cachehelper is present.
  1002. *
  1003. * @return void
  1004. */
  1005. public function testRenderStrippingNoCacheTagsOnlyCacheHelper() {
  1006. Configure::write('Cache.check', false);
  1007. $View = $this->PostsController->createView();
  1008. $View->set(array('superman' => 'clark', 'variable' => 'var'));
  1009. $View->helpers = array('Html', 'Form', 'Cache');
  1010. $View->layout = 'cache_layout';
  1011. $result = $View->render('index');
  1012. $this->assertNotRegExp('/cake:nocache/', $result);
  1013. }
  1014. /**
  1015. * Test that render() will remove the cake:nocache tags when only the Cache.check is true.
  1016. *
  1017. * @return void
  1018. */
  1019. public function testRenderStrippingNoCacheTagsOnlyCacheCheck() {
  1020. Configure::write('Cache.check', true);
  1021. $View = $this->PostsController->createView();
  1022. $View->set(array('superman' => 'clark', 'variable' => 'var'));
  1023. $View->helpers = array('Html', 'Form');
  1024. $View->layout = 'cache_layout';
  1025. $result = $View->render('index');
  1026. $this->assertNotRegExp('/cake:nocache/', $result);
  1027. }
  1028. /**
  1029. * Test creating a block with capturing output.
  1030. *
  1031. * @return void
  1032. */
  1033. public function testBlockCaptureOverwrite() {
  1034. $this->View->start('test');
  1035. echo 'Block content';
  1036. $this->View->end();
  1037. $this->View->start('test');
  1038. echo 'New content';
  1039. $this->View->end();
  1040. $result = $this->View->fetch('test');
  1041. $this->assertEquals('New content', $result);
  1042. }
  1043. /**
  1044. * Test that blocks can be fetched inside a block with the same name
  1045. *
  1046. * @return void
  1047. */
  1048. public function testBlockExtend() {
  1049. $this->View->start('test');
  1050. echo 'Block content';
  1051. $this->View->end();
  1052. $this->View->start('test');
  1053. echo $this->View->fetch('test');
  1054. echo 'New content';
  1055. $this->View->end();
  1056. $result = $this->View->fetch('test');
  1057. $this->assertEquals('Block contentNew content', $result);
  1058. }
  1059. /**
  1060. * Test creating a block with capturing output.
  1061. *
  1062. * @return void
  1063. */
  1064. public function testBlockCapture() {
  1065. $this->View->start('test');
  1066. echo 'Block content';
  1067. $this->View->end();
  1068. $result = $this->View->fetch('test');
  1069. $this->assertEquals('Block content', $result);
  1070. }
  1071. /**
  1072. * Test appending to a block with capturing output.
  1073. *
  1074. * @return void
  1075. */
  1076. public function testBlockAppendCapture() {
  1077. $this->View->start('test');
  1078. echo 'Content ';
  1079. $this->View->end();
  1080. $this->View->append('test');
  1081. echo 'appended';
  1082. $this->View->end();
  1083. $result = $this->View->fetch('test');
  1084. $this->assertEquals('Content appended', $result);
  1085. }
  1086. /**
  1087. * Test setting a block's content.
  1088. *
  1089. * @return void
  1090. */
  1091. public function testBlockSet() {
  1092. $this->View->assign('test', 'Block content');
  1093. $result = $this->View->fetch('test');
  1094. $this->assertEquals('Block content', $result);
  1095. }
  1096. /**
  1097. * Test resetting a block's content.
  1098. *
  1099. * @return void
  1100. */
  1101. public function testBlockReset() {
  1102. $this->View->assign('test', '');
  1103. $result = $this->View->fetch('test', 'This should not be returned');
  1104. $this->assertSame('', $result);
  1105. }
  1106. /**
  1107. * Test setting a block's content to null
  1108. *
  1109. * @return void
  1110. * @link https://cakephp.lighthouseapp.com/projects/42648/tickets/3938-this-redirectthis-auth-redirecturl-broken
  1111. */
  1112. public function testBlockSetNull() {
  1113. $this->View->assign('testWithNull', null);
  1114. $result = $this->View->fetch('testWithNull');
  1115. $this->assertSame('', $result);
  1116. }
  1117. /**
  1118. * Test setting a block's content to an object with __toString magic method
  1119. *
  1120. * @return void
  1121. */
  1122. public function testBlockSetObjectWithToString() {
  1123. $objectWithToString = new TestObjectWithToString();
  1124. $this->View->assign('testWithObjectWithToString', $objectWithToString);
  1125. $result = $this->View->fetch('testWithObjectWithToString');
  1126. $this->assertSame("I'm ObjectWithToString", $result);
  1127. }
  1128. /**
  1129. * Test setting a block's content to an object without __toString magic method
  1130. *
  1131. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1132. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1133. *
  1134. * @expectedException PHPUnit_Framework_Error
  1135. * @return void
  1136. */
  1137. public function testBlockSetObjectWithoutToString() {
  1138. $objectWithToString = new TestObjectWithoutToString();
  1139. $this->View->assign('testWithObjectWithoutToString', $objectWithToString);
  1140. }
  1141. /**
  1142. * Test setting a block's content to a decimal
  1143. *
  1144. * @return void
  1145. */
  1146. public function testBlockSetDecimal() {
  1147. $this->View->assign('testWithDecimal', 1.23456789);
  1148. $result = $this->View->fetch('testWithDecimal');
  1149. $this->assertEquals('1.23456789', $result);
  1150. }
  1151. /**
  1152. * Data provider for block related tests.
  1153. *
  1154. * @return array
  1155. */
  1156. public static function blockValueProvider() {
  1157. return array(
  1158. 'string' => array('A string value'),
  1159. 'decimal' => array(1.23456),
  1160. 'object with __toString' => array(new TestObjectWithToString()),
  1161. );
  1162. }
  1163. /**
  1164. * Test appending to a block with append.
  1165. *
  1166. * @param mixed $value Value
  1167. * @return void
  1168. * @dataProvider blockValueProvider
  1169. */
  1170. public function testBlockAppend($value) {
  1171. $this->View->assign('testBlock', 'Block');
  1172. $this->View->append('testBlock', $value);
  1173. $result = $this->View->fetch('testBlock');
  1174. $this->assertSame('Block' . $value, $result);
  1175. }
  1176. /**
  1177. * Test appending an object without __toString magic method to a block with append.
  1178. *
  1179. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1180. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1181. *
  1182. * @expectedException PHPUnit_Framework_Error
  1183. * @return void
  1184. */
  1185. public function testBlockAppendObjectWithoutToString() {
  1186. $object = new TestObjectWithoutToString();
  1187. $this->View->assign('testBlock', 'Block ');
  1188. $this->View->append('testBlock', $object);
  1189. }
  1190. /**
  1191. * Test prepending to a block with prepend.
  1192. *
  1193. * @param mixed $value Value
  1194. * @return void
  1195. * @dataProvider blockValueProvider
  1196. */
  1197. public function testBlockPrepend($value) {
  1198. $this->View->assign('test', 'Block');
  1199. $this->View->prepend('test', $value);
  1200. $result = $this->View->fetch('test');
  1201. $this->assertEquals($value . 'Block', $result);
  1202. }
  1203. /**
  1204. * Test prepending an object without __toString magic method to a block with prepend.
  1205. *
  1206. * This should produce a "Object of class TestObjectWithoutToString could not be converted to string" error
  1207. * which gets thrown as a PHPUnit_Framework_Error Exception by PHPUnit.
  1208. *
  1209. * @expectedException PHPUnit_Framework_Error
  1210. * @return void
  1211. */
  1212. public function testBlockPrependObjectWithoutToString() {
  1213. $object = new TestObjectWithoutToString();
  1214. $this->View->assign('test', 'Block ');
  1215. $this->View->prepend('test', $object);
  1216. }
  1217. /**
  1218. * You should be able to append to undefined blocks.
  1219. *
  1220. * @return void
  1221. */
  1222. public function testBlockAppendUndefined() {
  1223. $this->View->append('test', 'Unknown');
  1224. $result = $this->View->fetch('test');
  1225. $this->assertEquals('Unknown', $result);
  1226. }
  1227. /**
  1228. * You should be able to prepend to undefined blocks.
  1229. *
  1230. * @return void
  1231. */
  1232. public function testBlockPrependUndefined() {
  1233. $this->View->prepend('test', 'Unknown');
  1234. $result = $this->View->fetch('test');
  1235. $this->assertEquals('Unknown', $result);
  1236. }
  1237. /**
  1238. * Test getting block names
  1239. *
  1240. * @return void
  1241. */
  1242. public function testBlocks() {
  1243. $this->View->append('test', 'one');
  1244. $this->View->assign('test1', 'one');
  1245. $this->assertEquals(array('test', 'test1'), $this->View->blocks());
  1246. }
  1247. /**
  1248. * Test that blocks can be nested.
  1249. *
  1250. * @return void
  1251. */
  1252. public function testNestedBlocks() {
  1253. $this->View->start('first');
  1254. echo 'In first ';
  1255. $this->View->start('second');
  1256. echo 'In second';
  1257. $this->View->end();
  1258. echo 'In first';
  1259. $this->View->end();
  1260. $this->assertEquals('In first In first', $this->View->fetch('first'));
  1261. $this->assertEquals('In second', $this->View->fetch('second'));
  1262. }
  1263. /**
  1264. * Test that starting the same block twice throws an exception
  1265. *
  1266. * @expectedException \Cake\Error\Exception
  1267. * @return void
  1268. */
  1269. public function testStartBlocksTwice() {
  1270. $this->View->start('first');
  1271. $this->View->start('first');
  1272. }
  1273. /**
  1274. * Test that an exception gets thrown when you leave a block open at the end
  1275. * of a view.
  1276. *
  1277. * @expectedException \Cake\Error\Exception
  1278. * @return void
  1279. */
  1280. public function testExceptionOnOpenBlock() {
  1281. $this->View->render('open_block');
  1282. }
  1283. /**
  1284. * Test nested extended views.
  1285. *
  1286. * @return void
  1287. */
  1288. public function testExtendNested() {
  1289. $this->View->layout = false;
  1290. $content = $this->View->render('nested_extends');
  1291. $expected = <<<TEXT
  1292. This is the second parent.
  1293. This is the first parent.
  1294. This is the first template.
  1295. Sidebar Content.
  1296. TEXT;
  1297. $this->assertEquals($expected, $content);
  1298. }
  1299. /**
  1300. * Make sure that extending the current view with itself causes an exception
  1301. *
  1302. * @expectedException LogicException
  1303. * @return void
  1304. */
  1305. public function testExtendSelf() {
  1306. $this->View->layout = false;
  1307. $this->View->render('extend_self');
  1308. }
  1309. /**
  1310. * Make sure that extending in a loop causes an exception
  1311. *
  1312. * @expectedException LogicException
  1313. * @return void
  1314. */
  1315. public function testExtendLoop() {
  1316. $this->View->layout = false;
  1317. $this->View->render('extend_loop');
  1318. }
  1319. /**
  1320. * Test extend() in an element and a view.
  1321. *
  1322. * @return void
  1323. */
  1324. public function testExtendElement() {
  1325. $this->View->layout = false;
  1326. $content = $this->View->render('extend_element');
  1327. $expected = <<<TEXT
  1328. Parent View.
  1329. View content.
  1330. Parent Element.
  1331. Element content.
  1332. TEXT;
  1333. $this->assertEquals($expected, $content);
  1334. }
  1335. /**
  1336. * Extending an element which doesn't exist should throw a missing view exception
  1337. *
  1338. * @expectedException LogicException
  1339. * @return void
  1340. */
  1341. public function testExtendMissingElement() {
  1342. $this->View->layout = false;
  1343. $this->View->render('extend_missing_element');
  1344. }
  1345. /**
  1346. * Test extend() preceeded by an element()
  1347. *
  1348. * @return void
  1349. */
  1350. public function testExtendWithElementBeforeExtend() {
  1351. $this->View->layout = false;
  1352. $result = $this->View->render('extend_with_element');
  1353. $expected = <<<TEXT
  1354. Parent View.
  1355. this is the test elementThe view
  1356. TEXT;
  1357. $this->assertEquals($expected, $result);
  1358. }
  1359. /**
  1360. * Test memory leaks that existed in _paths at one point.
  1361. *
  1362. * @return void
  1363. */
  1364. public function testMemoryLeakInPaths() {
  1365. $this->ThemeController->plugin = null;
  1366. $this->ThemeController->name = 'Posts';
  1367. $this->ThemeController->viewPath = 'Posts';
  1368. $this->ThemeController->layout = 'whatever';
  1369. $this->ThemeController->theme = 'TestTheme';
  1370. $View = $this->ThemeController->createView();
  1371. $View->element('test_element');
  1372. $start = memory_get_usage();
  1373. for ($i = 0; $i < 10; $i++) {
  1374. $View->element('test_element');
  1375. }
  1376. $end = memory_get_usage();
  1377. $this->assertLessThanOrEqual($start + 5000, $end);
  1378. }
  1379. /**
  1380. * Tests that a view block uses default value when not assigned and uses assigned value when it is
  1381. *
  1382. * @return void
  1383. */
  1384. public function testBlockDefaultValue() {
  1385. $default = 'Default';
  1386. $result = $this->View->fetch('title', $default);
  1387. $this->assertEquals($default, $result);
  1388. $expected = 'My Title';
  1389. $this->View->assign('title', $expected);
  1390. $result = $this->View->fetch('title', $default);
  1391. $this->assertEquals($expected, $result);
  1392. }
  1393. /**
  1394. * Tests that a view variable uses default value when not assigned and uses assigned value when it is
  1395. *
  1396. * @return void
  1397. */
  1398. public function testViewVarDefaultValue() {
  1399. $default = 'Default';
  1400. $result = $this->View->get('title', $default);
  1401. $this->assertEquals($default, $result);
  1402. $expected = 'Back to the Future';
  1403. $this->View->set('title', $expected);
  1404. $result = $this->View->get('title', $default);
  1405. $this->assertEquals($expected, $result);
  1406. }
  1407. /**
  1408. * Test the helpers() method.
  1409. *
  1410. * @return void
  1411. */
  1412. public function testHelpers() {
  1413. $this->assertInstanceOf('Cake\View\HelperRegistry', $this->View->helpers());
  1414. $result = $this->View->helpers();
  1415. $this->assertSame($result, $this->View->helpers());
  1416. }
  1417. }