StringTemplateTest.php 6.4 KB

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