ManagerTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 Mapper|\PHPUnit_Framework_MockObject_MockObject */
  63. private $mapper;
  64. /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
  65. private $url;
  66. /** @var AccountManager|\PHPUnit_Framework_MockObject_MockObject */
  67. private $accountManager;
  68. /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
  69. private $groupManager;
  70. /** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */
  71. private $l10nFactory;
  72. /** @var \OC_Defaults|\PHPUnit_Framework_MockObject_MockObject */
  73. private $defaults;
  74. /** @var IAppManager */
  75. private $appManager;
  76. public function setUp() {
  77. parent::setUp();
  78. $this->logger = $this->createMock(ILogger::class);
  79. $this->dbConnection = $this->createMock(IDBConnection::class);
  80. $this->l10n = $this->createMock(IL10N::class);
  81. $this->config = $this->createMock(IConfig::class);
  82. $this->encryptionManager = $this->createMock(IManager::class);
  83. $this->userManager = $this->createMock(IUserManager::class);
  84. $this->lockingProvider = $this->createMock(ILockingProvider::class);
  85. $this->request = $this->createMock(IRequest::class);
  86. $this->mapper = $this->createMock(Mapper::class);
  87. $this->url = $this->createMock(IURLGenerator::class);
  88. $this->accountManager = $this->createMock(AccountManager::class);
  89. $this->groupManager = $this->createMock(IGroupManager::class);
  90. $this->l10nFactory = $this->createMock(IFactory::class);
  91. $this->defaults = $this->createMock(\OC_Defaults::class);
  92. $this->appManager = $this->createMock(IAppManager::class);
  93. $this->manager = new Manager(
  94. $this->logger,
  95. $this->dbConnection,
  96. $this->l10n,
  97. $this->config,
  98. $this->encryptionManager,
  99. $this->userManager,
  100. $this->lockingProvider,
  101. $this->request,
  102. $this->mapper,
  103. $this->url,
  104. $this->accountManager,
  105. $this->groupManager,
  106. $this->l10nFactory,
  107. $this->defaults,
  108. $this->appManager
  109. );
  110. }
  111. public function settingsTypeProvider() {
  112. return [
  113. ['admin', 'admin_settings'],
  114. ['personal', 'personal_settings'],
  115. ];
  116. }
  117. /**
  118. * @dataProvider settingsTypeProvider
  119. * @param string $type
  120. * @param string $table
  121. */
  122. public function testSetupSettingsUpdate($type, $table) {
  123. $className = 'OCA\Files\Settings\Admin';
  124. $this->mapper->expects($this->any())
  125. ->method('has')
  126. ->with($table, $className)
  127. ->will($this->returnValue(true));
  128. $this->mapper->expects($this->once())
  129. ->method('update')
  130. ->with($table,
  131. 'class',
  132. $className, [
  133. 'section' => 'additional',
  134. 'priority' => 5
  135. ]);
  136. $this->mapper->expects($this->never())
  137. ->method('add');
  138. $this->manager->setupSettings([
  139. $type => $className,
  140. ]);
  141. }
  142. /**
  143. * @dataProvider settingsTypeProvider
  144. * @param string $type
  145. * @param string $table
  146. */
  147. public function testSetupSettingsAdd($type, $table) {
  148. $this->mapper->expects($this->any())
  149. ->method('has')
  150. ->with($table, 'OCA\Files\Settings\Admin')
  151. ->will($this->returnValue(false));
  152. $this->mapper->expects($this->once())
  153. ->method('add')
  154. ->with($table, [
  155. 'class' => 'OCA\Files\Settings\Admin',
  156. 'section' => 'additional',
  157. 'priority' => 5
  158. ]);
  159. $this->mapper->expects($this->never())
  160. ->method('update');
  161. $this->manager->setupSettings([
  162. $type => 'OCA\Files\Settings\Admin',
  163. ]);
  164. }
  165. public function testGetAdminSections() {
  166. $this->l10n
  167. ->expects($this->any())
  168. ->method('t')
  169. ->will($this->returnArgument(0));
  170. $this->mapper->expects($this->once())
  171. ->method('getAdminSectionsFromDB')
  172. ->will($this->returnValue([
  173. ['class' => \OCA\WorkflowEngine\Settings\Section::class, 'priority' => 90]
  174. ]));
  175. $this->url->expects($this->exactly(6))
  176. ->method('imagePath')
  177. ->willReturnMap([
  178. ['settings', 'admin.svg', '1'],
  179. ['core', 'actions/share.svg', '2'],
  180. ['core', 'actions/password.svg', '3'],
  181. ['core', 'actions/settings-dark.svg', '4'],
  182. ['settings', 'help.svg', '5'],
  183. ]);
  184. $this->assertEquals([
  185. 0 => [new Section('server', 'Basic settings', 0, '1')],
  186. 5 => [new Section('sharing', 'Sharing', 0, '2')],
  187. 10 => [new Section('security', 'Security', 0, '3')],
  188. 45 => [new Section('encryption', 'Encryption', 0, '3')],
  189. 90 => [\OC::$server->query(\OCA\WorkflowEngine\Settings\Section::class)],
  190. 98 => [new Section('additional', 'Additional settings', 0, '4')],
  191. 99 => [new Section('tips-tricks', 'Tips & tricks', 0, '5')],
  192. ], $this->manager->getAdminSections());
  193. }
  194. public function testGetPersonalSections() {
  195. $this->l10n
  196. ->expects($this->any())
  197. ->method('t')
  198. ->will($this->returnArgument(0));
  199. $this->mapper->expects($this->once())
  200. ->method('getPersonalSectionsFromDB')
  201. ->will($this->returnValue([
  202. ['class' => \OCA\WorkflowEngine\Settings\Section::class, 'priority' => 90]
  203. ]));
  204. $this->url->expects($this->exactly(3))
  205. ->method('imagePath')
  206. ->willReturnMap([
  207. ['core', 'actions/info.svg', '1'],
  208. ['settings', 'password.svg', '2'],
  209. ['settings', 'change.svg', '3'],
  210. ]);
  211. $this->assertArraySubset([
  212. 0 => [new Section('personal-info', 'Personal info', 0, '1')],
  213. 5 => [new Section('security', 'Security', 0, '2')],
  214. 15 => [new Section('sync-clients', 'Sync clients', 0, '3')],
  215. 90 => [\OC::$server->query(\OCA\WorkflowEngine\Settings\Section::class)],
  216. ], $this->manager->getPersonalSections());
  217. }
  218. public function testGetAdminSectionsEmptySection() {
  219. $this->l10n
  220. ->expects($this->any())
  221. ->method('t')
  222. ->will($this->returnArgument(0));
  223. $this->mapper->expects($this->once())
  224. ->method('getAdminSectionsFromDB')
  225. ->will($this->returnValue([
  226. ]));
  227. $this->url->expects($this->exactly(6))
  228. ->method('imagePath')
  229. ->willReturnMap([
  230. ['settings', 'admin.svg', '1'],
  231. ['core', 'actions/share.svg', '2'],
  232. ['core', 'actions/password.svg', '3'],
  233. ['core', 'actions/settings-dark.svg', '4'],
  234. ['settings', 'help.svg', '5'],
  235. ]);
  236. $this->assertEquals([
  237. 0 => [new Section('server', 'Basic settings', 0, '1')],
  238. 5 => [new Section('sharing', 'Sharing', 0, '2')],
  239. 10 => [new Section('security', 'Security', 0, '3')],
  240. 45 => [new Section('encryption', 'Encryption', 0, '3')],
  241. 98 => [new Section('additional', 'Additional settings', 0, '4')],
  242. 99 => [new Section('tips-tricks', 'Tips & tricks', 0, '5')],
  243. ], $this->manager->getAdminSections());
  244. }
  245. public function testGetPersonalSectionsEmptySection() {
  246. $this->l10n
  247. ->expects($this->any())
  248. ->method('t')
  249. ->will($this->returnArgument(0));
  250. $this->mapper->expects($this->once())
  251. ->method('getPersonalSectionsFromDB')
  252. ->will($this->returnValue([]));
  253. $this->url->expects($this->exactly(3))
  254. ->method('imagePath')
  255. ->willReturnMap([
  256. ['core', 'actions/info.svg', '1'],
  257. ['settings', 'password.svg', '2'],
  258. ['settings', 'change.svg', '3'],
  259. ]);
  260. $this->assertArraySubset([
  261. 0 => [new Section('personal-info', 'Personal info', 0, '1')],
  262. 5 => [new Section('security', 'Security', 0, '2')],
  263. 15 => [new Section('sync-clients', 'Sync clients', 0, '3')],
  264. ], $this->manager->getPersonalSections());
  265. }
  266. public function testGetAdminSettings() {
  267. $this->mapper->expects($this->any())
  268. ->method('getAdminSettingsFromDB')
  269. ->will($this->returnValue([]));
  270. $this->assertEquals([
  271. 0 => [new Sharing($this->config)],
  272. ], $this->manager->getAdminSettings('sharing'));
  273. }
  274. public function testGetPersonalSettings() {
  275. $this->mapper->expects($this->any())
  276. ->method('getPersonalSettingsFromDB')
  277. ->will($this->returnValue([]));
  278. $this->assertEquals([
  279. 10 => [new Security()],
  280. ], $this->manager->getPersonalSettings('security'));
  281. }
  282. }