StringTemplateTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\View;
  17. use Cake\Core\Exception\CakeException;
  18. use Cake\TestSuite\TestCase;
  19. use Cake\View\StringTemplate;
  20. use RuntimeException;
  21. use stdClass;
  22. class StringTemplateTest extends TestCase
  23. {
  24. /**
  25. * @var \Cake\View\StringTemplate
  26. */
  27. protected $template;
  28. /**
  29. * setUp
  30. */
  31. public function setUp(): void
  32. {
  33. parent::setUp();
  34. $this->template = new StringTemplate();
  35. }
  36. /**
  37. * Test adding templates through the constructor.
  38. */
  39. public function testConstructorAdd(): void
  40. {
  41. $templates = [
  42. 'link' => '<a href="{{url}}">{{text}}</a>',
  43. ];
  44. $template = new StringTemplate($templates);
  45. $this->assertSame($templates['link'], $template->get('link'));
  46. }
  47. /**
  48. * test adding templates.
  49. */
  50. public function testAdd(): void
  51. {
  52. $templates = [
  53. 'link' => '<a href="{{url}}">{{text}}</a>',
  54. ];
  55. $result = $this->template->add($templates);
  56. $this->assertSame(
  57. $this->template,
  58. $result,
  59. 'The same instance should be returned'
  60. );
  61. $this->assertSame($templates['link'], $this->template->get('link'));
  62. }
  63. /**
  64. * Test remove.
  65. */
  66. public function testRemove(): void
  67. {
  68. $templates = [
  69. 'link' => '<a href="{{url}}">{{text}}</a>',
  70. ];
  71. $this->template->add($templates);
  72. $this->template->remove('link');
  73. $this->assertNull($this->template->get('link'), 'Template should be gone.');
  74. }
  75. /**
  76. * Test formatting strings.
  77. */
  78. public function testFormat(): void
  79. {
  80. $templates = [
  81. 'link' => '<a href="{{url}}">{{text}}</a>',
  82. 'text' => '{{text}}',
  83. 'custom' => '<custom {{standard}} v1="{{var1}}" v2="{{var2}}" />',
  84. ];
  85. $this->template->add($templates);
  86. $result = $this->template->format('text', ['text' => '']);
  87. $this->assertSame('', $result);
  88. $result = $this->template->format('text', []);
  89. $this->assertSame('', $result);
  90. $result = $this->template->format('link', [
  91. 'url' => '/',
  92. 'text' => 'example',
  93. ]);
  94. $this->assertSame('<a href="/">example</a>', $result);
  95. $result = $this->template->format('custom', [
  96. 'standard' => 'default',
  97. 'templateVars' => ['var1' => 'foo'],
  98. ]);
  99. $this->assertSame('<custom default v1="foo" v2="" />', $result);
  100. }
  101. /**
  102. * Test formatting strings with URL encoding
  103. */
  104. public function testFormatUrlEncoding(): void
  105. {
  106. $templates = [
  107. 'test' => '<img src="/img/foo%20bar.jpg">{{text}}',
  108. ];
  109. $this->template->add($templates);
  110. $result = $this->template->format('test', ['text' => 'stuff!']);
  111. $this->assertSame('<img src="/img/foo%20bar.jpg">stuff!', $result);
  112. }
  113. /**
  114. * Formatting array data should not trigger errors.
  115. */
  116. public function testFormatArrayData(): void
  117. {
  118. $templates = [
  119. 'link' => '<a href="{{url}}">{{text}}</a>',
  120. ];
  121. $this->template->add($templates);
  122. $result = $this->template->format('link', [
  123. 'url' => '/',
  124. 'text' => ['example', 'text'],
  125. ]);
  126. $this->assertSame('<a href="/">exampletext</a>', $result);
  127. $result = $this->template->format('link', [
  128. 'url' => '/',
  129. 'text' => ['key' => 'example', 'text'],
  130. ]);
  131. $this->assertSame('<a href="/">exampletext</a>', $result);
  132. }
  133. /**
  134. * Test formatting a missing template.
  135. */
  136. public function testFormatMissingTemplate(): void
  137. {
  138. $this->expectException(RuntimeException::class);
  139. $this->expectExceptionMessage('Cannot find template named \'missing\'');
  140. $templates = [
  141. 'text' => '{{text}}',
  142. ];
  143. $this->template->add($templates);
  144. $this->template->format('missing', ['text' => 'missing']);
  145. }
  146. /**
  147. * Test loading templates files in the app.
  148. */
  149. public function testLoad(): void
  150. {
  151. $this->template->remove('attribute');
  152. $this->template->remove('compactAttribute');
  153. $this->assertEquals([], $this->template->get());
  154. $this->template->load('test_templates');
  155. $this->assertSame('<a href="{{url}}">{{text}}</a>', $this->template->get('link'));
  156. }
  157. /**
  158. * Test loading templates files from a plugin
  159. */
  160. public function testLoadPlugin(): void
  161. {
  162. $this->loadPlugins(['TestPlugin']);
  163. $this->template->load('TestPlugin.test_templates');
  164. $this->assertSame('<em>{{text}}</em>', $this->template->get('italic'));
  165. $this->clearPlugins();
  166. }
  167. /**
  168. * Test that loading nonexistent templates causes errors.
  169. */
  170. public function testLoadErrorNoFile(): void
  171. {
  172. $this->expectException(CakeException::class);
  173. $this->expectExceptionMessage('Could not load configuration file');
  174. $this->template->load('no_such_file');
  175. }
  176. /**
  177. * Test formatting compact attributes.
  178. */
  179. public function testFormatAttributesCompact(): void
  180. {
  181. $attrs = ['disabled' => true, 'selected' => 1, 'checked' => '1', 'multiple' => 'multiple'];
  182. $result = $this->template->formatAttributes($attrs);
  183. $this->assertSame(
  184. ' disabled="disabled" selected="selected" checked="checked" multiple="multiple"',
  185. $result
  186. );
  187. $attrs = ['disabled' => false, 'selected' => 0, 'checked' => '0', 'multiple' => null];
  188. $result = $this->template->formatAttributes($attrs);
  189. $this->assertSame(
  190. '',
  191. $result
  192. );
  193. }
  194. /**
  195. * Test formatting normal attributes.
  196. */
  197. public function testFormatAttributes(): void
  198. {
  199. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>', 'spellcheck' => 'true'];
  200. $result = $this->template->formatAttributes($attrs);
  201. $this->assertSame(
  202. ' name="bruce" data-hero="&lt;batman&gt;" spellcheck="true"',
  203. $result
  204. );
  205. $attrs = ['escape' => false, 'name' => 'bruce', 'data-hero' => '<batman>'];
  206. $result = $this->template->formatAttributes($attrs);
  207. $this->assertSame(
  208. ' name="bruce" data-hero="<batman>"',
  209. $result
  210. );
  211. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>'];
  212. $result = $this->template->formatAttributes($attrs, ['name']);
  213. $this->assertSame(
  214. ' data-hero="&lt;batman&gt;"',
  215. $result
  216. );
  217. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>', 'templateVars' => ['foo' => 'bar']];
  218. $result = $this->template->formatAttributes($attrs, ['name']);
  219. $this->assertSame(
  220. ' data-hero="&lt;batman&gt;"',
  221. $result
  222. );
  223. $evilKey = '><script>alert(1)</script>';
  224. $attrs = [$evilKey => 'some value'];
  225. $result = $this->template->formatAttributes($attrs);
  226. $this->assertSame(
  227. ' &gt;&lt;script&gt;alert(1)&lt;/script&gt;="some value"',
  228. $result
  229. );
  230. }
  231. /**
  232. * Test formatting array attributes.
  233. */
  234. public function testFormatAttributesArray(): void
  235. {
  236. $attrs = ['name' => ['bruce', 'wayne']];
  237. $result = $this->template->formatAttributes($attrs);
  238. $this->assertSame(
  239. ' name="bruce wayne"',
  240. $result
  241. );
  242. }
  243. /**
  244. * test push/pop templates.
  245. */
  246. public function testPushPopTemplates(): void
  247. {
  248. $this->template->add(['name' => '{{name}} is my name']);
  249. $this->template->push();
  250. $this->template->add(['name' => 'my name']);
  251. $this->assertSame('my name', $this->template->get('name'));
  252. $this->template->pop();
  253. $this->assertSame('{{name}} is my name', $this->template->get('name'));
  254. $this->template->pop();
  255. $this->template->pop();
  256. }
  257. /**
  258. * Test addClass method newClass parameter
  259. *
  260. * Tests null, string, array and false for `input`
  261. */
  262. public function testAddClassMethodNewClass(): void
  263. {
  264. $result = $this->template->addClass([], 'new_class');
  265. $this->assertEquals($result, ['class' => ['new_class']]);
  266. $result = $this->template->addClass([], ['new_class']);
  267. $this->assertEquals($result, ['class' => ['new_class']]);
  268. $result = $this->template->addClass([], false);
  269. $this->assertEquals($result, []);
  270. $result = $this->template->addClass([], null);
  271. $this->assertEquals($result, []);
  272. $result = $this->template->addClass(null, null);
  273. $this->assertNull($result);
  274. }
  275. /**
  276. * Test addClass method input (currentClass) parameter
  277. *
  278. * Tests null, string, array, false and object
  279. */
  280. public function testAddClassMethodCurrentClass(): void
  281. {
  282. $result = $this->template->addClass(['class' => ['current']], 'new_class');
  283. $this->assertEquals($result, ['class' => ['current', 'new_class']]);
  284. $result = $this->template->addClass('', 'new_class');
  285. $this->assertEquals($result, ['class' => ['new_class']]);
  286. $result = $this->template->addClass(null, 'new_class');
  287. $this->assertEquals($result, ['class' => ['new_class']]);
  288. $result = $this->template->addClass(false, 'new_class');
  289. $this->assertEquals($result, ['class' => ['new_class']]);
  290. $result = $this->template->addClass(new stdClass(), 'new_class');
  291. $this->assertEquals($result, ['class' => ['new_class']]);
  292. }
  293. /**
  294. * Test addClass method string parameter, it should fallback to string
  295. */
  296. public function testAddClassMethodFallbackToString(): void
  297. {
  298. $result = $this->template->addClass('current', 'new_class');
  299. $this->assertEquals($result, ['class' => ['current', 'new_class']]);
  300. }
  301. /**
  302. * Test addClass method to make sure the returned array is unique
  303. */
  304. public function testAddClassMethodUnique(): void
  305. {
  306. $result = $this->template->addClass(['class' => ['new_class']], 'new_class');
  307. $this->assertEquals($result, ['class' => ['new_class']]);
  308. }
  309. /**
  310. * Test addClass method useIndex param
  311. *
  312. * Tests for useIndex being the default, 'my_class' and false
  313. */
  314. public function testAddClassMethodUseIndex(): void
  315. {
  316. $result = $this->template->addClass(
  317. [
  318. 'class' => 'current_class',
  319. 'other_index1' => false,
  320. 'type' => 'text',
  321. ],
  322. 'new_class',
  323. 'class'
  324. );
  325. $this->assertEquals($result, [
  326. 'class' => ['current_class', 'new_class'],
  327. 'other_index1' => false,
  328. 'type' => 'text',
  329. ]);
  330. $result = $this->template->addClass(
  331. [
  332. 'my_class' => 'current_class',
  333. 'other_index1' => false,
  334. 'type' => 'text',
  335. ],
  336. 'new_class',
  337. 'my_class'
  338. );
  339. $this->assertEquals($result, [
  340. 'other_index1' => false,
  341. 'type' => 'text',
  342. 'my_class' => ['current_class', 'new_class'],
  343. ]);
  344. $result = $this->template->addClass(
  345. [
  346. 'class' => [
  347. 'current_class',
  348. 'text',
  349. ],
  350. ],
  351. 'new_class',
  352. 'nonexistent'
  353. );
  354. $this->assertEquals($result, [
  355. 'class' => [
  356. 'current_class',
  357. 'text',
  358. ],
  359. 'nonexistent' => ['new_class'],
  360. ]);
  361. }
  362. }