1
0

CalDavContext.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. require __DIR__ . '/../../vendor/autoload.php';
  24. use GuzzleHttp\Client;
  25. use GuzzleHttp\Message\ResponseInterface;
  26. class CalDavContext implements \Behat\Behat\Context\Context {
  27. /** @var string */
  28. private $baseUrl;
  29. /** @var Client */
  30. private $client;
  31. /** @var ResponseInterface */
  32. private $response;
  33. /** @var string */
  34. private $responseXml = '';
  35. /**
  36. * @param string $baseUrl
  37. */
  38. public function __construct($baseUrl) {
  39. $this->baseUrl = $baseUrl;
  40. // in case of ci deployment we take the server url from the environment
  41. $testServerUrl = getenv('TEST_SERVER_URL');
  42. if ($testServerUrl !== false) {
  43. $this->baseUrl = substr($testServerUrl, 0, -5);
  44. }
  45. }
  46. /** @BeforeScenario */
  47. public function setUpScenario() {
  48. $this->client = new Client();
  49. $this->responseXml = '';
  50. }
  51. /** @AfterScenario */
  52. public function afterScenario() {
  53. $davUrl = $this->baseUrl. '/remote.php/dav/calendars/admin/MyCalendar';
  54. try {
  55. $this->client->delete(
  56. $davUrl,
  57. [
  58. 'auth' => [
  59. 'admin',
  60. 'admin',
  61. ],
  62. ]
  63. );
  64. } catch (\GuzzleHttp\Exception\ClientException $e) {}
  65. }
  66. /**
  67. * @When :user requests calendar :calendar on the endpoint :endpoint
  68. * @param string $user
  69. * @param string $calendar
  70. * @param string $endpoint
  71. */
  72. public function requestsCalendar($user, $calendar, $endpoint) {
  73. $davUrl = $this->baseUrl . $endpoint . $calendar;
  74. $password = ($user === 'admin') ? 'admin' : '123456';
  75. try {
  76. $request = $this->client->createRequest(
  77. 'PROPFIND',
  78. $davUrl,
  79. [
  80. 'auth' => [
  81. $user,
  82. $password,
  83. ]
  84. ]
  85. );
  86. $this->response = $this->client->send($request);
  87. } catch (\GuzzleHttp\Exception\ClientException $e) {
  88. $this->response = $e->getResponse();
  89. }
  90. }
  91. /**
  92. * @Then The CalDAV HTTP status code should be :code
  93. * @param int $code
  94. * @throws \Exception
  95. */
  96. public function theCaldavHttpStatusCodeShouldBe($code) {
  97. if((int)$code !== $this->response->getStatusCode()) {
  98. throw new \Exception(
  99. sprintf(
  100. 'Expected %s got %s',
  101. (int)$code,
  102. $this->response->getStatusCode()
  103. )
  104. );
  105. }
  106. $body = $this->response->getBody()->getContents();
  107. if($body && substr($body, 0, 1) === '<') {
  108. $reader = new Sabre\Xml\Reader();
  109. $reader->xml($body);
  110. $this->responseXml = $reader->parse();
  111. }
  112. }
  113. /**
  114. * @Then The exception is :message
  115. * @param string $message
  116. * @throws \Exception
  117. */
  118. public function theExceptionIs($message) {
  119. $result = $this->responseXml['value'][0]['value'];
  120. if($message !== $result) {
  121. throw new \Exception(
  122. sprintf(
  123. 'Expected %s got %s',
  124. $message,
  125. $result
  126. )
  127. );
  128. }
  129. }
  130. /**
  131. * @Then The error message is :message
  132. * @param string $message
  133. * @throws \Exception
  134. */
  135. public function theErrorMessageIs($message) {
  136. $result = $this->responseXml['value'][1]['value'];
  137. if($message !== $result) {
  138. throw new \Exception(
  139. sprintf(
  140. 'Expected %s got %s',
  141. $message,
  142. $result
  143. )
  144. );
  145. }
  146. }
  147. /**
  148. * @Given :user creates a calendar named :name
  149. * @param string $user
  150. * @param string $name
  151. */
  152. public function createsACalendarNamed($user, $name) {
  153. $davUrl = $this->baseUrl . '/remote.php/dav/calendars/'.$user.'/'.$name;
  154. $password = ($user === 'admin') ? 'admin' : '123456';
  155. $request = $this->client->createRequest(
  156. 'MKCALENDAR',
  157. $davUrl,
  158. [
  159. '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>',
  160. 'auth' => [
  161. $user,
  162. $password,
  163. ],
  164. ]
  165. );
  166. $this->response = $this->client->send($request);
  167. }
  168. }