HelperTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\tests\cases\template;
  9. use lithium\tests\mocks\template\MockHelper;
  10. use lithium\tests\mocks\template\MockRenderer;
  11. class HelperTest extends \lithium\test\Unit {
  12. public function setUp() {
  13. $this->helper = new MockHelper();
  14. }
  15. /**
  16. * Tests that constructor parameters are properly assigned to protected properties.
  17. *
  18. * @return void
  19. */
  20. public function testObjectConstructionWithParameters() {
  21. $this->assertNull($this->helper->_context);
  22. $params = array(
  23. 'context' => new MockRenderer(),
  24. 'handlers' => array('content' => function($value) { return "\n{$value}\n"; })
  25. );
  26. $helper = new MockHelper($params);
  27. $this->assertEqual($helper->_context, $params['context']);
  28. }
  29. /**
  30. * Tests the default escaping for HTML output. When implementing helpers that do not output
  31. * HTML/XML, the `escape()` method should be overridden accordingly.
  32. *
  33. * @return void
  34. */
  35. public function testDefaultEscaping() {
  36. $result = $this->helper->escape('<script>alert("XSS!");</script>');
  37. $expected = '&lt;script&gt;alert(&quot;XSS!&quot;);&lt;/script&gt;';
  38. $this->assertEqual($expected, $result);
  39. $result = $this->helper->escape('<script>//alert("XSS!");</script>', null, array(
  40. 'escape' => false
  41. ));
  42. $expected = '<script>//alert("XSS!");</script>';
  43. $this->assertEqual($expected, $result);
  44. $result = $this->helper->escape(array(
  45. '<script>alert("XSS!");</script>', '<script>alert("XSS!");</script>'
  46. ));
  47. $expected = array(
  48. '&lt;script&gt;alert(&quot;XSS!&quot;);&lt;/script&gt;',
  49. '&lt;script&gt;alert(&quot;XSS!&quot;);&lt;/script&gt;'
  50. );
  51. $this->assertEqual($expected, $result);
  52. }
  53. /**
  54. * Tests unescaped values passed through the escape() method. Unescaped values
  55. * should be returned exactly the same as the original value.
  56. *
  57. * @return void
  58. */
  59. public function testUnescapedValue() {
  60. $value = '<blockquote>"Thou shalt not escape!"</blockquote>';
  61. $result = $this->helper->escape($value, null, array('escape' => false));
  62. $this->assertEqual($value, $result);
  63. }
  64. public function testOptions() {
  65. $defaults = array('value' => null);
  66. $options = array('value' => 1, 'title' => 'one');
  67. $expected = array(
  68. array('value' => 1, 'title' => 'one'),
  69. array('title' => 'one')
  70. );
  71. $result = $this->helper->testOptions($defaults, $options);
  72. $this->assertEqual($expected, $result);
  73. }
  74. public function testAttributes() {
  75. $attributes = array('value' => 1, 'title' => 'one');
  76. $expected = ' value="1" title="one"';
  77. $result = $this->helper->testAttributes($attributes);
  78. $this->assertEqual($expected, $result);
  79. $attributes = ' value="1" title="one"';
  80. $result = $this->helper->testAttributes('value="1" title="one"');
  81. $this->assertEqual($expected, $result);
  82. $attributes = array('checked' => true, 'title' => 'one');
  83. $expected = ' checked="checked" title="one"';
  84. $result = $this->helper->testAttributes($attributes);
  85. $this->assertEqual($expected, $result);
  86. $attributes = array('checked' => false);
  87. $result = $this->helper->testAttributes($attributes);
  88. $this->assertEqual('', $result);
  89. }
  90. public function testAttributeEscaping() {
  91. $attributes = array('checked' => true, 'title' => '<foo>');
  92. $expected = ' checked="checked" title="&lt;foo&gt;"';
  93. $result = $this->helper->testAttributes($attributes);
  94. $this->assertEqual($expected, $result);
  95. $attributes = array('checked' => true, 'title' => '<foo>');
  96. $expected = ' checked="checked" title="<foo>"';
  97. $result = $this->helper->testAttributes($attributes, null, array('escape' => false));
  98. $this->assertEqual($expected, $result);
  99. }
  100. public function testAttributeMinimization() {
  101. $attributes = array('selected' => 1);
  102. $expected = ' selected="selected"';
  103. $result = $this->helper->testAttributes($attributes);
  104. $this->assertEqual($expected, $result);
  105. $attributes = array('selected' => true);
  106. $expected = ' selected="selected"';
  107. $result = $this->helper->testAttributes($attributes);
  108. $this->assertEqual($expected, $result);
  109. $attributes = array('selected' => 'true');
  110. $expected = ' selected="true"';
  111. $result = $this->helper->testAttributes($attributes);
  112. $this->assertEqual($expected, $result);
  113. }
  114. public function testInstantiationWithNoContext() {
  115. $this->helper = new MockHelper();
  116. $result = $this->helper->testRender(null, "foo {:bar}", array('bar' => 'baz'));
  117. $this->assertEqual("foo baz", $result);
  118. }
  119. public function testRender() {
  120. $params = array(
  121. 'context' => new MockRenderer(),
  122. 'handlers' => array('content' => function($value) { return "\n{$value}\n"; })
  123. );
  124. $helper = new MockHelper($params);
  125. $config = array(
  126. 'title' => 'cool',
  127. 'url' => '/here',
  128. 'options' => array('value' => 1, 'title' => 'one')
  129. );
  130. $expected = '<a href="/here" value="1" title="one">cool</a>';
  131. $result = $helper->testRender('link', 'link', $config);
  132. $this->assertEqual($expected, $result);
  133. $handlers = array('path' => function($path) { return "/webroot{$path}"; });
  134. $params = array('context' => new MockRenderer(compact('handlers')));
  135. $helper = new MockHelper($params);
  136. $handlers = array('url' => 'path');
  137. $expected = '<a href="/webroot/here" value="1" title="one">cool</a>';
  138. $result = $helper->testRender('link', 'link', $config, compact('handlers'));
  139. $this->assertEqual($expected, $result);
  140. }
  141. }
  142. ?>