ViewTest.php 44 KB

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