|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* The MIT License (MIT) |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2014-2018 Spomky-Labs |
|
9
|
|
|
* |
|
10
|
|
|
* This software may be modified and distributed under the terms |
|
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace OAuth2Framework\Component\AuthorizationEndpoint\Tests\ConsentScreen; |
|
15
|
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\Authorization; |
|
17
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\ConsentScreen\Extension; |
|
18
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\ConsentScreen\ExtensionManager; |
|
19
|
|
|
use OAuth2Framework\Component\Core\Client\Client; |
|
20
|
|
|
use OAuth2Framework\Component\Core\Client\ClientId; |
|
21
|
|
|
use OAuth2Framework\Component\Core\DataBag\DataBag; |
|
22
|
|
|
use PHPUnit\Framework\TestCase; |
|
23
|
|
|
use Prophecy\Argument; |
|
24
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @group ConsentScreenExtensionManager |
|
28
|
|
|
*/ |
|
29
|
|
|
class ConsentScreenExtensionManagerTest extends TestCase |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @test |
|
33
|
|
|
*/ |
|
34
|
|
|
public function theManagerCanCallExtensionsBeforeConsentScreenExtension() |
|
35
|
|
|
{ |
|
36
|
|
|
$request = $this->prophesize(ServerRequestInterface::class); |
|
37
|
|
|
$client = Client::createEmpty(); |
|
38
|
|
|
$client = $client->create( |
|
39
|
|
|
ClientId::create('CLIENT_ID'), |
|
40
|
|
|
DataBag::create([]), |
|
41
|
|
|
null |
|
42
|
|
|
); |
|
43
|
|
|
$client->eraseMessages(); |
|
44
|
|
|
$authorization = Authorization::create($client, []); |
|
45
|
|
|
$authorization = $this->getExtensionManager()->processBefore($request->reveal(), $authorization); |
|
46
|
|
|
self::assertTrue($authorization->hasData('Before Consent')); |
|
47
|
|
|
self::assertTrue($authorization->getData('Before Consent')); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @test |
|
52
|
|
|
*/ |
|
53
|
|
|
public function theManagerCanCallExtensionsAfterConsentScreenExtension() |
|
54
|
|
|
{ |
|
55
|
|
|
$request = $this->prophesize(ServerRequestInterface::class); |
|
56
|
|
|
$client = Client::createEmpty(); |
|
57
|
|
|
$client = $client->create( |
|
58
|
|
|
ClientId::create('CLIENT_ID'), |
|
59
|
|
|
DataBag::create([]), |
|
60
|
|
|
null |
|
61
|
|
|
); |
|
62
|
|
|
$client->eraseMessages(); |
|
63
|
|
|
$authorization = Authorization::create($client, []); |
|
64
|
|
|
$authorization = $this->getExtensionManager()->processAfter($request->reveal(), $authorization); |
|
65
|
|
|
self::assertTrue($authorization->hasData('After Consent')); |
|
66
|
|
|
self::assertTrue($authorization->getData('After Consent')); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @var null|ExtensionManager |
|
71
|
|
|
*/ |
|
72
|
|
|
private $extensionManager = null; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return ExtensionManager |
|
76
|
|
|
*/ |
|
77
|
|
|
private function getExtensionManager(): ExtensionManager |
|
78
|
|
|
{ |
|
79
|
|
|
if (null === $this->extensionManager) { |
|
80
|
|
|
$extension = $this->prophesize(Extension::class); |
|
81
|
|
|
$extension |
|
82
|
|
|
->processBefore(Argument::type(ServerRequestInterface::class), Argument::type(Authorization::class)) |
|
83
|
|
|
->will(function ($args) { |
|
84
|
|
|
/** @var Authorization $authorization */ |
|
85
|
|
|
$authorization = $args[1]; |
|
86
|
|
|
$authorization = $authorization->withData('Before Consent', true); |
|
87
|
|
|
|
|
88
|
|
|
return $authorization; |
|
89
|
|
|
}); |
|
90
|
|
|
$extension |
|
91
|
|
|
->processAfter(Argument::type(ServerRequestInterface::class), Argument::type(Authorization::class)) |
|
92
|
|
|
->will(function ($args) { |
|
93
|
|
|
/** @var Authorization $authorization */ |
|
94
|
|
|
$authorization = $args[1]; |
|
95
|
|
|
$authorization = $authorization->withData('After Consent', true); |
|
96
|
|
|
|
|
97
|
|
|
return $authorization; |
|
98
|
|
|
}); |
|
99
|
|
|
|
|
100
|
|
|
$this->extensionManager = new ExtensionManager(); |
|
101
|
|
|
$this->extensionManager->add($extension->reveal()); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $this->extensionManager; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|