CalDavContext.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 Psr\Http\Message\ResponseInterface;
  30. class CalDavContext implements \Behat\Behat\Context\Context {
  31. /** @var string */
  32. private $baseUrl;
  33. /** @var Client */
  34. private $client;
  35. /** @var ResponseInterface */
  36. private $response;
  37. /** @var string */
  38. private $responseXml = '';
  39. /**
  40. * @param string $baseUrl
  41. */
  42. public function __construct($baseUrl) {
  43. $this->baseUrl = $baseUrl;
  44. // in case of ci deployment we take the server url from the environment
  45. $testServerUrl = getenv('TEST_SERVER_URL');
  46. if ($testServerUrl !== false) {
  47. $this->baseUrl = substr($testServerUrl, 0, -5);
  48. }
  49. }
  50. /** @BeforeScenario */
  51. public function setUpScenario() {
  52. $this->client = new Client();
  53. $this->responseXml = '';
  54. }
  55. /** @AfterScenario */
  56. public function afterScenario() {
  57. $davUrl = $this->baseUrl. '/remote.php/dav/calendars/admin/MyCalendar';
  58. try {
  59. $this->client->delete(
  60. $davUrl,
  61. [
  62. 'auth' => [
  63. 'admin',
  64. 'admin',
  65. ],
  66. ]
  67. );
  68. } catch (\GuzzleHttp\Exception\ClientException $e) {
  69. }
  70. }
  71. /**
  72. * @When :user requests calendar :calendar on the endpoint :endpoint
  73. * @param string $user
  74. * @param string $calendar
  75. * @param string $endpoint
  76. */
  77. public function requestsCalendar($user, $calendar, $endpoint) {
  78. $davUrl = $this->baseUrl . $endpoint . $calendar;
  79. $password = ($user === 'admin') ? 'admin' : '123456';
  80. try {
  81. $this->response = $this->client->request(
  82. 'PROPFIND',
  83. $davUrl,
  84. [
  85. 'auth' => [
  86. $user,
  87. $password,
  88. ],
  89. ]
  90. );
  91. } catch (\GuzzleHttp\Exception\ClientException $e) {
  92. $this->response = $e->getResponse();
  93. }
  94. }
  95. /**
  96. * @Then The CalDAV HTTP status code should be :code
  97. * @param int $code
  98. * @throws \Exception
  99. */
  100. public function theCaldavHttpStatusCodeShouldBe($code) {
  101. if ((int)$code !== $this->response->getStatusCode()) {
  102. throw new \Exception(
  103. sprintf(
  104. 'Expected %s got %s',
  105. (int)$code,
  106. $this->response->getStatusCode()
  107. )
  108. );
  109. }
  110. $body = $this->response->getBody()->getContents();
  111. if ($body && substr($body, 0, 1) === '<') {
  112. $reader = new Sabre\Xml\Reader();
  113. $reader->xml($body);
  114. $this->responseXml = $reader->parse();
  115. }
  116. }
  117. /**
  118. * @Then The exception is :message
  119. * @param string $message
  120. * @throws \Exception
  121. */
  122. public function theExceptionIs($message) {
  123. $result = $this->responseXml['value'][0]['value'];
  124. if ($message !== $result) {
  125. throw new \Exception(
  126. sprintf(
  127. 'Expected %s got %s',
  128. $message,
  129. $result
  130. )
  131. );
  132. }
  133. }
  134. /**
  135. * @Then The error message is :message
  136. * @param string $message
  137. * @throws \Exception
  138. */
  139. public function theErrorMessageIs($message) {
  140. $result = $this->responseXml['value'][1]['value'];
  141. if ($message !== $result) {
  142. throw new \Exception(
  143. sprintf(
  144. 'Expected %s got %s',
  145. $message,
  146. $result
  147. )
  148. );
  149. }
  150. }
  151. /**
  152. * @Given :user creates a calendar named :name
  153. * @param string $user
  154. * @param string $name
  155. */
  156. public function createsACalendarNamed($user, $name) {
  157. $davUrl = $this->baseUrl . '/remote.php/dav/calendars/'.$user.'/'.$name;
  158. $password = ($user === 'admin') ? 'admin' : '123456';
  159. $this->response = $this->client->request(
  160. 'MKCALENDAR',
  161. $davUrl,
  162. [
  163. '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>',
  164. 'auth' => [
  165. $user,
  166. $password,
  167. ],
  168. ]
  169. );
  170. }
  171. /**
  172. * @Then :user publicly shares the calendar named :name
  173. *
  174. * @param string $user
  175. * @param string $name
  176. */
  177. public function publiclySharesTheCalendarNamed($user, $name) {
  178. $davUrl = $this->baseUrl . '/remote.php/dav/calendars/'.$user.'/'.$name;
  179. $password = ($user === 'admin') ? 'admin' : '123456';
  180. $this->response = $this->client->request(
  181. 'POST',
  182. $davUrl,
  183. [
  184. 'body' => '<cs:publish-calendar xmlns:cs="http://calendarserver.org/ns/"/>',
  185. 'auth' => [
  186. $user,
  187. $password,
  188. ],
  189. 'headers' => [
  190. 'Content-Type' => 'application/xml; charset=UTF-8',
  191. ],
  192. ]
  193. );
  194. }
  195. /**
  196. * @Then There should be :amount calendars in the response body
  197. *
  198. * @param string $amount
  199. */
  200. public function t($amount) {
  201. $jsonEncoded = json_encode($this->responseXml);
  202. $arrayElement = json_decode($jsonEncoded, true);
  203. $actual = count($arrayElement['value']) - 1;
  204. if ($actual !== (int)$amount) {
  205. throw new InvalidArgumentException(
  206. sprintf(
  207. 'Expected %s got %s',
  208. $amount,
  209. $actual
  210. )
  211. );
  212. }
  213. }
  214. }