1
0

ManagerTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Settings\Tests\AppInfo;
  7. use OC\Settings\AuthorizedGroupMapper;
  8. use OC\Settings\Manager;
  9. use OCP\Group\ISubAdmin;
  10. use OCP\IDBConnection;
  11. use OCP\IGroupManager;
  12. use OCP\IL10N;
  13. use OCP\IServerContainer;
  14. use OCP\IURLGenerator;
  15. use OCP\L10N\IFactory;
  16. use OCP\Settings\ISettings;
  17. use OCP\Settings\ISubAdminSettings;
  18. use PHPUnit\Framework\MockObject\MockObject;
  19. use Psr\Log\LoggerInterface;
  20. use Test\TestCase;
  21. class ManagerTest extends TestCase {
  22. /** @var Manager|MockObject */
  23. private $manager;
  24. /** @var LoggerInterface|MockObject */
  25. private $logger;
  26. /** @var IDBConnection|MockObject */
  27. private $l10n;
  28. /** @var IFactory|MockObject */
  29. private $l10nFactory;
  30. /** @var IURLGenerator|MockObject */
  31. private $url;
  32. /** @var IServerContainer|MockObject */
  33. private $container;
  34. /** @var AuthorizedGroupMapper|MockObject */
  35. private $mapper;
  36. /** @var IGroupManager|MockObject */
  37. private $groupManager;
  38. /** @var ISubAdmin|MockObject */
  39. private $subAdmin;
  40. protected function setUp(): void {
  41. parent::setUp();
  42. $this->logger = $this->createMock(LoggerInterface::class);
  43. $this->l10n = $this->createMock(IL10N::class);
  44. $this->l10nFactory = $this->createMock(IFactory::class);
  45. $this->url = $this->createMock(IURLGenerator::class);
  46. $this->container = $this->createMock(IServerContainer::class);
  47. $this->mapper = $this->createMock(AuthorizedGroupMapper::class);
  48. $this->groupManager = $this->createMock(IGroupManager::class);
  49. $this->subAdmin = $this->createMock(ISubAdmin::class);
  50. $this->manager = new Manager(
  51. $this->logger,
  52. $this->l10nFactory,
  53. $this->url,
  54. $this->container,
  55. $this->mapper,
  56. $this->groupManager,
  57. $this->subAdmin,
  58. );
  59. }
  60. public function testGetAdminSections() {
  61. $this->manager->registerSection('admin', \OCA\WorkflowEngine\Settings\Section::class);
  62. $section = \OC::$server->query(\OCA\WorkflowEngine\Settings\Section::class);
  63. $this->container->method('get')
  64. ->with(\OCA\WorkflowEngine\Settings\Section::class)
  65. ->willReturn($section);
  66. $this->assertEquals([
  67. 55 => [$section],
  68. ], $this->manager->getAdminSections());
  69. }
  70. public function testGetPersonalSections() {
  71. $this->manager->registerSection('personal', \OCA\WorkflowEngine\Settings\Section::class);
  72. $section = \OC::$server->query(\OCA\WorkflowEngine\Settings\Section::class);
  73. $this->container->method('get')
  74. ->with(\OCA\WorkflowEngine\Settings\Section::class)
  75. ->willReturn($section);
  76. $this->assertEquals([
  77. 55 => [$section],
  78. ], $this->manager->getPersonalSections());
  79. }
  80. public function testGetAdminSectionsEmptySection() {
  81. $this->assertEquals([], $this->manager->getAdminSections());
  82. }
  83. public function testGetPersonalSectionsEmptySection() {
  84. $this->l10nFactory
  85. ->expects($this->once())
  86. ->method('get')
  87. ->with('lib')
  88. ->willReturn($this->l10n);
  89. $this->l10n
  90. ->expects($this->any())
  91. ->method('t')
  92. ->willReturnArgument(0);
  93. $this->assertEquals([], $this->manager->getPersonalSections());
  94. }
  95. public function testGetAdminSettings() {
  96. $section = $this->createMock(ISettings::class);
  97. $section->method('getPriority')
  98. ->willReturn(13);
  99. $section->method('getSection')
  100. ->willReturn('sharing');
  101. $this->container->method('get')
  102. ->with('myAdminClass')
  103. ->willReturn($section);
  104. $this->manager->registerSetting('admin', 'myAdminClass');
  105. $settings = $this->manager->getAdminSettings('sharing');
  106. $this->assertEquals([
  107. 13 => [$section]
  108. ], $settings);
  109. }
  110. public function testGetAdminSettingsAsSubAdmin() {
  111. $section = $this->createMock(ISettings::class);
  112. $section->method('getPriority')
  113. ->willReturn(13);
  114. $section->method('getSection')
  115. ->willReturn('sharing');
  116. $this->container->method('get')
  117. ->with('myAdminClass')
  118. ->willReturn($section);
  119. $this->manager->registerSetting('admin', 'myAdminClass');
  120. $settings = $this->manager->getAdminSettings('sharing', true);
  121. $this->assertEquals([], $settings);
  122. }
  123. public function testGetSubAdminSettingsAsSubAdmin() {
  124. $section = $this->createMock(ISubAdminSettings::class);
  125. $section->method('getPriority')
  126. ->willReturn(13);
  127. $section->method('getSection')
  128. ->willReturn('sharing');
  129. $this->container->expects($this->once())
  130. ->method('get')
  131. ->with('mySubAdminClass')
  132. ->willReturn($section);
  133. $this->manager->registerSetting('admin', 'mySubAdminClass');
  134. $settings = $this->manager->getAdminSettings('sharing', true);
  135. $this->assertEquals([
  136. 13 => [$section]
  137. ], $settings);
  138. }
  139. public function testGetPersonalSettings() {
  140. $section = $this->createMock(ISettings::class);
  141. $section->method('getPriority')
  142. ->willReturn(16);
  143. $section->method('getSection')
  144. ->willReturn('security');
  145. $section2 = $this->createMock(ISettings::class);
  146. $section2->method('getPriority')
  147. ->willReturn(100);
  148. $section2->method('getSection')
  149. ->willReturn('security');
  150. $this->manager->registerSetting('personal', 'section1');
  151. $this->manager->registerSetting('personal', 'section2');
  152. $this->container->expects($this->exactly(2))
  153. ->method('get')
  154. ->withConsecutive(
  155. ['section1'],
  156. ['section2']
  157. )
  158. ->willReturnMap([
  159. ['section1', $section],
  160. ['section2', $section2],
  161. ]);
  162. $settings = $this->manager->getPersonalSettings('security');
  163. $this->assertEquals([
  164. 16 => [$section],
  165. 100 => [$section2],
  166. ], $settings);
  167. }
  168. public function testSameSectionAsPersonalAndAdmin() {
  169. $this->l10nFactory
  170. ->expects($this->once())
  171. ->method('get')
  172. ->with('lib')
  173. ->willReturn($this->l10n);
  174. $this->l10n
  175. ->expects($this->any())
  176. ->method('t')
  177. ->willReturnArgument(0);
  178. $this->manager->registerSection('personal', \OCA\WorkflowEngine\Settings\Section::class);
  179. $this->manager->registerSection('admin', \OCA\WorkflowEngine\Settings\Section::class);
  180. $section = \OC::$server->query(\OCA\WorkflowEngine\Settings\Section::class);
  181. $this->container->method('get')
  182. ->with(\OCA\WorkflowEngine\Settings\Section::class)
  183. ->willReturn($section);
  184. $this->assertEquals([
  185. 55 => [$section],
  186. ], $this->manager->getPersonalSections());
  187. $this->assertEquals([
  188. 55 => [$section],
  189. ], $this->manager->getAdminSections());
  190. }
  191. }