2016-02-29 19:30:02 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2016-07-21 19:13:36 +03:00
|
|
|
*
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Phil Davis <phil.davis@inf.org>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-07-21 19:13:36 +03:00
|
|
|
*
|
2016-02-29 19:30:02 +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-29 19:30:02 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
require __DIR__ . '/../../vendor/autoload.php';
|
|
|
|
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use GuzzleHttp\Message\ResponseInterface;
|
|
|
|
|
|
|
|
class CardDavContext implements \Behat\Behat\Context\Context {
|
|
|
|
/** @var string */
|
|
|
|
private $baseUrl;
|
|
|
|
/** @var Client */
|
|
|
|
private $client;
|
|
|
|
/** @var ResponseInterface */
|
|
|
|
private $response;
|
|
|
|
/** @var string */
|
|
|
|
private $responseXml = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @BeforeScenario */
|
2017-02-21 18:58:10 +03:00
|
|
|
public function setUpScenario() {
|
2016-02-29 19:30:02 +03:00
|
|
|
$this->client = new Client();
|
|
|
|
$this->responseXml = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @AfterScenario */
|
|
|
|
public function afterScenario() {
|
|
|
|
$davUrl = $this->baseUrl . '/remote.php/dav/addressbooks/users/admin/MyAddressbook';
|
|
|
|
try {
|
|
|
|
$this->client->delete(
|
|
|
|
$davUrl,
|
|
|
|
[
|
|
|
|
'auth' => [
|
|
|
|
'admin',
|
|
|
|
'admin',
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
2020-04-10 15:19:56 +03:00
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) {
|
|
|
|
}
|
2016-02-29 19:30:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-10 16:01:26 +03:00
|
|
|
* @When :user requests addressbook :addressBook with statuscode :statusCode on the endpoint :endpoint
|
2016-03-11 12:42:57 +03:00
|
|
|
* @param string $user
|
|
|
|
* @param string $addressBook
|
|
|
|
* @param int $statusCode
|
2017-02-10 16:01:26 +03:00
|
|
|
* @param string $endpoint
|
2016-03-11 12:42:57 +03:00
|
|
|
* @throws \Exception
|
2016-02-29 19:30:02 +03:00
|
|
|
*/
|
2017-02-10 16:01:26 +03:00
|
|
|
public function requestsAddressbookWithStatuscodeOnTheEndpoint($user, $addressBook, $statusCode, $endpoint) {
|
|
|
|
$davUrl = $this->baseUrl . $endpoint . $addressBook;
|
2016-02-29 19:30:02 +03:00
|
|
|
|
|
|
|
$password = ($user === 'admin') ? 'admin' : '123456';
|
|
|
|
try {
|
2018-02-09 15:19:39 +03:00
|
|
|
$this->response = $this->client->request(
|
2017-02-10 16:01:26 +03:00
|
|
|
'PROPFIND',
|
2016-02-29 19:30:02 +03:00
|
|
|
$davUrl,
|
|
|
|
[
|
|
|
|
'auth' => [
|
|
|
|
$user,
|
|
|
|
$password,
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) {
|
|
|
|
$this->response = $e->getResponse();
|
|
|
|
}
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if ((int)$statusCode !== $this->response->getStatusCode()) {
|
2016-02-29 19:30:02 +03:00
|
|
|
throw new \Exception(
|
|
|
|
sprintf(
|
|
|
|
'Expected %s got %s',
|
|
|
|
(int)$statusCode,
|
|
|
|
$this->response->getStatusCode()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$body = $this->response->getBody()->getContents();
|
2020-04-10 15:19:56 +03:00
|
|
|
if (substr($body, 0, 1) === '<') {
|
2016-02-29 19:30:02 +03:00
|
|
|
$reader = new Sabre\Xml\Reader();
|
|
|
|
$reader->xml($body);
|
|
|
|
$this->responseXml = $reader->parse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Given :user creates an addressbook named :addressBook with statuscode :statusCode
|
2016-03-11 12:42:57 +03:00
|
|
|
* @param string $user
|
|
|
|
* @param string $addressBook
|
|
|
|
* @param int $statusCode
|
|
|
|
* @throws \Exception
|
2016-02-29 19:30:02 +03:00
|
|
|
*/
|
|
|
|
public function createsAnAddressbookNamedWithStatuscode($user, $addressBook, $statusCode) {
|
|
|
|
$davUrl = $this->baseUrl . '/remote.php/dav/addressbooks/users/'.$user.'/'.$addressBook;
|
|
|
|
$password = ($user === 'admin') ? 'admin' : '123456';
|
|
|
|
|
2018-02-09 15:19:39 +03:00
|
|
|
$this->response = $this->client->request(
|
2016-02-29 19:30:02 +03:00
|
|
|
'MKCOL',
|
|
|
|
$davUrl,
|
|
|
|
[
|
2016-03-11 12:42:57 +03:00
|
|
|
'body' => '<d:mkcol xmlns:card="urn:ietf:params:xml:ns:carddav"
|
2016-02-29 19:30:02 +03:00
|
|
|
xmlns:d="DAV:">
|
|
|
|
<d:set>
|
|
|
|
<d:prop>
|
|
|
|
<d:resourcetype>
|
|
|
|
<d:collection />,<card:addressbook />
|
|
|
|
</d:resourcetype>,<d:displayname>'.$addressBook.'</d:displayname>
|
|
|
|
</d:prop>
|
|
|
|
</d:set>
|
|
|
|
</d:mkcol>',
|
|
|
|
'auth' => [
|
|
|
|
$user,
|
|
|
|
$password,
|
|
|
|
],
|
|
|
|
'headers' => [
|
|
|
|
'Content-Type' => 'application/xml;charset=UTF-8',
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($this->response->getStatusCode() !== (int)$statusCode) {
|
2016-02-29 19:30:02 +03:00
|
|
|
throw new \Exception(
|
|
|
|
sprintf(
|
|
|
|
'Expected %s got %s',
|
|
|
|
(int)$statusCode,
|
|
|
|
$this->response->getStatusCode()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @When The CardDAV exception is :message
|
2016-03-11 12:42:57 +03:00
|
|
|
* @param string $message
|
|
|
|
* @throws \Exception
|
2016-02-29 19:30:02 +03:00
|
|
|
*/
|
|
|
|
public function theCarddavExceptionIs($message) {
|
|
|
|
$result = $this->responseXml['value'][0]['value'];
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($message !== $result) {
|
2016-02-29 19:30:02 +03:00
|
|
|
throw new \Exception(
|
|
|
|
sprintf(
|
|
|
|
'Expected %s got %s',
|
|
|
|
$message,
|
|
|
|
$result
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @When The CardDAV error message is :arg1
|
2016-03-11 12:42:57 +03:00
|
|
|
* @param string $message
|
|
|
|
* @throws \Exception
|
2016-02-29 19:30:02 +03:00
|
|
|
*/
|
|
|
|
public function theCarddavErrorMessageIs($message) {
|
|
|
|
$result = $this->responseXml['value'][1]['value'];
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($message !== $result) {
|
2016-02-29 19:30:02 +03:00
|
|
|
throw new \Exception(
|
|
|
|
sprintf(
|
|
|
|
'Expected %s got %s',
|
|
|
|
$message,
|
|
|
|
$result
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 19:35:27 +03:00
|
|
|
/**
|
|
|
|
* @Given :user uploads the contact :fileName to the addressbook :addressbook
|
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
public function uploadsTheContactToTheAddressbook($user, $fileName, $addressBook) {
|
2016-12-14 19:35:27 +03:00
|
|
|
$davUrl = $this->baseUrl . '/remote.php/dav/addressbooks/users/'.$user.'/'.$addressBook . '/' . $fileName;
|
|
|
|
$password = ($user === 'admin') ? 'admin' : '123456';
|
|
|
|
|
2018-02-09 15:19:39 +03:00
|
|
|
$this->response = $this->client->request(
|
2016-12-14 19:35:27 +03:00
|
|
|
'PUT',
|
|
|
|
$davUrl,
|
|
|
|
[
|
|
|
|
'body' => file_get_contents(__DIR__ . '/../../data/' . $fileName),
|
|
|
|
'auth' => [
|
|
|
|
$user,
|
|
|
|
$password,
|
|
|
|
],
|
|
|
|
'headers' => [
|
|
|
|
'Content-Type' => 'application/xml;charset=UTF-8',
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($this->response->getStatusCode() !== 201) {
|
2016-12-14 19:35:27 +03:00
|
|
|
throw new \Exception(
|
|
|
|
sprintf(
|
|
|
|
'Expected %s got %s',
|
|
|
|
201,
|
|
|
|
$this->response->getStatusCode()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @When Exporting the picture of contact :fileName from addressbook :addressBook as user :user
|
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
public function whenExportingThePictureOfContactFromAddressbookAsUser($fileName, $addressBook, $user) {
|
2016-12-14 19:35:27 +03:00
|
|
|
$davUrl = $this->baseUrl . '/remote.php/dav/addressbooks/users/'.$user.'/'.$addressBook . '/' . $fileName . '?photo=true';
|
|
|
|
$password = ($user === 'admin') ? 'admin' : '123456';
|
|
|
|
|
|
|
|
try {
|
2018-02-09 15:19:39 +03:00
|
|
|
$this->response = $this->client->request(
|
2016-12-14 19:35:27 +03:00
|
|
|
'GET',
|
|
|
|
$davUrl,
|
|
|
|
[
|
|
|
|
'auth' => [
|
|
|
|
$user,
|
|
|
|
$password,
|
|
|
|
],
|
|
|
|
'headers' => [
|
|
|
|
'Content-Type' => 'application/xml;charset=UTF-8',
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) {
|
|
|
|
$this->response = $e->getResponse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @When Downloading the contact :fileName from addressbook :addressBook as user :user
|
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
public function whenDownloadingTheContactFromAddressbookAsUser($fileName, $addressBook, $user) {
|
2016-12-14 19:35:27 +03:00
|
|
|
$davUrl = $this->baseUrl . '/remote.php/dav/addressbooks/users/'.$user.'/'.$addressBook . '/' . $fileName;
|
|
|
|
$password = ($user === 'admin') ? 'admin' : '123456';
|
|
|
|
|
|
|
|
try {
|
2018-02-09 15:19:39 +03:00
|
|
|
$this->response = $this->client->request(
|
2016-12-14 19:35:27 +03:00
|
|
|
'GET',
|
|
|
|
$davUrl,
|
|
|
|
[
|
|
|
|
'auth' => [
|
|
|
|
$user,
|
|
|
|
$password,
|
|
|
|
],
|
|
|
|
'headers' => [
|
|
|
|
'Content-Type' => 'application/xml;charset=UTF-8',
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) {
|
2020-04-10 15:19:56 +03:00
|
|
|
$this->response = $e->getResponse();
|
2016-12-14 19:35:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Then The following HTTP headers should be set
|
|
|
|
* @param \Behat\Gherkin\Node\TableNode $table
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function theFollowingHttpHeadersShouldBeSet(\Behat\Gherkin\Node\TableNode $table) {
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($table->getTable() as $header) {
|
2016-12-14 19:35:27 +03:00
|
|
|
$headerName = $header[0];
|
|
|
|
$expectedHeaderValue = $header[1];
|
2018-02-09 15:19:39 +03:00
|
|
|
$returnedHeader = $this->response->getHeader($headerName)[0];
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($returnedHeader !== $expectedHeaderValue) {
|
2016-12-14 19:35:27 +03:00
|
|
|
throw new \Exception(
|
|
|
|
sprintf(
|
|
|
|
"Expected value '%s' for header '%s', got '%s'",
|
|
|
|
$expectedHeaderValue,
|
|
|
|
$headerName,
|
|
|
|
$returnedHeader
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-29 19:30:02 +03:00
|
|
|
}
|