SharingTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author Vincent Petry <vincent@nextcloud.com>
  15. *
  16. * @license GNU AGPL version 3 or any later version
  17. *
  18. * This program is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License as
  20. * published by the Free Software Foundation, either version 3 of the
  21. * License, or (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30. *
  31. */
  32. namespace OCA\Settings\Tests\Settings\Admin;
  33. use OCA\Settings\Settings\Admin\Sharing;
  34. use OCP\App\IAppManager;
  35. use OCP\AppFramework\Http\TemplateResponse;
  36. use OCP\AppFramework\Services\IInitialState;
  37. use OCP\Constants;
  38. use OCP\IConfig;
  39. use OCP\IL10N;
  40. use OCP\IURLGenerator;
  41. use OCP\Share\IManager;
  42. use PHPUnit\Framework\MockObject\MockObject;
  43. use Test\TestCase;
  44. class SharingTest extends TestCase {
  45. /** @var Sharing */
  46. private $admin;
  47. /** @var IConfig */
  48. private $config;
  49. /** @var IL10N|MockObject */
  50. private $l10n;
  51. /** @var IManager|MockObject */
  52. private $shareManager;
  53. /** @var IAppManager|MockObject */
  54. private $appManager;
  55. /** @var IURLGenerator|MockObject */
  56. private $urlGenerator;
  57. /** @var IInitialState|MockObject */
  58. private $initialState;
  59. protected function setUp(): void {
  60. parent::setUp();
  61. /** @var IConfig|MockObject */
  62. $this->config = $this->getMockBuilder(IConfig::class)->getMock();
  63. /** @var IL10N|MockObject */
  64. $this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
  65. /** @var IManager|MockObject */
  66. $this->shareManager = $this->getMockBuilder(IManager::class)->getMock();
  67. /** @var IAppManager|MockObject */
  68. $this->appManager = $this->getMockBuilder(IAppManager::class)->getMock();
  69. /** @var IURLGenerator|MockObject */
  70. $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
  71. /** @var IInitialState|MockObject */
  72. $this->initialState = $this->getMockBuilder(IInitialState::class)->getMock();
  73. $this->admin = new Sharing(
  74. $this->config,
  75. $this->l10n,
  76. $this->shareManager,
  77. $this->appManager,
  78. $this->urlGenerator,
  79. $this->initialState,
  80. "settings",
  81. );
  82. }
  83. public function testGetFormWithoutExcludedGroups(): void {
  84. $this->config
  85. ->method('getAppValue')
  86. ->willReturnMap([
  87. ['core', 'shareapi_exclude_groups_list', '', ''],
  88. ['core', 'shareapi_allow_links_exclude_groups', '', ''],
  89. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  90. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  91. ['core', 'shareapi_allow_public_upload', 'yes', 'yes'],
  92. ['core', 'shareapi_allow_resharing', 'yes', 'yes'],
  93. ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', 'yes'],
  94. ['core', 'shareapi_restrict_user_enumeration_to_group', 'no', 'no'],
  95. ['core', 'shareapi_restrict_user_enumeration_to_phone', 'no', 'no'],
  96. ['core', 'shareapi_restrict_user_enumeration_full_match', 'yes', 'yes'],
  97. ['core', 'shareapi_restrict_user_enumeration_full_match_userid', 'yes', 'yes'],
  98. ['core', 'shareapi_restrict_user_enumeration_full_match_email', 'yes', 'yes'],
  99. ['core', 'shareapi_restrict_user_enumeration_full_match_ignore_second_dn', 'no', 'no'],
  100. ['core', 'shareapi_enabled', 'yes', 'yes'],
  101. ['core', 'shareapi_default_expire_date', 'no', 'no'],
  102. ['core', 'shareapi_expire_after_n_days', '7', '7'],
  103. ['core', 'shareapi_enforce_expire_date', 'no', 'no'],
  104. ['core', 'shareapi_exclude_groups', 'no', 'no'],
  105. ['core', 'shareapi_public_link_disclaimertext', null, 'Lorem ipsum'],
  106. ['core', 'shareapi_enable_link_password_by_default', 'no', 'yes'],
  107. ['core', 'shareapi_default_permissions', (string)Constants::PERMISSION_ALL, Constants::PERMISSION_ALL],
  108. ['core', 'shareapi_default_internal_expire_date', 'no', 'no'],
  109. ['core', 'shareapi_internal_expire_after_n_days', '7', '7'],
  110. ['core', 'shareapi_enforce_internal_expire_date', 'no', 'no'],
  111. ['core', 'shareapi_default_remote_expire_date', 'no', 'no'],
  112. ['core', 'shareapi_remote_expire_after_n_days', '7', '7'],
  113. ['core', 'shareapi_enforce_remote_expire_date', 'no', 'no'],
  114. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  115. ['core', 'shareapi_only_share_with_group_members_exclude_group_list', '', '[]'],
  116. ]);
  117. $this->shareManager->method('shareWithGroupMembersOnly')
  118. ->willReturn(false);
  119. $this->appManager->method('isEnabledForUser')->with('files_sharing')->willReturn(false);
  120. $this->initialState
  121. ->expects($this->exactly(3))
  122. ->method('provideInitialState')
  123. ->withConsecutive(
  124. ['sharingAppEnabled', false],
  125. ['sharingDocumentation', ''],
  126. [
  127. 'sharingSettings',
  128. [
  129. 'allowGroupSharing' => true,
  130. 'allowLinks' => true,
  131. 'allowPublicUpload' => true,
  132. 'allowResharing' => true,
  133. 'allowShareDialogUserEnumeration' => true,
  134. 'restrictUserEnumerationToGroup' => false,
  135. 'restrictUserEnumerationToPhone' => false,
  136. 'restrictUserEnumerationFullMatch' => true,
  137. 'restrictUserEnumerationFullMatchUserId' => true,
  138. 'restrictUserEnumerationFullMatchEmail' => true,
  139. 'restrictUserEnumerationFullMatchIgnoreSecondDN' => false,
  140. 'enforceLinksPassword' => false,
  141. 'onlyShareWithGroupMembers' => false,
  142. 'enabled' => true,
  143. 'defaultExpireDate' => false,
  144. 'expireAfterNDays' => '7',
  145. 'enforceExpireDate' => false,
  146. 'excludeGroups' => false,
  147. 'excludeGroupsList' => [],
  148. 'publicShareDisclaimerText' => 'Lorem ipsum',
  149. 'enableLinkPasswordByDefault' => true,
  150. 'defaultPermissions' => Constants::PERMISSION_ALL,
  151. 'defaultInternalExpireDate' => false,
  152. 'internalExpireAfterNDays' => '7',
  153. 'enforceInternalExpireDate' => false,
  154. 'defaultRemoteExpireDate' => false,
  155. 'remoteExpireAfterNDays' => '7',
  156. 'enforceRemoteExpireDate' => false,
  157. 'allowLinksExcludeGroups' => [],
  158. 'passwordExcludedGroups' => [],
  159. 'passwordExcludedGroupsFeatureEnabled' => false,
  160. 'onlyShareWithGroupMembersExcludeGroupList' => [],
  161. ]
  162. ],
  163. );
  164. $expected = new TemplateResponse(
  165. 'settings',
  166. 'settings/admin/sharing',
  167. [],
  168. ''
  169. );
  170. $this->assertEquals($expected, $this->admin->getForm());
  171. }
  172. public function testGetFormWithExcludedGroups(): void {
  173. $this->config
  174. ->method('getAppValue')
  175. ->willReturnMap([
  176. ['core', 'shareapi_exclude_groups_list', '', '["NoSharers","OtherNoSharers"]'],
  177. ['core', 'shareapi_allow_links_exclude_groups', '', ''],
  178. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  179. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  180. ['core', 'shareapi_allow_public_upload', 'yes', 'yes'],
  181. ['core', 'shareapi_allow_resharing', 'yes', 'yes'],
  182. ['core', 'shareapi_allow_share_dialog_user_enumeration', 'yes', 'yes'],
  183. ['core', 'shareapi_restrict_user_enumeration_to_group', 'no', 'no'],
  184. ['core', 'shareapi_restrict_user_enumeration_to_phone', 'no', 'no'],
  185. ['core', 'shareapi_restrict_user_enumeration_full_match', 'yes', 'yes'],
  186. ['core', 'shareapi_restrict_user_enumeration_full_match_userid', 'yes', 'yes'],
  187. ['core', 'shareapi_restrict_user_enumeration_full_match_email', 'yes', 'yes'],
  188. ['core', 'shareapi_restrict_user_enumeration_full_match_ignore_second_dn', 'no', 'no'],
  189. ['core', 'shareapi_enabled', 'yes', 'yes'],
  190. ['core', 'shareapi_default_expire_date', 'no', 'no'],
  191. ['core', 'shareapi_expire_after_n_days', '7', '7'],
  192. ['core', 'shareapi_enforce_expire_date', 'no', 'no'],
  193. ['core', 'shareapi_exclude_groups', 'no', 'yes'],
  194. ['core', 'shareapi_public_link_disclaimertext', null, 'Lorem ipsum'],
  195. ['core', 'shareapi_enable_link_password_by_default', 'no', 'yes'],
  196. ['core', 'shareapi_default_permissions', (string)Constants::PERMISSION_ALL, Constants::PERMISSION_ALL],
  197. ['core', 'shareapi_default_internal_expire_date', 'no', 'no'],
  198. ['core', 'shareapi_internal_expire_after_n_days', '7', '7'],
  199. ['core', 'shareapi_enforce_internal_expire_date', 'no', 'no'],
  200. ['core', 'shareapi_default_remote_expire_date', 'no', 'no'],
  201. ['core', 'shareapi_remote_expire_after_n_days', '7', '7'],
  202. ['core', 'shareapi_enforce_remote_expire_date', 'no', 'no'],
  203. ['core', 'shareapi_enforce_links_password_excluded_groups', '', ''],
  204. ['core', 'shareapi_only_share_with_group_members_exclude_group_list', '', '[]'],
  205. ]);
  206. $this->shareManager->method('shareWithGroupMembersOnly')
  207. ->willReturn(false);
  208. $this->appManager->method('isEnabledForUser')->with('files_sharing')->willReturn(true);
  209. $this->initialState
  210. ->expects($this->exactly(3))
  211. ->method('provideInitialState')
  212. ->withConsecutive(
  213. ['sharingAppEnabled', true],
  214. ['sharingDocumentation', ''],
  215. [
  216. 'sharingSettings',
  217. [
  218. 'allowGroupSharing' => true,
  219. 'allowLinks' => true,
  220. 'allowPublicUpload' => true,
  221. 'allowResharing' => true,
  222. 'allowShareDialogUserEnumeration' => true,
  223. 'restrictUserEnumerationToGroup' => false,
  224. 'restrictUserEnumerationToPhone' => false,
  225. 'restrictUserEnumerationFullMatch' => true,
  226. 'restrictUserEnumerationFullMatchUserId' => true,
  227. 'restrictUserEnumerationFullMatchEmail' => true,
  228. 'restrictUserEnumerationFullMatchIgnoreSecondDN' => false,
  229. 'enforceLinksPassword' => false,
  230. 'onlyShareWithGroupMembers' => false,
  231. 'enabled' => true,
  232. 'defaultExpireDate' => false,
  233. 'expireAfterNDays' => '7',
  234. 'enforceExpireDate' => false,
  235. 'excludeGroups' => true,
  236. 'excludeGroupsList' => ['NoSharers','OtherNoSharers'],
  237. 'publicShareDisclaimerText' => 'Lorem ipsum',
  238. 'enableLinkPasswordByDefault' => true,
  239. 'defaultPermissions' => Constants::PERMISSION_ALL,
  240. 'defaultInternalExpireDate' => false,
  241. 'internalExpireAfterNDays' => '7',
  242. 'enforceInternalExpireDate' => false,
  243. 'defaultRemoteExpireDate' => false,
  244. 'remoteExpireAfterNDays' => '7',
  245. 'enforceRemoteExpireDate' => false,
  246. 'allowLinksExcludeGroups' => [],
  247. 'passwordExcludedGroups' => [],
  248. 'passwordExcludedGroupsFeatureEnabled' => false,
  249. 'onlyShareWithGroupMembersExcludeGroupList' => [],
  250. ]
  251. ],
  252. );
  253. $expected = new TemplateResponse(
  254. 'settings',
  255. 'settings/admin/sharing',
  256. [],
  257. ''
  258. );
  259. $this->assertEquals($expected, $this->admin->getForm());
  260. }
  261. public function testGetSection(): void {
  262. $this->assertSame('sharing', $this->admin->getSection());
  263. }
  264. public function testGetPriority(): void {
  265. $this->assertSame(0, $this->admin->getPriority());
  266. }
  267. }