StringTemplateTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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('not there', []);
  90. $this->assertNull($result);
  91. $result = $this->template->format('text', ['text' => '']);
  92. $this->assertSame('', $result);
  93. $result = $this->template->format('text', []);
  94. $this->assertSame('', $result);
  95. $result = $this->template->format('link', [
  96. 'url' => '/',
  97. 'text' => 'example'
  98. ]);
  99. $this->assertEquals('<a href="/">example</a>', $result);
  100. $result = $this->template->format('custom', [
  101. 'standard' => 'default',
  102. 'templateVars' => ['var1' => 'foo']
  103. ]);
  104. $this->assertEquals('<custom default v1="foo" v2="" />', $result);
  105. }
  106. /**
  107. * Test loading templates files in the app.
  108. *
  109. * @return void
  110. */
  111. public function testLoad()
  112. {
  113. $this->template->remove('attribute');
  114. $this->template->remove('compactAttribute');
  115. $this->assertEquals([], $this->template->get());
  116. $this->assertNull($this->template->load('test_templates'));
  117. $this->assertEquals('<a href="{{url}}">{{text}}</a>', $this->template->get('link'));
  118. }
  119. /**
  120. * Test loading templates files from a plugin
  121. *
  122. * @return void
  123. */
  124. public function testLoadPlugin()
  125. {
  126. Plugin::load('TestPlugin');
  127. $this->assertNull($this->template->load('TestPlugin.test_templates'));
  128. $this->assertEquals('<em>{{text}}</em>', $this->template->get('italic'));
  129. }
  130. /**
  131. * Test that loading non-existing templates causes errors.
  132. *
  133. * @expectedException \Cake\Core\Exception\Exception
  134. * @expectedExceptionMessage Could not load configuration file
  135. */
  136. public function testLoadErrorNoFile()
  137. {
  138. $this->template->load('no_such_file');
  139. }
  140. /**
  141. * Test formatting compact attributes.
  142. *
  143. * @return void
  144. */
  145. public function testFormatAttributesCompact()
  146. {
  147. $attrs = ['disabled' => true, 'selected' => 1, 'checked' => '1', 'multiple' => 'multiple'];
  148. $result = $this->template->formatAttributes($attrs);
  149. $this->assertEquals(
  150. ' disabled="disabled" selected="selected" checked="checked" multiple="multiple"',
  151. $result
  152. );
  153. $attrs = ['disabled' => false, 'selected' => 0, 'checked' => '0', 'multiple' => null];
  154. $result = $this->template->formatAttributes($attrs);
  155. $this->assertEquals(
  156. '',
  157. $result
  158. );
  159. }
  160. /**
  161. * Test formatting normal attributes.
  162. *
  163. * @return void
  164. */
  165. public function testFormatAttributes()
  166. {
  167. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>'];
  168. $result = $this->template->formatAttributes($attrs);
  169. $this->assertEquals(
  170. ' name="bruce" data-hero="&lt;batman&gt;"',
  171. $result
  172. );
  173. $attrs = ['escape' => false, 'name' => 'bruce', 'data-hero' => '<batman>'];
  174. $result = $this->template->formatAttributes($attrs);
  175. $this->assertEquals(
  176. ' name="bruce" data-hero="<batman>"',
  177. $result
  178. );
  179. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>'];
  180. $result = $this->template->formatAttributes($attrs, ['name']);
  181. $this->assertEquals(
  182. ' data-hero="&lt;batman&gt;"',
  183. $result
  184. );
  185. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>', 'templateVars' => ['foo' => 'bar']];
  186. $result = $this->template->formatAttributes($attrs, ['name']);
  187. $this->assertEquals(
  188. ' data-hero="&lt;batman&gt;"',
  189. $result
  190. );
  191. }
  192. /**
  193. * Test formatting array attributes.
  194. *
  195. * @return void
  196. */
  197. public function testFormatAttributesArray()
  198. {
  199. $attrs = ['name' => ['bruce', 'wayne']];
  200. $result = $this->template->formatAttributes($attrs);
  201. $this->assertEquals(
  202. ' name="bruce wayne"',
  203. $result
  204. );
  205. }
  206. /**
  207. * test push/pop templates.
  208. *
  209. * @return void
  210. */
  211. public function testPushPopTemplates()
  212. {
  213. $this->template->add(['name' => '{{name}} is my name']);
  214. $this->assertNull($this->template->push());
  215. $this->template->add(['name' => 'my name']);
  216. $this->assertEquals('my name', $this->template->get('name'));
  217. $this->assertNull($this->template->pop());
  218. $this->assertEquals('{{name}} is my name', $this->template->get('name'));
  219. $this->assertNull($this->template->pop());
  220. $this->assertNull($this->template->pop());
  221. }
  222. }