2016-02-29 19:30:02 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2016-07-21 19:13:36 +03:00
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
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;
|
2019-06-03 16:24:26 +03:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2016-02-29 19:30:02 +03:00
|
|
|
|
|
|
|
class CalDavContext 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/calendars/admin/MyCalendar';
|
|
|
|
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 calendar :calendar on the endpoint :endpoint
|
2016-03-11 12:39:42 +03:00
|
|
|
* @param string $user
|
|
|
|
* @param string $calendar
|
2017-02-10 16:01:26 +03:00
|
|
|
* @param string $endpoint
|
2016-02-29 19:30:02 +03:00
|
|
|
*/
|
2020-04-09 14:53:40 +03:00
|
|
|
public function requestsCalendar($user, $calendar, $endpoint) {
|
2017-02-10 16:01:26 +03:00
|
|
|
$davUrl = $this->baseUrl . $endpoint . $calendar;
|
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,
|
2017-04-05 23:43:05 +03:00
|
|
|
],
|
2016-02-29 19:30:02 +03:00
|
|
|
]
|
|
|
|
);
|
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $e) {
|
|
|
|
$this->response = $e->getResponse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Then The CalDAV HTTP status code should be :code
|
2016-03-11 12:39:42 +03:00
|
|
|
* @param int $code
|
|
|
|
* @throws \Exception
|
2016-02-29 19:30:02 +03:00
|
|
|
*/
|
|
|
|
public function theCaldavHttpStatusCodeShouldBe($code) {
|
2020-04-10 15:19:56 +03:00
|
|
|
if ((int)$code !== $this->response->getStatusCode()) {
|
2016-02-29 19:30:02 +03:00
|
|
|
throw new \Exception(
|
|
|
|
sprintf(
|
|
|
|
'Expected %s got %s',
|
|
|
|
(int)$code,
|
|
|
|
$this->response->getStatusCode()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$body = $this->response->getBody()->getContents();
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($body && substr($body, 0, 1) === '<') {
|
2016-02-29 19:30:02 +03:00
|
|
|
$reader = new Sabre\Xml\Reader();
|
|
|
|
$reader->xml($body);
|
|
|
|
$this->responseXml = $reader->parse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Then The exception is :message
|
2016-03-11 12:39:42 +03:00
|
|
|
* @param string $message
|
|
|
|
* @throws \Exception
|
2016-02-29 19:30:02 +03:00
|
|
|
*/
|
|
|
|
public function theExceptionIs($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
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Then The error message is :message
|
2016-03-11 12:39:42 +03:00
|
|
|
* @param string $message
|
|
|
|
* @throws \Exception
|
2016-02-29 19:30:02 +03:00
|
|
|
*/
|
|
|
|
public function theErrorMessageIs($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
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Given :user creates a calendar named :name
|
2016-03-11 12:39:42 +03:00
|
|
|
* @param string $user
|
|
|
|
* @param string $name
|
2016-02-29 19:30:02 +03:00
|
|
|
*/
|
|
|
|
public function createsACalendarNamed($user, $name) {
|
|
|
|
$davUrl = $this->baseUrl . '/remote.php/dav/calendars/'.$user.'/'.$name;
|
|
|
|
$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
|
|
|
'MKCALENDAR',
|
|
|
|
$davUrl,
|
|
|
|
[
|
|
|
|
'body' => '<c:mkcalendar xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:" xmlns:a="http://apple.com/ns/ical/" xmlns:o="http://owncloud.org/ns"><d:set><d:prop><d:displayname>test</d:displayname><o:calendar-enabled>1</o:calendar-enabled><a:calendar-color>#21213D</a:calendar-color><c:supported-calendar-component-set><c:comp name="VEVENT"/></c:supported-calendar-component-set></d:prop></d:set></c:mkcalendar>',
|
|
|
|
'auth' => [
|
|
|
|
$user,
|
|
|
|
$password,
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-04-05 23:43:05 +03:00
|
|
|
/**
|
|
|
|
* @Then :user publicly shares the calendar named :name
|
|
|
|
*
|
|
|
|
* @param string $user
|
|
|
|
* @param string $name
|
|
|
|
*/
|
|
|
|
public function publiclySharesTheCalendarNamed($user, $name) {
|
|
|
|
$davUrl = $this->baseUrl . '/remote.php/dav/calendars/'.$user.'/'.$name;
|
|
|
|
$password = ($user === 'admin') ? 'admin' : '123456';
|
|
|
|
|
2018-02-09 15:19:39 +03:00
|
|
|
$this->response = $this->client->request(
|
2017-04-05 23:43:05 +03:00
|
|
|
'POST',
|
|
|
|
$davUrl,
|
|
|
|
[
|
|
|
|
'body' => '<cs:publish-calendar xmlns:cs="http://calendarserver.org/ns/"/>',
|
|
|
|
'auth' => [
|
|
|
|
$user,
|
|
|
|
$password,
|
|
|
|
],
|
|
|
|
'headers' => [
|
|
|
|
'Content-Type' => 'application/xml; charset=UTF-8',
|
|
|
|
],
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Then There should be :amount calendars in the response body
|
|
|
|
*
|
|
|
|
* @param string $amount
|
|
|
|
*/
|
|
|
|
public function t($amount) {
|
|
|
|
$jsonEncoded = json_encode($this->responseXml);
|
|
|
|
$arrayElement = json_decode($jsonEncoded, true);
|
|
|
|
$actual = count($arrayElement['value']) - 1;
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($actual !== (int)$amount) {
|
2017-04-05 23:43:05 +03:00
|
|
|
throw new InvalidArgumentException(
|
|
|
|
sprintf(
|
|
|
|
'Expected %s got %s',
|
|
|
|
$amount,
|
|
|
|
$actual
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2016-02-29 19:30:02 +03:00
|
|
|
}
|