|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace SimpleSAML\Test\Module\oidc\unit\Controllers\Admin; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 8 | +use PHPUnit\Framework\MockObject\MockObject; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use SimpleSAML\Module\oidc\Admin\Authorization; |
| 11 | +use SimpleSAML\Module\oidc\Controllers\Admin\ConfigController; |
| 12 | +use SimpleSAML\Module\oidc\Factories\TemplateFactory; |
| 13 | +use SimpleSAML\Module\oidc\ModuleConfig; |
| 14 | +use SimpleSAML\Module\oidc\Services\DatabaseMigration; |
| 15 | +use SimpleSAML\Module\oidc\Services\SessionMessagesService; |
| 16 | +use SimpleSAML\Module\oidc\Utils\Routes; |
| 17 | +use SimpleSAML\OpenID\Federation; |
| 18 | +use SimpleSAML\OpenID\Federation\Factories\TrustMarkFactory; |
| 19 | + |
| 20 | +#[CoversClass(ConfigController::class)] |
| 21 | +class ConfigControllerTest extends TestCase |
| 22 | +{ |
| 23 | + protected MockObject $moduleConfigMock; |
| 24 | + protected MockObject $templateFactoryMock; |
| 25 | + protected MockObject $authorizationMock; |
| 26 | + protected MockObject $databaseMigrationMock; |
| 27 | + protected MockObject $sessionMessagesServiceMock; |
| 28 | + protected MockObject $federationMock; |
| 29 | + protected MockObject $routesMock; |
| 30 | + protected MockObject $trustMarkFactoryMock; |
| 31 | + |
| 32 | + protected function setUp(): void |
| 33 | + { |
| 34 | + $this->moduleConfigMock = $this->createMock(ModuleConfig::class); |
| 35 | + $this->templateFactoryMock = $this->createMock(TemplateFactory::class); |
| 36 | + $this->authorizationMock = $this->createMock(Authorization::class); |
| 37 | + $this->databaseMigrationMock = $this->createMock(DatabaseMigration::class); |
| 38 | + $this->sessionMessagesServiceMock = $this->createMock(SessionMessagesService::class); |
| 39 | + $this->federationMock = $this->createMock(Federation::class); |
| 40 | + $this->routesMock = $this->createMock(Routes::class); |
| 41 | + |
| 42 | + $this->trustMarkFactoryMock = $this->createMock(TrustMarkFactory::class); |
| 43 | + $this->federationMock->method('trustMarkFactory')->willReturn($this->trustMarkFactoryMock); |
| 44 | + } |
| 45 | + |
| 46 | + public function sut( |
| 47 | + ?ModuleConfig $moduleConfig = null, |
| 48 | + ?TemplateFactory $templateFactory = null, |
| 49 | + ?Authorization $authorization = null, |
| 50 | + ?DatabaseMigration $databaseMigration = null, |
| 51 | + ?SessionMessagesService $sessionMessagesService = null, |
| 52 | + ?Federation $federation = null, |
| 53 | + ?Routes $routes = null, |
| 54 | + ): ConfigController { |
| 55 | + $moduleConfig ??= $this->moduleConfigMock; |
| 56 | + $templateFactory ??= $this->templateFactoryMock; |
| 57 | + $authorization ??= $this->authorizationMock; |
| 58 | + $databaseMigration ??= $this->databaseMigrationMock; |
| 59 | + $sessionMessagesService ??= $this->sessionMessagesServiceMock; |
| 60 | + $federation ??= $this->federationMock; |
| 61 | + $routes ??= $this->routesMock; |
| 62 | + |
| 63 | + return new ConfigController( |
| 64 | + $moduleConfig, |
| 65 | + $templateFactory, |
| 66 | + $authorization, |
| 67 | + $databaseMigration, |
| 68 | + $sessionMessagesService, |
| 69 | + $federation, |
| 70 | + $routes, |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + public function testCanCreateInstance(): void |
| 75 | + { |
| 76 | + $this->authorizationMock->expects($this->once())->method('requireAdmin'); |
| 77 | + $this->assertInstanceOf(ConfigController::class, $this->sut()); |
| 78 | + } |
| 79 | + |
| 80 | + public function testCanShowMigrationsScreen(): void |
| 81 | + { |
| 82 | + $this->templateFactoryMock->expects($this->once())->method('build') |
| 83 | + ->with('oidc:config/migrations.twig'); |
| 84 | + |
| 85 | + $this->sut()->migrations(); |
| 86 | + } |
| 87 | + |
| 88 | + public function testCanRunMigrations(): void |
| 89 | + { |
| 90 | + $this->databaseMigrationMock->expects($this->once())->method('migrate'); |
| 91 | + $this->sessionMessagesServiceMock->expects($this->once())->method('addMessage') |
| 92 | + ->with($this->stringContains('migrated')); |
| 93 | + |
| 94 | + $this->sut()->runMigrations(); |
| 95 | + } |
| 96 | + |
| 97 | + public function testWontRunMigrationsIfAlreadyMigrated(): void |
| 98 | + { |
| 99 | + $this->databaseMigrationMock->expects($this->once())->method('isMigrated')->willReturn(true); |
| 100 | + $this->databaseMigrationMock->expects($this->never())->method('migrate'); |
| 101 | + |
| 102 | + $this->sut()->runMigrations(); |
| 103 | + } |
| 104 | + |
| 105 | + public function testCanShowProtocolSettingsScreen(): void |
| 106 | + { |
| 107 | + $this->templateFactoryMock->expects($this->once())->method('build') |
| 108 | + ->with('oidc:config/protocol.twig'); |
| 109 | + |
| 110 | + $this->sut()->protocolSettings(); |
| 111 | + } |
| 112 | + |
| 113 | + public function testCanShowFederationSettingsScreen(): void |
| 114 | + { |
| 115 | + $this->templateFactoryMock->expects($this->once())->method('build') |
| 116 | + ->with('oidc:config/federation.twig'); |
| 117 | + |
| 118 | + $this->sut()->federationSettings(); |
| 119 | + } |
| 120 | + |
| 121 | + public function testCanIncludeTrustMarksInFederationSettings(): void |
| 122 | + { |
| 123 | + $this->moduleConfigMock->method('getFederationTrustMarkTokens')->willReturn(['token']); |
| 124 | + $this->trustMarkFactoryMock->expects($this->once())->method('fromToken') |
| 125 | + ->with($this->stringContains('token')); |
| 126 | + |
| 127 | + $this->templateFactoryMock->expects($this->once())->method('build') |
| 128 | + ->with('oidc:config/federation.twig'); |
| 129 | + |
| 130 | + $this->sut()->federationSettings(); |
| 131 | + } |
| 132 | +} |
0 commit comments