|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 4 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 5 | + */ |
| 6 | + |
| 7 | +require __DIR__ . '/../../vendor/autoload.php'; |
| 8 | + |
| 9 | +use Behat\Behat\Context\Context; |
| 10 | +use GuzzleHttp\BodySummarizer; |
| 11 | +use GuzzleHttp\Client; |
| 12 | +use GuzzleHttp\HandlerStack; |
| 13 | +use GuzzleHttp\Middleware; |
| 14 | +use GuzzleHttp\Utils; |
| 15 | +use Psr\Http\Message\ResponseInterface; |
| 16 | + |
| 17 | +class PrincipalPropertySearchContext implements Context { |
| 18 | + private string $baseUrl; |
| 19 | + private Client $client; |
| 20 | + private ResponseInterface $response; |
| 21 | + |
| 22 | + public function __construct(string $baseUrl) { |
| 23 | + $this->baseUrl = $baseUrl; |
| 24 | + |
| 25 | + // in case of ci deployment we take the server url from the environment |
| 26 | + $testServerUrl = getenv('TEST_SERVER_URL'); |
| 27 | + if ($testServerUrl !== false) { |
| 28 | + $this->baseUrl = substr($testServerUrl, 0, -5); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + /** @BeforeScenario */ |
| 33 | + public function setUpScenario(): void { |
| 34 | + $this->client = $this->createGuzzleInstance(); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Create a Guzzle client with a higher truncateAt value to read full error responses. |
| 39 | + */ |
| 40 | + private function createGuzzleInstance(): Client { |
| 41 | + $bodySummarizer = new BodySummarizer(2048); |
| 42 | + |
| 43 | + $stack = new HandlerStack(Utils::chooseHandler()); |
| 44 | + $stack->push(Middleware::httpErrors($bodySummarizer), 'http_errors'); |
| 45 | + $stack->push(Middleware::redirect(), 'allow_redirects'); |
| 46 | + $stack->push(Middleware::cookies(), 'cookies'); |
| 47 | + $stack->push(Middleware::prepareBody(), 'prepare_body'); |
| 48 | + |
| 49 | + return new Client(['handler' => $stack]); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @When searching for a principal matching :match |
| 54 | + * @param string $match |
| 55 | + * @throws \Exception |
| 56 | + */ |
| 57 | + public function principalPropertySearch(string $match) { |
| 58 | + $davUrl = $this->baseUrl . '/remote.php/dav/'; |
| 59 | + $user = 'admin'; |
| 60 | + $password = 'admin'; |
| 61 | + |
| 62 | + $this->response = $this->client->request( |
| 63 | + 'REPORT', |
| 64 | + $davUrl, |
| 65 | + [ |
| 66 | + 'body' => '<x0:principal-property-search xmlns:x0="DAV:" test="anyof"> |
| 67 | + <x0:property-search> |
| 68 | + <x0:prop> |
| 69 | + <x0:displayname/> |
| 70 | + <x2:email-address xmlns:x2="http://sabredav.org/ns"/> |
| 71 | + </x0:prop> |
| 72 | + <x0:match>' . $match . '</x0:match> |
| 73 | + </x0:property-search> |
| 74 | + <x0:prop> |
| 75 | + <x0:displayname/> |
| 76 | + <x1:calendar-user-type xmlns:x1="urn:ietf:params:xml:ns:caldav"/> |
| 77 | + <x1:calendar-user-address-set xmlns:x1="urn:ietf:params:xml:ns:caldav"/> |
| 78 | + <x0:principal-URL/> |
| 79 | + <x0:alternate-URI-set/> |
| 80 | + <x2:email-address xmlns:x2="http://sabredav.org/ns"/> |
| 81 | + <x3:language xmlns:x3="http://nextcloud.com/ns"/> |
| 82 | + <x1:calendar-home-set xmlns:x1="urn:ietf:params:xml:ns:caldav"/> |
| 83 | + <x1:schedule-inbox-URL xmlns:x1="urn:ietf:params:xml:ns:caldav"/> |
| 84 | + <x1:schedule-outbox-URL xmlns:x1="urn:ietf:params:xml:ns:caldav"/> |
| 85 | + <x1:schedule-default-calendar-URL xmlns:x1="urn:ietf:params:xml:ns:caldav"/> |
| 86 | + <x3:resource-type xmlns:x3="http://nextcloud.com/ns"/> |
| 87 | + <x3:resource-vehicle-type xmlns:x3="http://nextcloud.com/ns"/> |
| 88 | + <x3:resource-vehicle-make xmlns:x3="http://nextcloud.com/ns"/> |
| 89 | + <x3:resource-vehicle-model xmlns:x3="http://nextcloud.com/ns"/> |
| 90 | + <x3:resource-vehicle-is-electric xmlns:x3="http://nextcloud.com/ns"/> |
| 91 | + <x3:resource-vehicle-range xmlns:x3="http://nextcloud.com/ns"/> |
| 92 | + <x3:resource-vehicle-seating-capacity xmlns:x3="http://nextcloud.com/ns"/> |
| 93 | + <x3:resource-contact-person xmlns:x3="http://nextcloud.com/ns"/> |
| 94 | + <x3:resource-contact-person-vcard xmlns:x3="http://nextcloud.com/ns"/> |
| 95 | + <x3:room-type xmlns:x3="http://nextcloud.com/ns"/> |
| 96 | + <x3:room-seating-capacity xmlns:x3="http://nextcloud.com/ns"/> |
| 97 | + <x3:room-building-address xmlns:x3="http://nextcloud.com/ns"/> |
| 98 | + <x3:room-building-story xmlns:x3="http://nextcloud.com/ns"/> |
| 99 | + <x3:room-building-room-number xmlns:x3="http://nextcloud.com/ns"/> |
| 100 | + <x3:room-features xmlns:x3="http://nextcloud.com/ns"/> |
| 101 | + </x0:prop> |
| 102 | + <x0:apply-to-principal-collection-set/> |
| 103 | +</x0:principal-property-search> |
| 104 | +', |
| 105 | + 'auth' => [ |
| 106 | + $user, |
| 107 | + $password, |
| 108 | + ], |
| 109 | + 'headers' => [ |
| 110 | + 'Content-Type' => 'application/xml; charset=UTF-8', |
| 111 | + 'Depth' => '0', |
| 112 | + ], |
| 113 | + ] |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @Then The search HTTP status code should be :code |
| 119 | + * @param string $code |
| 120 | + * @throws \Exception |
| 121 | + */ |
| 122 | + public function theHttpStatusCodeShouldBe(string $code): void { |
| 123 | + if ((int)$code !== $this->response->getStatusCode()) { |
| 124 | + throw new \Exception('Expected ' . (int)$code . ' got ' . $this->response->getStatusCode()); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @Then The search response should contain :needle |
| 130 | + * @param string $needle |
| 131 | + * @throws \Exception |
| 132 | + */ |
| 133 | + public function theResponseShouldContain(string $needle): void { |
| 134 | + $body = $this->response->getBody()->getContents(); |
| 135 | + |
| 136 | + if (str_contains($body, $needle) === false) { |
| 137 | + throw new \Exception('Response does not contain "' . $needle . '"'); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments