From 9869c4dc0e810cdb02471dc0182c447e05096bae Mon Sep 17 00:00:00 2001 From: martin Date: Tue, 4 Mar 2025 11:50:19 +0100 Subject: [PATCH 1/4] default parameters --- src/User/Sign.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/User/Sign.php b/src/User/Sign.php index edb07d1..0fa580a 100644 --- a/src/User/Sign.php +++ b/src/User/Sign.php @@ -26,6 +26,8 @@ class Sign private Logger $logger; + private array $default_parameters = []; + public function __construct(Settings $settings, Connection $connection, Url $url, Configuration $configuration, Language $language, Logger $logger) { @@ -37,6 +39,14 @@ public function __construct(Settings $settings, Connection $connection, Url $url $this->logger = $logger; } + /** + * @param array $parameters + */ + public function setDefaultParameters(array $parameters): void + { + $this->default_parameters = $parameters; + } + /** * @param array $parameters @@ -53,7 +63,7 @@ public function authenticate(bool $reload = false, array $parameters = []): ?str 'application_version' => $this->configuration->version(), 'application_parameters' => array_merge([ 'guest' => $token === null, - ], $parameters), + ], $parameters, $this->default_parameters), ], $token ?? ''); } From 02af5a4e32300cbebae87cf385018a8aa5f4ab78 Mon Sep 17 00:00:00 2001 From: martin Date: Tue, 4 Mar 2025 13:33:22 +0100 Subject: [PATCH 2/4] application_parameters precedence + tests --- src/User/Sign.php | 10 +++- tests/User/SignTestJwt.phpt | 109 ++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 tests/User/SignTestJwt.phpt diff --git a/src/User/Sign.php b/src/User/Sign.php index 0fa580a..8d771f9 100644 --- a/src/User/Sign.php +++ b/src/User/Sign.php @@ -61,9 +61,13 @@ public function authenticate(bool $reload = false, array $parameters = []): ?str 'application_product' => $this->configuration->product(), 'application_language' => $this->language->get(), 'application_version' => $this->configuration->version(), - 'application_parameters' => array_merge([ - 'guest' => $token === null, - ], $parameters, $this->default_parameters), + 'application_parameters' => array_merge( + $this->default_parameters, + $parameters, + [ + 'guest' => $token === null, + ], + ), ], $token ?? ''); } diff --git a/tests/User/SignTestJwt.phpt b/tests/User/SignTestJwt.phpt new file mode 100644 index 0000000..66f214a --- /dev/null +++ b/tests/User/SignTestJwt.phpt @@ -0,0 +1,109 @@ +setDefaultParameters(['test1' => 'default_test1', 'test3' => 'default_test3']); + + $settings->shouldReceive('load')->with('static:application_token', true)->andReturn('test_application_token'); + $settings->shouldReceive('load')->with('static:application_id')->andReturn(12345); + $language->shouldReceive('get')->withNoArgs()->once()->andReturn('cs'); + + $jwt = Mockery::mock('overload:' . Jwt::class); + $jwt->shouldReceive('encode')->with([ + 'application_id' => 12345, + 'application_installation' => 'url', + 'application_product' => 'eshop', + 'application_language' => 'cs', + 'application_version' => '1.0', + 'application_parameters' => [ + 'guest' => false, + 'test1' => 'test1', + 'test2' => 'test2', + 'test3' => 'default_test3' + ] + ], 'test_application_token')->once()->andReturn('0.0.0'); + + + Assert::match('~^[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+$~', $sign->authenticate(true, ['test1' => 'test1', 'test2' => 'test2'])); + } + + public function testGuestParameterOverride(): void + { + $sign = new Sign($settings = Mockery::mock(Settings::class), Mockery::mock(Connection::class), new Url(), new ConfigurationDefault('url', 'eshop', '1.0', 'Test Eshop'), $language = Mockery::mock(Language::class), Mockery::mock(Logger::class)); + + $settings->shouldReceive('load')->with('static:application_token', true)->andReturn('test_application_token'); + $settings->shouldReceive('load')->with('static:application_id')->andReturn(12345); + $language->shouldReceive('get')->withNoArgs()->once()->andReturn('cs'); + + $jwt = Mockery::mock('overload:' . Jwt::class); + $jwt->shouldReceive('encode')->with([ + 'application_id' => 12345, + 'application_installation' => 'url', + 'application_product' => 'eshop', + 'application_language' => 'cs', + 'application_version' => '1.0', + 'application_parameters' => [ + 'guest' => false, + ] + ], 'test_application_token')->once()->andReturn('0.0.0'); + + Assert::match('~^[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+$~', $sign->authenticate(true, ['guest' => 'someValue'])); + } + + public function testGuestParameterNullToken(): void + { + $sign = new Sign($settings = Mockery::mock(Settings::class), Mockery::mock(Connection::class), new Url(), new ConfigurationDefault('url', 'eshop', '1.0', 'Test Eshop'), $language = Mockery::mock(Language::class), Mockery::mock(Logger::class)); + + $settings->shouldReceive('load')->with('static:application_token', false)->andReturn(null); + $settings->shouldReceive('load')->with('static:application_id')->andReturn(12345); + $language->shouldReceive('get')->withNoArgs()->once()->andReturn('cs'); + + $jwt = Mockery::mock('overload:' . Jwt::class); + $jwt->shouldReceive('encode')->with([ + 'application_id' => 12345, + 'application_installation' => 'url', + 'application_product' => 'eshop', + 'application_language' => 'cs', + 'application_version' => '1.0', + 'application_parameters' => [ + 'guest' => true, + ] + ], '')->once()->andReturn('0.0.0'); + + Assert::match('~^[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+$~', $sign->authenticate()); + } + + + public function tearDown(): void + { + Mockery::close(); + } +} + +(new SignTest())->run(); From 44bd6ee404fa7acaa86efcc78ad8879e850eeeb7 Mon Sep 17 00:00:00 2001 From: martin Date: Tue, 4 Mar 2025 13:37:51 +0100 Subject: [PATCH 3/4] fix type --- src/User/Sign.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/User/Sign.php b/src/User/Sign.php index 8d771f9..9b228d5 100644 --- a/src/User/Sign.php +++ b/src/User/Sign.php @@ -26,6 +26,9 @@ class Sign private Logger $logger; + /** + * @var array + */ private array $default_parameters = []; From 4c95faa22e4c666de819adc54086f136a4a1762f Mon Sep 17 00:00:00 2001 From: martin Date: Tue, 4 Mar 2025 13:49:35 +0100 Subject: [PATCH 4/4] php 7.4 - 8.3 matrix --- .github/workflows/php.yml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index a197be2..59bb87e 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -6,7 +6,12 @@ jobs: build: runs-on: ubuntu-latest - container: ghcr.io/bulkgate/plugin:latest + + strategy: + matrix: + php-version: [7.4, 8.3] + + container: ghcr.io/bulkgate/plugin:${{ matrix.php-version }} steps: - uses: actions/checkout@v1