1
0

CalDavContext.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Phil Davis <phil.davis@inf.org>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. require __DIR__ . '/../../vendor/autoload.php';
  28. use GuzzleHttp\Client;
  29. use GuzzleHttp\Exception\GuzzleException;
  30. use Psr\Http\Message\ResponseInterface;
  31. class CalDavContext implements \Behat\Behat\Context\Context {
  32. /** @var string */
  33. private $baseUrl;
  34. /** @var Client */
  35. private $client;
  36. /** @var ResponseInterface */
  37. private $response;
  38. /** @var string */
  39. private $responseXml = '';
  40. /**
  41. * @param string $baseUrl
  42. */
  43. public function __construct($baseUrl) {
  44. $this->baseUrl = $baseUrl;
  45. // in case of ci deployment we take the server url from the environment
  46. $testServerUrl = getenv('TEST_SERVER_URL');
  47. if ($testServerUrl !== false) {
  48. $this->baseUrl = substr($testServerUrl, 0, -5);
  49. }
  50. }
  51. /** @BeforeScenario */
  52. public function setUpScenario() {
  53. $this->client = new Client();
  54. $this->responseXml = '';
  55. }
  56. /** @AfterScenario */
  57. public function afterScenario() {
  58. $davUrl = $this->baseUrl. '/remote.php/dav/calendars/admin/MyCalendar';
  59. try {
  60. $this->client->delete(
  61. $davUrl,
  62. [
  63. 'auth' => [
  64. 'admin',
  65. 'admin',
  66. ],
  67. 'headers' => [
  68. 'X-NC-CalDAV-No-Trashbin' => '1',
  69. ]
  70. ]
  71. );
  72. } catch (\GuzzleHttp\Exception\ClientException $e) {
  73. }
  74. }
  75. /**
  76. * @When :user requests calendar :calendar on the endpoint :endpoint
  77. * @param string $user
  78. * @param string $calendar
  79. * @param string $endpoint
  80. */
  81. public function requestsCalendar($user, $calendar, $endpoint) {
  82. $davUrl = $this->baseUrl . $endpoint . $calendar;
  83. $password = ($user === 'admin') ? 'admin' : '123456';
  84. try {
  85. $this->response = $this->client->request(
  86. 'PROPFIND',
  87. $davUrl,
  88. [
  89. 'auth' => [
  90. $user,
  91. $password,
  92. ],
  93. ]
  94. );
  95. } catch (\GuzzleHttp\Exception\ClientException $e) {
  96. $this->response = $e->getResponse();
  97. }
  98. }
  99. /**
  100. * @Then The CalDAV HTTP status code should be :code
  101. * @param int $code
  102. * @throws \Exception
  103. */
  104. public function theCaldavHttpStatusCodeShouldBe($code) {
  105. if ((int)$code !== $this->response->getStatusCode()) {
  106. throw new \Exception(
  107. sprintf(
  108. 'Expected %s got %s',
  109. (int)$code,
  110. $this->response->getStatusCode()
  111. )
  112. );
  113. }
  114. $body = $this->response->getBody()->getContents();
  115. if ($body && substr($body, 0, 1) === '<') {
  116. $reader = new Sabre\Xml\Reader();
  117. $reader->xml($body);
  118. $this->responseXml = $reader->parse();
  119. }
  120. }
  121. /**
  122. * @Then The exception is :message
  123. * @param string $message
  124. * @throws \Exception
  125. */
  126. public function theExceptionIs($message) {
  127. $result = $this->responseXml['value'][0]['value'];
  128. if ($message !== $result) {
  129. throw new \Exception(
  130. sprintf(
  131. 'Expected %s got %s',
  132. $message,
  133. $result
  134. )
  135. );
  136. }
  137. }
  138. /**
  139. * @Then The error message is :message
  140. * @param string $message
  141. * @throws \Exception
  142. */
  143. public function theErrorMessageIs($message) {
  144. $result = $this->responseXml['value'][1]['value'];
  145. if ($message !== $result) {
  146. throw new \Exception(
  147. sprintf(
  148. 'Expected %s got %s',
  149. $message,
  150. $result
  151. )
  152. );
  153. }
  154. }
  155. /**
  156. * @Given :user creates a calendar named :name
  157. * @param string $user
  158. * @param string $name
  159. */
  160. public function createsACalendarNamed($user, $name) {
  161. $davUrl = $this->baseUrl . '/remote.php/dav/calendars/'.$user.'/'.$name;
  162. $password = ($user === 'admin') ? 'admin' : '123456';
  163. $this->response = $this->client->request(
  164. 'MKCALENDAR',
  165. $davUrl,
  166. [
  167. '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>',
  168. 'auth' => [
  169. $user,
  170. $password,
  171. ],
  172. ]
  173. );
  174. }
  175. /**
  176. * @Then :user publicly shares the calendar named :name
  177. *
  178. * @param string $user
  179. * @param string $name
  180. */
  181. public function publiclySharesTheCalendarNamed($user, $name) {
  182. $davUrl = $this->baseUrl . '/remote.php/dav/calendars/'.$user.'/'.$name;
  183. $password = ($user === 'admin') ? 'admin' : '123456';
  184. $this->response = $this->client->request(
  185. 'POST',
  186. $davUrl,
  187. [
  188. 'body' => '<cs:publish-calendar xmlns:cs="http://calendarserver.org/ns/"/>',
  189. 'auth' => [
  190. $user,
  191. $password,
  192. ],
  193. 'headers' => [
  194. 'Content-Type' => 'application/xml; charset=UTF-8',
  195. ],
  196. ]
  197. );
  198. }
  199. /**
  200. * @Then There should be :amount calendars in the response body
  201. *
  202. * @param string $amount
  203. */
  204. public function t($amount) {
  205. $jsonEncoded = json_encode($this->responseXml);
  206. $arrayElement = json_decode($jsonEncoded, true);
  207. $actual = count($arrayElement['value']) - 1;
  208. if ($actual !== (int)$amount) {
  209. throw new InvalidArgumentException(
  210. sprintf(
  211. 'Expected %s got %s',
  212. $amount,
  213. $actual
  214. )
  215. );
  216. }
  217. }
  218. /**
  219. * @When :user sends a create calendar request to :calendar on the endpoint :endpoint
  220. */
  221. public function sendsCreateCalendarRequest(string $user, string $calendar, string $endpoint) {
  222. $davUrl = $this->baseUrl . $endpoint . $calendar;
  223. $password = ($user === 'admin') ? 'admin' : '123456';
  224. try {
  225. $this->response = $this->client->request(
  226. 'MKCALENDAR',
  227. $davUrl,
  228. [
  229. '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>',
  230. 'auth' => [
  231. $user,
  232. $password,
  233. ],
  234. ]
  235. );
  236. } catch (GuzzleException $e) {
  237. $this->response = $e->getResponse();
  238. }
  239. }
  240. }