CalDavContext.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. 'headers' => [
  67. 'X-NC-CalDAV-No-Trashbin' => '1',
  68. ]
  69. ]
  70. );
  71. } catch (\GuzzleHttp\Exception\ClientException $e) {
  72. }
  73. }
  74. /**
  75. * @When :user requests calendar :calendar on the endpoint :endpoint
  76. * @param string $user
  77. * @param string $calendar
  78. * @param string $endpoint
  79. */
  80. public function requestsCalendar($user, $calendar, $endpoint) {
  81. $davUrl = $this->baseUrl . $endpoint . $calendar;
  82. $password = ($user === 'admin') ? 'admin' : '123456';
  83. try {
  84. $this->response = $this->client->request(
  85. 'PROPFIND',
  86. $davUrl,
  87. [
  88. 'auth' => [
  89. $user,
  90. $password,
  91. ],
  92. ]
  93. );
  94. } catch (\GuzzleHttp\Exception\ClientException $e) {
  95. $this->response = $e->getResponse();
  96. }
  97. }
  98. /**
  99. * @Then The CalDAV HTTP status code should be :code
  100. * @param int $code
  101. * @throws \Exception
  102. */
  103. public function theCaldavHttpStatusCodeShouldBe($code) {
  104. if ((int)$code !== $this->response->getStatusCode()) {
  105. throw new \Exception(
  106. sprintf(
  107. 'Expected %s got %s',
  108. (int)$code,
  109. $this->response->getStatusCode()
  110. )
  111. );
  112. }
  113. $body = $this->response->getBody()->getContents();
  114. if ($body && substr($body, 0, 1) === '<') {
  115. $reader = new Sabre\Xml\Reader();
  116. $reader->xml($body);
  117. $this->responseXml = $reader->parse();
  118. }
  119. }
  120. /**
  121. * @Then The exception is :message
  122. * @param string $message
  123. * @throws \Exception
  124. */
  125. public function theExceptionIs($message) {
  126. $result = $this->responseXml['value'][0]['value'];
  127. if ($message !== $result) {
  128. throw new \Exception(
  129. sprintf(
  130. 'Expected %s got %s',
  131. $message,
  132. $result
  133. )
  134. );
  135. }
  136. }
  137. /**
  138. * @Then The error message is :message
  139. * @param string $message
  140. * @throws \Exception
  141. */
  142. public function theErrorMessageIs($message) {
  143. $result = $this->responseXml['value'][1]['value'];
  144. if ($message !== $result) {
  145. throw new \Exception(
  146. sprintf(
  147. 'Expected %s got %s',
  148. $message,
  149. $result
  150. )
  151. );
  152. }
  153. }
  154. /**
  155. * @Given :user creates a calendar named :name
  156. * @param string $user
  157. * @param string $name
  158. */
  159. public function createsACalendarNamed($user, $name) {
  160. $davUrl = $this->baseUrl . '/remote.php/dav/calendars/'.$user.'/'.$name;
  161. $password = ($user === 'admin') ? 'admin' : '123456';
  162. $this->response = $this->client->request(
  163. 'MKCALENDAR',
  164. $davUrl,
  165. [
  166. '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>',
  167. 'auth' => [
  168. $user,
  169. $password,
  170. ],
  171. ]
  172. );
  173. }
  174. /**
  175. * @Then :user publicly shares the calendar named :name
  176. *
  177. * @param string $user
  178. * @param string $name
  179. */
  180. public function publiclySharesTheCalendarNamed($user, $name) {
  181. $davUrl = $this->baseUrl . '/remote.php/dav/calendars/'.$user.'/'.$name;
  182. $password = ($user === 'admin') ? 'admin' : '123456';
  183. $this->response = $this->client->request(
  184. 'POST',
  185. $davUrl,
  186. [
  187. 'body' => '<cs:publish-calendar xmlns:cs="http://calendarserver.org/ns/"/>',
  188. 'auth' => [
  189. $user,
  190. $password,
  191. ],
  192. 'headers' => [
  193. 'Content-Type' => 'application/xml; charset=UTF-8',
  194. ],
  195. ]
  196. );
  197. }
  198. /**
  199. * @Then There should be :amount calendars in the response body
  200. *
  201. * @param string $amount
  202. */
  203. public function t($amount) {
  204. $jsonEncoded = json_encode($this->responseXml);
  205. $arrayElement = json_decode($jsonEncoded, true);
  206. $actual = count($arrayElement['value']) - 1;
  207. if ($actual !== (int)$amount) {
  208. throw new InvalidArgumentException(
  209. sprintf(
  210. 'Expected %s got %s',
  211. $amount,
  212. $actual
  213. )
  214. );
  215. }
  216. }
  217. }