StringTemplateTest.php 13 KB

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