StringTemplateTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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 InvalidArgumentException;
  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 adding a template config with a null value
  65. */
  66. public function testAddWithInvalidTemplate(): void
  67. {
  68. $templates = [
  69. 'link' => '<a href="{{url}}">{{text}}</a>',
  70. 'invalid' => null,
  71. ];
  72. $this->expectException(InvalidArgumentException::class);
  73. $this->template->add($templates);
  74. }
  75. /**
  76. * Test remove.
  77. */
  78. public function testRemove(): void
  79. {
  80. $templates = [
  81. 'link' => '<a href="{{url}}">{{text}}</a>',
  82. ];
  83. $this->template->add($templates);
  84. $this->template->remove('link');
  85. $this->assertNull($this->template->get('link'), 'Template should be gone.');
  86. }
  87. /**
  88. * Test formatting strings.
  89. */
  90. public function testFormat(): void
  91. {
  92. $templates = [
  93. 'link' => '<a href="{{url}}">{{text}}</a>',
  94. 'text' => '{{text}}',
  95. 'custom' => '<custom {{standard}} v1="{{var1}}" v2="{{var2}}" />',
  96. ];
  97. $this->template->add($templates);
  98. $result = $this->template->format('text', ['text' => '']);
  99. $this->assertSame('', $result);
  100. $result = $this->template->format('text', []);
  101. $this->assertSame('', $result);
  102. $result = $this->template->format('link', [
  103. 'url' => '/',
  104. 'text' => 'example',
  105. ]);
  106. $this->assertSame('<a href="/">example</a>', $result);
  107. $result = $this->template->format('custom', [
  108. 'standard' => 'default',
  109. 'templateVars' => ['var1' => 'foo'],
  110. ]);
  111. $this->assertSame('<custom default v1="foo" v2="" />', $result);
  112. }
  113. /**
  114. * Test formatting strings with URL encoding
  115. */
  116. public function testFormatUrlEncoding(): void
  117. {
  118. $templates = [
  119. 'test' => '<img src="/img/foo%20bar.jpg">{{text}}',
  120. ];
  121. $this->template->add($templates);
  122. $result = $this->template->format('test', ['text' => 'stuff!']);
  123. $this->assertSame('<img src="/img/foo%20bar.jpg">stuff!', $result);
  124. }
  125. /**
  126. * Formatting array data should not trigger errors.
  127. */
  128. public function testFormatArrayData(): void
  129. {
  130. $templates = [
  131. 'link' => '<a href="{{url}}">{{text}}</a>',
  132. ];
  133. $this->template->add($templates);
  134. $result = $this->template->format('link', [
  135. 'url' => '/',
  136. 'text' => ['example', 'text'],
  137. ]);
  138. $this->assertSame('<a href="/">exampletext</a>', $result);
  139. $result = $this->template->format('link', [
  140. 'url' => '/',
  141. 'text' => ['key' => 'example', 'text'],
  142. ]);
  143. $this->assertSame('<a href="/">exampletext</a>', $result);
  144. }
  145. /**
  146. * Test formatting a missing template.
  147. */
  148. public function testFormatMissingTemplate(): void
  149. {
  150. $this->expectException(InvalidArgumentException::class);
  151. $this->expectExceptionMessage('Cannot find template named `missing`.');
  152. $templates = [
  153. 'text' => '{{text}}',
  154. ];
  155. $this->template->add($templates);
  156. $this->template->format('missing', ['text' => 'missing']);
  157. }
  158. /**
  159. * Test loading templates files in the app.
  160. */
  161. public function testLoad(): void
  162. {
  163. $this->template->remove('attribute');
  164. $this->template->remove('compactAttribute');
  165. $this->assertEquals([], $this->template->get());
  166. $this->template->load('test_templates');
  167. $this->assertSame('<a href="{{url}}">{{text}}</a>', $this->template->get('link'));
  168. }
  169. /**
  170. * Test loading templates files from a plugin
  171. */
  172. public function testLoadPlugin(): void
  173. {
  174. $this->loadPlugins(['TestPlugin']);
  175. $this->template->load('TestPlugin.test_templates');
  176. $this->assertSame('<em>{{text}}</em>', $this->template->get('italic'));
  177. $this->clearPlugins();
  178. }
  179. /**
  180. * Test that loading nonexistent templates causes errors.
  181. */
  182. public function testLoadErrorNoFile(): void
  183. {
  184. $this->expectException(CakeException::class);
  185. $this->expectExceptionMessage('Could not load configuration file');
  186. $this->template->load('no_such_file');
  187. }
  188. /**
  189. * Test formatting compact attributes.
  190. */
  191. public function testFormatAttributesCompact(): void
  192. {
  193. $attrs = ['disabled' => true, 'selected' => 1, 'checked' => '1', 'multiple' => 'multiple'];
  194. $result = $this->template->formatAttributes($attrs);
  195. $this->assertSame(
  196. ' disabled="disabled" selected="selected" checked="checked" multiple="multiple"',
  197. $result
  198. );
  199. $attrs = ['disabled' => false, 'selected' => 0, 'checked' => '0', 'multiple' => null];
  200. $result = $this->template->formatAttributes($attrs);
  201. $this->assertSame(
  202. '',
  203. $result
  204. );
  205. }
  206. /**
  207. * Test formatting normal attributes.
  208. */
  209. public function testFormatAttributes(): void
  210. {
  211. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>', 'spellcheck' => 'true'];
  212. $result = $this->template->formatAttributes($attrs);
  213. $this->assertSame(
  214. ' name="bruce" data-hero="&lt;batman&gt;" spellcheck="true"',
  215. $result
  216. );
  217. $attrs = ['escape' => false, 'name' => 'bruce', 'data-hero' => '<batman>'];
  218. $result = $this->template->formatAttributes($attrs);
  219. $this->assertSame(
  220. ' name="bruce" data-hero="<batman>"',
  221. $result
  222. );
  223. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>'];
  224. $result = $this->template->formatAttributes($attrs, ['name']);
  225. $this->assertSame(
  226. ' data-hero="&lt;batman&gt;"',
  227. $result
  228. );
  229. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>', 'templateVars' => ['foo' => 'bar']];
  230. $result = $this->template->formatAttributes($attrs, ['name']);
  231. $this->assertSame(
  232. ' data-hero="&lt;batman&gt;"',
  233. $result
  234. );
  235. $evilKey = '><script>alert(1)</script>';
  236. $attrs = [$evilKey => 'some value'];
  237. $result = $this->template->formatAttributes($attrs);
  238. $this->assertSame(
  239. ' &gt;&lt;script&gt;alert(1)&lt;/script&gt;="some value"',
  240. $result
  241. );
  242. }
  243. /**
  244. * Test formatting array attributes.
  245. */
  246. public function testFormatAttributesArray(): void
  247. {
  248. $attrs = ['name' => ['bruce', 'wayne']];
  249. $result = $this->template->formatAttributes($attrs);
  250. $this->assertSame(
  251. ' name="bruce wayne"',
  252. $result
  253. );
  254. }
  255. /**
  256. * test push/pop templates.
  257. */
  258. public function testPushPopTemplates(): void
  259. {
  260. $this->template->add(['name' => '{{name}} is my name']);
  261. $this->template->push();
  262. $this->template->add(['name' => 'my name']);
  263. $this->assertSame('my name', $this->template->get('name'));
  264. $this->template->pop();
  265. $this->assertSame('{{name}} is my name', $this->template->get('name'));
  266. $this->template->pop();
  267. $this->template->pop();
  268. }
  269. /**
  270. * Test addClass method newClass parameter
  271. *
  272. * Tests null, string, array and false for `input`
  273. */
  274. public function testAddClassMethodNewClass(): void
  275. {
  276. $result = $this->template->addClass([], 'new_class');
  277. $this->assertEquals($result, ['class' => ['new_class']]);
  278. $result = $this->template->addClass([], ['new_class']);
  279. $this->assertEquals($result, ['class' => ['new_class']]);
  280. $result = $this->template->addClass([], false);
  281. $this->assertEquals($result, []);
  282. $result = $this->template->addClass([], null);
  283. $this->assertEquals($result, []);
  284. $result = $this->template->addClass(null, null);
  285. $this->assertNull($result);
  286. }
  287. /**
  288. * Test addClass method input (currentClass) parameter
  289. *
  290. * Tests null, string, array, false and object
  291. */
  292. public function testAddClassMethodCurrentClass(): void
  293. {
  294. $result = $this->template->addClass(['class' => ['current']], 'new_class');
  295. $this->assertEquals($result, ['class' => ['current', 'new_class']]);
  296. $result = $this->template->addClass('', 'new_class');
  297. $this->assertEquals($result, ['class' => ['new_class']]);
  298. $result = $this->template->addClass(null, 'new_class');
  299. $this->assertEquals($result, ['class' => ['new_class']]);
  300. $result = $this->template->addClass(false, 'new_class');
  301. $this->assertEquals($result, ['class' => ['new_class']]);
  302. $result = $this->template->addClass(new stdClass(), 'new_class');
  303. $this->assertEquals($result, ['class' => ['new_class']]);
  304. }
  305. /**
  306. * Test addClass method string parameter, it should fallback to string
  307. */
  308. public function testAddClassMethodFallbackToString(): void
  309. {
  310. $result = $this->template->addClass('current', 'new_class');
  311. $this->assertEquals($result, ['class' => ['current', 'new_class']]);
  312. }
  313. /**
  314. * Test addClass method to make sure the returned array is unique
  315. */
  316. public function testAddClassMethodUnique(): void
  317. {
  318. $result = $this->template->addClass(['class' => ['new_class']], 'new_class');
  319. $this->assertEquals($result, ['class' => ['new_class']]);
  320. }
  321. /**
  322. * Test addClass method useIndex param
  323. *
  324. * Tests for useIndex being the default, 'my_class' and false
  325. */
  326. public function testAddClassMethodUseIndex(): void
  327. {
  328. $result = $this->template->addClass(
  329. [
  330. 'class' => 'current_class',
  331. 'other_index1' => false,
  332. 'type' => 'text',
  333. ],
  334. 'new_class',
  335. 'class'
  336. );
  337. $this->assertEquals($result, [
  338. 'class' => ['current_class', 'new_class'],
  339. 'other_index1' => false,
  340. 'type' => 'text',
  341. ]);
  342. $result = $this->template->addClass(
  343. [
  344. 'my_class' => 'current_class',
  345. 'other_index1' => false,
  346. 'type' => 'text',
  347. ],
  348. 'new_class',
  349. 'my_class'
  350. );
  351. $this->assertEquals($result, [
  352. 'other_index1' => false,
  353. 'type' => 'text',
  354. 'my_class' => ['current_class', 'new_class'],
  355. ]);
  356. $result = $this->template->addClass(
  357. [
  358. 'class' => [
  359. 'current_class',
  360. 'text',
  361. ],
  362. ],
  363. 'new_class',
  364. 'nonexistent'
  365. );
  366. $this->assertEquals($result, [
  367. 'class' => [
  368. 'current_class',
  369. 'text',
  370. ],
  371. 'nonexistent' => ['new_class'],
  372. ]);
  373. }
  374. }