2016-02-19 01:07:41 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2016-07-21 19:13:36 +03:00
|
|
|
*
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-07-21 19:13:36 +03:00
|
|
|
*
|
2016-02-19 01:07:41 +03:00
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2016-02-19 01:07:41 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
require __DIR__ . '/../../vendor/autoload.php';
|
|
|
|
|
|
|
|
class CommentsContext implements \Behat\Behat\Context\Context {
|
|
|
|
/** @var string */
|
|
|
|
private $baseUrl;
|
|
|
|
/** @var array */
|
|
|
|
private $response;
|
|
|
|
/** @var int */
|
|
|
|
private $commentId;
|
|
|
|
/** @var int */
|
|
|
|
private $fileId;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $baseUrl
|
|
|
|
*/
|
|
|
|
public function __construct($baseUrl) {
|
|
|
|
$this->baseUrl = $baseUrl;
|
|
|
|
|
|
|
|
// in case of ci deployment we take the server url from the environment
|
|
|
|
$testServerUrl = getenv('TEST_SERVER_URL');
|
|
|
|
if ($testServerUrl !== false) {
|
|
|
|
$this->baseUrl = substr($testServerUrl, 0, -5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @AfterScenario */
|
2016-03-11 12:52:38 +03:00
|
|
|
public function teardownScenario() {
|
2016-02-19 01:07:41 +03:00
|
|
|
$client = new \GuzzleHttp\Client();
|
|
|
|
try {
|
|
|
|
$client->delete(
|
2018-02-09 15:19:39 +03:00
|
|
|
$this->baseUrl . '/remote.php/webdav/myFileToComment.txt',
|
2016-02-19 01:07:41 +03:00
|
|
|
[
|
|
|
|
'auth' => [
|
|
|
|
'user0',
|
|
|
|
'123456',
|
|
|
|
],
|
|
|
|
'headers' => [
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) {
|
|
|
|
$e->getResponse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-03-11 12:52:38 +03:00
|
|
|
* @param string $path
|
2016-02-19 01:07:41 +03:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
private function getFileIdForPath($path) {
|
2018-02-09 15:19:39 +03:00
|
|
|
$url = $this->baseUrl . '/remote.php/webdav/' . $path;
|
2020-03-26 11:30:18 +03:00
|
|
|
$context = stream_context_create([
|
|
|
|
'http' => [
|
2016-02-19 01:07:41 +03:00
|
|
|
'method' => 'PROPFIND',
|
|
|
|
'header' => "Authorization: Basic dXNlcjA6MTIzNDU2\r\nContent-Type: application/x-www-form-urlencoded",
|
|
|
|
'content' => '<?xml version="1.0"?>
|
|
|
|
<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
|
|
|
|
<d:prop>
|
|
|
|
<oc:fileid />
|
|
|
|
</d:prop>
|
|
|
|
</d:propfind>'
|
2020-03-26 11:30:18 +03:00
|
|
|
]
|
|
|
|
]);
|
2016-02-19 01:07:41 +03:00
|
|
|
|
|
|
|
$response = file_get_contents($url, false, $context);
|
|
|
|
preg_match_all('/\<oc:fileid\>(.*)\<\/oc:fileid\>/', $response, $matches);
|
|
|
|
return (int)$matches[1][0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @When :user posts a comment with content :content on the file named :fileName it should return :statusCode
|
2016-03-11 12:52:38 +03:00
|
|
|
* @param string $user
|
|
|
|
* @param string $content
|
|
|
|
* @param string $fileName
|
|
|
|
* @param int $statusCode
|
|
|
|
* @throws \Exception
|
2016-02-19 01:07:41 +03:00
|
|
|
*/
|
2018-02-09 15:19:39 +03:00
|
|
|
public function postsACommentWithContentOnTheFileNamedItShouldReturn($user, $content, $fileName, $statusCode) {
|
2016-02-19 01:07:41 +03:00
|
|
|
$fileId = $this->getFileIdForPath($fileName);
|
|
|
|
$this->fileId = (int)$fileId;
|
2018-02-09 15:19:39 +03:00
|
|
|
$url = $this->baseUrl . '/remote.php/dav/comments/files/' . $fileId . '/';
|
2016-02-19 01:07:41 +03:00
|
|
|
|
|
|
|
$client = new \GuzzleHttp\Client();
|
|
|
|
try {
|
|
|
|
$res = $client->post(
|
|
|
|
$url,
|
|
|
|
[
|
|
|
|
'body' => '{"actorId":"user0","actorDisplayName":"user0","actorType":"users","verb":"comment","message":"' . $content . '","creationDateTime":"Thu, 18 Feb 2016 17:04:18 GMT","objectType":"files"}',
|
|
|
|
'auth' => [
|
|
|
|
$user,
|
|
|
|
'123456',
|
|
|
|
],
|
|
|
|
'headers' => [
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) {
|
|
|
|
$res = $e->getResponse();
|
|
|
|
}
|
|
|
|
|
2018-02-09 15:19:39 +03:00
|
|
|
if ($res->getStatusCode() !== (int)$statusCode) {
|
|
|
|
throw new \Exception("Response status code was not $statusCode (" . $res->getStatusCode() . ")");
|
2016-02-19 01:07:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Then As :user load all the comments of the file named :fileName it should return :statusCode
|
2016-03-11 12:52:38 +03:00
|
|
|
* @param string $user
|
|
|
|
* @param string $fileName
|
|
|
|
* @param int $statusCode
|
|
|
|
* @throws \Exception
|
2016-02-19 01:07:41 +03:00
|
|
|
*/
|
|
|
|
public function asLoadloadAllTheCommentsOfTheFileNamedItShouldReturn($user, $fileName, $statusCode) {
|
|
|
|
$fileId = $this->getFileIdForPath($fileName);
|
2018-02-09 15:19:39 +03:00
|
|
|
$url = $this->baseUrl . '/remote.php/dav/comments/files/' . $fileId . '/';
|
2016-02-19 01:07:41 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
$client = new \GuzzleHttp\Client();
|
2018-02-09 15:19:39 +03:00
|
|
|
$res = $client->request(
|
2016-02-19 01:07:41 +03:00
|
|
|
'REPORT',
|
|
|
|
$url,
|
|
|
|
[
|
|
|
|
'body' => '<?xml version="1.0" encoding="utf-8" ?>
|
2016-03-11 12:52:38 +03:00
|
|
|
<oc:filter-comments xmlns:oc="http://owncloud.org/ns">
|
2016-02-19 01:07:41 +03:00
|
|
|
<oc:limit>200</oc:limit>
|
|
|
|
<oc:offset>0</oc:offset>
|
|
|
|
</oc:filter-comments>
|
|
|
|
',
|
|
|
|
'auth' => [
|
|
|
|
$user,
|
|
|
|
'123456',
|
|
|
|
],
|
|
|
|
'headers' => [
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) {
|
|
|
|
$res = $e->getResponse();
|
|
|
|
}
|
|
|
|
|
2018-02-09 15:19:39 +03:00
|
|
|
if ($res->getStatusCode() !== (int)$statusCode) {
|
|
|
|
throw new \Exception("Response status code was not $statusCode (" . $res->getStatusCode() . ")");
|
2016-02-19 01:07:41 +03:00
|
|
|
}
|
|
|
|
|
2018-02-09 15:19:39 +03:00
|
|
|
if ($res->getStatusCode() === 207) {
|
2016-02-19 01:07:41 +03:00
|
|
|
$service = new Sabre\Xml\Service();
|
|
|
|
$this->response = $service->parse($res->getBody()->getContents());
|
|
|
|
$this->commentId = (int)$this->response[0]['value'][2]['value'][0]['value'][0]['value'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Given As :user sending :verb to :url with
|
2016-03-11 12:52:38 +03:00
|
|
|
* @param string $user
|
|
|
|
* @param string $verb
|
|
|
|
* @param string $url
|
|
|
|
* @param \Behat\Gherkin\Node\TableNode $body
|
|
|
|
* @throws \Exception
|
2016-02-19 01:07:41 +03:00
|
|
|
*/
|
|
|
|
public function asUserSendingToWith($user, $verb, $url, \Behat\Gherkin\Node\TableNode $body) {
|
|
|
|
$client = new \GuzzleHttp\Client();
|
|
|
|
$options = [];
|
|
|
|
$options['auth'] = [$user, '123456'];
|
|
|
|
$fd = $body->getRowsHash();
|
2018-02-09 15:19:39 +03:00
|
|
|
$options['form_params'] = $fd;
|
2016-08-01 09:33:49 +03:00
|
|
|
$options['headers'] = [
|
|
|
|
'OCS-APIREQUEST' => 'true',
|
|
|
|
];
|
2018-02-09 15:19:39 +03:00
|
|
|
$client->request($verb, $this->baseUrl . '/ocs/v1.php/' . $url, $options);
|
2016-02-19 01:07:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Then As :user delete the created comment it should return :statusCode
|
2016-03-11 12:52:38 +03:00
|
|
|
* @param string $user
|
|
|
|
* @param int $statusCode
|
|
|
|
* @throws \Exception
|
2016-02-19 01:07:41 +03:00
|
|
|
*/
|
|
|
|
public function asDeleteTheCreatedCommentItShouldReturn($user, $statusCode) {
|
2018-02-09 15:19:39 +03:00
|
|
|
$url = $this->baseUrl . '/remote.php/dav/comments/files/' . $this->fileId . '/' . $this->commentId;
|
2016-02-19 01:07:41 +03:00
|
|
|
|
|
|
|
$client = new \GuzzleHttp\Client();
|
|
|
|
try {
|
|
|
|
$res = $client->delete(
|
|
|
|
$url,
|
|
|
|
[
|
|
|
|
'auth' => [
|
|
|
|
$user,
|
|
|
|
'123456',
|
|
|
|
],
|
|
|
|
'headers' => [
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) {
|
|
|
|
$res = $e->getResponse();
|
|
|
|
}
|
|
|
|
|
2018-02-09 15:19:39 +03:00
|
|
|
if ($res->getStatusCode() !== (int)$statusCode) {
|
|
|
|
throw new \Exception("Response status code was not $statusCode (" . $res->getStatusCode() . ")");
|
2016-02-19 01:07:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Then the response should contain a property :key with value :value
|
2016-03-11 12:52:38 +03:00
|
|
|
* @param string $key
|
|
|
|
* @param string $value
|
|
|
|
* @throws \Exception
|
2016-02-19 01:07:41 +03:00
|
|
|
*/
|
|
|
|
public function theResponseShouldContainAPropertyWithValue($key, $value) {
|
|
|
|
$keys = $this->response[0]['value'][2]['value'][0]['value'];
|
|
|
|
$found = false;
|
2018-02-09 15:19:39 +03:00
|
|
|
foreach ($keys as $singleKey) {
|
|
|
|
if ($singleKey['name'] === '{http://owncloud.org/ns}' . substr($key, 3)) {
|
|
|
|
if ($singleKey['value'] === $value) {
|
2016-02-19 01:07:41 +03:00
|
|
|
$found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-02-09 15:19:39 +03:00
|
|
|
if ($found === false) {
|
2016-02-19 01:07:41 +03:00
|
|
|
throw new \Exception("Cannot find property $key with $value");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Then the response should contain only :number comments
|
2016-03-11 12:52:38 +03:00
|
|
|
* @param int $number
|
|
|
|
* @throws \Exception
|
2016-02-19 01:07:41 +03:00
|
|
|
*/
|
|
|
|
public function theResponseShouldContainOnlyComments($number) {
|
2019-11-29 11:59:35 +03:00
|
|
|
$count = 0;
|
|
|
|
if ($this->response !== null) {
|
|
|
|
$count = count($this->response);
|
|
|
|
}
|
|
|
|
if ($count !== (int)$number) {
|
|
|
|
throw new \Exception("Found more comments than $number (" . $count . ")");
|
2016-02-19 01:07:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Then As :user edit the last created comment and set text to :text it should return :statusCode
|
2016-03-11 12:52:38 +03:00
|
|
|
* @param string $user
|
|
|
|
* @param string $text
|
|
|
|
* @param int $statusCode
|
|
|
|
* @throws \Exception
|
2016-02-19 01:07:41 +03:00
|
|
|
*/
|
|
|
|
public function asEditTheLastCreatedCommentAndSetTextToItShouldReturn($user, $text, $statusCode) {
|
|
|
|
$client = new \GuzzleHttp\Client();
|
|
|
|
$options = [];
|
|
|
|
$options['auth'] = [$user, '123456'];
|
|
|
|
$options['body'] = '<?xml version="1.0"?>
|
|
|
|
<d:propertyupdate xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
|
|
|
|
<d:set>
|
|
|
|
<d:prop>
|
2018-02-09 15:19:39 +03:00
|
|
|
<oc:message>' . $text . '</oc:message>
|
2016-02-19 01:07:41 +03:00
|
|
|
</d:prop>
|
|
|
|
</d:set>
|
|
|
|
</d:propertyupdate>';
|
|
|
|
try {
|
2018-02-09 15:19:39 +03:00
|
|
|
$res = $client->request('PROPPATCH', $this->baseUrl . '/remote.php/dav/comments/files/' . $this->fileId . '/' . $this->commentId, $options);
|
2016-02-19 01:07:41 +03:00
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) {
|
|
|
|
$res = $e->getResponse();
|
|
|
|
}
|
|
|
|
|
2018-02-09 15:19:39 +03:00
|
|
|
if ($res->getStatusCode() !== (int)$statusCode) {
|
|
|
|
throw new \Exception("Response status code was not $statusCode (" . $res->getStatusCode() . ")");
|
2016-02-19 01:07:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|