StringTemplateTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 CakePHP(tm) v3.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->assertNull($result, 'No return');
  52. $this->assertEquals($templates['link'], $this->template->get('link'));
  53. }
  54. /**
  55. * Test remove.
  56. *
  57. * @return void
  58. */
  59. public function testRemove() {
  60. $templates = [
  61. 'link' => '<a href="{{url}}">{{text}}</a>'
  62. ];
  63. $this->template->add($templates);
  64. $this->assertNull($this->template->remove('link'), 'No return');
  65. $this->assertNull($this->template->get('link'), 'Template should be gone.');
  66. }
  67. /**
  68. * Test formatting strings.
  69. *
  70. * @return void
  71. */
  72. public function testFormat() {
  73. $templates = [
  74. 'link' => '<a href="{{url}}">{{text}}</a>'
  75. ];
  76. $this->template->add($templates);
  77. $result = $this->template->format('not there', []);
  78. $this->assertSame('', $result);
  79. $result = $this->template->format('link', [
  80. 'url' => '/',
  81. 'text' => 'example'
  82. ]);
  83. $this->assertEquals('<a href="/">example</a>', $result);
  84. }
  85. /**
  86. * Test loading templates files in the app.
  87. *
  88. * @return void
  89. */
  90. public function testLoad() {
  91. $this->template->remove('attribute');
  92. $this->template->remove('compactAttribute');
  93. $this->assertEquals([], $this->template->get());
  94. $this->assertNull($this->template->load('test_templates'));
  95. $this->assertEquals('<a href="{{url}}">{{text}}</a>', $this->template->get('link'));
  96. }
  97. /**
  98. * Test loading templates files from a plugin
  99. *
  100. * @return void
  101. */
  102. public function testLoadPlugin() {
  103. Plugin::load('TestPlugin');
  104. $this->assertNull($this->template->load('TestPlugin.test_templates'));
  105. $this->assertEquals('<em>{{text}}</em>', $this->template->get('italic'));
  106. }
  107. /**
  108. * Test that loading non-existing templates causes errors.
  109. *
  110. * @expectedException \Cake\Error\Exception
  111. * @expectedExceptionMessage Could not load configuration file
  112. */
  113. public function testLoadErrorNoFile() {
  114. $this->template->load('no_such_file');
  115. }
  116. /**
  117. * Test formatting compact attributes.
  118. *
  119. * @return void
  120. */
  121. public function testFormatAttributesCompact() {
  122. $attrs = ['disabled' => true, 'selected' => 1, 'checked' => '1', 'multiple' => 'multiple'];
  123. $result = $this->template->formatAttributes($attrs);
  124. $this->assertEquals(
  125. ' disabled="disabled" selected="selected" checked="checked" multiple="multiple"',
  126. $result
  127. );
  128. $attrs = ['disabled' => false, 'selected' => 0, 'checked' => '0', 'multiple' => null];
  129. $result = $this->template->formatAttributes($attrs);
  130. $this->assertEquals(
  131. '',
  132. $result
  133. );
  134. }
  135. /**
  136. * Test formatting normal attributes.
  137. *
  138. * @return void
  139. */
  140. public function testFormatAttributes() {
  141. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>'];
  142. $result = $this->template->formatAttributes($attrs);
  143. $this->assertEquals(
  144. ' name="bruce" data-hero="&lt;batman&gt;"',
  145. $result
  146. );
  147. $attrs = ['escape' => false, 'name' => 'bruce', 'data-hero' => '<batman>'];
  148. $result = $this->template->formatAttributes($attrs);
  149. $this->assertEquals(
  150. ' name="bruce" data-hero="<batman>"',
  151. $result
  152. );
  153. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>'];
  154. $result = $this->template->formatAttributes($attrs, ['name']);
  155. $this->assertEquals(
  156. ' data-hero="&lt;batman&gt;"',
  157. $result
  158. );
  159. }
  160. /**
  161. * Test formatting array attributes.
  162. *
  163. * @return void
  164. */
  165. public function testFormatAttributesArray() {
  166. $attrs = ['name' => ['bruce', 'wayne']];
  167. $result = $this->template->formatAttributes($attrs);
  168. $this->assertEquals(
  169. ' name="bruce wayne"',
  170. $result
  171. );
  172. }
  173. }