StringTemplateTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. App::uses('StringTemplate', 'Tools.View');
  3. class StringTemplateTest extends CakeTestCase {
  4. /**
  5. * setUp
  6. *
  7. * @return void
  8. */
  9. public function setUp() {
  10. parent::setUp();
  11. $this->template = new StringTemplate();
  12. }
  13. /**
  14. * Test adding templates through the constructor.
  15. *
  16. * @return void
  17. */
  18. public function testConstructorAdd() {
  19. $templates = [
  20. 'link' => '<a href="{{url}}">{{text}}</a>'
  21. ];
  22. $template = new StringTemplate($templates);
  23. debug($template->config('link'));
  24. $this->assertEquals($templates['link'], $template->config('link'));
  25. }
  26. /**
  27. * test adding templates.
  28. *
  29. * @return void
  30. */
  31. public function testAdd() {
  32. $templates = [
  33. 'link' => '<a href="{{url}}">{{text}}</a>'
  34. ];
  35. $result = $this->template->add($templates);
  36. $this->assertSame(
  37. $this->template,
  38. $result,
  39. 'The same instance should be returned'
  40. );
  41. $this->assertEquals($templates['link'], $this->template->config('link'));
  42. }
  43. /**
  44. * Test remove.
  45. *
  46. * @return void
  47. */
  48. public function testRemove() {
  49. $templates = [
  50. 'link' => '<a href="{{url}}">{{text}}</a>'
  51. ];
  52. $this->template->add($templates);
  53. $this->assertNull($this->template->remove('link'), 'No return');
  54. $this->assertNull($this->template->config('link'), 'Template should be gone.');
  55. }
  56. /**
  57. * Test formatting strings.
  58. *
  59. * @return void
  60. */
  61. public function testFormat() {
  62. $templates = [
  63. 'link' => '<a href="{{url}}">{{text}}</a>'
  64. ];
  65. $this->template->add($templates);
  66. $result = $this->template->format('not there', []);
  67. $this->assertSame('', $result);
  68. $result = $this->template->format('link', [
  69. 'url' => '/',
  70. 'text' => 'example'
  71. ]);
  72. $this->assertEquals('<a href="/">example</a>', $result);
  73. }
  74. /**
  75. * Test loading templates files in the app.
  76. *
  77. * @return void
  78. */
  79. public function testLoad() {
  80. $this->skipIf(true, 'Find a way to mock the path from /Tools/Config to /Tools/Test/test_app/Config');
  81. $this->template->remove('attribute');
  82. $this->template->remove('compactAttribute');
  83. $this->assertEquals([], $this->template->config());
  84. $this->assertNull($this->template->load('Tools.test_templates'));
  85. $this->assertEquals('<a href="{{url}}">{{text}}</a>', $this->template->config('link'));
  86. }
  87. /**
  88. * Test that loading non-existing templates causes errors.
  89. *
  90. * @expectedException ConfigureException
  91. * @expectedExceptionMessage Could not load configuration file
  92. */
  93. public function testLoadErrorNoFile() {
  94. $this->template->load('no_such_file');
  95. }
  96. /**
  97. * Test formatting compact attributes.
  98. *
  99. * @return void
  100. */
  101. public function testFormatAttributesCompact() {
  102. $attrs = ['disabled' => true, 'selected' => 1, 'checked' => '1', 'multiple' => 'multiple'];
  103. $result = $this->template->formatAttributes($attrs);
  104. $this->assertEquals(
  105. ' disabled="disabled" selected="selected" checked="checked" multiple="multiple"',
  106. $result
  107. );
  108. $attrs = ['disabled' => false, 'selected' => 0, 'checked' => '0', 'multiple' => null];
  109. $result = $this->template->formatAttributes($attrs);
  110. $this->assertEquals(
  111. '',
  112. $result
  113. );
  114. }
  115. /**
  116. * Test formatting normal attributes.
  117. *
  118. * @return void
  119. */
  120. public function testFormatAttributes() {
  121. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>'];
  122. $result = $this->template->formatAttributes($attrs);
  123. $this->assertEquals(
  124. ' name="bruce" data-hero="&lt;batman&gt;"',
  125. $result
  126. );
  127. $attrs = ['escape' => false, 'name' => 'bruce', 'data-hero' => '<batman>'];
  128. $result = $this->template->formatAttributes($attrs);
  129. $this->assertEquals(
  130. ' name="bruce" data-hero="<batman>"',
  131. $result
  132. );
  133. $attrs = ['name' => 'bruce', 'data-hero' => '<batman>'];
  134. $result = $this->template->formatAttributes($attrs, ['name']);
  135. $this->assertEquals(
  136. ' data-hero="&lt;batman&gt;"',
  137. $result
  138. );
  139. }
  140. /**
  141. * Test formatting array attributes.
  142. *
  143. * @return void
  144. */
  145. public function testFormatAttributesArray() {
  146. $attrs = ['name' => ['bruce', 'wayne']];
  147. $result = $this->template->formatAttributes($attrs);
  148. $this->assertEquals(
  149. ' name="bruce wayne"',
  150. $result
  151. );
  152. }
  153. /**
  154. * Tests that compile information is refreshed on adds and removes
  155. *
  156. * @return void
  157. */
  158. public function testCopiledInfoRefresh() {
  159. $compilation = $this->template->compile('link');
  160. $this->template->add([
  161. 'link' => '<a bar="{{foo}}">{{baz}}</a>'
  162. ]);
  163. $this->assertNotEquals($compilation, $this->template->compile('link'));
  164. $this->template->remove('link');
  165. $this->assertEquals([null, null], $this->template->compile('link'));
  166. }
  167. }