StringTemplateTest.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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('not there', []);
  90. $this->assertNull($result);
  91. $result = $this->template->format('text', ['text' => '']);
  92. $this->assertSame('', $result);
  93. $result = $this->template->format('text', []);
  94. $this->assertSame('', $result);
  95. $result = $this->template->format('link', [
  96. 'url' => '/',
  97. 'text' => 'example'
  98. ]);
  99. $this->assertEquals('<a href="/">example</a>', $result);
  100. $result = $this->template->format('custom', [
  101. 'standard' => 'default',
  102. 'templateVars' => ['var1' => 'foo']
  103. ]);
  104. $this->assertEquals('<custom default v1="foo" v2="" />', $result);
  105. }
  106. /**
  107. * Formatting array data should not trigger errors.
  108. *
  109. * @return void
  110. */
  111. public function testFormatArrayData()
  112. {
  113. $templates = [
  114. 'link' => '<a href="{{url}}">{{text}}</a>'
  115. ];
  116. $this->template->add($templates);
  117. $result = $this->template->format('link', [
  118. 'url' => '/',
  119. 'text' => ['example', 'text']
  120. ]);
  121. $this->assertEquals('<a href="/">exampletext</a>', $result);
  122. $result = $this->template->format('link', [
  123. 'url' => '/',
  124. 'text' => ['key' => 'example', 'text']
  125. ]);
  126. $this->assertEquals('<a href="/">exampletext</a>', $result);
  127. }
  128. /**
  129. * Test loading templates files in the app.
  130. *
  131. * @return void
  132. */
  133. public function testLoad()
  134. {
  135. $this->template->remove('attribute');
  136. $this->template->remove('compactAttribute');
  137. $this->assertEquals([], $this->template->get());
  138. $this->assertNull($this->template->load('test_templates'));
  139. $this->assertEquals('<a href="{{url}}">{{text}}</a>', $this->template->get('link'));
  140. }
  141. /**
  142. * Test loading templates files from a plugin
  143. *
  144. * @return void
  145. */
  146. public function testLoadPlugin()
  147. {
  148. Plugin::load('TestPlugin');
  149. $this->assertNull($this->template->load('TestPlugin.test_templates'));
  150. $this->assertEquals('<em>{{text}}</em>', $this->template->get('italic'));
  151. }
  152. /**
  153. * Test that loading non-existing templates causes errors.
  154. *
  155. * @expectedException \Cake\Core\Exception\Exception
  156. * @expectedExceptionMessage Could not load configuration file
  157. */
  158. public function testLoadErrorNoFile()
  159. {
  160. $this->template->load('no_such_file');
  161. }
  162. /**
  163. * Test formatting compact attributes.
  164. *
  165. * @return void
  166. */
  167. public function testFormatAttributesCompact()
  168. {
  169. $attrs = ['disabled' => true, 'selected' => 1, 'checked' => '1', 'multiple' => 'multiple'];
  170. $result = $this->template->formatAttributes($attrs);
  171. $this->assertEquals(
  172. ' disabled="disabled" selected="selected" checked="checked" multiple="multiple"',
  173. $result
  174. );
  175. $attrs = ['disabled' => false, 'selected' => 0, 'checked' => '0', 'multiple' => null];
  176. $result = $this->template->formatAttributes($attrs);
  177. $this->assertEquals(
  178. '',
  179. $result
  180. );
  181. }
  182. /**
  183. * Test formatting normal attributes.
  184. *
  185. * @return void
  186. */
  187. public function testFormatAttributes()
  188. {
  189. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>', 'spellcheck' => 'true'];
  190. $result = $this->template->formatAttributes($attrs);
  191. $this->assertEquals(
  192. ' name="bruce" data-hero="&lt;batman&gt;" spellcheck="true"',
  193. $result
  194. );
  195. $attrs = ['escape' => false, 'name' => 'bruce', 'data-hero' => '<batman>'];
  196. $result = $this->template->formatAttributes($attrs);
  197. $this->assertEquals(
  198. ' name="bruce" data-hero="<batman>"',
  199. $result
  200. );
  201. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>'];
  202. $result = $this->template->formatAttributes($attrs, ['name']);
  203. $this->assertEquals(
  204. ' data-hero="&lt;batman&gt;"',
  205. $result
  206. );
  207. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>', 'templateVars' => ['foo' => 'bar']];
  208. $result = $this->template->formatAttributes($attrs, ['name']);
  209. $this->assertEquals(
  210. ' data-hero="&lt;batman&gt;"',
  211. $result
  212. );
  213. }
  214. /**
  215. * Test formatting array attributes.
  216. *
  217. * @return void
  218. */
  219. public function testFormatAttributesArray()
  220. {
  221. $attrs = ['name' => ['bruce', 'wayne']];
  222. $result = $this->template->formatAttributes($attrs);
  223. $this->assertEquals(
  224. ' name="bruce wayne"',
  225. $result
  226. );
  227. }
  228. /**
  229. * test push/pop templates.
  230. *
  231. * @return void
  232. */
  233. public function testPushPopTemplates()
  234. {
  235. $this->template->add(['name' => '{{name}} is my name']);
  236. $this->assertNull($this->template->push());
  237. $this->template->add(['name' => 'my name']);
  238. $this->assertEquals('my name', $this->template->get('name'));
  239. $this->assertNull($this->template->pop());
  240. $this->assertEquals('{{name}} is my name', $this->template->get('name'));
  241. $this->assertNull($this->template->pop());
  242. $this->assertNull($this->template->pop());
  243. }
  244. }