EscaperTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. require_once __DIR__.'/../../../bootstrap.php';
  11. use Symfony\Components\OutputEscaper\Escaper;
  12. use Symfony\Components\OutputEscaper\Safe;
  13. use Symfony\Components\OutputEscaper\IteratorDecorator;
  14. use Symfony\Components\OutputEscaper\ArrayDecorator;
  15. use Symfony\Components\OutputEscaper\ObjectDecorator;
  16. $t = new LimeTest(39);
  17. class OutputEscaperTestClass
  18. {
  19. public $title = '<strong>escaped!</strong>';
  20. public function getTitle()
  21. {
  22. return $this->title;
  23. }
  24. public function getTitleTitle()
  25. {
  26. $o = new self;
  27. return $o->getTitle();
  28. }
  29. }
  30. class OutputEscaperTestClassChild extends OutputEscaperTestClass
  31. {
  32. }
  33. // ::escape()
  34. $t->diag('::escape()');
  35. $t->diag('::escape() does not escape special values');
  36. $t->ok(Escaper::escape('esc_entities', null) === null, '::escape() returns null if the value to escape is null');
  37. $t->ok(Escaper::escape('esc_entities', false) === false, '::escape() returns false if the value to escape is false');
  38. $t->ok(Escaper::escape('esc_entities', true) === true, '::escape() returns true if the value to escape is true');
  39. $t->diag('::escape() does not escape a value when escaping method is ESC_RAW');
  40. $t->is(Escaper::escape('esc_raw', '<strong>escaped!</strong>'), '<strong>escaped!</strong>', '::escape() takes an escaping strategy function name as its first argument');
  41. $t->diag('::escape() escapes strings');
  42. $t->is(Escaper::escape('esc_entities', '<strong>escaped!</strong>'), '&lt;strong&gt;escaped!&lt;/strong&gt;', '::escape() returns an escaped string if the value to escape is a string');
  43. $t->is(Escaper::escape('esc_entities', '<strong>échappé</strong>'), '&lt;strong&gt;&eacute;chapp&eacute;&lt;/strong&gt;', '::escape() returns an escaped string if the value to escape is a string');
  44. $t->diag('::escape() escapes arrays');
  45. $input = array(
  46. 'foo' => '<strong>escaped!</strong>',
  47. 'bar' => array('foo' => '<strong>escaped!</strong>'),
  48. );
  49. $output = Escaper::escape('esc_entities', $input);
  50. $t->ok($output instanceof ArrayDecorator, '::escape() returns a ArrayDecorator object if the value to escape is an array');
  51. $t->is($output['foo'], '&lt;strong&gt;escaped!&lt;/strong&gt;', '::escape() escapes all elements of the original array');
  52. $t->is($output['bar']['foo'], '&lt;strong&gt;escaped!&lt;/strong&gt;', '::escape() is recursive');
  53. $t->is($output->getRawValue(), $input, '->getRawValue() returns the unescaped value');
  54. $t->diag('::escape() escapes objects');
  55. $input = new OutputEscaperTestClass();
  56. $output = Escaper::escape('esc_entities', $input);
  57. $t->ok($output instanceof ObjectDecorator, '::escape() returns a ObjectDecorator object if the value to escape is an object');
  58. $t->is($output->getTitle(), '&lt;strong&gt;escaped!&lt;/strong&gt;', '::escape() escapes all methods of the original object');
  59. $t->is($output->title, '&lt;strong&gt;escaped!&lt;/strong&gt;', '::escape() escapes all properties of the original object');
  60. $t->is($output->getTitleTitle(), '&lt;strong&gt;escaped!&lt;/strong&gt;', '::escape() is recursive');
  61. $t->is($output->getRawValue(), $input, '->getRawValue() returns the unescaped value');
  62. $t->is(Escaper::escape('esc_entities', $output)->getTitle(), '&lt;strong&gt;escaped!&lt;/strong&gt;', '::escape() does not double escape an object');
  63. $t->ok(Escaper::escape('esc_entities', new \DirectoryIterator('.')) instanceof IteratorDecorator, '::escape() returns a IteratorDecorator object if the value to escape is an object that implements the ArrayAccess interface');
  64. $t->diag('::escape() does not escape object marked as being safe');
  65. $t->ok(Escaper::escape('esc_entities', new Safe(new OutputEscaperTestClass())) instanceof OutputEscaperTestClass, '::escape() returns the original value if it is marked as being safe');
  66. Escaper::markClassAsSafe('OutputEscaperTestClass');
  67. $t->ok(Escaper::escape('esc_entities', new OutputEscaperTestClass()) instanceof OutputEscaperTestClass, '::escape() returns the original value if the object class is marked as being safe');
  68. $t->ok(Escaper::escape('esc_entities', new OutputEscaperTestClassChild()) instanceof OutputEscaperTestClassChild, '::escape() returns the original value if one of the object parent class is marked as being safe');
  69. $t->diag('::escape() cannot escape resources');
  70. $fh = fopen(__FILE__, 'r');
  71. try
  72. {
  73. Escaper::escape('esc_entities', $fh);
  74. $t->fail('::escape() throws an InvalidArgumentException if the value cannot be escaped');
  75. }
  76. catch (InvalidArgumentException $e)
  77. {
  78. $t->pass('::escape() throws an InvalidArgumentException if the value cannot be escaped');
  79. }
  80. // ::unescape()
  81. $t->diag('::unescape()');
  82. $t->diag('::unescape() does not unescape special values');
  83. $t->ok(Escaper::unescape(null) === null, '::unescape() returns null if the value to unescape is null');
  84. $t->ok(Escaper::unescape(false) === false, '::unescape() returns false if the value to unescape is false');
  85. $t->ok(Escaper::unescape(true) === true, '::unescape() returns true if the value to unescape is true');
  86. $t->diag('::unescape() unescapes strings');
  87. $t->is(Escaper::unescape('&lt;strong&gt;escaped!&lt;/strong&gt;'), '<strong>escaped!</strong>', '::unescape() returns an unescaped string if the value to unescape is a string');
  88. $t->is(Escaper::unescape('&lt;strong&gt;&eacute;chapp&eacute;&lt;/strong&gt;'), '<strong>échappé</strong>', '::unescape() returns an unescaped string if the value to unescape is a string');
  89. $t->diag('::unescape() unescapes arrays');
  90. $input = Escaper::escape('esc_entities', array(
  91. 'foo' => '<strong>escaped!</strong>',
  92. 'bar' => array('foo' => '<strong>escaped!</strong>'),
  93. ));
  94. $output = Escaper::unescape($input);
  95. $t->ok(is_array($output), '::unescape() returns an array if the input is a ArrayDecorator object');
  96. $t->is($output['foo'], '<strong>escaped!</strong>', '::unescape() unescapes all elements of the original array');
  97. $t->is($output['bar']['foo'], '<strong>escaped!</strong>', '::unescape() is recursive');
  98. $t->diag('::unescape() unescapes objects');
  99. $object = new OutputEscaperTestClass();
  100. $input = Escaper::escape('esc_entities', $object);
  101. $output = Escaper::unescape($input);
  102. $t->ok($output instanceof OutputEscaperTestClass, '::unescape() returns the original object when a ObjectDecorator object is passed');
  103. $t->is($output->getTitle(), '<strong>escaped!</strong>', '::unescape() unescapes all methods of the original object');
  104. $t->is($output->title, '<strong>escaped!</strong>', '::unescape() unescapes all properties of the original object');
  105. $t->is($output->getTitleTitle(), '<strong>escaped!</strong>', '::unescape() is recursive');
  106. $t->ok(IteratorDecorator::unescape(Escaper::escape('esc_entities', new DirectoryIterator('.'))) instanceof DirectoryIterator, '::unescape() unescapes IteratorDecorator objects');
  107. $t->diag('::unescape() does not unescape object marked as being safe');
  108. $t->ok(Escaper::unescape(Escaper::escape('esc_entities', new Safe(new OutputEscaperTestClass()))) instanceof OutputEscaperTestClass, '::unescape() returns the original value if it is marked as being safe');
  109. Escaper::markClassAsSafe('OutputEscaperTestClass');
  110. $t->ok(Escaper::unescape(Escaper::escape('esc_entities', new OutputEscaperTestClass())) instanceof OutputEscaperTestClass, '::unescape() returns the original value if the object class is marked as being safe');
  111. $t->ok(Escaper::unescape(Escaper::escape('esc_entities', new OutputEscaperTestClassChild())) instanceof OutputEscaperTestClassChild, '::unescape() returns the original value if one of the object parent class is marked as being safe');
  112. $t->diag('::unescape() do nothing to resources');
  113. $fh = fopen(__FILE__, 'r');
  114. $t->is(Escaper::unescape($fh), $fh, '::unescape() do nothing to resources');
  115. $t->diag('::unescape() unescapes mixed arrays');
  116. $object = new OutputEscaperTestClass();
  117. $input = array(
  118. 'foo' => 'bar',
  119. 'bar' => Escaper::escape('esc_entities', '<strong>bar</strong>'),
  120. 'foobar' => Escaper::escape('esc_entities', $object),
  121. );
  122. $output = array(
  123. 'foo' => 'bar',
  124. 'bar' => '<strong>bar</strong>',
  125. 'foobar' => $object,
  126. );
  127. $t->is(Escaper::unescape($input), $output, '::unescape() unescapes values with some escaped and unescaped values');