ManagerTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 Tests\Settings;
  24. use OC\Accounts\AccountManager;
  25. use OC\Settings\Admin\Sharing;
  26. use OC\Settings\Manager;
  27. use OC\Settings\Mapper;
  28. use OC\Settings\Personal\Security;
  29. use OC\Settings\Section;
  30. use OCP\App\IAppManager;
  31. use OCP\Encryption\IManager;
  32. use OCP\IConfig;
  33. use OCP\IDBConnection;
  34. use OCP\IGroupManager;
  35. use OCP\IL10N;
  36. use OCP\ILogger;
  37. use OCP\IRequest;
  38. use OCP\IURLGenerator;
  39. use OCP\IUserManager;
  40. use OCP\L10N\IFactory;
  41. use OCP\Lock\ILockingProvider;
  42. use Test\TestCase;
  43. class ManagerTest extends TestCase {
  44. /** @var Manager|\PHPUnit_Framework_MockObject_MockObject */
  45. private $manager;
  46. /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
  47. private $logger;
  48. /** @var IDBConnection|\PHPUnit_Framework_MockObject_MockObject */
  49. private $dbConnection;
  50. /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
  51. private $l10n;
  52. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  53. private $config;
  54. /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
  55. private $encryptionManager;
  56. /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
  57. private $userManager;
  58. /** @var ILockingProvider|\PHPUnit_Framework_MockObject_MockObject */
  59. private $lockingProvider;
  60. /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
  61. private $request;
  62. /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
  63. private $url;
  64. /** @var AccountManager|\PHPUnit_Framework_MockObject_MockObject */
  65. private $accountManager;
  66. /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
  67. private $groupManager;
  68. /** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */
  69. private $l10nFactory;
  70. /** @var IAppManager */
  71. private $appManager;
  72. public function setUp() {
  73. parent::setUp();
  74. $this->logger = $this->createMock(ILogger::class);
  75. $this->dbConnection = $this->createMock(IDBConnection::class);
  76. $this->l10n = $this->createMock(IL10N::class);
  77. $this->config = $this->createMock(IConfig::class);
  78. $this->encryptionManager = $this->createMock(IManager::class);
  79. $this->userManager = $this->createMock(IUserManager::class);
  80. $this->lockingProvider = $this->createMock(ILockingProvider::class);
  81. $this->request = $this->createMock(IRequest::class);
  82. $this->url = $this->createMock(IURLGenerator::class);
  83. $this->accountManager = $this->createMock(AccountManager::class);
  84. $this->groupManager = $this->createMock(IGroupManager::class);
  85. $this->l10nFactory = $this->createMock(IFactory::class);
  86. $this->appManager = $this->createMock(IAppManager::class);
  87. $this->manager = new Manager(
  88. $this->logger,
  89. $this->dbConnection,
  90. $this->l10n,
  91. $this->config,
  92. $this->encryptionManager,
  93. $this->userManager,
  94. $this->lockingProvider,
  95. $this->request,
  96. $this->url,
  97. $this->accountManager,
  98. $this->groupManager,
  99. $this->l10nFactory,
  100. $this->appManager
  101. );
  102. }
  103. public function testGetAdminSections() {
  104. $this->l10n
  105. ->expects($this->any())
  106. ->method('t')
  107. ->will($this->returnArgument(0));
  108. $this->manager->registerSection('admin', \OCA\WorkflowEngine\Settings\Section::class);
  109. $this->url->expects($this->exactly(7))
  110. ->method('imagePath')
  111. ->willReturnMap([
  112. ['settings', 'admin.svg', '0'],
  113. ['core', 'actions/settings-dark.svg', '1'],
  114. ['core', 'actions/share.svg', '2'],
  115. ['core', 'actions/password.svg', '3'],
  116. ['core', 'places/contacts.svg', '5'],
  117. ['settings', 'help.svg', '4'],
  118. ]);
  119. $this->assertEquals([
  120. 0 => [new Section('overview', 'Overview', 0, '0')],
  121. 1 => [new Section('server', 'Basic settings', 0, '1')],
  122. 5 => [new Section('sharing', 'Sharing', 0, '2')],
  123. 10 => [new Section('security', 'Security', 0, '3')],
  124. 45 => [new Section('encryption', 'Encryption', 0, '3')],
  125. 50 => [new Section('groupware', 'Groupware', 0, '5')],
  126. 55 => [\OC::$server->query(\OCA\WorkflowEngine\Settings\Section::class)],
  127. 98 => [new Section('additional', 'Additional settings', 0, '1')],
  128. ], $this->manager->getAdminSections());
  129. }
  130. public function testGetPersonalSections() {
  131. $this->l10n
  132. ->expects($this->any())
  133. ->method('t')
  134. ->will($this->returnArgument(0));
  135. $this->manager->registerSection('personal', \OCA\WorkflowEngine\Settings\Section::class);
  136. $this->url->expects($this->exactly(3))
  137. ->method('imagePath')
  138. ->willReturnMap([
  139. ['core', 'actions/info.svg', '1'],
  140. ['settings', 'password.svg', '2'],
  141. ['core', 'clients/phone.svg', '3'],
  142. ]);
  143. $this->assertEquals([
  144. 0 => [new Section('personal-info', 'Personal info', 0, '1')],
  145. 5 => [new Section('security', 'Security', 0, '2')],
  146. 15 => [new Section('sync-clients', 'Mobile & desktop', 0, '3')],
  147. 55 => [\OC::$server->query(\OCA\WorkflowEngine\Settings\Section::class)],
  148. ], $this->manager->getPersonalSections());
  149. }
  150. public function testGetAdminSectionsEmptySection() {
  151. $this->l10n
  152. ->expects($this->any())
  153. ->method('t')
  154. ->will($this->returnArgument(0));
  155. $this->url->expects($this->exactly(7))
  156. ->method('imagePath')
  157. ->willReturnMap([
  158. ['settings', 'admin.svg', '0'],
  159. ['core', 'actions/settings-dark.svg', '1'],
  160. ['core', 'actions/share.svg', '2'],
  161. ['core', 'actions/password.svg', '3'],
  162. ['core', 'places/contacts.svg', '5'],
  163. ['settings', 'help.svg', '4'],
  164. ]);
  165. $this->assertEquals([
  166. 0 => [new Section('overview', 'Overview', 0, '0')],
  167. 1 => [new Section('server', 'Basic settings', 0, '1')],
  168. 5 => [new Section('sharing', 'Sharing', 0, '2')],
  169. 10 => [new Section('security', 'Security', 0, '3')],
  170. 45 => [new Section('encryption', 'Encryption', 0, '3')],
  171. 50 => [new Section('groupware', 'Groupware', 0, '5')],
  172. 98 => [new Section('additional', 'Additional settings', 0, '1')],
  173. ], $this->manager->getAdminSections());
  174. }
  175. public function testGetPersonalSectionsEmptySection() {
  176. $this->l10n
  177. ->expects($this->any())
  178. ->method('t')
  179. ->will($this->returnArgument(0));
  180. $this->url->expects($this->exactly(3))
  181. ->method('imagePath')
  182. ->willReturnMap([
  183. ['core', 'actions/info.svg', '1'],
  184. ['settings', 'password.svg', '2'],
  185. ['core', 'clients/phone.svg', '3'],
  186. ]);
  187. $this->assertArraySubset([
  188. 0 => [new Section('personal-info', 'Personal info', 0, '1')],
  189. 5 => [new Section('security', 'Security', 0, '2')],
  190. 15 => [new Section('sync-clients', 'Mobile & desktop', 0, '3')],
  191. ], $this->manager->getPersonalSections());
  192. }
  193. public function testGetAdminSettings() {
  194. $this->assertEquals([
  195. 0 => [new Sharing($this->config, $this->l10n)],
  196. ], $this->manager->getAdminSettings('sharing'));
  197. }
  198. public function testGetPersonalSettings() {
  199. $this->assertEquals([
  200. 10 => [new Security($this->userManager)],
  201. ], $this->manager->getPersonalSettings('security'));
  202. }
  203. }