-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add hash validation and exception in hasher
- Loading branch information
1 parent
a38ad00
commit 71a2ed7
Showing
8 changed files
with
222 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
/** | ||
* Matomo - Open source web analytics | ||
* | ||
* @link https://matomo.org | ||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later | ||
* | ||
* @copyright (c) 2024 Joubert RedRat | ||
* @author Joubert RedRat <[email protected]> | ||
* @license MIT | ||
* @category Matomo_Plugins | ||
* @package ProtectTrackID | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Piwik\Plugins\ProtectTrackID; | ||
|
||
class IdSite | ||
{ | ||
public static function isValid($value): bool | ||
{ | ||
return \filter_var($value, FILTER_VALIDATE_INT) !== false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/** | ||
* Matomo - Open source web analytics | ||
* | ||
* @link https://matomo.org | ||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later | ||
* | ||
* @copyright (c) 2024 Joubert RedRat | ||
* @author Joubert RedRat <[email protected]> | ||
* @license MIT | ||
* @category Matomo_Plugins | ||
* @package ProtectTrackID | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Piwik\Plugins\ProtectTrackID; | ||
|
||
use InvalidArgumentException; | ||
|
||
class InvalidHasherValueException extends InvalidArgumentException | ||
{ | ||
public static function handleEncode(string $value): self | ||
{ | ||
return new self(sprintf( | ||
'Invalid value for Hasher encode, expected id site integer, got %1$s.', | ||
$value | ||
)); | ||
} | ||
|
||
public static function handleDecode(string $value): self | ||
{ | ||
return new self(sprintf( | ||
'Invalid value for Hasher decode, expected valid hash, got %1$s.', | ||
$value | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/** | ||
* Matomo - free/libre analytics platform | ||
* | ||
* @link https://matomo.org | ||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later | ||
* | ||
* @copyright (c) 2024 Joubert RedRat | ||
* @author Joubert RedRat <[email protected]> | ||
* @license MIT | ||
* @category Matomo_Plugins | ||
* @package ProtectTrackID | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Piwik\Plugins\ProtectTrackID\tests\Unit; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Piwik\Plugins\ProtectTrackID\IdSite; | ||
|
||
/** | ||
* @group ProtectTrackID | ||
* @group IdSiteTest | ||
* @group Plugins | ||
*/ | ||
class IdSiteTest extends TestCase | ||
{ | ||
public function testIsValidString(): void | ||
{ | ||
self::assertTrue(IdSite::isValid('2')); | ||
} | ||
|
||
public function testIsValidInt(): void | ||
{ | ||
self::assertTrue(IdSite::isValid(2)); | ||
} | ||
|
||
public function testIsNotValidStringFloat(): void | ||
{ | ||
self::assertFalse(IdSite::isValid('2.1')); | ||
} | ||
|
||
public function testIsNotValidString(): void | ||
{ | ||
self::assertFalse(IdSite::isValid('foo')); | ||
} | ||
|
||
public function testIsNotValidNull(): void | ||
{ | ||
self::assertFalse(IdSite::isValid(null)); | ||
} | ||
|
||
public function testIsNotValidFloat(): void | ||
{ | ||
self::assertFalse(IdSite::isValid(2.1)); | ||
} | ||
|
||
public function testIsNotValidBool(): void | ||
{ | ||
self::assertFalse(IdSite::isValid(false)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
/** | ||
* Matomo - free/libre analytics platform | ||
* | ||
* @link https://matomo.org | ||
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later | ||
* | ||
* @copyright (c) 2024 Joubert RedRat | ||
* @author Joubert RedRat <[email protected]> | ||
* @license MIT | ||
* @category Matomo_Plugins | ||
* @package ProtectTrackID | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Piwik\Plugins\ProtectTrackID\tests\Unit; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Piwik\Plugins\ProtectTrackID\InvalidHasherValueException; | ||
|
||
/** | ||
* @group ProtectTrackID | ||
* @group InvalidHasherValueExceptionTest | ||
* @group Plugins | ||
*/ | ||
|
||
class InvalidHasherValueExceptionTest extends TestCase | ||
{ | ||
public function testHandleEncode(): void | ||
{ | ||
$this->expectException(InvalidHasherValueException::class); | ||
$this->expectExceptionMessage('Invalid value for Hasher encode, expected id site integer, got foo.'); | ||
|
||
throw InvalidHasherValueException::handleEncode('foo'); | ||
} | ||
|
||
public function testHandleDecode(): void | ||
{ | ||
$this->expectException(InvalidHasherValueException::class); | ||
$this->expectExceptionMessage('Invalid value for Hasher decode, expected valid hash, got foo.'); | ||
|
||
throw InvalidHasherValueException::handleDecode('foo'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters