StringTemplateTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license https://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. * Test formatting strings with URL encoding
  106. *
  107. * @return void
  108. */
  109. public function testFormatUrlEncoding()
  110. {
  111. $templates = [
  112. 'test' => '<img src="/img/foo%20bar.jpg">{{text}}',
  113. ];
  114. $this->template->add($templates);
  115. $result = $this->template->format('test', ['text' => 'stuff!']);
  116. $this->assertSame('<img src="/img/foo%20bar.jpg">stuff!', $result);
  117. }
  118. /**
  119. * Formatting array data should not trigger errors.
  120. *
  121. * @return void
  122. */
  123. public function testFormatArrayData()
  124. {
  125. $templates = [
  126. 'link' => '<a href="{{url}}">{{text}}</a>',
  127. ];
  128. $this->template->add($templates);
  129. $result = $this->template->format('link', [
  130. 'url' => '/',
  131. 'text' => ['example', 'text'],
  132. ]);
  133. $this->assertEquals('<a href="/">exampletext</a>', $result);
  134. $result = $this->template->format('link', [
  135. 'url' => '/',
  136. 'text' => ['key' => 'example', 'text'],
  137. ]);
  138. $this->assertEquals('<a href="/">exampletext</a>', $result);
  139. }
  140. /**
  141. * Test formatting a missing template.
  142. *
  143. * @return void
  144. */
  145. public function testFormatMissingTemplate()
  146. {
  147. $this->expectException(\RuntimeException::class);
  148. $this->expectExceptionMessage('Cannot find template named \'missing\'');
  149. $templates = [
  150. 'text' => '{{text}}',
  151. ];
  152. $this->template->add($templates);
  153. $this->template->format('missing', ['text' => 'missing']);
  154. }
  155. /**
  156. * Test loading templates files in the app.
  157. *
  158. * @return void
  159. */
  160. public function testLoad()
  161. {
  162. $this->template->remove('attribute');
  163. $this->template->remove('compactAttribute');
  164. $this->assertEquals([], $this->template->get());
  165. $this->assertNull($this->template->load('test_templates'));
  166. $this->assertEquals('<a href="{{url}}">{{text}}</a>', $this->template->get('link'));
  167. }
  168. /**
  169. * Test loading templates files from a plugin
  170. *
  171. * @return void
  172. */
  173. public function testLoadPlugin()
  174. {
  175. $this->loadPlugins(['TestPlugin']);
  176. $this->assertNull($this->template->load('TestPlugin.test_templates'));
  177. $this->assertEquals('<em>{{text}}</em>', $this->template->get('italic'));
  178. $this->clearPlugins();
  179. }
  180. /**
  181. * Test that loading non-existing templates causes errors.
  182. *
  183. */
  184. public function testLoadErrorNoFile()
  185. {
  186. $this->expectException(\Cake\Core\Exception\Exception::class);
  187. $this->expectExceptionMessage('Could not load configuration file');
  188. $this->template->load('no_such_file');
  189. }
  190. /**
  191. * Test formatting compact attributes.
  192. *
  193. * @return void
  194. */
  195. public function testFormatAttributesCompact()
  196. {
  197. $attrs = ['disabled' => true, 'selected' => 1, 'checked' => '1', 'multiple' => 'multiple'];
  198. $result = $this->template->formatAttributes($attrs);
  199. $this->assertEquals(
  200. ' disabled="disabled" selected="selected" checked="checked" multiple="multiple"',
  201. $result
  202. );
  203. $attrs = ['disabled' => false, 'selected' => 0, 'checked' => '0', 'multiple' => null];
  204. $result = $this->template->formatAttributes($attrs);
  205. $this->assertEquals(
  206. '',
  207. $result
  208. );
  209. }
  210. /**
  211. * Test formatting normal attributes.
  212. *
  213. * @return void
  214. */
  215. public function testFormatAttributes()
  216. {
  217. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>', 'spellcheck' => 'true'];
  218. $result = $this->template->formatAttributes($attrs);
  219. $this->assertEquals(
  220. ' name="bruce" data-hero="&lt;batman&gt;" spellcheck="true"',
  221. $result
  222. );
  223. $attrs = ['escape' => false, 'name' => 'bruce', 'data-hero' => '<batman>'];
  224. $result = $this->template->formatAttributes($attrs);
  225. $this->assertEquals(
  226. ' name="bruce" data-hero="<batman>"',
  227. $result
  228. );
  229. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>'];
  230. $result = $this->template->formatAttributes($attrs, ['name']);
  231. $this->assertEquals(
  232. ' data-hero="&lt;batman&gt;"',
  233. $result
  234. );
  235. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>', 'templateVars' => ['foo' => 'bar']];
  236. $result = $this->template->formatAttributes($attrs, ['name']);
  237. $this->assertEquals(
  238. ' data-hero="&lt;batman&gt;"',
  239. $result
  240. );
  241. $evilKey = "><script>alert(1)</script>";
  242. $attrs = [$evilKey => 'some value'];
  243. $result = $this->template->formatAttributes($attrs);
  244. $this->assertEquals(
  245. ' &gt;&lt;script&gt;alert(1)&lt;/script&gt;="some value"',
  246. $result
  247. );
  248. }
  249. /**
  250. * Test formatting array attributes.
  251. *
  252. * @return void
  253. */
  254. public function testFormatAttributesArray()
  255. {
  256. $attrs = ['name' => ['bruce', 'wayne']];
  257. $result = $this->template->formatAttributes($attrs);
  258. $this->assertEquals(
  259. ' name="bruce wayne"',
  260. $result
  261. );
  262. }
  263. /**
  264. * test push/pop templates.
  265. *
  266. * @return void
  267. */
  268. public function testPushPopTemplates()
  269. {
  270. $this->template->add(['name' => '{{name}} is my name']);
  271. $this->assertNull($this->template->push());
  272. $this->template->add(['name' => 'my name']);
  273. $this->assertEquals('my name', $this->template->get('name'));
  274. $this->assertNull($this->template->pop());
  275. $this->assertEquals('{{name}} is my name', $this->template->get('name'));
  276. $this->assertNull($this->template->pop());
  277. $this->assertNull($this->template->pop());
  278. }
  279. /**
  280. * Test addClass method newClass parameter
  281. *
  282. * Tests null, string, array and false for `input`
  283. *
  284. * @return void
  285. */
  286. public function testAddClassMethodNewClass()
  287. {
  288. $result = $this->template->addClass([], 'new_class');
  289. $this->assertEquals($result, ['class' => ['new_class']]);
  290. $result = $this->template->addClass([], ['new_class']);
  291. $this->assertEquals($result, ['class' => ['new_class']]);
  292. $result = $this->template->addClass([], false);
  293. $this->assertEquals($result, []);
  294. $result = $this->template->addClass([], null);
  295. $this->assertEquals($result, []);
  296. $result = $this->template->addClass(null, null);
  297. $this->assertNull($result);
  298. }
  299. /**
  300. * Test addClass method input (currentClass) parameter
  301. *
  302. * Tests null, string, array, false and object
  303. *
  304. * @return void
  305. */
  306. public function testAddClassMethodCurrentClass()
  307. {
  308. $result = $this->template->addClass(['class' => ['current']], 'new_class');
  309. $this->assertEquals($result, ['class' => ['current', 'new_class']]);
  310. $result = $this->template->addClass('', 'new_class');
  311. $this->assertEquals($result, ['class' => ['new_class']]);
  312. $result = $this->template->addClass(null, 'new_class');
  313. $this->assertEquals($result, ['class' => ['new_class']]);
  314. $result = $this->template->addClass(false, 'new_class');
  315. $this->assertEquals($result, ['class' => ['new_class']]);
  316. $result = $this->template->addClass(new \StdClass(), 'new_class');
  317. $this->assertEquals($result, ['class' => ['new_class']]);
  318. }
  319. /**
  320. * Test addClass method string parameter, it should fallback to string
  321. *
  322. * @return void
  323. */
  324. public function testAddClassMethodFallbackToString()
  325. {
  326. $result = $this->template->addClass('current', 'new_class');
  327. $this->assertEquals($result, ['class' => ['current', 'new_class']]);
  328. }
  329. /**
  330. * Test addClass method to make sure the returned array is unique
  331. *
  332. * @return void
  333. */
  334. public function testAddClassMethodUnique()
  335. {
  336. $result = $this->template->addClass(['class' => ['new_class']], 'new_class');
  337. $this->assertEquals($result, ['class' => ['new_class']]);
  338. }
  339. /**
  340. * Test addClass method useIndex param
  341. *
  342. * Tests for useIndex being the default, 'my_class' and false
  343. *
  344. * @return void
  345. */
  346. public function testAddClassMethodUseIndex()
  347. {
  348. $result = $this->template->addClass(
  349. [
  350. 'class' => 'current_class',
  351. 'other_index1' => false,
  352. 'type' => 'text',
  353. ],
  354. 'new_class',
  355. 'class'
  356. );
  357. $this->assertEquals($result, [
  358. 'class' => ['current_class', 'new_class'],
  359. 'other_index1' => false,
  360. 'type' => 'text',
  361. ]);
  362. $result = $this->template->addClass(
  363. [
  364. 'my_class' => 'current_class',
  365. 'other_index1' => false,
  366. 'type' => 'text',
  367. ],
  368. 'new_class',
  369. 'my_class'
  370. );
  371. $this->assertEquals($result, [
  372. 'other_index1' => false,
  373. 'type' => 'text',
  374. 'my_class' => ['current_class', 'new_class'],
  375. ]);
  376. $result = $this->template->addClass(
  377. [
  378. 'class' => [
  379. 'current_class',
  380. 'text',
  381. ],
  382. ],
  383. 'new_class',
  384. 'non-existent'
  385. );
  386. $this->assertEquals($result, [
  387. 'class' => [
  388. 'current_class',
  389. 'text',
  390. ],
  391. 'non-existent' => ['new_class'],
  392. ]);
  393. }
  394. }