2016-08-15 10:24:56 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2016-08-15 10:24:56 -04:00
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2016-08-15 10:24:56 -04:00
|
|
|
*/
|
|
|
|
|
|
2022-05-31 04:23:52 -04:00
|
|
|
namespace OC\Settings\Tests\AppInfo;
|
2016-08-15 10:24:56 -04:00
|
|
|
|
2021-07-22 05:41:29 -04:00
|
|
|
use OC\Settings\AuthorizedGroupMapper;
|
2016-08-15 10:24:56 -04:00
|
|
|
use OC\Settings\Manager;
|
2025-06-12 12:31:58 -04:00
|
|
|
use OCA\WorkflowEngine\Settings\Section;
|
2021-07-22 05:41:29 -04:00
|
|
|
use OCP\Group\ISubAdmin;
|
2016-08-15 10:24:56 -04:00
|
|
|
use OCP\IDBConnection;
|
2021-07-22 05:41:29 -04:00
|
|
|
use OCP\IGroupManager;
|
2016-08-15 10:24:56 -04:00
|
|
|
use OCP\IL10N;
|
2018-09-26 10:46:35 -04:00
|
|
|
use OCP\IServerContainer;
|
2017-01-19 05:02:56 -05:00
|
|
|
use OCP\IURLGenerator;
|
2019-05-21 16:25:11 -04:00
|
|
|
use OCP\L10N\IFactory;
|
2025-06-12 12:31:58 -04:00
|
|
|
use OCP\Server;
|
2020-01-30 07:30:45 -05:00
|
|
|
use OCP\Settings\ISettings;
|
2019-05-22 04:48:51 -04:00
|
|
|
use OCP\Settings\ISubAdminSettings;
|
2023-11-23 04:22:34 -05:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2021-07-22 05:41:29 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
2016-08-15 10:24:56 -04:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
|
|
class ManagerTest extends TestCase {
|
2022-11-14 10:14:35 -05:00
|
|
|
/** @var Manager|MockObject */
|
2016-08-15 10:24:56 -04:00
|
|
|
private $manager;
|
2022-11-14 10:14:35 -05:00
|
|
|
/** @var LoggerInterface|MockObject */
|
2016-08-15 10:24:56 -04:00
|
|
|
private $logger;
|
2022-11-14 10:14:35 -05:00
|
|
|
/** @var IDBConnection|MockObject */
|
2016-08-15 10:24:56 -04:00
|
|
|
private $l10n;
|
2022-11-14 10:14:35 -05:00
|
|
|
/** @var IFactory|MockObject */
|
2019-05-21 16:25:11 -04:00
|
|
|
private $l10nFactory;
|
2022-11-14 10:14:35 -05:00
|
|
|
/** @var IURLGenerator|MockObject */
|
2017-01-19 05:02:56 -05:00
|
|
|
private $url;
|
2022-11-14 10:14:35 -05:00
|
|
|
/** @var IServerContainer|MockObject */
|
2018-09-26 10:46:35 -04:00
|
|
|
private $container;
|
2022-11-14 10:14:35 -05:00
|
|
|
/** @var AuthorizedGroupMapper|MockObject */
|
|
|
|
|
private $mapper;
|
|
|
|
|
/** @var IGroupManager|MockObject */
|
2021-07-22 05:41:29 -04:00
|
|
|
private $groupManager;
|
2022-11-14 10:14:35 -05:00
|
|
|
/** @var ISubAdmin|MockObject */
|
2021-07-22 05:41:29 -04:00
|
|
|
private $subAdmin;
|
2016-08-15 10:24:56 -04:00
|
|
|
|
2019-11-27 09:27:18 -05:00
|
|
|
protected function setUp(): void {
|
2016-08-15 10:24:56 -04:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2021-07-22 05:41:29 -04:00
|
|
|
$this->logger = $this->createMock(LoggerInterface::class);
|
2017-01-19 05:02:56 -05:00
|
|
|
$this->l10n = $this->createMock(IL10N::class);
|
2019-05-21 16:25:11 -04:00
|
|
|
$this->l10nFactory = $this->createMock(IFactory::class);
|
2017-01-19 05:02:56 -05:00
|
|
|
$this->url = $this->createMock(IURLGenerator::class);
|
2018-09-26 10:46:35 -04:00
|
|
|
$this->container = $this->createMock(IServerContainer::class);
|
2021-07-22 05:41:29 -04:00
|
|
|
$this->mapper = $this->createMock(AuthorizedGroupMapper::class);
|
|
|
|
|
$this->groupManager = $this->createMock(IGroupManager::class);
|
|
|
|
|
$this->subAdmin = $this->createMock(ISubAdmin::class);
|
2016-08-15 10:24:56 -04:00
|
|
|
|
|
|
|
|
$this->manager = new Manager(
|
|
|
|
|
$this->logger,
|
2019-05-21 16:25:11 -04:00
|
|
|
$this->l10nFactory,
|
2017-05-19 11:34:57 -04:00
|
|
|
$this->url,
|
2021-07-22 05:41:29 -04:00
|
|
|
$this->container,
|
|
|
|
|
$this->mapper,
|
|
|
|
|
$this->groupManager,
|
|
|
|
|
$this->subAdmin,
|
2016-08-15 10:24:56 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testGetAdminSections(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->manager->registerSection('admin', Section::class);
|
2016-12-28 10:58:02 -05:00
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
$section = Server::get(Section::class);
|
2022-05-31 04:23:52 -04:00
|
|
|
$this->container->method('get')
|
2025-06-12 12:31:58 -04:00
|
|
|
->with(Section::class)
|
2022-05-31 04:23:52 -04:00
|
|
|
->willReturn($section);
|
|
|
|
|
|
2016-12-28 10:58:02 -05:00
|
|
|
$this->assertEquals([
|
2022-05-31 04:23:52 -04:00
|
|
|
55 => [$section],
|
2016-12-28 10:58:02 -05:00
|
|
|
], $this->manager->getAdminSections());
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testGetPersonalSections(): void {
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->manager->registerSection('personal', Section::class);
|
2017-05-19 11:34:57 -04:00
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
$section = Server::get(Section::class);
|
2022-05-31 04:23:52 -04:00
|
|
|
$this->container->method('get')
|
2025-06-12 12:31:58 -04:00
|
|
|
->with(Section::class)
|
2022-05-31 04:23:52 -04:00
|
|
|
->willReturn($section);
|
|
|
|
|
|
2018-01-29 07:14:56 -05:00
|
|
|
$this->assertEquals([
|
2022-05-31 04:23:52 -04:00
|
|
|
55 => [$section],
|
2017-05-19 11:34:57 -04:00
|
|
|
], $this->manager->getPersonalSections());
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testGetAdminSectionsEmptySection(): void {
|
2020-01-28 06:38:11 -05:00
|
|
|
$this->assertEquals([], $this->manager->getAdminSections());
|
2016-08-15 10:24:56 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testGetPersonalSectionsEmptySection(): void {
|
2019-05-21 16:25:11 -04:00
|
|
|
$this->l10nFactory
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('get')
|
|
|
|
|
->with('lib')
|
|
|
|
|
->willReturn($this->l10n);
|
2017-05-19 11:34:57 -04:00
|
|
|
$this->l10n
|
|
|
|
|
->expects($this->any())
|
|
|
|
|
->method('t')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturnArgument(0);
|
2017-05-19 11:34:57 -04:00
|
|
|
|
2020-01-27 14:47:41 -05:00
|
|
|
$this->assertEquals([], $this->manager->getPersonalSections());
|
2017-05-19 11:34:57 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testGetAdminSettings(): void {
|
2020-01-31 10:55:17 -05:00
|
|
|
$section = $this->createMock(ISettings::class);
|
|
|
|
|
$section->method('getPriority')
|
2018-09-26 10:46:35 -04:00
|
|
|
->willReturn(13);
|
2020-01-31 10:55:17 -05:00
|
|
|
$section->method('getSection')
|
|
|
|
|
->willReturn('sharing');
|
2021-07-22 05:41:29 -04:00
|
|
|
$this->container->method('get')
|
2020-01-31 10:55:17 -05:00
|
|
|
->with('myAdminClass')
|
2018-09-26 10:46:35 -04:00
|
|
|
->willReturn($section);
|
|
|
|
|
|
2020-01-31 10:55:17 -05:00
|
|
|
$this->manager->registerSetting('admin', 'myAdminClass');
|
2018-09-26 10:46:35 -04:00
|
|
|
$settings = $this->manager->getAdminSettings('sharing');
|
|
|
|
|
|
2016-08-15 10:24:56 -04:00
|
|
|
$this->assertEquals([
|
2018-09-26 10:46:35 -04:00
|
|
|
13 => [$section]
|
|
|
|
|
], $settings);
|
2016-08-15 10:24:56 -04:00
|
|
|
}
|
2017-05-19 11:34:57 -04:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testGetAdminSettingsAsSubAdmin(): void {
|
2020-01-31 10:55:17 -05:00
|
|
|
$section = $this->createMock(ISettings::class);
|
|
|
|
|
$section->method('getPriority')
|
|
|
|
|
->willReturn(13);
|
|
|
|
|
$section->method('getSection')
|
|
|
|
|
->willReturn('sharing');
|
2021-07-22 05:41:29 -04:00
|
|
|
$this->container->method('get')
|
2020-01-31 10:55:17 -05:00
|
|
|
->with('myAdminClass')
|
2019-05-22 04:48:51 -04:00
|
|
|
->willReturn($section);
|
|
|
|
|
|
2020-01-31 10:55:17 -05:00
|
|
|
$this->manager->registerSetting('admin', 'myAdminClass');
|
2019-05-22 04:48:51 -04:00
|
|
|
$settings = $this->manager->getAdminSettings('sharing', true);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals([], $settings);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testGetSubAdminSettingsAsSubAdmin(): void {
|
2019-05-22 04:48:51 -04:00
|
|
|
$section = $this->createMock(ISubAdminSettings::class);
|
2020-01-31 10:55:17 -05:00
|
|
|
$section->method('getPriority')
|
2019-05-22 04:48:51 -04:00
|
|
|
->willReturn(13);
|
2020-01-31 10:55:17 -05:00
|
|
|
$section->method('getSection')
|
|
|
|
|
->willReturn('sharing');
|
2019-05-22 04:48:51 -04:00
|
|
|
$this->container->expects($this->once())
|
2021-07-22 05:41:29 -04:00
|
|
|
->method('get')
|
2020-01-31 10:55:17 -05:00
|
|
|
->with('mySubAdminClass')
|
2019-05-22 04:48:51 -04:00
|
|
|
->willReturn($section);
|
|
|
|
|
|
2020-01-31 10:55:17 -05:00
|
|
|
$this->manager->registerSetting('admin', 'mySubAdminClass');
|
2019-05-22 04:48:51 -04:00
|
|
|
$settings = $this->manager->getAdminSettings('sharing', true);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
|
13 => [$section]
|
|
|
|
|
], $settings);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testGetPersonalSettings(): void {
|
2020-01-30 07:30:45 -05:00
|
|
|
$section = $this->createMock(ISettings::class);
|
|
|
|
|
$section->method('getPriority')
|
2018-09-26 10:46:35 -04:00
|
|
|
->willReturn(16);
|
2020-01-30 07:30:45 -05:00
|
|
|
$section->method('getSection')
|
|
|
|
|
->willReturn('security');
|
|
|
|
|
$section2 = $this->createMock(ISettings::class);
|
|
|
|
|
$section2->method('getPriority')
|
2019-09-09 16:12:29 -04:00
|
|
|
->willReturn(100);
|
2020-01-30 07:30:45 -05:00
|
|
|
$section2->method('getSection')
|
|
|
|
|
->willReturn('security');
|
|
|
|
|
|
|
|
|
|
$this->manager->registerSetting('personal', 'section1');
|
|
|
|
|
$this->manager->registerSetting('personal', 'section2');
|
|
|
|
|
|
2022-05-31 04:23:52 -04:00
|
|
|
$this->container->expects($this->exactly(2))
|
2021-07-22 05:41:29 -04:00
|
|
|
->method('get')
|
2022-05-31 04:23:52 -04:00
|
|
|
->willReturnMap([
|
|
|
|
|
['section1', $section],
|
|
|
|
|
['section2', $section2],
|
|
|
|
|
]);
|
2018-09-26 10:46:35 -04:00
|
|
|
|
|
|
|
|
$settings = $this->manager->getPersonalSettings('security');
|
|
|
|
|
|
2017-05-19 11:34:57 -04:00
|
|
|
$this->assertEquals([
|
2019-09-09 16:12:29 -04:00
|
|
|
16 => [$section],
|
|
|
|
|
100 => [$section2],
|
2018-09-26 10:46:35 -04:00
|
|
|
], $settings);
|
2017-05-19 11:34:57 -04:00
|
|
|
}
|
2018-08-25 10:48:37 -04:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testSameSectionAsPersonalAndAdmin(): void {
|
2019-05-21 16:25:11 -04:00
|
|
|
$this->l10nFactory
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('get')
|
|
|
|
|
->with('lib')
|
|
|
|
|
->willReturn($this->l10n);
|
2018-08-25 10:48:37 -04:00
|
|
|
$this->l10n
|
|
|
|
|
->expects($this->any())
|
|
|
|
|
->method('t')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturnArgument(0);
|
2018-08-25 10:48:37 -04:00
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
$this->manager->registerSection('personal', Section::class);
|
|
|
|
|
$this->manager->registerSection('admin', Section::class);
|
2018-08-25 10:48:37 -04:00
|
|
|
|
2022-05-31 04:23:52 -04:00
|
|
|
|
2025-06-12 12:31:58 -04:00
|
|
|
$section = Server::get(Section::class);
|
2022-05-31 04:23:52 -04:00
|
|
|
$this->container->method('get')
|
2025-06-12 12:31:58 -04:00
|
|
|
->with(Section::class)
|
2022-05-31 04:23:52 -04:00
|
|
|
->willReturn($section);
|
|
|
|
|
|
2018-08-25 10:48:37 -04:00
|
|
|
$this->assertEquals([
|
2022-05-31 04:23:52 -04:00
|
|
|
55 => [$section],
|
2018-08-25 10:48:37 -04:00
|
|
|
], $this->manager->getPersonalSections());
|
|
|
|
|
|
|
|
|
|
$this->assertEquals([
|
2022-05-31 04:23:52 -04:00
|
|
|
55 => [$section],
|
2018-08-25 10:48:37 -04:00
|
|
|
], $this->manager->getAdminSections());
|
|
|
|
|
}
|
2016-08-15 10:24:56 -04:00
|
|
|
}
|