123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- <?php
- /**
- * 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';
- use GuzzleHttp\Client;
- use GuzzleHttp\Exception\GuzzleException;
- 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 */
- public function setUpScenario() {
- $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',
- ],
- ]
- );
- } catch (\GuzzleHttp\Exception\ClientException $e) {
- }
- }
- /**
- * @When :user requests addressbook :addressBook with statuscode :statusCode on the endpoint :endpoint
- * @param string $user
- * @param string $addressBook
- * @param int $statusCode
- * @param string $endpoint
- * @throws \Exception
- */
- public function requestsAddressbookWithStatuscodeOnTheEndpoint($user, $addressBook, $statusCode, $endpoint) {
- $davUrl = $this->baseUrl . $endpoint . $addressBook;
- $password = ($user === 'admin') ? 'admin' : '123456';
- try {
- $this->response = $this->client->request(
- 'PROPFIND',
- $davUrl,
- [
- 'auth' => [
- $user,
- $password,
- ],
- ]
- );
- } catch (\GuzzleHttp\Exception\ClientException $e) {
- $this->response = $e->getResponse();
- }
- if ((int)$statusCode !== $this->response->getStatusCode()) {
- throw new \Exception(
- sprintf(
- 'Expected %s got %s',
- (int)$statusCode,
- $this->response->getStatusCode()
- )
- );
- }
- $body = $this->response->getBody()->getContents();
- if (substr($body, 0, 1) === '<') {
- $reader = new Sabre\Xml\Reader();
- $reader->xml($body);
- $this->responseXml = $reader->parse();
- }
- }
- /**
- * @Given :user creates an addressbook named :addressBook with statuscode :statusCode
- * @param string $user
- * @param string $addressBook
- * @param int $statusCode
- * @throws \Exception
- */
- public function createsAnAddressbookNamedWithStatuscode($user, $addressBook, $statusCode) {
- $davUrl = $this->baseUrl . '/remote.php/dav/addressbooks/users/'.$user.'/'.$addressBook;
- $password = ($user === 'admin') ? 'admin' : '123456';
- $this->response = $this->client->request(
- 'MKCOL',
- $davUrl,
- [
- 'body' => '<d:mkcol xmlns:card="urn:ietf:params:xml:ns:carddav"
- 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',
- ],
- ]
- );
- if ($this->response->getStatusCode() !== (int)$statusCode) {
- throw new \Exception(
- sprintf(
- 'Expected %s got %s',
- (int)$statusCode,
- $this->response->getStatusCode()
- )
- );
- }
- }
- /**
- * @When The CardDAV exception is :message
- * @param string $message
- * @throws \Exception
- */
- public function theCarddavExceptionIs($message) {
- $result = $this->responseXml['value'][0]['value'];
- if ($message !== $result) {
- throw new \Exception(
- sprintf(
- 'Expected %s got %s',
- $message,
- $result
- )
- );
- }
- }
- /**
- * @When The CardDAV error message is :arg1
- * @param string $message
- * @throws \Exception
- */
- public function theCarddavErrorMessageIs($message) {
- $result = $this->responseXml['value'][1]['value'];
- if ($message !== $result) {
- throw new \Exception(
- sprintf(
- 'Expected %s got %s',
- $message,
- $result
- )
- );
- }
- }
- /**
- * @Given :user uploads the contact :fileName to the addressbook :addressbook
- */
- public function uploadsTheContactToTheAddressbook($user, $fileName, $addressBook) {
- $davUrl = $this->baseUrl . '/remote.php/dav/addressbooks/users/'.$user.'/'.$addressBook . '/' . $fileName;
- $password = ($user === 'admin') ? 'admin' : '123456';
- $this->response = $this->client->request(
- 'PUT',
- $davUrl,
- [
- 'body' => file_get_contents(__DIR__ . '/../../data/' . $fileName),
- 'auth' => [
- $user,
- $password,
- ],
- 'headers' => [
- 'Content-Type' => 'application/xml;charset=UTF-8',
- ],
- ]
- );
- if ($this->response->getStatusCode() !== 201) {
- 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
- */
- public function whenExportingThePictureOfContactFromAddressbookAsUser($fileName, $addressBook, $user) {
- $davUrl = $this->baseUrl . '/remote.php/dav/addressbooks/users/'.$user.'/'.$addressBook . '/' . $fileName . '?photo=true';
- $password = ($user === 'admin') ? 'admin' : '123456';
- try {
- $this->response = $this->client->request(
- '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
- */
- public function whenDownloadingTheContactFromAddressbookAsUser($fileName, $addressBook, $user) {
- $davUrl = $this->baseUrl . '/remote.php/dav/addressbooks/users/'.$user.'/'.$addressBook . '/' . $fileName;
- $password = ($user === 'admin') ? 'admin' : '123456';
- try {
- $this->response = $this->client->request(
- 'GET',
- $davUrl,
- [
- 'auth' => [
- $user,
- $password,
- ],
- 'headers' => [
- 'Content-Type' => 'application/xml;charset=UTF-8',
- ],
- ]
- );
- } catch (\GuzzleHttp\Exception\ClientException $e) {
- $this->response = $e->getResponse();
- }
- }
- /**
- * @Then The following HTTP headers should be set
- * @param \Behat\Gherkin\Node\TableNode $table
- * @throws \Exception
- */
- public function theFollowingHttpHeadersShouldBeSet(\Behat\Gherkin\Node\TableNode $table) {
- foreach ($table->getTable() as $header) {
- $headerName = $header[0];
- $expectedHeaderValue = $header[1];
- $returnedHeader = $this->response->getHeader($headerName)[0];
- if ($returnedHeader !== $expectedHeaderValue) {
- throw new \Exception(
- sprintf(
- "Expected value '%s' for header '%s', got '%s'",
- $expectedHeaderValue,
- $headerName,
- $returnedHeader
- )
- );
- }
- }
- }
- /**
- * @When :user sends a create addressbook request to :addressbook on the endpoint :endpoint
- */
- public function sendsCreateAddressbookRequest(string $user, string $addressbook, string $endpoint) {
- $davUrl = $this->baseUrl . $endpoint . $addressbook;
- $password = ($user === 'admin') ? 'admin' : '123456';
- try {
- $this->response = $this->client->request(
- 'MKCOL',
- $davUrl,
- [
- 'body' => '<d:mkcol xmlns:card="urn:ietf:params:xml:ns:carddav"
- 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',
- ],
- ]
- );
- } catch (GuzzleException $e) {
- $this->response = $e->getResponse();
- }
- }
- /**
- * @Then The CardDAV HTTP status code should be :code
- * @param int $code
- * @throws \Exception
- */
- public function theCarddavHttpStatusCodeShouldBe($code) {
- if ((int)$code !== $this->response->getStatusCode()) {
- throw new \Exception(
- sprintf(
- 'Expected %s got %s',
- (int)$code,
- $this->response->getStatusCode()
- )
- );
- }
- $body = $this->response->getBody()->getContents();
- if ($body && substr($body, 0, 1) === '<') {
- $reader = new Sabre\Xml\Reader();
- $reader->xml($body);
- $this->responseXml = $reader->parse();
- }
- }
- }
|