[TASK] Shorten the extend class in all unit tests
[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 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
19
20 /**
21 * Testcase for class \TYPO3\CMS\Core\Utility\DebugUtility
22 */
23 class DebugUtilityTest extends UnitTestCase
24 {
25 protected function tearDown()
26 {
27 parent::tearDown();
28 DebugUtility::usePlainTextOutput(true);
29 DebugUtility::useAnsiColor(true);
30 }
31
32 /**
33 * @test
34 */
35 public function debugNotEncodesHtmlInputIfPlainText()
36 {
37 DebugUtility::usePlainTextOutput(true);
38 DebugUtility::useAnsiColor(false);
39
40 ob_start();
41 DebugUtility::debug('<script>alert(\'Hello world!\')</script>');
42 $output = ob_get_contents();
43 ob_end_clean();
44
45 $this->assertContains(
46 '<script>alert(\'Hello world!\')</script>',
47 $output
48 );
49 }
50
51 /**
52 * @test
53 */
54 public function debugEncodesHtmlInputIfNoPlainText()
55 {
56 DebugUtility::usePlainTextOutput(false);
57 DebugUtility::useAnsiColor(false);
58
59 ob_start();
60 DebugUtility::debug('<script>alert(\'Hello world!\')</script>');
61 $output = ob_get_contents();
62 ob_end_clean();
63
64 $this->assertContains(
65 '&lt;script&gt;alert(\'Hello world!\')&lt;/script&gt;',
66 $output
67 );
68 }
69
70 /**
71 * @return array
72 */
73 public function convertVariableToStringReturnsVariableContentDataProvider()
74 {
75 $object = new \stdClass();
76 $object->foo = 42;
77 $object->bar = ['baz'];
78
79 return [
80 'Debug string' => [
81 'Hello world!',
82 '"Hello world!" (12 chars)',
83 ],
84 'Debug array' => [
85 [
86 'foo',
87 'bar',
88 'baz' => [
89 42,
90 ],
91 ],
92 'array(3 items)' . PHP_EOL
93 . ' 0 => "foo" (3 chars)' . PHP_EOL
94 . ' 1 => "bar" (3 chars)' . PHP_EOL
95 . ' baz => array(1 item)' . PHP_EOL
96 . ' 0 => 42 (integer)',
97 ],
98 'Debug object' => [
99 $object,
100 'stdClass prototype object' . PHP_EOL
101 . ' foo => public 42 (integer)' . PHP_EOL
102 . ' bar => public array(1 item)' . PHP_EOL
103 . ' 0 => "baz" (3 chars)'
104 ],
105 ];
106 }
107
108 /**
109 * @test
110 * @dataProvider convertVariableToStringReturnsVariableContentDataProvider
111 * @param mixed $variable
112 * @param string $expected
113 */
114 public function convertVariableToStringReturnsVariableContent($variable, $expected)
115 {
116 $this->assertSame($expected, DebugUtility::convertVariableToString($variable));
117 }
118 }