ViewTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\template;
  9. use Closure;
  10. use lithium\core\Libraries;
  11. use lithium\template\View;
  12. use lithium\action\Response;
  13. use lithium\template\view\adapter\Simple;
  14. use lithium\tests\mocks\template\MockView;
  15. use lithium\tests\mocks\template\view\adapters\TestRenderer;
  16. class ViewTest extends \lithium\test\Unit {
  17. protected $_view = null;
  18. public function setUp() {
  19. $this->_view = new View();
  20. }
  21. public function testInitialization() {
  22. $expected = new Simple();
  23. $this->_view = new MockView(array('renderer' => $expected));
  24. $result = $this->_view->renderer();
  25. $this->assertEqual($expected, $result);
  26. }
  27. public function testInitializationWithBadLoader() {
  28. $this->expectException("Class `Badness` of type `adapter.template.view` not found.");
  29. new View(array('loader' => 'Badness'));
  30. }
  31. public function testInitializationWithBadRenderer() {
  32. $this->expectException("Class `Badness` of type `adapter.template.view` not found.");
  33. new View(array('renderer' => 'Badness'));
  34. }
  35. public function testEscapeOutputFilter() {
  36. $h = $this->_view->outputFilters['h'];
  37. $expected = '&lt;p&gt;Foo, Bar &amp; Baz&lt;/p&gt;';
  38. $result = $h('<p>Foo, Bar & Baz</p>');
  39. $this->assertEqual($expected, $result);
  40. }
  41. /**
  42. * Tests that the output-escaping handler correctly inherits its encoding from the `Response`
  43. * object, if provided.
  44. *
  45. * @return void
  46. */
  47. public function testEscapeOutputFilterWithInjectedEncoding() {
  48. $message = "Multibyte string support must be enabled to test character encodings.";
  49. $this->skipIf(!function_exists('mb_convert_encoding'), $message);
  50. $string = "Joël";
  51. $response = new Response();
  52. $response->encoding = 'UTF-8';
  53. $view = new View(compact('response'));
  54. $handler = $view->outputFilters['h'];
  55. $this->assertTrue(mb_check_encoding($handler($string), "UTF-8"));
  56. $response = new Response();
  57. $response->encoding = 'ISO-8859-1';
  58. $view = new View(compact('response'));
  59. $handler = $view->outputFilters['h'];
  60. $this->assertTrue(mb_check_encoding($handler($string), "ISO-8859-1"));
  61. }
  62. public function testBasicRenderModes() {
  63. $view = new View(array('loader' => 'Simple', 'renderer' => 'Simple'));
  64. $result = $view->render('template', array('content' => 'world'), array(
  65. 'template' => 'Hello {:content}!'
  66. ));
  67. $expected = 'Hello world!';
  68. $this->assertEqual($expected, $result);
  69. $result = $view->render(array('element' => 'Logged in as: {:name}.'), array(
  70. 'name' => "Cap'n Crunch"
  71. ));
  72. $expected = "Logged in as: Cap'n Crunch.";
  73. $this->assertEqual($expected, $result);
  74. $result = $view->render('element', array('name' => "Cap'n Crunch"), array(
  75. 'element' => 'Logged in as: {:name}.'
  76. ));
  77. $expected = "Logged in as: Cap'n Crunch.";
  78. $this->assertEqual($expected, $result);
  79. $xmlHeader = '<' . '?xml version="1.0" ?' . '>' . "\n";
  80. $result = $view->render('all', array('type' => 'auth', 'success' => 'true'), array(
  81. 'layout' => $xmlHeader . "\n{:content}\n",
  82. 'template' => '<{:type}>{:success}</{:type}>'
  83. ));
  84. $expected = "{$xmlHeader}\n<auth>true</auth>\n";
  85. $this->assertEqual($expected, $result);
  86. }
  87. public function testTwoStepRenderWithVariableCapture() {
  88. $view = new View(array('loader' => 'Simple', 'renderer' => 'Simple'));
  89. $result = $view->render(
  90. array(
  91. array('path' => 'element', 'capture' => array('data' => 'foo')),
  92. array('path' => 'template')
  93. ),
  94. array('name' => "Cap'n Crunch"),
  95. array('element' => 'Logged in as: {:name}.', 'template' => '--{:foo}--')
  96. );
  97. $this->assertEqual('--Logged in as: Cap\'n Crunch.--', $result);
  98. }
  99. public function testFullRenderNoLayout() {
  100. $view = new View(array('loader' => 'Simple', 'renderer' => 'Simple'));
  101. $result = $view->render('all', array('type' => 'auth', 'success' => 'true'), array(
  102. 'template' => '<{:type}>{:success}</{:type}>'
  103. ));
  104. $expected = '<auth>true</auth>';
  105. $this->assertEqual($expected, $result);
  106. }
  107. public function testNolayout() {
  108. $view = new View(array(
  109. 'loader' => 'lithium\tests\mocks\template\view\adapters\TestRenderer',
  110. 'renderer' => 'lithium\tests\mocks\template\view\adapters\TestRenderer',
  111. 'paths' => array(
  112. 'template' => '{:library}/tests/mocks/template/view/adapters/{:template}.html.php',
  113. 'layout' => false
  114. )
  115. ));
  116. $options = array(
  117. 'template' => 'testFile',
  118. 'library' => LITHIUM_LIBRARY_PATH . '/lithium'
  119. );
  120. $result = $view->render('all', array(), $options);
  121. $expected = 'This is a test.';
  122. $this->assertEqual($expected, $result);
  123. $templateData = TestRenderer::$templateData;
  124. $expectedPath = LITHIUM_LIBRARY_PATH;
  125. $expectedPath .= '/lithium/tests/mocks/template/view/adapters/testFile.html.php';
  126. $expected = array (array (
  127. 'type' => 'template',
  128. 'params' => array (
  129. 'template' => 'testFile',
  130. 'library' => LITHIUM_LIBRARY_PATH . '/lithium',
  131. 'type' => 'html'
  132. ),
  133. 'return' => $expectedPath
  134. ));
  135. $this->assertEqual($expected, $templateData);
  136. $renderData = TestRenderer::$renderData;
  137. $expected = array (
  138. array (
  139. 'template' => $expectedPath,
  140. 'data' => array (),
  141. 'options' => array (
  142. 'template' => 'testFile',
  143. 'library' => $options['library'],
  144. 'type' => 'html',
  145. 'layout' => null,
  146. 'context' => array()
  147. )
  148. )
  149. );
  150. $this->assertTrue($renderData[0]['data']['h'] instanceof Closure);
  151. unset($renderData[0]['data']['h']);
  152. $this->assertEqual($expected, $renderData);
  153. }
  154. public function testElementRenderingOptions() {
  155. $tmpDir = realpath(Libraries::get(true, 'resources') . '/tmp');
  156. $this->skipIf(!is_writable($tmpDir), "Can't write to resources directory.");
  157. $testApp = $tmpDir . '/tests/test_app';
  158. $viewDir = $testApp . '/views';
  159. mkdir($viewDir, 0777, true);
  160. Libraries::add('test_app', array('path' => $testApp));
  161. $body = '<?php echo isset($this->_options[$option]) ? $this->_options[$option] : ""; ?>';
  162. $template = $viewDir . '/template.html.php';
  163. file_put_contents($template, $body);
  164. $view = new View(array(
  165. 'paths' => array(
  166. 'template' => '{:library}/views/{:template}.html.php',
  167. 'layout' => false
  168. )
  169. ));
  170. $options = array(
  171. 'template' => 'template',
  172. 'library' => 'test_app'
  173. );
  174. $result = $view->render('all', array('option' => 'custom'), $options);
  175. $this->assertIdentical('', $result);
  176. $result = $view->render('all', array('option' => 'library'), $options);
  177. $this->assertIdentical('test_app', $result);
  178. $options = array(
  179. 'template' => 'template',
  180. 'library' => 'test_app',
  181. 'custom' => 'custom option'
  182. );
  183. $result = $view->render('all', array('option' => 'custom'), $options);
  184. $this->assertIdentical('custom option', $result);
  185. $result = $view->render('all', array('option' => 'library'), $options);
  186. $this->assertIdentical('test_app', $result);
  187. Libraries::remove('test_app');
  188. $this->_cleanUp();
  189. }
  190. }
  191. ?>