ManagerTest.php 6.1 KB

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