SharingTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\Settings\Tests\Settings\Admin;
  7. use OCA\Settings\Settings\Admin\Sharing;
  8. use OCP\App\IAppManager;
  9. use OCP\AppFramework\Http\TemplateResponse;
  10. use OCP\AppFramework\Services\IInitialState;
  11. use OCP\Constants;
  12. use OCP\IConfig;
  13. use OCP\IL10N;
  14. use OCP\IURLGenerator;
  15. use OCP\Share\IManager;
  16. use PHPUnit\Framework\MockObject\MockObject;
  17. use Test\TestCase;
  18. class SharingTest extends TestCase {
  19. /** @var Sharing */
  20. private $admin;
  21. /** @var IConfig&MockObject */
  22. private $config;
  23. /** @var IL10N&MockObject */
  24. private $l10n;
  25. /** @var IManager|MockObject */
  26. private $shareManager;
  27. /** @var IAppManager|MockObject */
  28. private $appManager;
  29. /** @var IURLGenerator|MockObject */
  30. private $urlGenerator;
  31. /** @var IInitialState|MockObject */
  32. private $initialState;
  33. protected function setUp(): void {
  34. parent::setUp();
  35. $this->config = $this->getMockBuilder(IConfig::class)->getMock();
  36. $this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
  37. /** @var IManager|MockObject */
  38. $this->shareManager = $this->getMockBuilder(IManager::class)->getMock();
  39. /** @var IAppManager|MockObject */
  40. $this->appManager = $this->getMockBuilder(IAppManager::class)->getMock();
  41. /** @var IURLGenerator|MockObject */
  42. $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
  43. /** @var IInitialState|MockObject */
  44. $this->initialState = $this->getMockBuilder(IInitialState::class)->getMock();
  45. $this->admin = new Sharing(
  46. $this->config,
  47. $this->l10n,
  48. $this->shareManager,
  49. $this->appManager,
  50. $this->urlGenerator,
  51. $this->initialState,
  52. 'settings',
  53. );
  54. }
  55. public function testGetFormWithoutExcludedGroups(): void {
  56. $this->config
  57. ->method('getAppValue')
  58. ->willReturnMap([
  59. ['core', 'shareapi_exclude_groups_list', '', ''],
  60. ['core', 'shareapi_allow_links_exclude_groups', '', ''],
  61. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  62. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  63. ['core', 'shareapi_allow_public_upload', 'yes', 'yes'],
  64. ['core', 'shareapi_allow_resharing', 'yes', 'yes'],
  65. ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', 'yes'],
  66. ['core', 'shareapi_restrict_user_enumeration_to_group', 'no', 'no'],
  67. ['core', 'shareapi_restrict_user_enumeration_to_phone', 'no', 'no'],
  68. ['core', 'shareapi_restrict_user_enumeration_full_match', 'yes', 'yes'],
  69. ['core', 'shareapi_restrict_user_enumeration_full_match_userid', 'yes', 'yes'],
  70. ['core', 'shareapi_restrict_user_enumeration_full_match_email', 'yes', 'yes'],
  71. ['core', 'shareapi_restrict_user_enumeration_full_match_ignore_second_dn', 'no', 'no'],
  72. ['core', 'shareapi_enabled', 'yes', 'yes'],
  73. ['core', 'shareapi_default_expire_date', 'no', 'no'],
  74. ['core', 'shareapi_expire_after_n_days', '7', '7'],
  75. ['core', 'shareapi_enforce_expire_date', 'no', 'no'],
  76. ['core', 'shareapi_exclude_groups', 'no', 'no'],
  77. ['core', 'shareapi_public_link_disclaimertext', '', 'Lorem ipsum'],
  78. ['core', 'shareapi_enable_link_password_by_default', 'no', 'yes'],
  79. ['core', 'shareapi_default_permissions', (string)Constants::PERMISSION_ALL, Constants::PERMISSION_ALL],
  80. ['core', 'shareapi_default_internal_expire_date', 'no', 'no'],
  81. ['core', 'shareapi_internal_expire_after_n_days', '7', '7'],
  82. ['core', 'shareapi_enforce_internal_expire_date', 'no', 'no'],
  83. ['core', 'shareapi_default_remote_expire_date', 'no', 'no'],
  84. ['core', 'shareapi_remote_expire_after_n_days', '7', '7'],
  85. ['core', 'shareapi_enforce_remote_expire_date', 'no', 'no'],
  86. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  87. ['core', 'shareapi_only_share_with_group_members_exclude_group_list', '', '[]'],
  88. ]);
  89. $this->shareManager->method('shareWithGroupMembersOnly')
  90. ->willReturn(false);
  91. $this->appManager->method('isEnabledForUser')->with('files_sharing')->willReturn(false);
  92. $initialStateCalls = [];
  93. $this->initialState
  94. ->expects($this->exactly(3))
  95. ->method('provideInitialState')
  96. ->willReturnCallback(function (string $key) use (&$initialStateCalls) {
  97. $initialStateCalls[$key] = func_get_args();
  98. });
  99. $expectedInitialStateCalls = [
  100. 'sharingAppEnabled' => false,
  101. 'sharingDocumentation' => '',
  102. 'sharingSettings' => [
  103. 'allowGroupSharing' => true,
  104. 'allowLinks' => true,
  105. 'allowPublicUpload' => true,
  106. 'allowResharing' => true,
  107. 'allowShareDialogUserEnumeration' => true,
  108. 'restrictUserEnumerationToGroup' => false,
  109. 'restrictUserEnumerationToPhone' => false,
  110. 'restrictUserEnumerationFullMatch' => true,
  111. 'restrictUserEnumerationFullMatchUserId' => true,
  112. 'restrictUserEnumerationFullMatchEmail' => true,
  113. 'restrictUserEnumerationFullMatchIgnoreSecondDN' => false,
  114. 'enforceLinksPassword' => false,
  115. 'onlyShareWithGroupMembers' => false,
  116. 'enabled' => true,
  117. 'defaultExpireDate' => false,
  118. 'expireAfterNDays' => '7',
  119. 'enforceExpireDate' => false,
  120. 'excludeGroups' => 'no',
  121. 'excludeGroupsList' => [],
  122. 'publicShareDisclaimerText' => 'Lorem ipsum',
  123. 'enableLinkPasswordByDefault' => true,
  124. 'defaultPermissions' => Constants::PERMISSION_ALL,
  125. 'defaultInternalExpireDate' => false,
  126. 'internalExpireAfterNDays' => '7',
  127. 'enforceInternalExpireDate' => false,
  128. 'defaultRemoteExpireDate' => false,
  129. 'remoteExpireAfterNDays' => '7',
  130. 'enforceRemoteExpireDate' => false,
  131. 'allowLinksExcludeGroups' => [],
  132. 'onlyShareWithGroupMembersExcludeGroupList' => [],
  133. 'enforceLinksPasswordExcludedGroups' => [],
  134. 'enforceLinksPasswordExcludedGroupsEnabled' => false,
  135. ]
  136. ];
  137. $expected = new TemplateResponse(
  138. 'settings',
  139. 'settings/admin/sharing',
  140. [],
  141. ''
  142. );
  143. $this->assertEquals($expected, $this->admin->getForm());
  144. $this->assertEquals(sort($expectedInitialStateCalls), sort($initialStateCalls), 'Provided initial state does not match');
  145. }
  146. public function testGetFormWithExcludedGroups(): void {
  147. $this->config
  148. ->method('getAppValue')
  149. ->willReturnMap([
  150. ['core', 'shareapi_exclude_groups_list', '', '["NoSharers","OtherNoSharers"]'],
  151. ['core', 'shareapi_allow_links_exclude_groups', '', ''],
  152. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  153. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  154. ['core', 'shareapi_allow_public_upload', 'yes', 'yes'],
  155. ['core', 'shareapi_allow_resharing', 'yes', 'yes'],
  156. ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', 'yes'],
  157. ['core', 'shareapi_restrict_user_enumeration_to_group', 'no', 'no'],
  158. ['core', 'shareapi_restrict_user_enumeration_to_phone', 'no', 'no'],
  159. ['core', 'shareapi_restrict_user_enumeration_full_match', 'yes', 'yes'],
  160. ['core', 'shareapi_restrict_user_enumeration_full_match_userid', 'yes', 'yes'],
  161. ['core', 'shareapi_restrict_user_enumeration_full_match_email', 'yes', 'yes'],
  162. ['core', 'shareapi_restrict_user_enumeration_full_match_ignore_second_dn', 'no', 'no'],
  163. ['core', 'shareapi_enabled', 'yes', 'yes'],
  164. ['core', 'shareapi_default_expire_date', 'no', 'no'],
  165. ['core', 'shareapi_expire_after_n_days', '7', '7'],
  166. ['core', 'shareapi_enforce_expire_date', 'no', 'no'],
  167. ['core', 'shareapi_exclude_groups', 'no', 'yes'],
  168. ['core', 'shareapi_public_link_disclaimertext', '', 'Lorem ipsum'],
  169. ['core', 'shareapi_enable_link_password_by_default', 'no', 'yes'],
  170. ['core', 'shareapi_default_permissions', (string)Constants::PERMISSION_ALL, Constants::PERMISSION_ALL],
  171. ['core', 'shareapi_default_internal_expire_date', 'no', 'no'],
  172. ['core', 'shareapi_internal_expire_after_n_days', '7', '7'],
  173. ['core', 'shareapi_enforce_internal_expire_date', 'no', 'no'],
  174. ['core', 'shareapi_default_remote_expire_date', 'no', 'no'],
  175. ['core', 'shareapi_remote_expire_after_n_days', '7', '7'],
  176. ['core', 'shareapi_enforce_remote_expire_date', 'no', 'no'],
  177. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  178. ['core', 'shareapi_only_share_with_group_members_exclude_group_list', '', '[]'],
  179. ]);
  180. $this->shareManager->method('shareWithGroupMembersOnly')
  181. ->willReturn(false);
  182. $this->appManager->method('isEnabledForUser')->with('files_sharing')->willReturn(true);
  183. $initialStateCalls = [];
  184. $this->initialState
  185. ->expects($this->exactly(3))
  186. ->method('provideInitialState')
  187. ->willReturnCallback(function (string $key) use (&$initialStateCalls) {
  188. $initialStateCalls[$key] = func_get_args();
  189. });
  190. $expectedInitialStateCalls = [
  191. 'sharingAppEnabled' => true,
  192. 'sharingDocumentation' => '',
  193. 'sharingSettings' => [
  194. 'allowGroupSharing' => true,
  195. 'allowLinks' => true,
  196. 'allowPublicUpload' => true,
  197. 'allowResharing' => true,
  198. 'allowShareDialogUserEnumeration' => true,
  199. 'restrictUserEnumerationToGroup' => false,
  200. 'restrictUserEnumerationToPhone' => false,
  201. 'restrictUserEnumerationFullMatch' => true,
  202. 'restrictUserEnumerationFullMatchUserId' => true,
  203. 'restrictUserEnumerationFullMatchEmail' => true,
  204. 'restrictUserEnumerationFullMatchIgnoreSecondDN' => false,
  205. 'enforceLinksPassword' => false,
  206. 'onlyShareWithGroupMembers' => false,
  207. 'enabled' => true,
  208. 'defaultExpireDate' => false,
  209. 'expireAfterNDays' => '7',
  210. 'enforceExpireDate' => false,
  211. 'excludeGroups' => 'yes',
  212. 'excludeGroupsList' => ['NoSharers','OtherNoSharers'],
  213. 'publicShareDisclaimerText' => 'Lorem ipsum',
  214. 'enableLinkPasswordByDefault' => true,
  215. 'defaultPermissions' => Constants::PERMISSION_ALL,
  216. 'defaultInternalExpireDate' => false,
  217. 'internalExpireAfterNDays' => '7',
  218. 'enforceInternalExpireDate' => false,
  219. 'defaultRemoteExpireDate' => false,
  220. 'remoteExpireAfterNDays' => '7',
  221. 'enforceRemoteExpireDate' => false,
  222. 'allowLinksExcludeGroups' => [],
  223. 'onlyShareWithGroupMembersExcludeGroupList' => [],
  224. 'enforceLinksPasswordExcludedGroups' => [],
  225. 'enforceLinksPasswordExcludedGroupsEnabled' => false,
  226. ],
  227. ];
  228. $expected = new TemplateResponse(
  229. 'settings',
  230. 'settings/admin/sharing',
  231. [],
  232. ''
  233. );
  234. $this->assertEquals($expected, $this->admin->getForm());
  235. $this->assertEquals(sort($expectedInitialStateCalls), sort($initialStateCalls), 'Provided initial state does not match');
  236. }
  237. public function testGetSection(): void {
  238. $this->assertSame('sharing', $this->admin->getSection());
  239. }
  240. public function testGetPriority(): void {
  241. $this->assertSame(0, $this->admin->getPriority());
  242. }
  243. }