CommentsContext.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. require __DIR__ . '/../../vendor/autoload.php';
  24. class CommentsContext implements \Behat\Behat\Context\Context {
  25. /** @var string */
  26. private $baseUrl;
  27. /** @var array */
  28. private $response;
  29. /** @var int */
  30. private $commentId;
  31. /** @var int */
  32. private $fileId;
  33. /**
  34. * @param string $baseUrl
  35. */
  36. public function __construct($baseUrl) {
  37. $this->baseUrl = $baseUrl;
  38. // in case of ci deployment we take the server url from the environment
  39. $testServerUrl = getenv('TEST_SERVER_URL');
  40. if ($testServerUrl !== false) {
  41. $this->baseUrl = substr($testServerUrl, 0, -5);
  42. }
  43. }
  44. /** @AfterScenario */
  45. public function teardownScenario() {
  46. $client = new \GuzzleHttp\Client();
  47. try {
  48. $client->delete(
  49. $this->baseUrl.'/remote.php/webdav/myFileToComment.txt',
  50. [
  51. 'auth' => [
  52. 'user0',
  53. '123456',
  54. ],
  55. 'headers' => [
  56. 'Content-Type' => 'application/json',
  57. ],
  58. ]
  59. );
  60. } catch (\GuzzleHttp\Exception\ClientException $e) {
  61. $e->getResponse();
  62. }
  63. }
  64. /**
  65. * @param string $path
  66. * @return int
  67. */
  68. private function getFileIdForPath($path) {
  69. $url = $this->baseUrl.'/remote.php/webdav/'.$path;
  70. $context = stream_context_create(array(
  71. 'http' => array(
  72. 'method' => 'PROPFIND',
  73. 'header' => "Authorization: Basic dXNlcjA6MTIzNDU2\r\nContent-Type: application/x-www-form-urlencoded",
  74. 'content' => '<?xml version="1.0"?>
  75. <d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
  76. <d:prop>
  77. <oc:fileid />
  78. </d:prop>
  79. </d:propfind>'
  80. )
  81. ));
  82. $response = file_get_contents($url, false, $context);
  83. preg_match_all('/\<oc:fileid\>(.*)\<\/oc:fileid\>/', $response, $matches);
  84. return (int)$matches[1][0];
  85. }
  86. /**
  87. * @When :user posts a comment with content :content on the file named :fileName it should return :statusCode
  88. * @param string $user
  89. * @param string $content
  90. * @param string $fileName
  91. * @param int $statusCode
  92. * @throws \Exception
  93. */
  94. public function postsACommentWithContentOnTheFileNamedItShouldReturn($user, $content, $fileName, $statusCode) {
  95. $fileId = $this->getFileIdForPath($fileName);
  96. $this->fileId = (int)$fileId;
  97. $url = $this->baseUrl.'/remote.php/dav/comments/files/'.$fileId.'/';
  98. $client = new \GuzzleHttp\Client();
  99. try {
  100. $res = $client->post(
  101. $url,
  102. [
  103. 'body' => '{"actorId":"user0","actorDisplayName":"user0","actorType":"users","verb":"comment","message":"' . $content . '","creationDateTime":"Thu, 18 Feb 2016 17:04:18 GMT","objectType":"files"}',
  104. 'auth' => [
  105. $user,
  106. '123456',
  107. ],
  108. 'headers' => [
  109. 'Content-Type' => 'application/json',
  110. ],
  111. ]
  112. );
  113. } catch (\GuzzleHttp\Exception\ClientException $e) {
  114. $res = $e->getResponse();
  115. }
  116. if($res->getStatusCode() !== (int)$statusCode) {
  117. throw new \Exception("Response status code was not $statusCode (".$res->getStatusCode().")");
  118. }
  119. }
  120. /**
  121. * @Then As :user load all the comments of the file named :fileName it should return :statusCode
  122. * @param string $user
  123. * @param string $fileName
  124. * @param int $statusCode
  125. * @throws \Exception
  126. */
  127. public function asLoadloadAllTheCommentsOfTheFileNamedItShouldReturn($user, $fileName, $statusCode) {
  128. $fileId = $this->getFileIdForPath($fileName);
  129. $url = $this->baseUrl.'/remote.php/dav/comments/files/'.$fileId.'/';
  130. try {
  131. $client = new \GuzzleHttp\Client();
  132. $res = $client->createRequest(
  133. 'REPORT',
  134. $url,
  135. [
  136. 'body' => '<?xml version="1.0" encoding="utf-8" ?>
  137. <oc:filter-comments xmlns:oc="http://owncloud.org/ns">
  138. <oc:limit>200</oc:limit>
  139. <oc:offset>0</oc:offset>
  140. </oc:filter-comments>
  141. ',
  142. 'auth' => [
  143. $user,
  144. '123456',
  145. ],
  146. 'headers' => [
  147. 'Content-Type' => 'application/json',
  148. ],
  149. ]
  150. );
  151. $res = $client->send($res);
  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['body'] = $fd;
  178. $options['headers'] = [
  179. 'OCS-APIREQUEST' => 'true',
  180. ];
  181. $client->send($client->createRequest($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. if(count($this->response) !== (int)$number) {
  239. throw new \Exception("Found more comments than $number (".count($this->response).")");
  240. }
  241. }
  242. /**
  243. * @Then As :user edit the last created comment and set text to :text it should return :statusCode
  244. * @param string $user
  245. * @param string $text
  246. * @param int $statusCode
  247. * @throws \Exception
  248. */
  249. public function asEditTheLastCreatedCommentAndSetTextToItShouldReturn($user, $text, $statusCode) {
  250. $client = new \GuzzleHttp\Client();
  251. $options = [];
  252. $options['auth'] = [$user, '123456'];
  253. $options['body'] = '<?xml version="1.0"?>
  254. <d:propertyupdate xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
  255. <d:set>
  256. <d:prop>
  257. <oc:message>'.$text.'</oc:message>
  258. </d:prop>
  259. </d:set>
  260. </d:propertyupdate>';
  261. try {
  262. $res = $client->send($client->createRequest('PROPPATCH', $this->baseUrl.'/remote.php/dav/comments/files/' . $this->fileId . '/' . $this->commentId, $options));
  263. } catch (\GuzzleHttp\Exception\ClientException $e) {
  264. $res = $e->getResponse();
  265. }
  266. if($res->getStatusCode() !== (int)$statusCode) {
  267. throw new \Exception("Response status code was not $statusCode (".$res->getStatusCode().")");
  268. }
  269. }
  270. }