StringTemplateTest.php 6.9 KB

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