ManagerTest.php 6.1 KB

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