StringTemplateTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\View;
  16. use Cake\Core\Plugin;
  17. use Cake\TestSuite\TestCase;
  18. use Cake\View\StringTemplate;
  19. class StringTemplateTest extends TestCase
  20. {
  21. /**
  22. * setUp
  23. *
  24. * @return void
  25. */
  26. public function setUp()
  27. {
  28. parent::setUp();
  29. $this->template = new StringTemplate();
  30. }
  31. /**
  32. * Test adding templates through the constructor.
  33. *
  34. * @return void
  35. */
  36. public function testConstructorAdd()
  37. {
  38. $templates = [
  39. 'link' => '<a href="{{url}}">{{text}}</a>'
  40. ];
  41. $template = new StringTemplate($templates);
  42. $this->assertEquals($templates['link'], $template->get('link'));
  43. }
  44. /**
  45. * test adding templates.
  46. *
  47. * @return void
  48. */
  49. public function testAdd()
  50. {
  51. $templates = [
  52. 'link' => '<a href="{{url}}">{{text}}</a>'
  53. ];
  54. $result = $this->template->add($templates);
  55. $this->assertSame(
  56. $this->template,
  57. $result,
  58. 'The same instance should be returned'
  59. );
  60. $this->assertEquals($templates['link'], $this->template->get('link'));
  61. }
  62. /**
  63. * Test remove.
  64. *
  65. * @return void
  66. */
  67. public function testRemove()
  68. {
  69. $templates = [
  70. 'link' => '<a href="{{url}}">{{text}}</a>'
  71. ];
  72. $this->template->add($templates);
  73. $this->assertNull($this->template->remove('link'), 'No return');
  74. $this->assertNull($this->template->get('link'), 'Template should be gone.');
  75. }
  76. /**
  77. * Test formatting strings.
  78. *
  79. * @return void
  80. */
  81. public function testFormat()
  82. {
  83. $templates = [
  84. 'link' => '<a href="{{url}}">{{text}}</a>',
  85. 'text' => '{{text}}',
  86. 'custom' => '<custom {{standard}} v1="{{var1}}" v2="{{var2}}" />'
  87. ];
  88. $this->template->add($templates);
  89. $result = $this->template->format('text', ['text' => '']);
  90. $this->assertSame('', $result);
  91. $result = $this->template->format('text', []);
  92. $this->assertSame('', $result);
  93. $result = $this->template->format('link', [
  94. 'url' => '/',
  95. 'text' => 'example'
  96. ]);
  97. $this->assertEquals('<a href="/">example</a>', $result);
  98. $result = $this->template->format('custom', [
  99. 'standard' => 'default',
  100. 'templateVars' => ['var1' => 'foo']
  101. ]);
  102. $this->assertEquals('<custom default v1="foo" v2="" />', $result);
  103. }
  104. /**
  105. * Formatting array data should not trigger errors.
  106. *
  107. * @return void
  108. */
  109. public function testFormatArrayData()
  110. {
  111. $templates = [
  112. 'link' => '<a href="{{url}}">{{text}}</a>'
  113. ];
  114. $this->template->add($templates);
  115. $result = $this->template->format('link', [
  116. 'url' => '/',
  117. 'text' => ['example', 'text']
  118. ]);
  119. $this->assertEquals('<a href="/">exampletext</a>', $result);
  120. $result = $this->template->format('link', [
  121. 'url' => '/',
  122. 'text' => ['key' => 'example', 'text']
  123. ]);
  124. $this->assertEquals('<a href="/">exampletext</a>', $result);
  125. }
  126. /**
  127. * Test formatting a missing template.
  128. *
  129. * @expectedException RuntimeException
  130. * @expectedExceptionMessage Cannot find template named 'missing'
  131. * @return void
  132. */
  133. public function testFormatMissingTemplate()
  134. {
  135. $templates = [
  136. 'text' => '{{text}}',
  137. ];
  138. $this->template->add($templates);
  139. $this->template->format('missing', ['text' => 'missing']);
  140. }
  141. /**
  142. * Test loading templates files in the app.
  143. *
  144. * @return void
  145. */
  146. public function testLoad()
  147. {
  148. $this->template->remove('attribute');
  149. $this->template->remove('compactAttribute');
  150. $this->assertEquals([], $this->template->get());
  151. $this->assertNull($this->template->load('test_templates'));
  152. $this->assertEquals('<a href="{{url}}">{{text}}</a>', $this->template->get('link'));
  153. }
  154. /**
  155. * Test loading templates files from a plugin
  156. *
  157. * @return void
  158. */
  159. public function testLoadPlugin()
  160. {
  161. Plugin::load('TestPlugin');
  162. $this->assertNull($this->template->load('TestPlugin.test_templates'));
  163. $this->assertEquals('<em>{{text}}</em>', $this->template->get('italic'));
  164. }
  165. /**
  166. * Test that loading non-existing templates causes errors.
  167. *
  168. * @expectedException \Cake\Core\Exception\Exception
  169. * @expectedExceptionMessage Could not load configuration file
  170. */
  171. public function testLoadErrorNoFile()
  172. {
  173. $this->template->load('no_such_file');
  174. }
  175. /**
  176. * Test formatting compact attributes.
  177. *
  178. * @return void
  179. */
  180. public function testFormatAttributesCompact()
  181. {
  182. $attrs = ['disabled' => true, 'selected' => 1, 'checked' => '1', 'multiple' => 'multiple'];
  183. $result = $this->template->formatAttributes($attrs);
  184. $this->assertEquals(
  185. ' disabled="disabled" selected="selected" checked="checked" multiple="multiple"',
  186. $result
  187. );
  188. $attrs = ['disabled' => false, 'selected' => 0, 'checked' => '0', 'multiple' => null];
  189. $result = $this->template->formatAttributes($attrs);
  190. $this->assertEquals(
  191. '',
  192. $result
  193. );
  194. }
  195. /**
  196. * Test formatting normal attributes.
  197. *
  198. * @return void
  199. */
  200. public function testFormatAttributes()
  201. {
  202. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>', 'spellcheck' => 'true'];
  203. $result = $this->template->formatAttributes($attrs);
  204. $this->assertEquals(
  205. ' name="bruce" data-hero="&lt;batman&gt;" spellcheck="true"',
  206. $result
  207. );
  208. $attrs = ['escape' => false, 'name' => 'bruce', 'data-hero' => '<batman>'];
  209. $result = $this->template->formatAttributes($attrs);
  210. $this->assertEquals(
  211. ' name="bruce" data-hero="<batman>"',
  212. $result
  213. );
  214. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>'];
  215. $result = $this->template->formatAttributes($attrs, ['name']);
  216. $this->assertEquals(
  217. ' data-hero="&lt;batman&gt;"',
  218. $result
  219. );
  220. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>', 'templateVars' => ['foo' => 'bar']];
  221. $result = $this->template->formatAttributes($attrs, ['name']);
  222. $this->assertEquals(
  223. ' data-hero="&lt;batman&gt;"',
  224. $result
  225. );
  226. }
  227. /**
  228. * Test formatting array attributes.
  229. *
  230. * @return void
  231. */
  232. public function testFormatAttributesArray()
  233. {
  234. $attrs = ['name' => ['bruce', 'wayne']];
  235. $result = $this->template->formatAttributes($attrs);
  236. $this->assertEquals(
  237. ' name="bruce wayne"',
  238. $result
  239. );
  240. }
  241. /**
  242. * test push/pop templates.
  243. *
  244. * @return void
  245. */
  246. public function testPushPopTemplates()
  247. {
  248. $this->template->add(['name' => '{{name}} is my name']);
  249. $this->assertNull($this->template->push());
  250. $this->template->add(['name' => 'my name']);
  251. $this->assertEquals('my name', $this->template->get('name'));
  252. $this->assertNull($this->template->pop());
  253. $this->assertEquals('{{name}} is my name', $this->template->get('name'));
  254. $this->assertNull($this->template->pop());
  255. $this->assertNull($this->template->pop());
  256. }
  257. }