StringTemplateTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. Plugin::load('TestPlugin');
  176. $this->assertNull($this->template->load('TestPlugin.test_templates'));
  177. $this->assertEquals('<em>{{text}}</em>', $this->template->get('italic'));
  178. }
  179. /**
  180. * Test that loading non-existing templates causes errors.
  181. *
  182. */
  183. public function testLoadErrorNoFile()
  184. {
  185. $this->expectException(\Cake\Core\Exception\Exception::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. * @return void
  193. */
  194. public function testFormatAttributesCompact()
  195. {
  196. $attrs = ['disabled' => true, 'selected' => 1, 'checked' => '1', 'multiple' => 'multiple'];
  197. $result = $this->template->formatAttributes($attrs);
  198. $this->assertEquals(
  199. ' disabled="disabled" selected="selected" checked="checked" multiple="multiple"',
  200. $result
  201. );
  202. $attrs = ['disabled' => false, 'selected' => 0, 'checked' => '0', 'multiple' => null];
  203. $result = $this->template->formatAttributes($attrs);
  204. $this->assertEquals(
  205. '',
  206. $result
  207. );
  208. }
  209. /**
  210. * Test formatting normal attributes.
  211. *
  212. * @return void
  213. */
  214. public function testFormatAttributes()
  215. {
  216. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>', 'spellcheck' => 'true'];
  217. $result = $this->template->formatAttributes($attrs);
  218. $this->assertEquals(
  219. ' name="bruce" data-hero="&lt;batman&gt;" spellcheck="true"',
  220. $result
  221. );
  222. $attrs = ['escape' => false, 'name' => 'bruce', 'data-hero' => '<batman>'];
  223. $result = $this->template->formatAttributes($attrs);
  224. $this->assertEquals(
  225. ' name="bruce" data-hero="<batman>"',
  226. $result
  227. );
  228. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>'];
  229. $result = $this->template->formatAttributes($attrs, ['name']);
  230. $this->assertEquals(
  231. ' data-hero="&lt;batman&gt;"',
  232. $result
  233. );
  234. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>', 'templateVars' => ['foo' => 'bar']];
  235. $result = $this->template->formatAttributes($attrs, ['name']);
  236. $this->assertEquals(
  237. ' data-hero="&lt;batman&gt;"',
  238. $result
  239. );
  240. $evilKey = "><script>alert(1)</script>";
  241. $attrs = [$evilKey => 'some value'];
  242. $result = $this->template->formatAttributes($attrs);
  243. $this->assertEquals(
  244. ' &gt;&lt;script&gt;alert(1)&lt;/script&gt;="some value"',
  245. $result
  246. );
  247. }
  248. /**
  249. * Test formatting array attributes.
  250. *
  251. * @return void
  252. */
  253. public function testFormatAttributesArray()
  254. {
  255. $attrs = ['name' => ['bruce', 'wayne']];
  256. $result = $this->template->formatAttributes($attrs);
  257. $this->assertEquals(
  258. ' name="bruce wayne"',
  259. $result
  260. );
  261. }
  262. /**
  263. * test push/pop templates.
  264. *
  265. * @return void
  266. */
  267. public function testPushPopTemplates()
  268. {
  269. $this->template->add(['name' => '{{name}} is my name']);
  270. $this->assertNull($this->template->push());
  271. $this->template->add(['name' => 'my name']);
  272. $this->assertEquals('my name', $this->template->get('name'));
  273. $this->assertNull($this->template->pop());
  274. $this->assertEquals('{{name}} is my name', $this->template->get('name'));
  275. $this->assertNull($this->template->pop());
  276. $this->assertNull($this->template->pop());
  277. }
  278. /**
  279. * Test addClass method newClass parameter
  280. *
  281. * Tests null, string, array and false for `input`
  282. *
  283. * @return void
  284. */
  285. public function testAddClassMethodNewClass()
  286. {
  287. $result = $this->template->addClass([], 'new_class');
  288. $this->assertEquals($result, ['class' => ['new_class']]);
  289. $result = $this->template->addClass([], ['new_class']);
  290. $this->assertEquals($result, ['class' => ['new_class']]);
  291. $result = $this->template->addClass([], false);
  292. $this->assertEquals($result, []);
  293. $result = $this->template->addClass([], null);
  294. $this->assertEquals($result, []);
  295. $result = $this->template->addClass(null, null);
  296. $this->assertNull($result);
  297. }
  298. /**
  299. * Test addClass method input (currentClass) parameter
  300. *
  301. * Tests null, string, array, false and object
  302. *
  303. * @return void
  304. */
  305. public function testAddClassMethodCurrentClass()
  306. {
  307. $result = $this->template->addClass(['class' => ['current']], 'new_class');
  308. $this->assertEquals($result, ['class' => ['current', 'new_class']]);
  309. $result = $this->template->addClass('', 'new_class');
  310. $this->assertEquals($result, ['class' => ['new_class']]);
  311. $result = $this->template->addClass(null, 'new_class');
  312. $this->assertEquals($result, ['class' => ['new_class']]);
  313. $result = $this->template->addClass(false, 'new_class');
  314. $this->assertEquals($result, ['class' => ['new_class']]);
  315. $result = $this->template->addClass(new \StdClass(), 'new_class');
  316. $this->assertEquals($result, ['class' => ['new_class']]);
  317. }
  318. /**
  319. * Test addClass method string parameter, it should fallback to string
  320. *
  321. * @return void
  322. */
  323. public function testAddClassMethodFallbackToString()
  324. {
  325. $result = $this->template->addClass('current', 'new_class');
  326. $this->assertEquals($result, ['class' => ['current', 'new_class']]);
  327. }
  328. /**
  329. * Test addClass method to make sure the returned array is unique
  330. *
  331. * @return void
  332. */
  333. public function testAddClassMethodUnique()
  334. {
  335. $result = $this->template->addClass(['class' => ['new_class']], 'new_class');
  336. $this->assertEquals($result, ['class' => ['new_class']]);
  337. }
  338. /**
  339. * Test addClass method useIndex param
  340. *
  341. * Tests for useIndex being the default, 'my_class' and false
  342. *
  343. * @return void
  344. */
  345. public function testAddClassMethodUseIndex()
  346. {
  347. $result = $this->template->addClass(
  348. [
  349. 'class' => 'current_class',
  350. 'other_index1' => false,
  351. 'type' => 'text'
  352. ],
  353. 'new_class',
  354. 'class'
  355. );
  356. $this->assertEquals($result, [
  357. 'class' => ['current_class', 'new_class'],
  358. 'other_index1' => false,
  359. 'type' => 'text'
  360. ]);
  361. $result = $this->template->addClass(
  362. [
  363. 'my_class' => 'current_class',
  364. 'other_index1' => false,
  365. 'type' => 'text'
  366. ],
  367. 'new_class',
  368. 'my_class'
  369. );
  370. $this->assertEquals($result, [
  371. 'other_index1' => false,
  372. 'type' => 'text',
  373. 'my_class' => ['current_class', 'new_class']
  374. ]);
  375. $result = $this->template->addClass(
  376. [
  377. 'class' => [
  378. 'current_class',
  379. 'text'
  380. ]
  381. ],
  382. 'new_class',
  383. 'non-existent'
  384. );
  385. $this->assertEquals($result, [
  386. 'class' => [
  387. 'current_class',
  388. 'text'
  389. ],
  390. 'non-existent' => ['new_class']
  391. ]);
  392. }
  393. }