StringTemplateTest.php 6.2 KB

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