CapabilitiesTest.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\Files_Sharing\Tests;
  27. use OCA\Files_Sharing\Capabilities;
  28. use OCP\IConfig;
  29. /**
  30. * Class CapabilitiesTest
  31. *
  32. * @group DB
  33. */
  34. class CapabilitiesTest extends \Test\TestCase {
  35. /**
  36. * Test for the general part in each return statement and assert.
  37. * Strip of the general part on the way.
  38. *
  39. * @param string[] $data Capabilities
  40. * @return string[]
  41. */
  42. private function getFilesSharingPart(array $data) {
  43. $this->assertArrayHasKey('files_sharing', $data);
  44. return $data['files_sharing'];
  45. }
  46. /**
  47. * Create a mock config object and insert the values in $map to the getAppValue
  48. * function. Then obtain the capabilities and extract the first few
  49. * levels in the array
  50. *
  51. * @param (string[])[] $map Map of arguments to return types for the getAppValue function in the mock
  52. * @return string[]
  53. */
  54. private function getResults(array $map) {
  55. $config = $this->getMockBuilder(IConfig::class)->disableOriginalConstructor()->getMock();
  56. $config->method('getAppValue')->will($this->returnValueMap($map));
  57. $cap = new Capabilities($config);
  58. $result = $this->getFilesSharingPart($cap->getCapabilities());
  59. return $result;
  60. }
  61. public function testEnabledSharingAPI() {
  62. $map = [
  63. ['core', 'shareapi_enabled', 'yes', 'yes'],
  64. ];
  65. $result = $this->getResults($map);
  66. $this->assertTrue($result['api_enabled']);
  67. $this->assertContains('public', $result);
  68. $this->assertContains('user', $result);
  69. $this->assertContains('resharing', $result);
  70. }
  71. public function testDisabledSharingAPI() {
  72. $map = [
  73. ['core', 'shareapi_enabled', 'yes', 'no'],
  74. ];
  75. $result = $this->getResults($map);
  76. $this->assertFalse($result['api_enabled']);
  77. $this->assertNotContains('public', $result);
  78. $this->assertNotContains('user', $result);
  79. $this->assertNotContains('resharing', $result);
  80. }
  81. public function testNoLinkSharing() {
  82. $map = [
  83. ['core', 'shareapi_enabled', 'yes', 'yes'],
  84. ['core', 'shareapi_allow_links', 'yes', 'no'],
  85. ];
  86. $result = $this->getResults($map);
  87. $this->assertInternalType('array', $result['public']);
  88. $this->assertFalse($result['public']['enabled']);
  89. }
  90. public function testOnlyLinkSharing() {
  91. $map = [
  92. ['core', 'shareapi_enabled', 'yes', 'yes'],
  93. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  94. ];
  95. $result = $this->getResults($map);
  96. $this->assertInternalType('array', $result['public']);
  97. $this->assertTrue($result['public']['enabled']);
  98. }
  99. public function testLinkPassword() {
  100. $map = [
  101. ['core', 'shareapi_enabled', 'yes', 'yes'],
  102. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  103. ['core', 'shareapi_enforce_links_password', 'no', 'yes'],
  104. ];
  105. $result = $this->getResults($map);
  106. $this->assertArrayHasKey('password', $result['public']);
  107. $this->assertArrayHasKey('enforced', $result['public']['password']);
  108. $this->assertTrue($result['public']['password']['enforced']);
  109. }
  110. public function testLinkNoPassword() {
  111. $map = [
  112. ['core', 'shareapi_enabled', 'yes', 'yes'],
  113. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  114. ['core', 'shareapi_enforce_links_password', 'no', 'no'],
  115. ];
  116. $result = $this->getResults($map);
  117. $this->assertArrayHasKey('password', $result['public']);
  118. $this->assertArrayHasKey('enforced', $result['public']['password']);
  119. $this->assertFalse($result['public']['password']['enforced']);
  120. }
  121. public function testLinkNoExpireDate() {
  122. $map = [
  123. ['core', 'shareapi_enabled', 'yes', 'yes'],
  124. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  125. ['core', 'shareapi_default_expire_date', 'no', 'no'],
  126. ];
  127. $result = $this->getResults($map);
  128. $this->assertArrayHasKey('expire_date', $result['public']);
  129. $this->assertInternalType('array', $result['public']['expire_date']);
  130. $this->assertFalse($result['public']['expire_date']['enabled']);
  131. }
  132. public function testLinkExpireDate() {
  133. $map = [
  134. ['core', 'shareapi_enabled', 'yes', 'yes'],
  135. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  136. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  137. ['core', 'shareapi_expire_after_n_days', '7', '7'],
  138. ['core', 'shareapi_enforce_expire_date', 'no', 'no'],
  139. ];
  140. $result = $this->getResults($map);
  141. $this->assertArrayHasKey('expire_date', $result['public']);
  142. $this->assertInternalType('array', $result['public']['expire_date']);
  143. $this->assertTrue($result['public']['expire_date']['enabled']);
  144. $this->assertArrayHasKey('days', $result['public']['expire_date']);
  145. $this->assertFalse($result['public']['expire_date']['enforced']);
  146. }
  147. public function testLinkExpireDateEnforced() {
  148. $map = [
  149. ['core', 'shareapi_enabled', 'yes', 'yes'],
  150. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  151. ['core', 'shareapi_default_expire_date', 'no', 'yes'],
  152. ['core', 'shareapi_enforce_expire_date', 'no', 'yes'],
  153. ];
  154. $result = $this->getResults($map);
  155. $this->assertArrayHasKey('expire_date', $result['public']);
  156. $this->assertInternalType('array', $result['public']['expire_date']);
  157. $this->assertTrue($result['public']['expire_date']['enforced']);
  158. }
  159. public function testLinkSendMail() {
  160. $map = [
  161. ['core', 'shareapi_enabled', 'yes', 'yes'],
  162. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  163. ['core', 'shareapi_allow_public_notification', 'no', 'yes'],
  164. ];
  165. $result = $this->getResults($map);
  166. $this->assertTrue($result['public']['send_mail']);
  167. }
  168. public function testLinkNoSendMail() {
  169. $map = [
  170. ['core', 'shareapi_enabled', 'yes', 'yes'],
  171. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  172. ['core', 'shareapi_allow_public_notification', 'no', 'no'],
  173. ];
  174. $result = $this->getResults($map);
  175. $this->assertFalse($result['public']['send_mail']);
  176. }
  177. public function testResharing() {
  178. $map = [
  179. ['core', 'shareapi_enabled', 'yes', 'yes'],
  180. ['core', 'shareapi_allow_resharing', 'yes', 'yes'],
  181. ];
  182. $result = $this->getResults($map);
  183. $this->assertTrue($result['resharing']);
  184. }
  185. public function testNoResharing() {
  186. $map = [
  187. ['core', 'shareapi_enabled', 'yes', 'yes'],
  188. ['core', 'shareapi_allow_resharing', 'yes', 'no'],
  189. ];
  190. $result = $this->getResults($map);
  191. $this->assertFalse($result['resharing']);
  192. }
  193. public function testLinkPublicUpload() {
  194. $map = [
  195. ['core', 'shareapi_enabled', 'yes', 'yes'],
  196. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  197. ['core', 'shareapi_allow_public_upload', 'yes', 'yes'],
  198. ];
  199. $result = $this->getResults($map);
  200. $this->assertTrue($result['public']['upload']);
  201. $this->assertTrue($result['public']['upload_files_drop']);
  202. }
  203. public function testLinkNoPublicUpload() {
  204. $map = [
  205. ['core', 'shareapi_enabled', 'yes', 'yes'],
  206. ['core', 'shareapi_allow_links', 'yes', 'yes'],
  207. ['core', 'shareapi_allow_public_upload', 'yes', 'no'],
  208. ];
  209. $result = $this->getResults($map);
  210. $this->assertFalse($result['public']['upload']);
  211. $this->assertFalse($result['public']['upload_files_drop']);
  212. }
  213. public function testNoGroupSharing() {
  214. $map = [
  215. ['core', 'shareapi_enabled', 'yes', 'yes'],
  216. ['core', 'shareapi_allow_group_sharing', 'yes', 'no'],
  217. ];
  218. $result = $this->getResults($map);
  219. $this->assertFalse($result['group_sharing']);
  220. }
  221. public function testGroupSharing() {
  222. $map = [
  223. ['core', 'shareapi_enabled', 'yes', 'yes'],
  224. ['core', 'shareapi_allow_group_sharing', 'yes', 'yes'],
  225. ];
  226. $result = $this->getResults($map);
  227. $this->assertTrue($result['group_sharing']);
  228. }
  229. public function testFederatedSharingIncoming() {
  230. $map = [
  231. ['files_sharing', 'incoming_server2server_share_enabled', 'yes', 'yes'],
  232. ];
  233. $result = $this->getResults($map);
  234. $this->assertArrayHasKey('federation', $result);
  235. $this->assertTrue($result['federation']['incoming']);
  236. }
  237. public function testFederatedSharingNoIncoming() {
  238. $map = [
  239. ['files_sharing', 'incoming_server2server_share_enabled', 'yes', 'no'],
  240. ];
  241. $result = $this->getResults($map);
  242. $this->assertArrayHasKey('federation', $result);
  243. $this->assertFalse($result['federation']['incoming']);
  244. }
  245. public function testFederatedSharingOutgoing() {
  246. $map = [
  247. ['files_sharing', 'outgoing_server2server_share_enabled', 'yes', 'yes'],
  248. ];
  249. $result = $this->getResults($map);
  250. $this->assertArrayHasKey('federation', $result);
  251. $this->assertTrue($result['federation']['outgoing']);
  252. }
  253. public function testFederatedSharingNoOutgoing() {
  254. $map = [
  255. ['files_sharing', 'outgoing_server2server_share_enabled', 'yes', 'no'],
  256. ];
  257. $result = $this->getResults($map);
  258. $this->assertArrayHasKey('federation', $result);
  259. $this->assertFalse($result['federation']['outgoing']);
  260. }
  261. }