StringTemplateTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.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. * @expectedException \RuntimeException
  144. * @expectedExceptionMessage Cannot find template named 'missing'
  145. * @return void
  146. */
  147. public function testFormatMissingTemplate()
  148. {
  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. * @expectedException \Cake\Core\Exception\Exception
  183. * @expectedExceptionMessage Could not load configuration file
  184. */
  185. public function testLoadErrorNoFile()
  186. {
  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. }
  241. /**
  242. * Test formatting array attributes.
  243. *
  244. * @return void
  245. */
  246. public function testFormatAttributesArray()
  247. {
  248. $attrs = ['name' => ['bruce', 'wayne']];
  249. $result = $this->template->formatAttributes($attrs);
  250. $this->assertEquals(
  251. ' name="bruce wayne"',
  252. $result
  253. );
  254. }
  255. /**
  256. * test push/pop templates.
  257. *
  258. * @return void
  259. */
  260. public function testPushPopTemplates()
  261. {
  262. $this->template->add(['name' => '{{name}} is my name']);
  263. $this->assertNull($this->template->push());
  264. $this->template->add(['name' => 'my name']);
  265. $this->assertEquals('my name', $this->template->get('name'));
  266. $this->assertNull($this->template->pop());
  267. $this->assertEquals('{{name}} is my name', $this->template->get('name'));
  268. $this->assertNull($this->template->pop());
  269. $this->assertNull($this->template->pop());
  270. }
  271. /**
  272. * Test addClass method newClass parameter
  273. *
  274. * Tests null, string, array and false for `input`
  275. */
  276. public function testAddClassMethodNewClass()
  277. {
  278. // Test new class as null, string, array, false etc
  279. $result = $this->template->addClass([], 'new_class');
  280. $this->assertEquals($result, ['class' => ['new_class']]);
  281. $result = $this->template->addClass([], ['new_class']);
  282. $this->assertEquals($result, ['class' => ['new_class']]);
  283. $result = $this->template->addClass([], false);
  284. $this->assertEquals($result, []);
  285. $result = $this->template->addClass([], null);
  286. $this->assertEquals($result, []);
  287. $result = $this->template->addClass(null, null);
  288. $this->assertNull($result);
  289. }
  290. /**
  291. * Test addClass method input (currentClass) parameter
  292. *
  293. * Tests null, string, array, false and object
  294. */
  295. public function testAddClassMethodCurrentClass()
  296. {
  297. $result = $this->template->addClass(['class' => ['current']], 'new_class');
  298. $this->assertEquals($result, ['class' => ['current', 'new_class']]);
  299. $result = $this->template->addClass('', 'new_class');
  300. $this->assertEquals($result, ['class' => ['new_class']]);
  301. $result = $this->template->addClass(null, 'new_class');
  302. $this->assertEquals($result, ['class' => ['new_class']]);
  303. $result = $this->template->addClass(false, 'new_class');
  304. $this->assertEquals($result, ['class' => ['new_class']]);
  305. $result = $this->template->addClass(new \StdClass(), 'new_class');
  306. $this->assertEquals($result, ['class' => ['new_class']]);
  307. }
  308. /**
  309. * Test addClass method string parameter, it should fallback to string
  310. */
  311. public function testAddClassMethodFallbackToString()
  312. {
  313. $result = $this->template->addClass('current', 'new_class');
  314. $this->assertEquals($result, ['class' => ['current', 'new_class']]);
  315. }
  316. /**
  317. * Test addClass method to make sure the returned array is unique
  318. */
  319. public function testAddClassMethodUnique()
  320. {
  321. $result = $this->template->addClass(['class' => ['new_class']], 'new_class');
  322. $this->assertEquals($result, ['class' => ['new_class']]);
  323. }
  324. /**
  325. * Test addClass method useIndex param
  326. *
  327. * Tests for useIndex being the default, 'my_class' and false
  328. */
  329. public function testAddClassMethodUseIndex()
  330. {
  331. $result = $this->template->addClass(
  332. [
  333. 'class' => 'current_class',
  334. 'other_index1' => false,
  335. 'type' => 'text'
  336. ],
  337. 'new_class',
  338. 'class'
  339. );
  340. $this->assertEquals($result, [
  341. 'class' => ['current_class', 'new_class'],
  342. 'other_index1' => false,
  343. 'type' => 'text'
  344. ]);
  345. $result = $this->template->addClass(
  346. [
  347. 'my_class' => 'current_class',
  348. 'other_index1' => false,
  349. 'type' => 'text'
  350. ],
  351. 'new_class',
  352. 'my_class'
  353. );
  354. $this->assertEquals($result, [
  355. 'other_index1' => false,
  356. 'type' => 'text',
  357. 'my_class' => ['current_class', 'new_class']
  358. ]);
  359. $result = $this->template->addClass(
  360. [
  361. 'current_class',
  362. 'text'
  363. ],
  364. 'new_class',
  365. false
  366. );
  367. $this->assertEquals($result, [
  368. 'current_class',
  369. 'text',
  370. 'new_class'
  371. ]);
  372. }
  373. }