You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommentsContext.php 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. require __DIR__ . '/../../vendor/autoload.php';
  25. class CommentsContext implements \Behat\Behat\Context\Context {
  26. /** @var string */
  27. private $baseUrl;
  28. /** @var array */
  29. private $response;
  30. /** @var int */
  31. private $commentId;
  32. /** @var int */
  33. private $fileId;
  34. /**
  35. * @param string $baseUrl
  36. */
  37. public function __construct($baseUrl) {
  38. $this->baseUrl = $baseUrl;
  39. // in case of ci deployment we take the server url from the environment
  40. $testServerUrl = getenv('TEST_SERVER_URL');
  41. if ($testServerUrl !== false) {
  42. $this->baseUrl = substr($testServerUrl, 0, -5);
  43. }
  44. }
  45. /** @AfterScenario */
  46. public function teardownScenario() {
  47. $client = new \GuzzleHttp\Client();
  48. try {
  49. $client->delete(
  50. $this->baseUrl . '/remote.php/webdav/myFileToComment.txt',
  51. [
  52. 'auth' => [
  53. 'user0',
  54. '123456',
  55. ],
  56. 'headers' => [
  57. 'Content-Type' => 'application/json',
  58. ],
  59. ]
  60. );
  61. } catch (\GuzzleHttp\Exception\ClientException $e) {
  62. $e->getResponse();
  63. }
  64. }
  65. /**
  66. * @param string $path
  67. * @return int
  68. */
  69. private function getFileIdForPath($path) {
  70. $url = $this->baseUrl . '/remote.php/webdav/' . $path;
  71. $context = stream_context_create(array(
  72. 'http' => array(
  73. 'method' => 'PROPFIND',
  74. 'header' => "Authorization: Basic dXNlcjA6MTIzNDU2\r\nContent-Type: application/x-www-form-urlencoded",
  75. 'content' => '<?xml version="1.0"?>
  76. <d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
  77. <d:prop>
  78. <oc:fileid />
  79. </d:prop>
  80. </d:propfind>'
  81. )
  82. ));
  83. $response = file_get_contents($url, false, $context);
  84. preg_match_all('/\<oc:fileid\>(.*)\<\/oc:fileid\>/', $response, $matches);
  85. return (int)$matches[1][0];
  86. }
  87. /**
  88. * @When :user posts a comment with content :content on the file named :fileName it should return :statusCode
  89. * @param string $user
  90. * @param string $content
  91. * @param string $fileName
  92. * @param int $statusCode
  93. * @throws \Exception
  94. */
  95. public function postsACommentWithContentOnTheFileNamedItShouldReturn($user, $content, $fileName, $statusCode) {
  96. $fileId = $this->getFileIdForPath($fileName);
  97. $this->fileId = (int)$fileId;
  98. $url = $this->baseUrl . '/remote.php/dav/comments/files/' . $fileId . '/';
  99. $client = new \GuzzleHttp\Client();
  100. try {
  101. $res = $client->post(
  102. $url,
  103. [
  104. 'body' => '{"actorId":"user0","actorDisplayName":"user0","actorType":"users","verb":"comment","message":"' . $content . '","creationDateTime":"Thu, 18 Feb 2016 17:04:18 GMT","objectType":"files"}',
  105. 'auth' => [
  106. $user,
  107. '123456',
  108. ],
  109. 'headers' => [
  110. 'Content-Type' => 'application/json',
  111. ],
  112. ]
  113. );
  114. } catch (\GuzzleHttp\Exception\ClientException $e) {
  115. $res = $e->getResponse();
  116. }
  117. if ($res->getStatusCode() !== (int)$statusCode) {
  118. throw new \Exception("Response status code was not $statusCode (" . $res->getStatusCode() . ")");
  119. }
  120. }
  121. /**
  122. * @Then As :user load all the comments of the file named :fileName it should return :statusCode
  123. * @param string $user
  124. * @param string $fileName
  125. * @param int $statusCode
  126. * @throws \Exception
  127. */
  128. public function asLoadloadAllTheCommentsOfTheFileNamedItShouldReturn($user, $fileName, $statusCode) {
  129. $fileId = $this->getFileIdForPath($fileName);
  130. $url = $this->baseUrl . '/remote.php/dav/comments/files/' . $fileId . '/';
  131. try {
  132. $client = new \GuzzleHttp\Client();
  133. $res = $client->request(
  134. 'REPORT',
  135. $url,
  136. [
  137. 'body' => '<?xml version="1.0" encoding="utf-8" ?>
  138. <oc:filter-comments xmlns:oc="http://owncloud.org/ns">
  139. <oc:limit>200</oc:limit>
  140. <oc:offset>0</oc:offset>
  141. </oc:filter-comments>
  142. ',
  143. 'auth' => [
  144. $user,
  145. '123456',
  146. ],
  147. 'headers' => [
  148. 'Content-Type' => 'application/json',
  149. ],
  150. ]
  151. );
  152. } catch (\GuzzleHttp\Exception\ClientException $e) {
  153. $res = $e->getResponse();
  154. }
  155. if ($res->getStatusCode() !== (int)$statusCode) {
  156. throw new \Exception("Response status code was not $statusCode (" . $res->getStatusCode() . ")");
  157. }
  158. if ($res->getStatusCode() === 207) {
  159. $service = new Sabre\Xml\Service();
  160. $this->response = $service->parse($res->getBody()->getContents());
  161. $this->commentId = (int)$this->response[0]['value'][2]['value'][0]['value'][0]['value'];
  162. }
  163. }
  164. /**
  165. * @Given As :user sending :verb to :url with
  166. * @param string $user
  167. * @param string $verb
  168. * @param string $url
  169. * @param \Behat\Gherkin\Node\TableNode $body
  170. * @throws \Exception
  171. */
  172. public function asUserSendingToWith($user, $verb, $url, \Behat\Gherkin\Node\TableNode $body) {
  173. $client = new \GuzzleHttp\Client();
  174. $options = [];
  175. $options['auth'] = [$user, '123456'];
  176. $fd = $body->getRowsHash();
  177. $options['form_params'] = $fd;
  178. $options['headers'] = [
  179. 'OCS-APIREQUEST' => 'true',
  180. ];
  181. $client->request($verb, $this->baseUrl . '/ocs/v1.php/' . $url, $options);
  182. }
  183. /**
  184. * @Then As :user delete the created comment it should return :statusCode
  185. * @param string $user
  186. * @param int $statusCode
  187. * @throws \Exception
  188. */
  189. public function asDeleteTheCreatedCommentItShouldReturn($user, $statusCode) {
  190. $url = $this->baseUrl . '/remote.php/dav/comments/files/' . $this->fileId . '/' . $this->commentId;
  191. $client = new \GuzzleHttp\Client();
  192. try {
  193. $res = $client->delete(
  194. $url,
  195. [
  196. 'auth' => [
  197. $user,
  198. '123456',
  199. ],
  200. 'headers' => [
  201. 'Content-Type' => 'application/json',
  202. ],
  203. ]
  204. );
  205. } catch (\GuzzleHttp\Exception\ClientException $e) {
  206. $res = $e->getResponse();
  207. }
  208. if ($res->getStatusCode() !== (int)$statusCode) {
  209. throw new \Exception("Response status code was not $statusCode (" . $res->getStatusCode() . ")");
  210. }
  211. }
  212. /**
  213. * @Then the response should contain a property :key with value :value
  214. * @param string $key
  215. * @param string $value
  216. * @throws \Exception
  217. */
  218. public function theResponseShouldContainAPropertyWithValue($key, $value) {
  219. $keys = $this->response[0]['value'][2]['value'][0]['value'];
  220. $found = false;
  221. foreach ($keys as $singleKey) {
  222. if ($singleKey['name'] === '{http://owncloud.org/ns}' . substr($key, 3)) {
  223. if ($singleKey['value'] === $value) {
  224. $found = true;
  225. }
  226. }
  227. }
  228. if ($found === false) {
  229. throw new \Exception("Cannot find property $key with $value");
  230. }
  231. }
  232. /**
  233. * @Then the response should contain only :number comments
  234. * @param int $number
  235. * @throws \Exception
  236. */
  237. public function theResponseShouldContainOnlyComments($number) {
  238. $count = 0;
  239. if ($this->response !== null) {
  240. $count = count($this->response);
  241. }
  242. if ($count !== (int)$number) {
  243. throw new \Exception("Found more comments than $number (" . $count . ")");
  244. }
  245. }
  246. /**
  247. * @Then As :user edit the last created comment and set text to :text it should return :statusCode
  248. * @param string $user
  249. * @param string $text
  250. * @param int $statusCode
  251. * @throws \Exception
  252. */
  253. public function asEditTheLastCreatedCommentAndSetTextToItShouldReturn($user, $text, $statusCode) {
  254. $client = new \GuzzleHttp\Client();
  255. $options = [];
  256. $options['auth'] = [$user, '123456'];
  257. $options['body'] = '<?xml version="1.0"?>
  258. <d:propertyupdate xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
  259. <d:set>
  260. <d:prop>
  261. <oc:message>' . $text . '</oc:message>
  262. </d:prop>
  263. </d:set>
  264. </d:propertyupdate>';
  265. try {
  266. $res = $client->request('PROPPATCH', $this->baseUrl . '/remote.php/dav/comments/files/' . $this->fileId . '/' . $this->commentId, $options);
  267. } catch (\GuzzleHttp\Exception\ClientException $e) {
  268. $res = $e->getResponse();
  269. }
  270. if ($res->getStatusCode() !== (int)$statusCode) {
  271. throw new \Exception("Response status code was not $statusCode (" . $res->getStatusCode() . ")");
  272. }
  273. }
  274. }