ViewTest.php 46 KB

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