Skip to content

Commit

Permalink
Dev: small text documentation changes, added unittests for ip anonymi…
Browse files Browse the repository at this point in the history
…zation
  • Loading branch information
Trischi80 committed Apr 15, 2020
1 parent e326a7a commit 1701fb4
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
4 changes: 2 additions & 2 deletions application/models/services/IpAddressAnonymizer.php
Expand Up @@ -28,7 +28,7 @@ public function __construct($ipAddress)
/**
* Checks if ip is a valid ipv4
*
* @return boolean
* @return boolean true if it is ipv4, false otherwise
*/
public function isIpv4()
{
Expand All @@ -44,7 +44,7 @@ public function isIpv4()
/**
* Checks if ip is a valid ipv6
*
* @return boolean false if not valid, otherwise the filtered ip address
* @return boolean true if ipv6 is valid, false otherwise
*/
public function isIpv6()
{
Expand Down
80 changes: 80 additions & 0 deletions tests/unit/models/IpAddressAnonymizerTest.php
@@ -0,0 +1,80 @@
<?php


//namespace unit\models;
namespace ls\tests;


use LimeSurvey\Models\Services\IpAddressAnonymizer;
use PHPUnit\Framework\TestCase;

class IpAddressAnonymizerTest extends TestCase
{

/**
* Test it is ipv4
*/
public function testIsIpV4()
{
$ipToBeTested = '192.168.3.1';
$ipAnonymizer = new IpAddressAnonymizer($ipToBeTested);

$this->assertTrue($ipAnonymizer->isIpv4());
}

/**
* Test not ipv4
*/
public function testIsNotIpv4(){
$ipToBeTested = '192.168.3.';
$ipAnonymizer = new IpAddressAnonymizer($ipToBeTested);

$this->assertFalse($ipAnonymizer->isIpv4());
}

/**
* Test it is ipv6
*/
public function testIsIpV6(){
$ipToBeTested = '2a03:2880:2117:df07:face:b00c:5:1';
$ipToBeTested2 = '2a03:2880:2117:df07::5:1';
$ipAnonymizer = new IpAddressAnonymizer($ipToBeTested);

$this->assertTrue($ipAnonymizer->isIpv6());
$ipAnonymizer = new IpAddressAnonymizer($ipToBeTested2);
$this->assertTrue($ipAnonymizer->isIpv6());
}

/**
* Test not ipv6
*/
public function testIsNotIpv6(){
$ipToBeTested = '192.3.4.5';
$ipAnonymizer = new IpAddressAnonymizer($ipToBeTested);

$this->assertFalse($ipAnonymizer->isIpv6());
}

/**
* Test ip anonymization with ipv4
* 192.168.3.4 is anomymized to 192.168.3.4
*/
public function testIpAnonymizeIpv4(){
$ipToBeTested = '192.168.3.4';
$ipAnonymizer = new IpAddressAnonymizer($ipToBeTested);

$this->assertEquals('192.168.3.0', $ipAnonymizer->anonymizeIpAddress());
}

/**
* Test ip anonymization with ipv6
* 2a03:2880:2117:df07:face:b00c:5:1 is anonymized to 2a03:2880:2117:0:0:0:0:0
*/
public function testIpAnonymizeIpv6(){
$ipToBeTested = '2a03:2880:2117:df07:face:b00c:5:1';
$ipAnonymizer = new IpAddressAnonymizer($ipToBeTested);

$this->assertEquals('2a03:2880:2117:0:0:0:0:0', $ipAnonymizer->anonymizeIpAddress());
}

}

0 comments on commit 1701fb4

Please sign in to comment.