‪TYPO3CMS  ‪main
IpAnonymizationUtilityTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24 
28 final class ‪IpAnonymizationUtilityTest extends UnitTestCase
29 {
33  public static function ‪anonymizeIpReturnsCorrectValueDataProvider(): array
34  {
35  return [
36  'empty address' => ['', 1, ''],
37  'IPv4 address with mask 0' => ['192.158.130.10', 0, '192.158.130.10'],
38  'IPv4 address with mask 1' => ['192.158.130.10', 1, '192.158.130.0'],
39  'IPv4 address with mask 2' => ['192.158.130.10', 2, '192.158.0.0'],
40  'IPv4 address with fallback' => ['192.158.130.10', null, '192.158.130.0'],
41  'IPv6 address with mask 0' => ['0064:ff9b:0000:0000:0000:0000:18.52.86.120', 0, '0064:ff9b:0000:0000:0000:0000:18.52.86.120'],
42  'IPv6 address with mask 1' => ['2002:6dcd:8c74:6501:fb2:61c:ac98:6bea', 1, '2002:6dcd:8c74:6501::'],
43  'IPv6 address with mask 2' => ['2002:6dcd:8c74:6501:fb2:61c:ac98:6bea', 2, '2002:6dcd:8c74::'],
44  'IPv6 address with fallback' => ['2002:6dcd:8c74:6501:fb2:61c:ac98:6bea', null, '2002:6dcd:8c74:6501::'],
45  'IPv4-Embedded IPv6 Address' => ['::ffff:18.52.86.120', 1, '::'],
46  'anonymized IPv4 address' => ['192.158.0.0', 1, '192.158.0.0'],
47  'invalid IPv4 address given' => ['127.0.01', 1, ''],
48  'invalid IPv6 address given' => ['ffff18.52.86.120', 1, ''],
49  ];
50  }
51 
55  #[DataProvider('anonymizeIpReturnsCorrectValueDataProvider')]
56  #[Test]
57  public function ‪anonymizeIpReturnsCorrectValue(string $address, int $mask = null, string $expected): void
58  {
59  // set the default if $mask is null
60  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['ipAnonymization'] = 1;
61  self::assertEquals($expected, ‪IpAnonymizationUtility::anonymizeIp($address, $mask));
62  }
63 
64  #[Test]
66  {
67  $this->expectException(\UnexpectedValueException::class);
68  $this->expectExceptionCode(1519739203);
69 
71  }
72 }
‪TYPO3\CMS\Core\Tests\Unit\Utility\IpAnonymizationUtilityTest\anonymizeIpReturnsCorrectValue
‪anonymizeIpReturnsCorrectValue(string $address, int $mask=null, string $expected)
Definition: IpAnonymizationUtilityTest.php:57
‪TYPO3\CMS\Core\Tests\Unit\Utility
‪TYPO3\CMS\Core\Tests\Unit\Utility\IpAnonymizationUtilityTest\wrongMaskForAnonymizeIpThrowsException
‪wrongMaskForAnonymizeIpThrowsException()
Definition: IpAnonymizationUtilityTest.php:65
‪TYPO3\CMS\Core\Utility\IpAnonymizationUtility
Definition: IpAnonymizationUtility.php:26
‪TYPO3\CMS\Core\Tests\Unit\Utility\IpAnonymizationUtilityTest\anonymizeIpReturnsCorrectValueDataProvider
‪static anonymizeIpReturnsCorrectValueDataProvider()
Definition: IpAnonymizationUtilityTest.php:33
‪TYPO3\CMS\Core\Utility\IpAnonymizationUtility\anonymizeIp
‪static anonymizeIp(string $address, int $mask=null)
Definition: IpAnonymizationUtility.php:60
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Tests\Unit\Utility\IpAnonymizationUtilityTest
Definition: IpAnonymizationUtilityTest.php:29