aboutsummaryrefslogtreecommitdiffstats
path: root/apps/settings/tests/Middleware/SubadminMiddlewareTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'apps/settings/tests/Middleware/SubadminMiddlewareTest.php')
-rw-r--r--apps/settings/tests/Middleware/SubadminMiddlewareTest.php124
1 files changed, 63 insertions, 61 deletions
diff --git a/apps/settings/tests/Middleware/SubadminMiddlewareTest.php b/apps/settings/tests/Middleware/SubadminMiddlewareTest.php
index a1330857e02..37cfb5ccc59 100644
--- a/apps/settings/tests/Middleware/SubadminMiddlewareTest.php
+++ b/apps/settings/tests/Middleware/SubadminMiddlewareTest.php
@@ -1,29 +1,13 @@
<?php
+
+declare(strict_types=1);
+
/**
- * @copyright 2014 Lukas Reschke lukas@owncloud.com
- *
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Lukas Reschke <lukas@statuscode.ch>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2014 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
+
namespace OCA\Settings\Tests\Middleware;
use OC\AppFramework\Middleware\Security\Exceptions\NotAdminException;
@@ -31,7 +15,11 @@ use OC\AppFramework\Utility\ControllerMethodReflector;
use OCA\Settings\Middleware\SubadminMiddleware;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\TemplateResponse;
+use OCP\Group\ISubAdmin;
use OCP\IL10N;
+use OCP\IUser;
+use OCP\IUserSession;
+use PHPUnit\Framework\MockObject\MockObject;
/**
* Verifies whether an user has at least subadmin rights.
@@ -40,81 +28,95 @@ use OCP\IL10N;
* @package Tests\Settings\Middleware
*/
class SubadminMiddlewareTest extends \Test\TestCase {
- /** @var SubadminMiddleware */
- private $subadminMiddlewareAsSubAdmin;
- /** @var SubadminMiddleware */
- private $subadminMiddleware;
- /** @var ControllerMethodReflector */
- private $reflector;
- /** @var Controller */
- private $controller;
- /** @var IL10N */
- private $l10n;
+ private SubadminMiddleware $subadminMiddleware;
+ private IUserSession&MockObject $userSession;
+ private ISubAdmin&MockObject $subAdminManager;
+ private ControllerMethodReflector&MockObject $reflector;
+ private Controller&MockObject $controller;
+ private IL10N&MockObject $l10n;
protected function setUp(): void {
parent::setUp();
- $this->reflector = $this->getMockBuilder(ControllerMethodReflector::class)
- ->disableOriginalConstructor()->getMock();
- $this->controller = $this->getMockBuilder(Controller::class)
- ->disableOriginalConstructor()->getMock();
+ $this->reflector = $this->createMock(ControllerMethodReflector::class);
+ $this->userSession = $this->createMock(IUserSession::class);
+ $this->subAdminManager = $this->createMock(ISubAdmin::class);
$this->l10n = $this->createMock(IL10N::class);
- $this->subadminMiddlewareAsSubAdmin = new SubadminMiddleware($this->reflector, true, $this->l10n);
- $this->subadminMiddleware = new SubadminMiddleware($this->reflector, false, $this->l10n);
+ $this->subadminMiddleware = new SubadminMiddleware(
+ $this->reflector,
+ $this->userSession,
+ $this->subAdminManager,
+ $this->l10n,
+ );
+
+ $this->controller = $this->createMock(Controller::class);
+
+ $this->userSession
+ ->expects(self::any())
+ ->method('getUser')
+ ->willReturn($this->createMock(IUser::class));
}
- public function testBeforeControllerAsUserWithExemption() {
- $this->expectException(\OC\AppFramework\Middleware\Security\Exceptions\NotAdminException::class);
+ public function testBeforeControllerAsUserWithoutAnnotation(): void {
+ $this->expectException(NotAdminException::class);
$this->reflector
->expects($this->exactly(2))
->method('hasAnnotation')
- ->withConsecutive(
- ['NoSubAdminRequired'],
- ['AuthorizedAdminSetting'],
- )->willReturn(false);
+ ->willReturnMap([
+ ['NoSubAdminRequired', false],
+ ['AuthorizedAdminSetting', false],
+ ]);
+
+ $this->subAdminManager
+ ->expects(self::once())
+ ->method('isSubAdmin')
+ ->willReturn(false);
+
$this->subadminMiddleware->beforeController($this->controller, 'foo');
}
- public function testBeforeControllerAsUserWithoutExemption() {
+ public function testBeforeControllerWithAnnotation(): void {
$this->reflector
->expects($this->once())
->method('hasAnnotation')
->with('NoSubAdminRequired')
->willReturn(true);
+
+ $this->subAdminManager
+ ->expects(self::never())
+ ->method('isSubAdmin');
+
$this->subadminMiddleware->beforeController($this->controller, 'foo');
}
- public function testBeforeControllerAsSubAdminWithoutExemption() {
+ public function testBeforeControllerAsSubAdminWithoutAnnotation(): void {
$this->reflector
->expects($this->exactly(2))
->method('hasAnnotation')
- ->withConsecutive(
- ['NoSubAdminRequired'],
- ['AuthorizedAdminSetting'],
- )->willReturn(false);
- $this->subadminMiddlewareAsSubAdmin->beforeController($this->controller, 'foo');
- }
+ ->willReturnMap([
+ ['NoSubAdminRequired', false],
+ ['AuthorizedAdminSetting', false],
+ ]);
- public function testBeforeControllerAsSubAdminWithExemption() {
- $this->reflector
- ->expects($this->once())
- ->method('hasAnnotation')
- ->with('NoSubAdminRequired')
+ $this->subAdminManager
+ ->expects(self::once())
+ ->method('isSubAdmin')
->willReturn(true);
- $this->subadminMiddlewareAsSubAdmin->beforeController($this->controller, 'foo');
+
+ $this->subadminMiddleware->beforeController($this->controller, 'foo');
}
- public function testAfterNotAdminException() {
+ public function testAfterNotAdminException(): void {
$expectedResponse = new TemplateResponse('core', '403', [], 'guest');
$expectedResponse->setStatus(403);
$this->assertEquals($expectedResponse, $this->subadminMiddleware->afterException($this->controller, 'foo', new NotAdminException('')));
}
- public function testAfterRegularException() {
+ public function testAfterRegularException(): void {
$this->expectException(\Exception::class);
$expectedResponse = new TemplateResponse('core', '403', [], 'guest');