PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/build/integration/features/bootstrap/Sharing.php

https://gitlab.com/wuhang2003/core
PHP | 473 lines | 358 code | 40 blank | 75 comment | 54 complexity | 9b7b21a36a11b4faa2e32e700fca8c00 MD5 | raw file
  1. <?php
  2. use GuzzleHttp\Client;
  3. use GuzzleHttp\Message\ResponseInterface;
  4. require __DIR__ . '/../../vendor/autoload.php';
  5. trait Sharing {
  6. use Provisioning;
  7. /** @var int */
  8. private $sharingApiVersion = 1;
  9. /** @var SimpleXMLElement */
  10. private $lastShareData = null;
  11. /** @var int */
  12. private $savedShareId = null;
  13. /**
  14. * @Given /^as "([^"]*)" creating a share with$/
  15. * @param string $user
  16. * @param \Behat\Gherkin\Node\TableNode|null $body
  17. */
  18. public function asCreatingAShareWith($user, $body) {
  19. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/files_sharing/api/v{$this->sharingApiVersion}/shares";
  20. $client = new Client();
  21. $options = [];
  22. if ($user === 'admin') {
  23. $options['auth'] = $this->adminUser;
  24. } else {
  25. $options['auth'] = [$user, $this->regularUser];
  26. }
  27. if ($body instanceof \Behat\Gherkin\Node\TableNode) {
  28. $fd = $body->getRowsHash();
  29. if (array_key_exists('expireDate', $fd)){
  30. $dateModification = $fd['expireDate'];
  31. $fd['expireDate'] = date('Y-m-d', strtotime($dateModification));
  32. }
  33. $options['body'] = $fd;
  34. }
  35. try {
  36. $this->response = $client->send($client->createRequest("POST", $fullUrl, $options));
  37. } catch (\GuzzleHttp\Exception\ClientException $ex) {
  38. $this->response = $ex->getResponse();
  39. }
  40. $this->lastShareData = $this->response->xml();
  41. }
  42. /**
  43. * @When /^creating a share with$/
  44. * @param \Behat\Gherkin\Node\TableNode|null $body
  45. */
  46. public function creatingShare($body) {
  47. $this->asCreatingAShareWith($this->currentUser, $body);
  48. }
  49. /**
  50. * @Then /^Public shared file "([^"]*)" can be downloaded$/
  51. */
  52. public function checkPublicSharedFile($filename) {
  53. $client = new Client();
  54. $options = [];
  55. if (count($this->lastShareData->data->element) > 0){
  56. $url = $this->lastShareData->data[0]->url;
  57. }
  58. else{
  59. $url = $this->lastShareData->data->url;
  60. }
  61. $fullUrl = $url . "/download";
  62. $options['save_to'] = "./$filename";
  63. $this->response = $client->get($fullUrl, $options);
  64. $finfo = new finfo;
  65. $fileinfo = $finfo->file("./$filename", FILEINFO_MIME_TYPE);
  66. PHPUnit_Framework_Assert::assertEquals($fileinfo, "text/plain");
  67. if (file_exists("./$filename")) {
  68. unlink("./$filename");
  69. }
  70. }
  71. /**
  72. * @Then /^Public shared file "([^"]*)" with password "([^"]*)" can be downloaded$/
  73. */
  74. public function checkPublicSharedFileWithPassword($filename, $password) {
  75. $client = new Client();
  76. $options = [];
  77. if (count($this->lastShareData->data->element) > 0){
  78. $token = $this->lastShareData->data[0]->token;
  79. }
  80. else{
  81. $token = $this->lastShareData->data->token;
  82. }
  83. $fullUrl = substr($this->baseUrl, 0, -4) . "public.php/webdav";
  84. $options['auth'] = [$token, $password];
  85. $options['save_to'] = "./$filename";
  86. $this->response = $client->get($fullUrl, $options);
  87. $finfo = new finfo;
  88. $fileinfo = $finfo->file("./$filename", FILEINFO_MIME_TYPE);
  89. PHPUnit_Framework_Assert::assertEquals($fileinfo, "text/plain");
  90. if (file_exists("./$filename")) {
  91. unlink("./$filename");
  92. }
  93. }
  94. /**
  95. * @When /^Adding expiration date to last share$/
  96. */
  97. public function addingExpirationDate() {
  98. $share_id = (string) $this->lastShareData->data[0]->id;
  99. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/files_sharing/api/v{$this->sharingApiVersion}/shares/$share_id";
  100. $client = new Client();
  101. $options = [];
  102. if ($this->currentUser === 'admin') {
  103. $options['auth'] = $this->adminUser;
  104. } else {
  105. $options['auth'] = [$this->currentUser, $this->regularUser];
  106. }
  107. $date = date('Y-m-d', strtotime("+3 days"));
  108. $options['body'] = ['expireDate' => $date];
  109. $this->response = $client->send($client->createRequest("PUT", $fullUrl, $options));
  110. PHPUnit_Framework_Assert::assertEquals(200, $this->response->getStatusCode());
  111. }
  112. /**
  113. * @When /^Updating last share with$/
  114. * @param \Behat\Gherkin\Node\TableNode|null $body
  115. */
  116. public function updatingLastShare($body) {
  117. $share_id = (string) $this->lastShareData->data[0]->id;
  118. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/files_sharing/api/v{$this->sharingApiVersion}/shares/$share_id";
  119. $client = new Client();
  120. $options = [];
  121. if ($this->currentUser === 'admin') {
  122. $options['auth'] = $this->adminUser;
  123. } else {
  124. $options['auth'] = [$this->currentUser, $this->regularUser];
  125. }
  126. if ($body instanceof \Behat\Gherkin\Node\TableNode) {
  127. $fd = $body->getRowsHash();
  128. if (array_key_exists('expireDate', $fd)){
  129. $dateModification = $fd['expireDate'];
  130. $fd['expireDate'] = date('Y-m-d', strtotime($dateModification));
  131. }
  132. $options['body'] = $fd;
  133. }
  134. try {
  135. $this->response = $client->send($client->createRequest("PUT", $fullUrl, $options));
  136. } catch (\GuzzleHttp\Exception\ClientException $ex) {
  137. $this->response = $ex->getResponse();
  138. }
  139. PHPUnit_Framework_Assert::assertEquals(200, $this->response->getStatusCode());
  140. }
  141. public function createShare($user,
  142. $path = null,
  143. $shareType = null,
  144. $shareWith = null,
  145. $publicUpload = null,
  146. $password = null,
  147. $permissions = null){
  148. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/files_sharing/api/v{$this->sharingApiVersion}/shares";
  149. $client = new Client();
  150. $options = [];
  151. if ($user === 'admin') {
  152. $options['auth'] = $this->adminUser;
  153. } else {
  154. $options['auth'] = [$user, $this->regularUser];
  155. }
  156. $fd = [];
  157. if (!is_null($path)){
  158. $fd['path'] = $path;
  159. }
  160. if (!is_null($shareType)){
  161. $fd['shareType'] = $shareType;
  162. }
  163. if (!is_null($shareWith)){
  164. $fd['shareWith'] = $shareWith;
  165. }
  166. if (!is_null($publicUpload)){
  167. $fd['publicUpload'] = $publicUpload;
  168. }
  169. if (!is_null($password)){
  170. $fd['password'] = $password;
  171. }
  172. if (!is_null($permissions)){
  173. $fd['permissions'] = $permissions;
  174. }
  175. $options['body'] = $fd;
  176. try {
  177. $this->response = $client->send($client->createRequest("POST", $fullUrl, $options));
  178. $this->lastShareData = $this->response->xml();
  179. } catch (\GuzzleHttp\Exception\ClientException $ex) {
  180. $this->response = $ex->getResponse();
  181. }
  182. }
  183. public function isFieldInResponse($field, $contentExpected){
  184. $data = $this->response->xml()->data[0];
  185. if ((string)$field == 'expiration'){
  186. $contentExpected = date('Y-m-d', strtotime($contentExpected)) . " 00:00:00";
  187. }
  188. if (count($data->element) > 0){
  189. foreach($data as $element) {
  190. if ($contentExpected == "A_TOKEN"){
  191. return (strlen((string)$element->$field) == 15);
  192. }
  193. elseif ($contentExpected == "A_NUMBER"){
  194. return is_numeric((string)$element->$field);
  195. }
  196. elseif($contentExpected == "AN_URL"){
  197. return $this->isExpectedUrl((string)$element->$field, "index.php/s/");
  198. }
  199. elseif ((string)$element->$field == $contentExpected){
  200. return True;
  201. }
  202. else{
  203. print($element->$field);
  204. }
  205. }
  206. return False;
  207. } else {
  208. if ($contentExpected == "A_TOKEN"){
  209. return (strlen((string)$data->$field) == 15);
  210. }
  211. elseif ($contentExpected == "A_NUMBER"){
  212. return is_numeric((string)$data->$field);
  213. }
  214. elseif($contentExpected == "AN_URL"){
  215. return $this->isExpectedUrl((string)$data->$field, "index.php/s/");
  216. }
  217. elseif ($data->$field == $contentExpected){
  218. return True;
  219. }
  220. return False;
  221. }
  222. }
  223. /**
  224. * @Then /^File "([^"]*)" should be included in the response$/
  225. *
  226. * @param string $filename
  227. */
  228. public function checkSharedFileInResponse($filename){
  229. PHPUnit_Framework_Assert::assertEquals(True, $this->isFieldInResponse('file_target', "/$filename"));
  230. }
  231. /**
  232. * @Then /^File "([^"]*)" should not be included in the response$/
  233. *
  234. * @param string $filename
  235. */
  236. public function checkSharedFileNotInResponse($filename){
  237. PHPUnit_Framework_Assert::assertEquals(False, $this->isFieldInResponse('file_target', "/$filename"));
  238. }
  239. /**
  240. * @Then /^User "([^"]*)" should be included in the response$/
  241. *
  242. * @param string $user
  243. */
  244. public function checkSharedUserInResponse($user){
  245. PHPUnit_Framework_Assert::assertEquals(True, $this->isFieldInResponse('share_with', "$user"));
  246. }
  247. /**
  248. * @Then /^User "([^"]*)" should not be included in the response$/
  249. *
  250. * @param string $user
  251. */
  252. public function checkSharedUserNotInResponse($user){
  253. PHPUnit_Framework_Assert::assertEquals(False, $this->isFieldInResponse('share_with', "$user"));
  254. }
  255. public function isUserOrGroupInSharedData($userOrGroup){
  256. $data = $this->response->xml()->data[0];
  257. foreach($data as $element) {
  258. if ($element->share_with == $userOrGroup){
  259. return True;
  260. }
  261. }
  262. return False;
  263. }
  264. /**
  265. * @Given /^file "([^"]*)" of user "([^"]*)" is shared with user "([^"]*)"$/
  266. *
  267. * @param string $filepath
  268. * @param string $user1
  269. * @param string $user2
  270. */
  271. public function assureFileIsShared($filepath, $user1, $user2){
  272. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/files_sharing/api/v{$this->sharingApiVersion}/shares" . "?path=$filepath";
  273. $client = new Client();
  274. $options = [];
  275. if ($user1 === 'admin') {
  276. $options['auth'] = $this->adminUser;
  277. } else {
  278. $options['auth'] = [$user1, $this->regularUser];
  279. }
  280. $this->response = $client->get($fullUrl, $options);
  281. if ($this->isUserOrGroupInSharedData($user2)){
  282. return;
  283. } else {
  284. $this->createShare($user1, $filepath, 0, $user2, null, null, null);
  285. }
  286. $this->response = $client->get($fullUrl, $options);
  287. PHPUnit_Framework_Assert::assertEquals(True, $this->isUserOrGroupInSharedData($user2));
  288. }
  289. /**
  290. * @Given /^file "([^"]*)" of user "([^"]*)" is shared with group "([^"]*)"$/
  291. *
  292. * @param string $filepath
  293. * @param string $user
  294. * @param string $group
  295. */
  296. public function assureFileIsSharedWithGroup($filepath, $user, $group){
  297. $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/files_sharing/api/v{$this->sharingApiVersion}/shares" . "?path=$filepath";
  298. $client = new Client();
  299. $options = [];
  300. if ($user === 'admin') {
  301. $options['auth'] = $this->adminUser;
  302. } else {
  303. $options['auth'] = [$user, $this->regularUser];
  304. }
  305. $this->response = $client->get($fullUrl, $options);
  306. if ($this->isUserOrGroupInSharedData($group)){
  307. return;
  308. } else {
  309. $this->createShare($user, $filepath, 1, $group, null, null, null);
  310. }
  311. $this->response = $client->get($fullUrl, $options);
  312. PHPUnit_Framework_Assert::assertEquals(True, $this->isUserOrGroupInSharedData($group));
  313. }
  314. /**
  315. * @When /^Deleting last share$/
  316. */
  317. public function deletingLastShare(){
  318. $share_id = $this->lastShareData->data[0]->id;
  319. $url = "/apps/files_sharing/api/v{$this->sharingApiVersion}/shares/$share_id";
  320. $this->sendingToWith("DELETE", $url, null);
  321. }
  322. /**
  323. * @When /^Getting info of last share$/
  324. */
  325. public function gettingInfoOfLastShare(){
  326. $share_id = $this->lastShareData->data[0]->id;
  327. $url = "/apps/files_sharing/api/v{$this->sharingApiVersion}/shares/$share_id";
  328. $this->sendingToWith("GET", $url, null);
  329. }
  330. /**
  331. * @Then /^last share_id is included in the answer$/
  332. */
  333. public function checkingLastShareIDIsIncluded(){
  334. $share_id = $this->lastShareData->data[0]->id;
  335. if (!$this->isFieldInResponse('id', $share_id)){
  336. PHPUnit_Framework_Assert::fail("Share id $share_id not found in response");
  337. }
  338. }
  339. /**
  340. * @Then /^last share_id is not included in the answer$/
  341. */
  342. public function checkingLastShareIDIsNotIncluded(){
  343. $share_id = $this->lastShareData->data[0]->id;
  344. if ($this->isFieldInResponse('id', $share_id)){
  345. PHPUnit_Framework_Assert::fail("Share id $share_id has been found in response");
  346. }
  347. }
  348. /**
  349. * @Then /^Share fields of last share match with$/
  350. * @param \Behat\Gherkin\Node\TableNode|null $body
  351. */
  352. public function checkShareFields($body){
  353. if ($body instanceof \Behat\Gherkin\Node\TableNode) {
  354. $fd = $body->getRowsHash();
  355. foreach($fd as $field => $value) {
  356. if (substr($field, 0, 10 ) === "share_with"){
  357. $value = str_replace("REMOTE", substr($this->remoteBaseUrl, 0, -5), $value);
  358. $value = str_replace("LOCAL", substr($this->localBaseUrl, 0, -5), $value);
  359. }
  360. if (substr($field, 0, 6 ) === "remote"){
  361. $value = str_replace("REMOTE", substr($this->remoteBaseUrl, 0, -4), $value);
  362. $value = str_replace("LOCAL", substr($this->localBaseUrl, 0, -4), $value);
  363. }
  364. if (!$this->isFieldInResponse($field, $value)){
  365. PHPUnit_Framework_Assert::fail("$field" . " doesn't have value " . "$value");
  366. }
  367. }
  368. }
  369. }
  370. /**
  371. * @Then As :user remove all shares from the file named :fileName
  372. */
  373. public function asRemoveAllSharesFromTheFileNamed($user, $fileName) {
  374. $url = $this->baseUrl . "v{$this->apiVersion}.php/apps/files_sharing/api/v{$this->sharingApiVersion}/shares?format=json";
  375. $client = new \GuzzleHttp\Client();
  376. $res = $client->get(
  377. $url,
  378. [
  379. 'auth' => [
  380. $user,
  381. '123456',
  382. ],
  383. 'headers' => [
  384. 'Content-Type' => 'application/json',
  385. ],
  386. ]
  387. );
  388. $json = json_decode($res->getBody()->getContents(), true);
  389. $deleted = false;
  390. foreach($json['ocs']['data'] as $data) {
  391. if (stripslashes($data['path']) === $fileName) {
  392. $id = $data['id'];
  393. $client->delete(
  394. $this->baseUrl . "v{$this->apiVersion}.php/apps/files_sharing/api/v{$this->sharingApiVersion}/shares/{$id}",
  395. [
  396. 'auth' => [
  397. $user,
  398. '123456',
  399. ],
  400. 'headers' => [
  401. 'Content-Type' => 'application/json',
  402. ],
  403. ]
  404. );
  405. $deleted = true;
  406. }
  407. }
  408. if($deleted === false) {
  409. throw new \Exception("Could not delete file $fileName");
  410. }
  411. }
  412. /**
  413. * @When save last share id
  414. */
  415. public function saveLastShareId()
  416. {
  417. $this->savedShareId = $this->lastShareData['data']['id'];
  418. }
  419. /**
  420. * @Then share ids should match
  421. */
  422. public function shareIdsShouldMatch()
  423. {
  424. if ($this->savedShareId !== $this->lastShareData['data']['id']) {
  425. throw new \Exception('Expected the same link share to be returned');
  426. }
  427. }
  428. }