[!!!][TASK] Extract testing framework for TYPO3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Utility / DebugUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Utility;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\DebugUtility;
18
19 /**
20 * Testcase for class \TYPO3\CMS\Core\Utility\DebugUtility
21 */
22 class DebugUtilityTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
23 {
24 protected function tearDown()
25 {
26 parent::tearDown();
27 DebugUtility::usePlainTextOutput(true);
28 DebugUtility::useAnsiColor(true);
29 }
30
31 /**
32 * @test
33 */
34 public function debugNotEncodesHtmlInputIfPlainText()
35 {
36 DebugUtility::usePlainTextOutput(true);
37 DebugUtility::useAnsiColor(false);
38
39 ob_start();
40 DebugUtility::debug('<script>alert(\'Hello world!\')</script>');
41 $output = ob_get_contents();
42 ob_end_clean();
43
44 $this->assertContains(
45 '<script>alert(\'Hello world!\')</script>',
46 $output
47 );
48 }
49
50 /**
51 * @test
52 */
53 public function debugEncodesHtmlInputIfNoPlainText()
54 {
55 DebugUtility::usePlainTextOutput(false);
56 DebugUtility::useAnsiColor(false);
57
58 ob_start();
59 DebugUtility::debug('<script>alert(\'Hello world!\')</script>');
60 $output = ob_get_contents();
61 ob_end_clean();
62
63 $this->assertContains(
64 '&lt;script&gt;alert(\'Hello world!\')&lt;/script&gt;',
65 $output
66 );
67 }
68
69 /**
70 * @return array
71 */
72 public function convertVariableToStringReturnsVariableContentDataProvider()
73 {
74 $object = new \stdClass();
75 $object->foo = 42;
76 $object->bar = ['baz'];
77
78 return [
79 'Debug string' => [
80 'Hello world!',
81 '"Hello world!" (12 chars)',
82 ],
83 'Debug array' => [
84 [
85 'foo',
86 'bar',
87 'baz' => [
88 42,
89 ],
90 ],
91 'array(3 items)' . PHP_EOL
92 . ' 0 => "foo" (3 chars)' . PHP_EOL
93 . ' 1 => "bar" (3 chars)' . PHP_EOL
94 . ' baz => array(1 item)' . PHP_EOL
95 . ' 0 => 42 (integer)',
96 ],
97 'Debug object' => [
98 $object,
99 'stdClass prototype object' . PHP_EOL
100 . ' foo => public 42 (integer)' . PHP_EOL
101 . ' bar => public array(1 item)' . PHP_EOL
102 . ' 0 => "baz" (3 chars)'
103 ],
104 ];
105 }
106
107 /**
108 * @test
109 * @dataProvider convertVariableToStringReturnsVariableContentDataProvider
110 * @param mixed $variable
111 * @param string $expected
112 */
113 public function convertVariableToStringReturnsVariableContent($variable, $expected)
114 {
115 $this->assertSame($expected, DebugUtility::convertVariableToString($variable));
116 }
117 }