diff options
Diffstat (limited to 'build/integration/features/bootstrap/CommentsContext.php')
-rw-r--r-- | build/integration/features/bootstrap/CommentsContext.php | 126 |
1 files changed, 73 insertions, 53 deletions
diff --git a/build/integration/features/bootstrap/CommentsContext.php b/build/integration/features/bootstrap/CommentsContext.php index e74e9580dcc..53001b1c204 100644 --- a/build/integration/features/bootstrap/CommentsContext.php +++ b/build/integration/features/bootstrap/CommentsContext.php @@ -1,24 +1,10 @@ <?php + /** - * @author Lukas Reschke <lukas@owncloud.com> - * - * @copyright Copyright (c) 2016, ownCloud, Inc. - * @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, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ - require __DIR__ . '/../../vendor/autoload.php'; class CommentsContext implements \Behat\Behat\Context\Context { @@ -44,12 +30,41 @@ class CommentsContext implements \Behat\Behat\Context\Context { } } + /** + * get a named entry from response instead of picking a random entry from values + * + * @param string $path + * + * @return array|string + * @throws Exception + */ + private function getValueFromNamedEntries(string $path, array $response): mixed { + $next = ''; + if (str_contains($path, ' ')) { + [$key, $next] = explode(' ', $path, 2); + } else { + $key = $path; + } + + foreach ($response as $entry) { + if ($entry['name'] === $key) { + if ($next !== '') { + return $this->getValueFromNamedEntries($next, $entry['value']); + } else { + return $entry['value']; + } + } + } + + return null; + } + /** @AfterScenario */ public function teardownScenario() { $client = new \GuzzleHttp\Client(); try { $client->delete( - $this->baseUrl.'/remote.php/webdav/myFileToComment.txt', + $this->baseUrl . '/remote.php/webdav/myFileToComment.txt', [ 'auth' => [ 'user0', @@ -70,9 +85,9 @@ class CommentsContext implements \Behat\Behat\Context\Context { * @return int */ private function getFileIdForPath($path) { - $url = $this->baseUrl.'/remote.php/webdav/'.$path; - $context = stream_context_create(array( - 'http' => array( + $url = $this->baseUrl . '/remote.php/webdav/' . $path; + $context = stream_context_create([ + 'http' => [ 'method' => 'PROPFIND', 'header' => "Authorization: Basic dXNlcjA6MTIzNDU2\r\nContent-Type: application/x-www-form-urlencoded", 'content' => '<?xml version="1.0"?> @@ -81,8 +96,8 @@ class CommentsContext implements \Behat\Behat\Context\Context { <oc:fileid /> </d:prop> </d:propfind>' - ) - )); + ] + ]); $response = file_get_contents($url, false, $context); preg_match_all('/\<oc:fileid\>(.*)\<\/oc:fileid\>/', $response, $matches); @@ -97,10 +112,10 @@ class CommentsContext implements \Behat\Behat\Context\Context { * @param int $statusCode * @throws \Exception */ - public function postsACommentWithContentOnTheFileNamedItShouldReturn($user, $content, $fileName, $statusCode) { + public function postsACommentWithContentOnTheFileNamedItShouldReturn($user, $content, $fileName, $statusCode) { $fileId = $this->getFileIdForPath($fileName); $this->fileId = (int)$fileId; - $url = $this->baseUrl.'/remote.php/dav/comments/files/'.$fileId.'/'; + $url = $this->baseUrl . '/remote.php/dav/comments/files/' . $fileId . '/'; $client = new \GuzzleHttp\Client(); try { @@ -121,8 +136,8 @@ class CommentsContext implements \Behat\Behat\Context\Context { $res = $e->getResponse(); } - if($res->getStatusCode() !== (int)$statusCode) { - throw new \Exception("Response status code was not $statusCode (".$res->getStatusCode().")"); + if ($res->getStatusCode() !== (int)$statusCode) { + throw new \Exception("Response status code was not $statusCode (" . $res->getStatusCode() . ')'); } } @@ -136,11 +151,11 @@ class CommentsContext implements \Behat\Behat\Context\Context { */ public function asLoadloadAllTheCommentsOfTheFileNamedItShouldReturn($user, $fileName, $statusCode) { $fileId = $this->getFileIdForPath($fileName); - $url = $this->baseUrl.'/remote.php/dav/comments/files/'.$fileId.'/'; + $url = $this->baseUrl . '/remote.php/dav/comments/files/' . $fileId . '/'; try { $client = new \GuzzleHttp\Client(); - $res = $client->createRequest( + $res = $client->request( 'REPORT', $url, [ @@ -159,19 +174,18 @@ class CommentsContext implements \Behat\Behat\Context\Context { ], ] ); - $res = $client->send($res); } catch (\GuzzleHttp\Exception\ClientException $e) { $res = $e->getResponse(); } - if($res->getStatusCode() !== (int)$statusCode) { - throw new \Exception("Response status code was not $statusCode (".$res->getStatusCode().")"); + if ($res->getStatusCode() !== (int)$statusCode) { + throw new \Exception("Response status code was not $statusCode (" . $res->getStatusCode() . ')'); } - if($res->getStatusCode() === 207) { + if ($res->getStatusCode() === 207) { $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']; + $this->commentId = (int)($this->getValueFromNamedEntries('{DAV:}response {DAV:}propstat {DAV:}prop {http://owncloud.org/ns}id', $this->response ?? []) ?? 0); } } @@ -188,8 +202,11 @@ class CommentsContext implements \Behat\Behat\Context\Context { $options = []; $options['auth'] = [$user, '123456']; $fd = $body->getRowsHash(); - $options['body'] = $fd; - $client->send($client->createRequest($verb, $this->baseUrl.'/ocs/v1.php/'.$url, $options)); + $options['form_params'] = $fd; + $options['headers'] = [ + 'OCS-APIREQUEST' => 'true', + ]; + $client->request($verb, $this->baseUrl . '/ocs/v1.php/' . $url, $options); } /** @@ -199,7 +216,7 @@ class CommentsContext implements \Behat\Behat\Context\Context { * @throws \Exception */ public function asDeleteTheCreatedCommentItShouldReturn($user, $statusCode) { - $url = $this->baseUrl.'/remote.php/dav/comments/files/'.$this->fileId.'/'.$this->commentId; + $url = $this->baseUrl . '/remote.php/dav/comments/files/' . $this->fileId . '/' . $this->commentId; $client = new \GuzzleHttp\Client(); try { @@ -219,8 +236,8 @@ class CommentsContext implements \Behat\Behat\Context\Context { $res = $e->getResponse(); } - if($res->getStatusCode() !== (int)$statusCode) { - throw new \Exception("Response status code was not $statusCode (".$res->getStatusCode().")"); + if ($res->getStatusCode() !== (int)$statusCode) { + throw new \Exception("Response status code was not $statusCode (" . $res->getStatusCode() . ')'); } } @@ -231,16 +248,17 @@ class CommentsContext implements \Behat\Behat\Context\Context { * @throws \Exception */ public function theResponseShouldContainAPropertyWithValue($key, $value) { - $keys = $this->response[0]['value'][2]['value'][0]['value']; + // $keys = $this->response[0]['value'][1]['value'][0]['value']; + $keys = $this->getValueFromNamedEntries('{DAV:}response {DAV:}propstat {DAV:}prop', $this->response); $found = false; - foreach($keys as $singleKey) { - if($singleKey['name'] === '{http://owncloud.org/ns}'.substr($key, 3)) { - if($singleKey['value'] === $value) { + foreach ($keys as $singleKey) { + if ($singleKey['name'] === '{http://owncloud.org/ns}' . substr($key, 3)) { + if ($singleKey['value'] === $value) { $found = true; } } } - if($found === false) { + if ($found === false) { throw new \Exception("Cannot find property $key with $value"); } } @@ -251,8 +269,12 @@ class CommentsContext implements \Behat\Behat\Context\Context { * @throws \Exception */ public function theResponseShouldContainOnlyComments($number) { - if(count($this->response) !== (int)$number) { - throw new \Exception("Found more comments than $number (".count($this->response).")"); + $count = 0; + if ($this->response !== null) { + $count = count($this->response); + } + if ($count !== (int)$number) { + throw new \Exception("Found more comments than $number (" . $count . ')'); } } @@ -271,20 +293,18 @@ class CommentsContext implements \Behat\Behat\Context\Context { <d:propertyupdate xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns"> <d:set> <d:prop> - <oc:message>'.$text.'</oc:message> + <oc:message>' . $text . '</oc:message> </d:prop> </d:set> </d:propertyupdate>'; try { - $res = $client->send($client->createRequest('PROPPATCH', $this->baseUrl.'/remote.php/dav/comments/files/' . $this->fileId . '/' . $this->commentId, $options)); + $res = $client->request('PROPPATCH', $this->baseUrl . '/remote.php/dav/comments/files/' . $this->fileId . '/' . $this->commentId, $options); } catch (\GuzzleHttp\Exception\ClientException $e) { $res = $e->getResponse(); } - if($res->getStatusCode() !== (int)$statusCode) { - throw new \Exception("Response status code was not $statusCode (".$res->getStatusCode().")"); + if ($res->getStatusCode() !== (int)$statusCode) { + throw new \Exception("Response status code was not $statusCode (" . $res->getStatusCode() . ')'); } } - - } |