82818518af74ef4490e5e1c8a58f4668718a8959
[Packages/TYPO3.CMS.git] / tests / typo3 / sysext / cms / tslib / class.tslib_feTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2011 Oliver Klee (typo3-coding@oliverklee.de)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Testcase for the "tslib_fe" class in the TYPO3 Core.
27 *
28 * @package TYPO3
29 * @subpackage tslib
30 *
31 * @author Oliver Klee <typo3-coding@oliverklee.de>
32 */
33 class tslib_feTest extends tx_phpunit_testcase {
34 /**
35 * @var tslib_fe
36 */
37 private $fixture;
38
39 public function setUp() {
40 // This creates an instance of the class without calling the
41 // original constructor.
42 $className = uniqid('tslib_fe');
43 eval(
44 'class ' . $className . ' extends tslib_fe {' .
45 'public function ' . $className . '() {}' .
46
47 'public function roundTripCryptString($string) {' .
48 'return parent::roundTripCryptString($string);' .
49 '}' .
50
51 'public function stripIPv4($strIP) {' .
52 'return parent::stripIPv4($strIP);' .
53 '}' .
54
55 'public function stripIPv6($strIP) {' .
56 'return parent::stripIPv6($strIP);' .
57 '}' .
58
59 '}'
60 );
61
62 $this->fixture = new $className();
63 $this->fixture->TYPO3_CONF_VARS = $GLOBALS['TYPO3_CONF_VARS'];
64 $this->fixture->TYPO3_CONF_VARS['SYS']['encryptionKey']
65 = '170928423746123078941623042360abceb12341234231';
66 }
67
68 public function tearDown() {
69 unset($this->fixture);
70 }
71
72
73 ////////////////////////////////
74 // Tests concerning codeString
75 ////////////////////////////////
76
77 /**
78 * @test
79 */
80 public function codeStringForNonEmptyStringReturns10CharacterHashAndCodedString() {
81 $this->assertRegExp(
82 '/^[0-9a-f]{10}:[a-zA-Z0-9+=\/]+$/',
83 $this->fixture->codeString('Hello world!')
84 );
85 }
86
87 /**
88 * @test
89 */
90 public function decodingCodedStringReturnsOriginalString() {
91 $clearText = 'Hello world!';
92
93 $this->assertEquals(
94 $clearText,
95 $this->fixture->codeString(
96 $this->fixture->codeString($clearText), TRUE
97 )
98 );
99 }
100
101
102 //////////////////////
103 // Tests concerning sL
104 //////////////////////
105
106 /**
107 * @test
108 */
109 public function localizationReturnsUnchangedStringIfNotLocallangLabel() {
110 $string = uniqid();
111 $this->assertEquals($string, $this->fixture->sL($string));
112 }
113
114
115 //////////////////////////////////////////
116 // Tests concerning roundTripCryptString
117 //////////////////////////////////////////
118
119 /**
120 * @test
121 */
122 public function roundTripCryptStringCreatesStringWithSameLengthAsInputString() {
123 $clearText = 'Hello world!';
124
125 $this->assertEquals(
126 strlen($clearText),
127 strlen($this->fixture->roundTripCryptString($clearText))
128 );
129 }
130
131 /**
132 * @test
133 */
134 public function roundTripCryptStringCreatesResultDifferentFromInputString() {
135 $clearText = 'Hello world!';
136
137 $this->assertNotEquals(
138 $clearText,
139 $this->fixture->roundTripCryptString($clearText)
140 );
141 }
142
143 /**
144 * @test
145 */
146 public function roundTripCryptStringAppliedTwoTimesReturnsOriginalString() {
147 $clearText = 'Hello world!';
148
149 $this->assertEquals(
150 $clearText,
151 $this->fixture->roundTripCryptString(
152 $this->fixture->roundTripCryptString($clearText)
153 )
154 );
155 }
156
157 //////////////////////////////////////
158 // Tests concerning stat-anonymization
159 //////////////////////////////////////
160
161 /**
162 * Data provider for stripIPv6Correct
163 *
164 * @return array Data sets
165 */
166 public static function stripIPv4DataProviderCorrect() {
167 return array(
168 'empty address, prefix-length 24' => array('0.0.0.0', '24', '0.0.0.0'),
169 'normal address 1, prefix-length 1' => array('1.2.3.4', '1', '0.0.0.0'),
170 'normal address 2, prefix-length 24' => array('192.168.5.79', '24', '192.168.5.0'),
171 'normal address 2, prefix-length 30' => array('192.168.5.79', '30', '192.168.5.76'),
172 // test for no anonymization; full prefix-length
173 'normal address 2, prefix-length 32' => array('192.168.5.79', '32', '192.168.5.79'),
174 // test for full anonymization; full prefix-length
175 'normal address 2, prefix-length 0' => array('192.168.5.79', '0', '0.0.0.0'),
176 );
177 }
178
179 /**
180 * @test
181 * @dataProvider stripIPv4DataProviderCorrect
182 */
183 public function stripIPv4Correct($address, $prefixLength, $anonymized) {
184 $oldConfig = $this->fixture->config;
185
186 $this->fixture->config = array('config' =>
187 array('stat_IP_anonymize' => '1',
188 'stat_IP_anonymize_mask_ipv4' => $prefixLength
189 )
190 );
191
192 $this->assertEquals(
193 $this->fixture->stripIPv4($address),
194 $anonymized
195 );
196 $this->fixture->config = $oldConfig;
197 }
198
199 /**
200 * Data provider for stripIPv6Correct
201 *
202 * @return array Data sets
203 */
204 public static function stripIPv6DataProviderCorrect() {
205 return array(
206 'empty address, prefix-length 96' => array('::', '96', '::'),
207 'normal address 1, prefix-length 1' => array('1:2:3::4', '1', '::'),
208 'normal address 2, prefix-length 4' => array('ffff::9876', '4', 'f000::'),
209 'normal address 2, prefix-length 1' => array('ffff::9876', '1', '8000::'),
210 'normal address 3, prefix-length 96' => array('abc:def::9876', '96', 'abc:def::'),
211 'normal address 3, prefix-length 120' => array('abc:def::9876', '120', 'abc:def::9800'),
212 // test for no anonymization; full prefix-length
213 'normal address 3, prefix-length 128' => array('abc:def::9876', '128', 'abc:def::9876'),
214 // test for full anonymization
215 'normal address 3, prefix-length 0' => array('abc:def::9876', '0', '::'),
216 );
217 }
218
219 /**
220 * @test
221 * @dataProvider stripIPv6DataProviderCorrect
222 */
223 public function stripIPv6Correct($address, $prefixLength, $anonymized) {
224 $oldConfig = $this->fixture->config;
225
226 $this->fixture->config = array('config' =>
227 array('stat_IP_anonymize' => '1',
228 'stat_IP_anonymize_mask_ipv6' => $prefixLength
229 )
230 );
231
232 $this->assertEquals(
233 $this->fixture->stripIPv6($address),
234 $anonymized
235 );
236 $this->fixture->config = $oldConfig;
237 }
238 }
239 ?>