Skip to content

Commit e4abbed

Browse files
committed
Extract ConsentHashServiceInterface
Extracting this interface from the existing service is allowing us to more easily test the service. As mocking a final class is not possible.
1 parent 118f9f1 commit e4abbed

File tree

5 files changed

+64
-59
lines changed

5 files changed

+64
-59
lines changed

library/EngineBlock/Corto/Model/Consent.php

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
use OpenConext\EngineBlock\Metadata\Entity\ServiceProvider;
2020
use OpenConext\EngineBlock\Authentication\Value\ConsentType;
21-
use OpenConext\EngineBlock\Service\Consent\ConsentHashService;
21+
use OpenConext\EngineBlock\Service\ConsentHashServiceInterface;
2222

2323
class EngineBlock_Corto_Model_Consent
2424
{
@@ -41,11 +41,6 @@ class EngineBlock_Corto_Model_Consent
4141
*/
4242
private $_responseAttributes;
4343

44-
/**
45-
* @var EngineBlock_Database_ConnectionFactory
46-
*/
47-
private $_databaseConnectionFactory;
48-
4944
/**
5045
* A reflection of the eb.run_all_manipulations_prior_to_consent feature flag
5146
*
@@ -61,7 +56,7 @@ class EngineBlock_Corto_Model_Consent
6156
private $_consentEnabled;
6257

6358
/**
64-
* @var ConsentHashService
59+
* @var ConsentHashServiceInterface
6560
*/
6661
private $_hashService;
6762

@@ -73,16 +68,14 @@ public function __construct(
7368
bool $mustStoreValues,
7469
EngineBlock_Saml2_ResponseAnnotationDecorator $response,
7570
array $responseAttributes,
76-
EngineBlock_Database_ConnectionFactory $databaseConnectionFactory,
7771
bool $amPriorToConsentEnabled,
7872
bool $consentEnabled,
79-
ConsentHashService $hashService
73+
ConsentHashServiceInterface $hashService
8074
) {
8175
$this->_tableName = $tableName;
8276
$this->_mustStoreValues = $mustStoreValues;
8377
$this->_response = $response;
8478
$this->_responseAttributes = $responseAttributes;
85-
$this->_databaseConnectionFactory = $databaseConnectionFactory;
8679
$this->_amPriorToConsentEnabled = $amPriorToConsentEnabled;
8780
$this->_hashService = $hashService;
8881
$this->_consentEnabled = $consentEnabled;
@@ -114,21 +107,8 @@ public function giveImplicitConsentFor(ServiceProvider $serviceProvider): bool
114107

115108
public function countTotalConsent(): int
116109
{
117-
$dbh = $this->_getConsentDatabaseConnection();
118-
if (!$dbh) {
119-
return 0;
120-
}
121-
122110
$consentUid = $this->_getConsentUid();
123-
return $this->_hashService->countTotalConsent($dbh, $consentUid);
124-
}
125-
126-
/**
127-
* @return bool|PDO
128-
*/
129-
protected function _getConsentDatabaseConnection()
130-
{
131-
return $this->_databaseConnectionFactory->create();
111+
return $this->_hashService->countTotalConsent($consentUid);
132112
}
133113

134114
protected function _getConsentUid()
@@ -152,36 +132,26 @@ protected function _getStableAttributesHash($attributes): string
152132

153133
private function _storeConsent(ServiceProvider $serviceProvider, $consentType): bool
154134
{
155-
$dbh = $this->_getConsentDatabaseConnection();
156-
if (!$dbh) {
157-
return false;
158-
}
159-
160135
$parameters = array(
161136
sha1($this->_getConsentUid()),
162137
$serviceProvider->entityId,
163138
$this->_getStableAttributesHash($this->_responseAttributes),
164139
$consentType,
165140
);
166141

167-
return $this->_hashService->storeConsentHashInDb($dbh, $parameters);
142+
return $this->_hashService->storeConsentHash($parameters);
168143
}
169144

170145
private function _hasStoredConsent(ServiceProvider $serviceProvider, $consentType): bool
171146
{
172-
$dbh = $this->_getConsentDatabaseConnection();
173-
if (!$dbh) {
174-
return false;
175-
}
176-
177147
$parameters = array(
178148
sha1($this->_getConsentUid()),
179149
$serviceProvider->entityId,
180150
$this->_getAttributesHash($this->_responseAttributes),
181151
$consentType,
182152
);
183153

184-
$hasUnstableConsentHash = $this->_hashService->retrieveConsentHashFromDb($dbh, $parameters);
154+
$hasUnstableConsentHash = $this->_hashService->retrieveConsentHash($parameters);
185155

186156
if ($hasUnstableConsentHash) {
187157
return true;
@@ -194,6 +164,6 @@ private function _hasStoredConsent(ServiceProvider $serviceProvider, $consentTyp
194164
$consentType,
195165
);
196166

197-
return $this->_hashService->retrieveConsentHashFromDb($dbh, $parameters);
167+
return $this->_hashService->retrieveConsentHash($parameters);
198168
}
199169
}

library/EngineBlock/Corto/Model/Consent/Factory.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,19 @@ class EngineBlock_Corto_Model_Consent_Factory
2626
/** @var EngineBlock_Corto_Filter_Command_Factory */
2727
private $_filterCommandFactory;
2828

29-
/** @var EngineBlock_Database_ConnectionFactory */
30-
private $_databaseConnectionFactory;
31-
3229
/**
3330
* @var ConsentHashService
3431
*/
3532
private $_hashService;
3633

3734
/**
3835
* @param EngineBlock_Corto_Filter_Command_Factory $filterCommandFactory
39-
* @param EngineBlock_Database_ConnectionFactory $databaseConnectionFactory
4036
*/
4137
public function __construct(
4238
EngineBlock_Corto_Filter_Command_Factory $filterCommandFactory,
43-
EngineBlock_Database_ConnectionFactory $databaseConnectionFactory,
4439
ConsentHashService $hashService
45-
)
46-
{
40+
) {
4741
$this->_filterCommandFactory = $filterCommandFactory;
48-
$this->_databaseConnectionFactory = $databaseConnectionFactory;
4942
$this->_hashService = $hashService;
5043
}
5144

@@ -76,7 +69,6 @@ public function create(
7669
$proxyServer->getConfig('ConsentStoreValues', true),
7770
$response,
7871
$attributes,
79-
$this->_databaseConnectionFactory,
8072
$amPriorToConsent,
8173
$consentEnabled,
8274
$this->_hashService

src/OpenConext/EngineBlock/Service/Consent/ConsentHashService.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
use function strtolower;
3434
use function unserialize;
3535

36-
final class ConsentHashService
36+
final class ConsentHashService implements ConsentHashServiceInterface
3737
{
3838
/**
3939
* @var ConsentRepository
@@ -45,12 +45,12 @@ public function __construct(ConsentRepository $consentHashRepository)
4545
$this->consentRepository = $consentHashRepository;
4646
}
4747

48-
public function retrieveConsentHashFromDb(array $parameters): bool
48+
public function retrieveConsentHash(array $parameters): bool
4949
{
5050
return $this->consentRepository->hasConsentHash($parameters);
5151
}
5252

53-
public function storeConsentHashInDb(array $parameters): bool
53+
public function storeConsentHash(array $parameters): bool
5454
{
5555
return $this->consentRepository->storeConsentHash($parameters);
5656
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2010 SURFnet B.V.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace OpenConext\EngineBlock\Service\Consent;
20+
21+
interface ConsentHashServiceInterface
22+
{
23+
public function retrieveConsentHash(array $parameters): bool;
24+
25+
public function storeConsentHash(array $parameters): bool;
26+
27+
public function countTotalConsent($consentUid): int;
28+
29+
public function getUnstableAttributesHash(array $attributes, bool $mustStoreValues): string;
30+
31+
public function getStableAttributesHash(array $attributes, bool $mustStoreValues) : string;
32+
}

tests/library/EngineBlock/Test/Corto/Model/ConsentTest.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,60 +16,71 @@
1616
* limitations under the License.
1717
*/
1818

19+
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
1920
use OpenConext\EngineBlock\Metadata\Entity\ServiceProvider;
21+
use OpenConext\EngineBlock\Service\ConsentHashServiceInterface;
2022
use PHPUnit\Framework\TestCase;
2123

2224
class EngineBlock_Corto_Model_Consent_Test extends TestCase
2325
{
26+
use MockeryPHPUnitIntegration;
27+
2428
private $consentDisabled;
2529
private $consent;
26-
private $mockedDatabaseConnection;
30+
private $consentService;
2731

2832
public function setup()
2933
{
30-
$this->mockedDatabaseConnection = Phake::mock('EngineBlock_Database_ConnectionFactory');
3134
$mockedResponse = Phake::mock('EngineBlock_Saml2_ResponseAnnotationDecorator');
3235

36+
$this->consentService = Mockery::mock(ConsentHashServiceInterface::class);
37+
3338
$this->consentDisabled = new EngineBlock_Corto_Model_Consent(
3439
"consent",
3540
true,
3641
$mockedResponse,
3742
[],
38-
$this->mockedDatabaseConnection,
3943
false,
40-
false
44+
false,
45+
$this->consentService
4146
);
4247

4348
$this->consent = new EngineBlock_Corto_Model_Consent(
4449
"consent",
4550
true,
4651
$mockedResponse,
4752
[],
48-
$this->mockedDatabaseConnection,
4953
false,
50-
true
54+
true,
55+
$this->consentService
5156
);
5257
}
5358

5459
public function testConsentDisabledDoesNotWriteToDatabase()
5560
{
5661
$serviceProvider = new ServiceProvider("service-provider-entity-id");
62+
63+
$this->consentService->shouldReceive('getUnstableAttributesHash');
64+
$this->consentService->shouldNotReceive('storeConsentHash');
5765
$this->consentDisabled->explicitConsentWasGivenFor($serviceProvider);
5866
$this->consentDisabled->implicitConsentWasGivenFor($serviceProvider);
5967
$this->consentDisabled->giveExplicitConsentFor($serviceProvider);
6068
$this->consentDisabled->giveImplicitConsentFor($serviceProvider);
61-
62-
Phake::verify($this->mockedDatabaseConnection, Phake::times(0))->create();
6369
}
6470

6571
public function testConsentWriteToDatabase()
6672
{
6773
$serviceProvider = new ServiceProvider("service-provider-entity-id");
74+
75+
$this->consentService->shouldReceive('getUnstableAttributesHash')->andReturn(sha1('unstable'));
76+
$this->consentService->shouldReceive('retrieveConsentHash')->andReturn(sha1('unstable'));
6877
$this->consent->explicitConsentWasGivenFor($serviceProvider);
78+
79+
$this->consentService->shouldReceive('getStableAttributesHash')->andReturn(sha1('stable'));
6980
$this->consent->implicitConsentWasGivenFor($serviceProvider);
81+
82+
$this->consentService->shouldReceive('storeConsentHash')->andReturn(true);
7083
$this->consent->giveExplicitConsentFor($serviceProvider);
7184
$this->consent->giveImplicitConsentFor($serviceProvider);
72-
73-
Phake::verify($this->mockedDatabaseConnection, Phake::times(4))->create();
7485
}
7586
}

0 commit comments

Comments
 (0)