|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +use Behat\Behat\Context\Context; |
| 10 | +use Behat\Behat\Hook\Scope\BeforeScenarioScope; |
| 11 | +use Behat\Hook\AfterScenario; |
| 12 | +use Behat\Hook\BeforeScenario; |
| 13 | +use Behat\Step\Then; |
| 14 | +use Behat\Step\When; |
| 15 | +use JuliusHaertl\NextcloudBehat\Context\ServerContext; |
| 16 | +use PHPUnit\Framework\Assert; |
| 17 | + |
| 18 | +class SettingsContext implements Context { |
| 19 | + /** @var ServerContext */ |
| 20 | + private $serverContext; |
| 21 | + |
| 22 | + /** @var GuzzleHttp\Client */ |
| 23 | + private $http; |
| 24 | + |
| 25 | + /** @var Psr\Http\Message\ResponseInterface */ |
| 26 | + private $httpResponse; |
| 27 | + |
| 28 | + public function __construct() { |
| 29 | + |
| 30 | + } |
| 31 | + |
| 32 | + #[BeforeScenario] |
| 33 | + public function gatherContexts(BeforeScenarioScope $scope) { |
| 34 | + $this->serverContext = $scope->getEnvironment()->getContext(ServerContext::class); |
| 35 | + |
| 36 | + $this->http = new GuzzleHttp\Client([ |
| 37 | + 'base_uri' => $this->serverContext->getBaseUrl() . 'index.php/apps/richdocuments/', |
| 38 | + 'http_errors' => false, |
| 39 | + ]); |
| 40 | + } |
| 41 | + |
| 42 | + #[AfterScenario] |
| 43 | + public function cleanup() { |
| 44 | + $this->httpResponse = null; |
| 45 | + } |
| 46 | + |
| 47 | + #[When('the admin settings are requested by a user')] |
| 48 | + public function userRequestAdminSettings() { |
| 49 | + $this->serverContext->actingAsUser('user1'); |
| 50 | + |
| 51 | + $options = $this->serverContext->getWebOptions(); |
| 52 | + $this->httpResponse = $this->http->get('ajax/settings.php', $options); |
| 53 | + } |
| 54 | + |
| 55 | + #[When('the admin settings are requested by an admin')] |
| 56 | + public function adminRequestAdminSettings() { |
| 57 | + $this->serverContext->actAsAdmin(function () { |
| 58 | + $options = $this->serverContext->getWebOptions(); |
| 59 | + $this->httpResponse = $this->http->get('ajax/settings.php', $options); |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + #[Then('the admin settings are forbidden')] |
| 64 | + public function adminSettingsRequestForbidden() { |
| 65 | + Assert::assertEquals(403, $this->httpResponse->getStatusCode()); |
| 66 | + |
| 67 | + Assert::assertJsonStringEqualsJsonString( |
| 68 | + '{"message":"Logged in account must be an admin"}', |
| 69 | + $this->httpResponse->getBody()->getContents(), |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + #[Then('the admin settings are returned')] |
| 74 | + public function adminSettingsRequestReturned() { |
| 75 | + Assert::assertEquals(200, $this->httpResponse->getStatusCode()); |
| 76 | + Assert::assertJsonStringNotEqualsJsonString( |
| 77 | + '{}', |
| 78 | + $this->httpResponse->getBody()->getContents(), |
| 79 | + ); |
| 80 | + } |
| 81 | +} |
0 commit comments