You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CalDavContext.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. * @author Phil Davis <phil.davis@inf.org>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. require __DIR__ . '/../../vendor/autoload.php';
  25. use GuzzleHttp\Client;
  26. use Psr\Http\Message\ResponseInterface;
  27. class CalDavContext implements \Behat\Behat\Context\Context {
  28. /** @var string */
  29. private $baseUrl;
  30. /** @var Client */
  31. private $client;
  32. /** @var ResponseInterface */
  33. private $response;
  34. /** @var string */
  35. private $responseXml = '';
  36. /**
  37. * @param string $baseUrl
  38. */
  39. public function __construct($baseUrl) {
  40. $this->baseUrl = $baseUrl;
  41. // in case of ci deployment we take the server url from the environment
  42. $testServerUrl = getenv('TEST_SERVER_URL');
  43. if ($testServerUrl !== false) {
  44. $this->baseUrl = substr($testServerUrl, 0, -5);
  45. }
  46. }
  47. /** @BeforeScenario */
  48. public function setUpScenario() {
  49. $this->client = new Client();
  50. $this->responseXml = '';
  51. }
  52. /** @AfterScenario */
  53. public function afterScenario() {
  54. $davUrl = $this->baseUrl. '/remote.php/dav/calendars/admin/MyCalendar';
  55. try {
  56. $this->client->delete(
  57. $davUrl,
  58. [
  59. 'auth' => [
  60. 'admin',
  61. 'admin',
  62. ],
  63. ]
  64. );
  65. } catch (\GuzzleHttp\Exception\ClientException $e) {}
  66. }
  67. /**
  68. * @When :user requests calendar :calendar on the endpoint :endpoint
  69. * @param string $user
  70. * @param string $calendar
  71. * @param string $endpoint
  72. */
  73. public function requestsCalendar($user, $calendar, $endpoint) {
  74. $davUrl = $this->baseUrl . $endpoint . $calendar;
  75. $password = ($user === 'admin') ? 'admin' : '123456';
  76. try {
  77. $this->response = $this->client->request(
  78. 'PROPFIND',
  79. $davUrl,
  80. [
  81. 'auth' => [
  82. $user,
  83. $password,
  84. ],
  85. ]
  86. );
  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. $this->response = $this->client->request(
  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. }
  167. /**
  168. * @Then :user publicly shares the calendar named :name
  169. *
  170. * @param string $user
  171. * @param string $name
  172. */
  173. public function publiclySharesTheCalendarNamed($user, $name) {
  174. $davUrl = $this->baseUrl . '/remote.php/dav/calendars/'.$user.'/'.$name;
  175. $password = ($user === 'admin') ? 'admin' : '123456';
  176. $this->response = $this->client->request(
  177. 'POST',
  178. $davUrl,
  179. [
  180. 'body' => '<cs:publish-calendar xmlns:cs="http://calendarserver.org/ns/"/>',
  181. 'auth' => [
  182. $user,
  183. $password,
  184. ],
  185. 'headers' => [
  186. 'Content-Type' => 'application/xml; charset=UTF-8',
  187. ],
  188. ]
  189. );
  190. }
  191. /**
  192. * @Then There should be :amount calendars in the response body
  193. *
  194. * @param string $amount
  195. */
  196. public function t($amount) {
  197. $jsonEncoded = json_encode($this->responseXml);
  198. $arrayElement = json_decode($jsonEncoded, true);
  199. $actual = count($arrayElement['value']) - 1;
  200. if($actual !== (int)$amount) {
  201. throw new InvalidArgumentException(
  202. sprintf(
  203. 'Expected %s got %s',
  204. $amount,
  205. $actual
  206. )
  207. );
  208. }
  209. }
  210. }