ManagerTest.php 7.7 KB

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