Skip to content

Commit

Permalink
Merge pull request #53 from joubertredrat/feature/unit-tests
Browse files Browse the repository at this point in the history
Unit tests support
  • Loading branch information
joubertredrat authored Apr 10, 2024
2 parents c3ca254 + 4d189af commit a38ad00
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 8 deletions.
53 changes: 53 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
name: Tests

on:
push:
paths-ignore:
- '*.md'
- '*.json'
- '*.png'
- 'license'
- '.editorconfig'

jobs:
tests:
strategy:
fail-fast: false
matrix:
php:
- '7.3'
- '7.4'
- '8.0'
- '8.1'
- '8.2'
- '8.3'
matomo-target:
- minimum_required_matomo
- maximum_supported_matomo
node:
- 12.x
- 20.x
runs-on: ubuntu-latest
permissions:
checks: write
pull-requests: write
contents: read
name: >-
Tests w/ PHP ${{ matrix.php }}, Node.js ${{ matrix.node }}, Target Matomo
'${{ matrix.matomo-target }}'
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
lfs: true
persist-credentials: false
- name: Run plugin tests
uses: matomo-org/github-action-tests@main
with:
plugin-name: ProtectTrackID
test-type: PluginTests
php-version: '${{ matrix.php }}'
matomo-test-branch: '${{ matrix.matomo-target }}'
node-version: '${{ matrix.node }}'
mysql-service: true
phpunit-test-options: --testdox
3 changes: 2 additions & 1 deletion Hasher.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

namespace Piwik\Plugins\ProtectTrackID;

require_once(__DIR__ . '/vendor/autoload.php');

use Hashids\Hashids;

class Hasher
Expand All @@ -25,7 +27,6 @@ class Hasher

public function __construct(PluginSettings $settings)
{
require_once(__DIR__ . '/vendor/autoload.php');
$this->hashids = new Hashids($settings->salt, $settings->length, $settings->base);
}

Expand Down
4 changes: 2 additions & 2 deletions SystemSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

namespace Piwik\Plugins\ProtectTrackID;

require_once(__DIR__ . '/vendor/autoload.php');

use Piwik\Settings\FieldConfig;
use Piwik\Settings\Plugin\SystemSetting;
use Piwik\Settings\Plugin\SystemSettings as MatomoPluginSystemSettings;
Expand All @@ -30,8 +32,6 @@ class SystemSettings extends MatomoPluginSystemSettings

protected function init(): void
{
require_once(__DIR__ . '/vendor/autoload.php');

$this->base = $this->createBaseSetting();
$this->salt = $this->createSaltSetting();
$this->length = $this->createLengthSetting();
Expand Down
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
{
"require": {
"php": ">=7.3.5",
"ext-bcmath": "*",
"ext-gmp": "*",
"ext-mbstring": "*",
"hashids/hashids": "^4.1",
"ramsey/uuid": "4.2.3"
},
"suggest": {
"ext-bcmath": "Required to use BC Math arbitrary precision mathematics (*).",
"ext-gmp": "Required to use GNU multiple precision mathematics (*)."
}
}
3 changes: 3 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

return [];
3 changes: 3 additions & 0 deletions config/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

return [];
3 changes: 3 additions & 0 deletions config/tracker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

return [];
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ProtectTrackID",
"version": "3.0.0",
"version": "3.1.0",
"description": "Provides a option to protect idSite using hash instead default numeric",
"license": "MIT",
"homepage": "https://github.com/joubertredrat/Matomo-ProtectTrackID",
Expand Down
60 changes: 60 additions & 0 deletions tests/Unit/HasherTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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
*/

namespace Piwik\Plugins\ProtectTrackID\tests\Unit;

use PHPUnit\Framework\TestCase;
use Piwik\Plugins\ProtectTrackID\Hasher;
use Piwik\Plugins\ProtectTrackID\PluginSettings;

/**
* @group ProtectTrackID
* @group HasherTest
* @group Plugins
*/
class HasherTest extends TestCase
{
const ID_1_RAW = '1';
const ID_1_HASEHD = '4jMymK3Eq1k21pxLOJlv';

public function testEncode(): void
{
$pluginSettings = new PluginSettings(
'ABCDEFGHIJKLMNOPijklmnopqrstuvxwyz12345',
'd4768387-2f45-47cc-b581-7f66c5b724af',
20
);

$hasher = new Hasher($pluginSettings);
$hashExpected = self::ID_1_HASEHD;
$hashGot = $hasher->encode(self::ID_1_RAW);

self::assertEquals($hashExpected, $hashGot);
}

public function testDecode(): void
{
$pluginSettings = new PluginSettings(
'ABCDEFGHIJKLMNOPijklmnopqrstuvxwyz12345',
'd4768387-2f45-47cc-b581-7f66c5b724af',
20
);

$hasher = new Hasher($pluginSettings);
$idExpected = self::ID_1_RAW;
$idGot = $hasher->decode(self::ID_1_HASEHD);

self::assertEquals($idExpected, $idGot);
}
}
50 changes: 50 additions & 0 deletions tests/Unit/InvalidSettingValueExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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
*/

namespace Piwik\Plugins\ProtectTrackID\tests\Unit;

use PHPUnit\Framework\TestCase;
use Piwik\Plugins\ProtectTrackID\InvalidSettingValueException;

/**
* @group ProtectTrackID
* @group InvalidSettingValueExceptionTest
* @group Plugins
*/
class InvalidSettingValueExceptionTest extends TestCase
{
public function testHandleBase(): void
{
$this->expectException(InvalidSettingValueException::class);
$this->expectExceptionMessage('Invalid value on base, was filled foo.');

throw InvalidSettingValueException::handleBase('foo');
}

public function testHandleSalt(): void
{
$this->expectException(InvalidSettingValueException::class);
$this->expectExceptionMessage('Invalid value on salt, was filled foo.');

throw InvalidSettingValueException::handleSalt('foo');
}

public function testHandleLenght(): void
{
$this->expectException(InvalidSettingValueException::class);
$this->expectExceptionMessage('Invalid value on length, expected between 5 and 25, was filled 30.');

throw InvalidSettingValueException::handleLenght(5, 25, 30);
}
}
90 changes: 90 additions & 0 deletions tests/Unit/PluginSettingsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?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
*/

namespace Piwik\Plugins\ProtectTrackID\tests\Unit;

use PHPUnit\Framework\TestCase;
use Piwik\Plugins\ProtectTrackID\PluginSettings;

/**
* @group ProtectTrackID
* @group PluginSettingsTest
* @group Plugins
*/
class PluginSettingsTest extends TestCase
{
public function testHasValidValues(): void
{
$pluginSettings = new PluginSettings(
'ABCDEFGHIJKLMNOPijklmnopqrstuvxwyz12345',
'd4768387-2f45-47cc-b581-7f66c5b724af',
20
);

self::assertTrue($pluginSettings->hasValidValues());
}

public function testHasNotValidValuesByBase(): void
{
$pluginSettings = new PluginSettings(
null,
'd4768387-2f45-47cc-b581-7f66c5b724af',
20
);

self::assertFalse($pluginSettings->hasValidValues());
}

public function testHasNotValidValuesBySalt(): void
{
$pluginSettings = new PluginSettings(
'ABCDEFGHIJKLMNOPijklmnopqrstuvxwyz12345',
null,
20
);

self::assertFalse($pluginSettings->hasValidValues());
}

public function testHasNotValidValuesByLength(): void
{
$pluginSettings = new PluginSettings(
'ABCDEFGHIJKLMNOPijklmnopqrstuvxwyz12345',
'd4768387-2f45-47cc-b581-7f66c5b724af',
null
);

self::assertFalse($pluginSettings->hasValidValues());
}

public function testIsValidBase(): void
{
self::assertTrue(PluginSettings::isValidBase('ABCDEFGHIJKLMNOPijklmnopqrstuvxwyz12345'));
}

public function testIsNotValidBase(): void
{
self::assertFalse(PluginSettings::isValidBase('ABCDEFGHIJKLMNO$Pijklm%nop#qrs@tu&vxwyz12345'));
}

public function testIsValidLenght(): void
{
self::assertTrue(PluginSettings::isValidLenght(20));
}

public function testIsNotValidLenght(): void
{
self::assertFalse(PluginSettings::isValidLenght(2));
}
}

0 comments on commit a38ad00

Please sign in to comment.