ManagerTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Settings\Tests\AppInfo;
  24. use OC\Settings\AuthorizedGroupMapper;
  25. use OC\Settings\Manager;
  26. use OCP\Group\ISubAdmin;
  27. use OCP\IDBConnection;
  28. use OCP\IGroupManager;
  29. use OCP\IL10N;
  30. use OCP\IServerContainer;
  31. use OCP\IURLGenerator;
  32. use OCP\L10N\IFactory;
  33. use OCP\Settings\ISettings;
  34. use OCP\Settings\ISubAdminSettings;
  35. use PHPUnit\Framework\MockObject\MockObject;
  36. use Psr\Log\LoggerInterface;
  37. use Test\TestCase;
  38. class ManagerTest extends TestCase {
  39. /** @var Manager|MockObject */
  40. private $manager;
  41. /** @var LoggerInterface|MockObject */
  42. private $logger;
  43. /** @var IDBConnection|MockObject */
  44. private $l10n;
  45. /** @var IFactory|MockObject */
  46. private $l10nFactory;
  47. /** @var IURLGenerator|MockObject */
  48. private $url;
  49. /** @var IServerContainer|MockObject */
  50. private $container;
  51. /** @var AuthorizedGroupMapper|MockObject */
  52. private $mapper;
  53. /** @var IGroupManager|MockObject */
  54. private $groupManager;
  55. /** @var ISubAdmin|MockObject */
  56. private $subAdmin;
  57. protected function setUp(): void {
  58. parent::setUp();
  59. $this->logger = $this->createMock(LoggerInterface::class);
  60. $this->l10n = $this->createMock(IL10N::class);
  61. $this->l10nFactory = $this->createMock(IFactory::class);
  62. $this->url = $this->createMock(IURLGenerator::class);
  63. $this->container = $this->createMock(IServerContainer::class);
  64. $this->mapper = $this->createMock(AuthorizedGroupMapper::class);
  65. $this->groupManager = $this->createMock(IGroupManager::class);
  66. $this->subAdmin = $this->createMock(ISubAdmin::class);
  67. $this->manager = new Manager(
  68. $this->logger,
  69. $this->l10nFactory,
  70. $this->url,
  71. $this->container,
  72. $this->mapper,
  73. $this->groupManager,
  74. $this->subAdmin,
  75. );
  76. }
  77. public function testGetAdminSections() {
  78. $this->manager->registerSection('admin', \OCA\WorkflowEngine\Settings\Section::class);
  79. $section = \OC::$server->query(\OCA\WorkflowEngine\Settings\Section::class);
  80. $this->container->method('get')
  81. ->with(\OCA\WorkflowEngine\Settings\Section::class)
  82. ->willReturn($section);
  83. $this->assertEquals([
  84. 55 => [$section],
  85. ], $this->manager->getAdminSections());
  86. }
  87. public function testGetPersonalSections() {
  88. $this->manager->registerSection('personal', \OCA\WorkflowEngine\Settings\Section::class);
  89. $section = \OC::$server->query(\OCA\WorkflowEngine\Settings\Section::class);
  90. $this->container->method('get')
  91. ->with(\OCA\WorkflowEngine\Settings\Section::class)
  92. ->willReturn($section);
  93. $this->assertEquals([
  94. 55 => [$section],
  95. ], $this->manager->getPersonalSections());
  96. }
  97. public function testGetAdminSectionsEmptySection() {
  98. $this->assertEquals([], $this->manager->getAdminSections());
  99. }
  100. public function testGetPersonalSectionsEmptySection() {
  101. $this->l10nFactory
  102. ->expects($this->once())
  103. ->method('get')
  104. ->with('lib')
  105. ->willReturn($this->l10n);
  106. $this->l10n
  107. ->expects($this->any())
  108. ->method('t')
  109. ->willReturnArgument(0);
  110. $this->assertEquals([], $this->manager->getPersonalSections());
  111. }
  112. public function testGetAdminSettings() {
  113. $section = $this->createMock(ISettings::class);
  114. $section->method('getPriority')
  115. ->willReturn(13);
  116. $section->method('getSection')
  117. ->willReturn('sharing');
  118. $this->container->method('get')
  119. ->with('myAdminClass')
  120. ->willReturn($section);
  121. $this->manager->registerSetting('admin', 'myAdminClass');
  122. $settings = $this->manager->getAdminSettings('sharing');
  123. $this->assertEquals([
  124. 13 => [$section]
  125. ], $settings);
  126. }
  127. public function testGetAdminSettingsAsSubAdmin() {
  128. $section = $this->createMock(ISettings::class);
  129. $section->method('getPriority')
  130. ->willReturn(13);
  131. $section->method('getSection')
  132. ->willReturn('sharing');
  133. $this->container->method('get')
  134. ->with('myAdminClass')
  135. ->willReturn($section);
  136. $this->manager->registerSetting('admin', 'myAdminClass');
  137. $settings = $this->manager->getAdminSettings('sharing', true);
  138. $this->assertEquals([], $settings);
  139. }
  140. public function testGetSubAdminSettingsAsSubAdmin() {
  141. $section = $this->createMock(ISubAdminSettings::class);
  142. $section->method('getPriority')
  143. ->willReturn(13);
  144. $section->method('getSection')
  145. ->willReturn('sharing');
  146. $this->container->expects($this->once())
  147. ->method('get')
  148. ->with('mySubAdminClass')
  149. ->willReturn($section);
  150. $this->manager->registerSetting('admin', 'mySubAdminClass');
  151. $settings = $this->manager->getAdminSettings('sharing', true);
  152. $this->assertEquals([
  153. 13 => [$section]
  154. ], $settings);
  155. }
  156. public function testGetPersonalSettings() {
  157. $section = $this->createMock(ISettings::class);
  158. $section->method('getPriority')
  159. ->willReturn(16);
  160. $section->method('getSection')
  161. ->willReturn('security');
  162. $section2 = $this->createMock(ISettings::class);
  163. $section2->method('getPriority')
  164. ->willReturn(100);
  165. $section2->method('getSection')
  166. ->willReturn('security');
  167. $this->manager->registerSetting('personal', 'section1');
  168. $this->manager->registerSetting('personal', 'section2');
  169. $this->container->expects($this->exactly(2))
  170. ->method('get')
  171. ->withConsecutive(
  172. ['section1'],
  173. ['section2']
  174. )
  175. ->willReturnMap([
  176. ['section1', $section],
  177. ['section2', $section2],
  178. ]);
  179. $settings = $this->manager->getPersonalSettings('security');
  180. $this->assertEquals([
  181. 16 => [$section],
  182. 100 => [$section2],
  183. ], $settings);
  184. }
  185. public function testSameSectionAsPersonalAndAdmin() {
  186. $this->l10nFactory
  187. ->expects($this->once())
  188. ->method('get')
  189. ->with('lib')
  190. ->willReturn($this->l10n);
  191. $this->l10n
  192. ->expects($this->any())
  193. ->method('t')
  194. ->willReturnArgument(0);
  195. $this->manager->registerSection('personal', \OCA\WorkflowEngine\Settings\Section::class);
  196. $this->manager->registerSection('admin', \OCA\WorkflowEngine\Settings\Section::class);
  197. $section = \OC::$server->query(\OCA\WorkflowEngine\Settings\Section::class);
  198. $this->container->method('get')
  199. ->with(\OCA\WorkflowEngine\Settings\Section::class)
  200. ->willReturn($section);
  201. $this->assertEquals([
  202. 55 => [$section],
  203. ], $this->manager->getPersonalSections());
  204. $this->assertEquals([
  205. 55 => [$section],
  206. ], $this->manager->getAdminSections());
  207. }
  208. }