ManagerTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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\Settings\Admin\Sharing;
  25. use OC\Settings\Manager;
  26. use OC\Settings\Mapper;
  27. use OC\Settings\Section;
  28. use OCP\Encryption\IManager;
  29. use OCP\IConfig;
  30. use OCP\IDBConnection;
  31. use OCP\IL10N;
  32. use OCP\ILogger;
  33. use OCP\IRequest;
  34. use OCP\IURLGenerator;
  35. use OCP\IUserManager;
  36. use OCP\Lock\ILockingProvider;
  37. use Test\TestCase;
  38. class ManagerTest extends TestCase {
  39. /** @var Manager|\PHPUnit_Framework_MockObject_MockObject */
  40. private $manager;
  41. /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
  42. private $logger;
  43. /** @var IDBConnection|\PHPUnit_Framework_MockObject_MockObject */
  44. private $dbConnection;
  45. /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
  46. private $l10n;
  47. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  48. private $config;
  49. /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
  50. private $encryptionManager;
  51. /** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
  52. private $userManager;
  53. /** @var ILockingProvider|\PHPUnit_Framework_MockObject_MockObject */
  54. private $lockingProvider;
  55. /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
  56. private $request;
  57. /** @var Mapper|\PHPUnit_Framework_MockObject_MockObject */
  58. private $mapper;
  59. /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
  60. private $url;
  61. public function setUp() {
  62. parent::setUp();
  63. $this->logger = $this->createMock(ILogger::class);
  64. $this->dbConnection = $this->createMock(IDBConnection::class);
  65. $this->l10n = $this->createMock(IL10N::class);
  66. $this->config = $this->createMock(IConfig::class);
  67. $this->encryptionManager = $this->createMock(IManager::class);
  68. $this->userManager = $this->createMock(IUserManager::class);
  69. $this->lockingProvider = $this->createMock(ILockingProvider::class);
  70. $this->request = $this->createMock(IRequest::class);
  71. $this->mapper = $this->createMock(Mapper::class);
  72. $this->url = $this->createMock(IURLGenerator::class);
  73. $this->manager = new Manager(
  74. $this->logger,
  75. $this->dbConnection,
  76. $this->l10n,
  77. $this->config,
  78. $this->encryptionManager,
  79. $this->userManager,
  80. $this->lockingProvider,
  81. $this->request,
  82. $this->mapper,
  83. $this->url
  84. );
  85. }
  86. public function testSetupSettingsUpdate() {
  87. $this->mapper->expects($this->any())
  88. ->method('has')
  89. ->with('admin_settings', 'OCA\Files\Settings\Admin')
  90. ->will($this->returnValue(true));
  91. $this->mapper->expects($this->once())
  92. ->method('update')
  93. ->with('admin_settings',
  94. 'class',
  95. 'OCA\Files\Settings\Admin', [
  96. 'section' => 'additional',
  97. 'priority' => 5
  98. ]);
  99. $this->mapper->expects($this->never())
  100. ->method('add');
  101. $this->manager->setupSettings([
  102. 'admin' => 'OCA\Files\Settings\Admin',
  103. ]);
  104. }
  105. public function testSetupSettingsAdd() {
  106. $this->mapper->expects($this->any())
  107. ->method('has')
  108. ->with('admin_settings', 'OCA\Files\Settings\Admin')
  109. ->will($this->returnValue(false));
  110. $this->mapper->expects($this->once())
  111. ->method('add')
  112. ->with('admin_settings', [
  113. 'class' => 'OCA\Files\Settings\Admin',
  114. 'section' => 'additional',
  115. 'priority' => 5
  116. ]);
  117. $this->mapper->expects($this->never())
  118. ->method('update');
  119. $this->manager->setupSettings([
  120. 'admin' => 'OCA\Files\Settings\Admin',
  121. ]);
  122. }
  123. public function testGetAdminSections() {
  124. $this->l10n
  125. ->expects($this->any())
  126. ->method('t')
  127. ->will($this->returnArgument(0));
  128. $this->mapper->expects($this->once())
  129. ->method('getAdminSectionsFromDB')
  130. ->will($this->returnValue([
  131. ['class' => \OCA\WorkflowEngine\Settings\Section::class, 'priority' => 90]
  132. ]));
  133. $this->url->expects($this->exactly(5))
  134. ->method('imagePath')
  135. ->willReturnMap([
  136. ['settings', 'admin.svg', '1'],
  137. ['core', 'actions/share.svg', '2'],
  138. ['core', 'actions/password.svg', '3'],
  139. ['core', 'actions/settings-dark.svg', '4'],
  140. ['settings', 'help.svg', '5'],
  141. ]);
  142. $this->assertEquals([
  143. 0 => [new Section('server', 'Server settings', 0, '1')],
  144. 5 => [new Section('sharing', 'Sharing', 0, '2')],
  145. 45 => [new Section('encryption', 'Encryption', 0, '3')],
  146. 90 => [\OC::$server->query(\OCA\WorkflowEngine\Settings\Section::class)],
  147. 98 => [new Section('additional', 'Additional settings', 0, '4')],
  148. 99 => [new Section('tips-tricks', 'Tips & tricks', 0, '5')],
  149. ], $this->manager->getAdminSections());
  150. }
  151. public function testGetAdminSectionsEmptySection() {
  152. $this->l10n
  153. ->expects($this->any())
  154. ->method('t')
  155. ->will($this->returnArgument(0));
  156. $this->mapper->expects($this->once())
  157. ->method('getAdminSectionsFromDB')
  158. ->will($this->returnValue([
  159. ]));
  160. $this->url->expects($this->exactly(5))
  161. ->method('imagePath')
  162. ->willReturnMap([
  163. ['settings', 'admin.svg', '1'],
  164. ['core', 'actions/share.svg', '2'],
  165. ['core', 'actions/password.svg', '3'],
  166. ['core', 'actions/settings-dark.svg', '4'],
  167. ['settings', 'help.svg', '5'],
  168. ]);
  169. $this->assertEquals([
  170. 0 => [new Section('server', 'Server settings', 0, '1')],
  171. 5 => [new Section('sharing', 'Sharing', 0, '2')],
  172. 45 => [new Section('encryption', 'Encryption', 0, '3')],
  173. 98 => [new Section('additional', 'Additional settings', 0, '4')],
  174. 99 => [new Section('tips-tricks', 'Tips & tricks', 0, '5')],
  175. ], $this->manager->getAdminSections());
  176. }
  177. public function testGetAdminSettings() {
  178. $this->mapper->expects($this->any())
  179. ->method('getAdminSettingsFromDB')
  180. ->will($this->returnValue([]));
  181. $this->assertEquals([
  182. 0 => [new Sharing($this->config)],
  183. ], $this->manager->getAdminSettings('sharing'));
  184. }
  185. }