StringTemplateTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. * setUp
  22. *
  23. * @return void
  24. */
  25. public function setUp() {
  26. parent::setUp();
  27. $this->template = new StringTemplate();
  28. }
  29. /**
  30. * Test adding templates through the constructor.
  31. *
  32. * @return void
  33. */
  34. public function testConstructorAdd() {
  35. $templates = [
  36. 'link' => '<a href="{{url}}">{{text}}</a>'
  37. ];
  38. $template = new StringTemplate($templates);
  39. $this->assertEquals($templates['link'], $template->get('link'));
  40. }
  41. /**
  42. * test adding templates.
  43. *
  44. * @return void
  45. */
  46. public function testAdd() {
  47. $templates = [
  48. 'link' => '<a href="{{url}}">{{text}}</a>'
  49. ];
  50. $result = $this->template->add($templates);
  51. $this->assertSame(
  52. $this->template,
  53. $result,
  54. 'The same instance should be returned'
  55. );
  56. $this->assertEquals($templates['link'], $this->template->get('link'));
  57. }
  58. /**
  59. * Test remove.
  60. *
  61. * @return void
  62. */
  63. public function testRemove() {
  64. $templates = [
  65. 'link' => '<a href="{{url}}">{{text}}</a>'
  66. ];
  67. $this->template->add($templates);
  68. $this->assertNull($this->template->remove('link'), 'No return');
  69. $this->assertNull($this->template->get('link'), 'Template should be gone.');
  70. }
  71. /**
  72. * Test formatting strings.
  73. *
  74. * @return void
  75. */
  76. public function testFormat() {
  77. $templates = [
  78. 'link' => '<a href="{{url}}">{{text}}</a>'
  79. ];
  80. $this->template->add($templates);
  81. $result = $this->template->format('not there', []);
  82. $this->assertSame('', $result);
  83. $result = $this->template->format('link', [
  84. 'url' => '/',
  85. 'text' => 'example'
  86. ]);
  87. $this->assertEquals('<a href="/">example</a>', $result);
  88. }
  89. /**
  90. * Test loading templates files in the app.
  91. *
  92. * @return void
  93. */
  94. public function testLoad() {
  95. $this->template->remove('attribute');
  96. $this->template->remove('compactAttribute');
  97. $this->assertEquals([], $this->template->get());
  98. $this->assertNull($this->template->load('test_templates'));
  99. $this->assertEquals('<a href="{{url}}">{{text}}</a>', $this->template->get('link'));
  100. }
  101. /**
  102. * Test loading templates files from a plugin
  103. *
  104. * @return void
  105. */
  106. public function testLoadPlugin() {
  107. Plugin::load('TestPlugin');
  108. $this->assertNull($this->template->load('TestPlugin.test_templates'));
  109. $this->assertEquals('<em>{{text}}</em>', $this->template->get('italic'));
  110. }
  111. /**
  112. * Test that loading non-existing templates causes errors.
  113. *
  114. * @expectedException \Cake\Core\Exception\Exception
  115. * @expectedExceptionMessage Could not load configuration file
  116. */
  117. public function testLoadErrorNoFile() {
  118. $this->template->load('no_such_file');
  119. }
  120. /**
  121. * Test formatting compact attributes.
  122. *
  123. * @return void
  124. */
  125. public function testFormatAttributesCompact() {
  126. $attrs = ['disabled' => true, 'selected' => 1, 'checked' => '1', 'multiple' => 'multiple'];
  127. $result = $this->template->formatAttributes($attrs);
  128. $this->assertEquals(
  129. ' disabled="disabled" selected="selected" checked="checked" multiple="multiple"',
  130. $result
  131. );
  132. $attrs = ['disabled' => false, 'selected' => 0, 'checked' => '0', 'multiple' => null];
  133. $result = $this->template->formatAttributes($attrs);
  134. $this->assertEquals(
  135. '',
  136. $result
  137. );
  138. }
  139. /**
  140. * Test formatting normal attributes.
  141. *
  142. * @return void
  143. */
  144. public function testFormatAttributes() {
  145. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>'];
  146. $result = $this->template->formatAttributes($attrs);
  147. $this->assertEquals(
  148. ' name="bruce" data-hero="&lt;batman&gt;"',
  149. $result
  150. );
  151. $attrs = ['escape' => false, 'name' => 'bruce', 'data-hero' => '<batman>'];
  152. $result = $this->template->formatAttributes($attrs);
  153. $this->assertEquals(
  154. ' name="bruce" data-hero="<batman>"',
  155. $result
  156. );
  157. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>'];
  158. $result = $this->template->formatAttributes($attrs, ['name']);
  159. $this->assertEquals(
  160. ' data-hero="&lt;batman&gt;"',
  161. $result
  162. );
  163. }
  164. /**
  165. * Test formatting array attributes.
  166. *
  167. * @return void
  168. */
  169. public function testFormatAttributesArray() {
  170. $attrs = ['name' => ['bruce', 'wayne']];
  171. $result = $this->template->formatAttributes($attrs);
  172. $this->assertEquals(
  173. ' name="bruce wayne"',
  174. $result
  175. );
  176. }
  177. /**
  178. * test push/pop templates.
  179. *
  180. * @return void
  181. */
  182. public function testPushPopTemplates() {
  183. $this->template->add(['name' => '{{name}} is my name']);
  184. $this->assertNull($this->template->push());
  185. $this->template->add(['name' => 'my name']);
  186. $this->assertEquals('my name', $this->template->get('name'));
  187. $this->assertNull($this->template->pop());
  188. $this->assertEquals('{{name}} is my name', $this->template->get('name'));
  189. $this->assertNull($this->template->pop());
  190. $this->assertNull($this->template->pop());
  191. }
  192. }