ManagerTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 Psr\Log\LoggerInterface;
  36. use Test\TestCase;
  37. class ManagerTest extends TestCase {
  38. /** @var Manager|\PHPUnit\Framework\MockObject\MockObject */
  39. private $manager;
  40. /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  41. private $logger;
  42. /** @var IDBConnection|\PHPUnit\Framework\MockObject\MockObject */
  43. private $l10n;
  44. /** @var IFactory|\PHPUnit\Framework\MockObject\MockObject */
  45. private $l10nFactory;
  46. /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
  47. private $url;
  48. /** @var IServerContainer|\PHPUnit\Framework\MockObject\MockObject */
  49. private $container;
  50. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
  51. private $groupManager;
  52. /** @var ISubAdmin|\PHPUnit\Framework\MockObject\MockObject */
  53. private $subAdmin;
  54. protected function setUp(): void {
  55. parent::setUp();
  56. $this->logger = $this->createMock(LoggerInterface::class);
  57. $this->l10n = $this->createMock(IL10N::class);
  58. $this->l10nFactory = $this->createMock(IFactory::class);
  59. $this->url = $this->createMock(IURLGenerator::class);
  60. $this->container = $this->createMock(IServerContainer::class);
  61. $this->mapper = $this->createMock(AuthorizedGroupMapper::class);
  62. $this->groupManager = $this->createMock(IGroupManager::class);
  63. $this->subAdmin = $this->createMock(ISubAdmin::class);
  64. $this->manager = new Manager(
  65. $this->logger,
  66. $this->l10nFactory,
  67. $this->url,
  68. $this->container,
  69. $this->mapper,
  70. $this->groupManager,
  71. $this->subAdmin,
  72. );
  73. }
  74. public function testGetAdminSections() {
  75. $this->manager->registerSection('admin', \OCA\WorkflowEngine\Settings\Section::class);
  76. $section = \OC::$server->query(\OCA\WorkflowEngine\Settings\Section::class);
  77. $this->container->method('get')
  78. ->with(\OCA\WorkflowEngine\Settings\Section::class)
  79. ->willReturn($section);
  80. $this->assertEquals([
  81. 55 => [$section],
  82. ], $this->manager->getAdminSections());
  83. }
  84. public function testGetPersonalSections() {
  85. $this->manager->registerSection('personal', \OCA\WorkflowEngine\Settings\Section::class);
  86. $section = \OC::$server->query(\OCA\WorkflowEngine\Settings\Section::class);
  87. $this->container->method('get')
  88. ->with(\OCA\WorkflowEngine\Settings\Section::class)
  89. ->willReturn($section);
  90. $this->assertEquals([
  91. 55 => [$section],
  92. ], $this->manager->getPersonalSections());
  93. }
  94. public function testGetAdminSectionsEmptySection() {
  95. $this->assertEquals([], $this->manager->getAdminSections());
  96. }
  97. public function testGetPersonalSectionsEmptySection() {
  98. $this->l10nFactory
  99. ->expects($this->once())
  100. ->method('get')
  101. ->with('lib')
  102. ->willReturn($this->l10n);
  103. $this->l10n
  104. ->expects($this->any())
  105. ->method('t')
  106. ->willReturnArgument(0);
  107. $this->assertEquals([], $this->manager->getPersonalSections());
  108. }
  109. public function testGetAdminSettings() {
  110. $section = $this->createMock(ISettings::class);
  111. $section->method('getPriority')
  112. ->willReturn(13);
  113. $section->method('getSection')
  114. ->willReturn('sharing');
  115. $this->container->method('get')
  116. ->with('myAdminClass')
  117. ->willReturn($section);
  118. $this->manager->registerSetting('admin', 'myAdminClass');
  119. $settings = $this->manager->getAdminSettings('sharing');
  120. $this->assertEquals([
  121. 13 => [$section]
  122. ], $settings);
  123. }
  124. public function testGetAdminSettingsAsSubAdmin() {
  125. $section = $this->createMock(ISettings::class);
  126. $section->method('getPriority')
  127. ->willReturn(13);
  128. $section->method('getSection')
  129. ->willReturn('sharing');
  130. $this->container->method('get')
  131. ->with('myAdminClass')
  132. ->willReturn($section);
  133. $this->manager->registerSetting('admin', 'myAdminClass');
  134. $settings = $this->manager->getAdminSettings('sharing', true);
  135. $this->assertEquals([], $settings);
  136. }
  137. public function testGetSubAdminSettingsAsSubAdmin() {
  138. $section = $this->createMock(ISubAdminSettings::class);
  139. $section->method('getPriority')
  140. ->willReturn(13);
  141. $section->method('getSection')
  142. ->willReturn('sharing');
  143. $this->container->expects($this->once())
  144. ->method('get')
  145. ->with('mySubAdminClass')
  146. ->willReturn($section);
  147. $this->manager->registerSetting('admin', 'mySubAdminClass');
  148. $settings = $this->manager->getAdminSettings('sharing', true);
  149. $this->assertEquals([
  150. 13 => [$section]
  151. ], $settings);
  152. }
  153. public function testGetPersonalSettings() {
  154. $section = $this->createMock(ISettings::class);
  155. $section->method('getPriority')
  156. ->willReturn(16);
  157. $section->method('getSection')
  158. ->willReturn('security');
  159. $section2 = $this->createMock(ISettings::class);
  160. $section2->method('getPriority')
  161. ->willReturn(100);
  162. $section2->method('getSection')
  163. ->willReturn('security');
  164. $this->manager->registerSetting('personal', 'section1');
  165. $this->manager->registerSetting('personal', 'section2');
  166. $this->container->expects($this->exactly(2))
  167. ->method('get')
  168. ->withConsecutive(
  169. ['section1'],
  170. ['section2']
  171. )
  172. ->willReturnMap([
  173. ['section1', $section],
  174. ['section2', $section2],
  175. ]);
  176. $settings = $this->manager->getPersonalSettings('security');
  177. $this->assertEquals([
  178. 16 => [$section],
  179. 100 => [$section2],
  180. ], $settings);
  181. }
  182. public function testSameSectionAsPersonalAndAdmin() {
  183. $this->l10nFactory
  184. ->expects($this->once())
  185. ->method('get')
  186. ->with('lib')
  187. ->willReturn($this->l10n);
  188. $this->l10n
  189. ->expects($this->any())
  190. ->method('t')
  191. ->willReturnArgument(0);
  192. $this->manager->registerSection('personal', \OCA\WorkflowEngine\Settings\Section::class);
  193. $this->manager->registerSection('admin', \OCA\WorkflowEngine\Settings\Section::class);
  194. $section = \OC::$server->query(\OCA\WorkflowEngine\Settings\Section::class);
  195. $this->container->method('get')
  196. ->with(\OCA\WorkflowEngine\Settings\Section::class)
  197. ->willReturn($section);
  198. $this->assertEquals([
  199. 55 => [$section],
  200. ], $this->manager->getPersonalSections());
  201. $this->assertEquals([
  202. 55 => [$section],
  203. ], $this->manager->getAdminSections());
  204. }
  205. }