1
0

SharingTest.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 */
  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. /** @var IConfig|MockObject */
  36. $this->config = $this->getMockBuilder(IConfig::class)->getMock();
  37. /** @var IL10N|MockObject */
  38. $this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
  39. /** @var IManager|MockObject */
  40. $this->shareManager = $this->getMockBuilder(IManager::class)->getMock();
  41. /** @var IAppManager|MockObject */
  42. $this->appManager = $this->getMockBuilder(IAppManager::class)->getMock();
  43. /** @var IURLGenerator|MockObject */
  44. $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
  45. /** @var IInitialState|MockObject */
  46. $this->initialState = $this->getMockBuilder(IInitialState::class)->getMock();
  47. $this->admin = new Sharing(
  48. $this->config,
  49. $this->l10n,
  50. $this->shareManager,
  51. $this->appManager,
  52. $this->urlGenerator,
  53. $this->initialState,
  54. "settings",
  55. );
  56. }
  57. public function testGetFormWithoutExcludedGroups(): void {
  58. $this->config
  59. ->method('getAppValue')
  60. ->willReturnMap([
  61. ['core', 'shareapi_exclude_groups_list', '', ''],
  62. ['core', 'shareapi_allow_links_exclude_groups', '', ''],
  63. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  64. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  65. ['core', 'shareapi_allow_public_upload', 'yes', 'yes'],
  66. ['core', 'shareapi_allow_resharing', 'yes', 'yes'],
  67. ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', 'yes'],
  68. ['core', 'shareapi_restrict_user_enumeration_to_group', 'no', 'no'],
  69. ['core', 'shareapi_restrict_user_enumeration_to_phone', 'no', 'no'],
  70. ['core', 'shareapi_restrict_user_enumeration_full_match', 'yes', 'yes'],
  71. ['core', 'shareapi_restrict_user_enumeration_full_match_userid', 'yes', 'yes'],
  72. ['core', 'shareapi_restrict_user_enumeration_full_match_email', 'yes', 'yes'],
  73. ['core', 'shareapi_restrict_user_enumeration_full_match_ignore_second_dn', 'no', 'no'],
  74. ['core', 'shareapi_enabled', 'yes', 'yes'],
  75. ['core', 'shareapi_default_expire_date', 'no', 'no'],
  76. ['core', 'shareapi_expire_after_n_days', '7', '7'],
  77. ['core', 'shareapi_enforce_expire_date', 'no', 'no'],
  78. ['core', 'shareapi_exclude_groups', 'no', 'no'],
  79. ['core', 'shareapi_public_link_disclaimertext', null, 'Lorem ipsum'],
  80. ['core', 'shareapi_enable_link_password_by_default', 'no', 'yes'],
  81. ['core', 'shareapi_default_permissions', (string)Constants::PERMISSION_ALL, Constants::PERMISSION_ALL],
  82. ['core', 'shareapi_default_internal_expire_date', 'no', 'no'],
  83. ['core', 'shareapi_internal_expire_after_n_days', '7', '7'],
  84. ['core', 'shareapi_enforce_internal_expire_date', 'no', 'no'],
  85. ['core', 'shareapi_default_remote_expire_date', 'no', 'no'],
  86. ['core', 'shareapi_remote_expire_after_n_days', '7', '7'],
  87. ['core', 'shareapi_enforce_remote_expire_date', 'no', 'no'],
  88. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  89. ['core', 'shareapi_only_share_with_group_members_exclude_group_list', '', '[]'],
  90. ]);
  91. $this->shareManager->method('shareWithGroupMembersOnly')
  92. ->willReturn(false);
  93. $this->appManager->method('isEnabledForUser')->with('files_sharing')->willReturn(false);
  94. $this->initialState
  95. ->expects($this->exactly(3))
  96. ->method('provideInitialState')
  97. ->withConsecutive(
  98. ['sharingAppEnabled', false],
  99. ['sharingDocumentation', ''],
  100. [
  101. 'sharingSettings',
  102. [
  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. );
  138. $expected = new TemplateResponse(
  139. 'settings',
  140. 'settings/admin/sharing',
  141. [],
  142. ''
  143. );
  144. $this->assertEquals($expected, $this->admin->getForm());
  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', null, '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. $this->initialState
  184. ->expects($this->exactly(3))
  185. ->method('provideInitialState')
  186. ->withConsecutive(
  187. ['sharingAppEnabled', true],
  188. ['sharingDocumentation', ''],
  189. [
  190. 'sharingSettings',
  191. [
  192. 'allowGroupSharing' => true,
  193. 'allowLinks' => true,
  194. 'allowPublicUpload' => true,
  195. 'allowResharing' => true,
  196. 'allowShareDialogUserEnumeration' => true,
  197. 'restrictUserEnumerationToGroup' => false,
  198. 'restrictUserEnumerationToPhone' => false,
  199. 'restrictUserEnumerationFullMatch' => true,
  200. 'restrictUserEnumerationFullMatchUserId' => true,
  201. 'restrictUserEnumerationFullMatchEmail' => true,
  202. 'restrictUserEnumerationFullMatchIgnoreSecondDN' => false,
  203. 'enforceLinksPassword' => false,
  204. 'onlyShareWithGroupMembers' => false,
  205. 'enabled' => true,
  206. 'defaultExpireDate' => false,
  207. 'expireAfterNDays' => '7',
  208. 'enforceExpireDate' => false,
  209. 'excludeGroups' => 'yes',
  210. 'excludeGroupsList' => ['NoSharers','OtherNoSharers'],
  211. 'publicShareDisclaimerText' => 'Lorem ipsum',
  212. 'enableLinkPasswordByDefault' => true,
  213. 'defaultPermissions' => Constants::PERMISSION_ALL,
  214. 'defaultInternalExpireDate' => false,
  215. 'internalExpireAfterNDays' => '7',
  216. 'enforceInternalExpireDate' => false,
  217. 'defaultRemoteExpireDate' => false,
  218. 'remoteExpireAfterNDays' => '7',
  219. 'enforceRemoteExpireDate' => false,
  220. 'allowLinksExcludeGroups' => [],
  221. 'onlyShareWithGroupMembersExcludeGroupList' => [],
  222. 'enforceLinksPasswordExcludedGroups' => [],
  223. 'enforceLinksPasswordExcludedGroupsEnabled' => false,
  224. ]
  225. ],
  226. );
  227. $expected = new TemplateResponse(
  228. 'settings',
  229. 'settings/admin/sharing',
  230. [],
  231. ''
  232. );
  233. $this->assertEquals($expected, $this->admin->getForm());
  234. }
  235. public function testGetSection(): void {
  236. $this->assertSame('sharing', $this->admin->getSection());
  237. }
  238. public function testGetPriority(): void {
  239. $this->assertSame(0, $this->admin->getPriority());
  240. }
  241. }