PluginTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Citharel <nextcloud@tcit.fr>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  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
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\DAV\Tests\unit\CalDAV\Schedule;
  28. use OCA\DAV\CalDAV\CalDavBackend;
  29. use OCA\DAV\CalDAV\Calendar;
  30. use OCA\DAV\CalDAV\CalendarHome;
  31. use OCA\DAV\CalDAV\Plugin as CalDAVPlugin;
  32. use OCA\DAV\CalDAV\Schedule\Plugin;
  33. use OCA\DAV\CalDAV\Trashbin\Plugin as TrashbinPlugin;
  34. use OCP\IConfig;
  35. use OCP\IL10N;
  36. use PHPUnit\Framework\MockObject\MockObject;
  37. use Psr\Log\LoggerInterface;
  38. use Sabre\DAV\PropFind;
  39. use Sabre\DAV\Server;
  40. use Sabre\DAV\Tree;
  41. use Sabre\DAV\Xml\Property\Href;
  42. use Sabre\DAV\Xml\Property\LocalHref;
  43. use Sabre\DAVACL\IPrincipal;
  44. use Sabre\HTTP\ResponseInterface;
  45. use Sabre\VObject\Parameter;
  46. use Sabre\VObject\Property\ICalendar\CalAddress;
  47. use Sabre\Xml\Service;
  48. use Test\TestCase;
  49. class PluginTest extends TestCase {
  50. /** @var Plugin */
  51. private $plugin;
  52. /** @var Server|MockObject */
  53. private $server;
  54. /** @var IConfig|MockObject */
  55. private $config;
  56. protected function setUp(): void {
  57. parent::setUp();
  58. $this->server = $this->createMock(Server::class);
  59. $this->config = $this->createMock(IConfig::class);
  60. $response = $this->getMockBuilder(ResponseInterface::class)
  61. ->disableOriginalConstructor()
  62. ->getMock();
  63. $this->server->httpResponse = $response;
  64. $this->server->xml = new Service();
  65. $this->plugin = new Plugin($this->config);
  66. $this->plugin->initialize($this->server);
  67. }
  68. public function testInitialize() {
  69. $plugin = new Plugin($this->config);
  70. $this->server->expects($this->exactly(10))
  71. ->method('on')
  72. ->withConsecutive(
  73. // Sabre\CalDAV\Schedule\Plugin events
  74. ['method:POST', [$plugin, 'httpPost']],
  75. ['propFind', [$plugin, 'propFind']],
  76. ['propPatch', [$plugin, 'propPatch']],
  77. ['calendarObjectChange', [$plugin, 'calendarObjectChange']],
  78. ['beforeUnbind', [$plugin, 'beforeUnbind']],
  79. ['schedule', [$plugin, 'scheduleLocalDelivery']],
  80. ['getSupportedPrivilegeSet', [$plugin, 'getSupportedPrivilegeSet']],
  81. // OCA\DAV\CalDAV\Schedule\Plugin events
  82. ['propFind', [$plugin, 'propFindDefaultCalendarUrl'], 90],
  83. ['afterWriteContent', [$plugin, 'dispatchSchedulingResponses']],
  84. ['afterCreateFile', [$plugin, 'dispatchSchedulingResponses']]
  85. );
  86. $plugin->initialize($this->server);
  87. }
  88. public function testGetAddressesForPrincipal() {
  89. $href = $this->createMock(Href::class);
  90. $href
  91. ->expects($this->once())
  92. ->method('getHrefs')
  93. ->willReturn(['lukas@nextcloud.com', 'rullzer@nextcloud.com']);
  94. $this->server
  95. ->expects($this->once())
  96. ->method('getProperties')
  97. ->with(
  98. 'MyPrincipal',
  99. [
  100. '{urn:ietf:params:xml:ns:caldav}calendar-user-address-set',
  101. ]
  102. )
  103. ->willReturn([
  104. '{urn:ietf:params:xml:ns:caldav}calendar-user-address-set' => $href
  105. ]);
  106. $result = $this->invokePrivate($this->plugin, 'getAddressesForPrincipal', ['MyPrincipal']);
  107. $this->assertSame(['lukas@nextcloud.com', 'rullzer@nextcloud.com'], $result);
  108. }
  109. public function testGetAddressesForPrincipalEmpty() {
  110. $this->server
  111. ->expects($this->once())
  112. ->method('getProperties')
  113. ->with(
  114. 'MyPrincipal',
  115. [
  116. '{urn:ietf:params:xml:ns:caldav}calendar-user-address-set',
  117. ]
  118. )
  119. ->willReturn(null);
  120. $result = $this->invokePrivate($this->plugin, 'getAddressesForPrincipal', ['MyPrincipal']);
  121. $this->assertSame([], $result);
  122. }
  123. public function testStripOffMailTo() {
  124. $this->assertEquals('test@example.com', $this->invokePrivate($this->plugin, 'stripOffMailTo', ['test@example.com']));
  125. $this->assertEquals('test@example.com', $this->invokePrivate($this->plugin, 'stripOffMailTo', ['mailto:test@example.com']));
  126. }
  127. public function testGetAttendeeRSVP() {
  128. $property1 = $this->createMock(CalAddress::class);
  129. $parameter1 = $this->createMock(Parameter::class);
  130. $property1->expects($this->once())
  131. ->method('offsetGet')
  132. ->with('RSVP')
  133. ->willReturn($parameter1);
  134. $parameter1->expects($this->once())
  135. ->method('getValue')
  136. ->with()
  137. ->willReturn('TRUE');
  138. $property2 = $this->createMock(CalAddress::class);
  139. $parameter2 = $this->createMock(Parameter::class);
  140. $property2->expects($this->once())
  141. ->method('offsetGet')
  142. ->with('RSVP')
  143. ->willReturn($parameter2);
  144. $parameter2->expects($this->once())
  145. ->method('getValue')
  146. ->with()
  147. ->willReturn('FALSE');
  148. $property3 = $this->createMock(CalAddress::class);
  149. $property3->expects($this->once())
  150. ->method('offsetGet')
  151. ->with('RSVP')
  152. ->willReturn(null);
  153. $this->assertTrue($this->invokePrivate($this->plugin, 'getAttendeeRSVP', [$property1]));
  154. $this->assertFalse($this->invokePrivate($this->plugin, 'getAttendeeRSVP', [$property2]));
  155. $this->assertFalse($this->invokePrivate($this->plugin, 'getAttendeeRSVP', [$property3]));
  156. }
  157. public function propFindDefaultCalendarUrlProvider(): array {
  158. return [
  159. [
  160. 'principals/users/myuser',
  161. 'calendars/myuser',
  162. false,
  163. CalDavBackend::PERSONAL_CALENDAR_URI,
  164. CalDavBackend::PERSONAL_CALENDAR_NAME,
  165. true
  166. ],
  167. [
  168. 'principals/users/myuser',
  169. 'calendars/myuser',
  170. false,
  171. CalDavBackend::PERSONAL_CALENDAR_URI,
  172. CalDavBackend::PERSONAL_CALENDAR_NAME,
  173. false,
  174. true
  175. ],
  176. [
  177. 'principals/users/myuser',
  178. 'calendars/myuser',
  179. false,
  180. CalDavBackend::PERSONAL_CALENDAR_URI,
  181. CalDavBackend::PERSONAL_CALENDAR_NAME,
  182. false
  183. ],
  184. [
  185. 'principals/users/myuser',
  186. null,
  187. false,
  188. CalDavBackend::PERSONAL_CALENDAR_URI,
  189. CalDavBackend::PERSONAL_CALENDAR_NAME,
  190. true
  191. ],
  192. [
  193. 'principals/users/myuser',
  194. 'calendars/myuser',
  195. false,
  196. CalDavBackend::PERSONAL_CALENDAR_URI,
  197. CalDavBackend::PERSONAL_CALENDAR_NAME,
  198. true,
  199. false,
  200. false,
  201. ],
  202. [
  203. 'principals/users/myuser',
  204. 'calendars/myuser',
  205. false,
  206. 'my_other_calendar',
  207. 'My Other Calendar',
  208. true
  209. ],
  210. [
  211. 'principals/calendar-resources',
  212. 'system-calendars/calendar-resources/myuser',
  213. true,
  214. CalDavBackend::RESOURCE_BOOKING_CALENDAR_URI,
  215. CalDavBackend::RESOURCE_BOOKING_CALENDAR_NAME,
  216. true
  217. ],
  218. [
  219. 'principals/calendar-resources',
  220. 'system-calendars/calendar-resources/myuser',
  221. true,
  222. CalDavBackend::RESOURCE_BOOKING_CALENDAR_URI,
  223. CalDavBackend::RESOURCE_BOOKING_CALENDAR_NAME,
  224. false
  225. ],
  226. [
  227. 'principals/something-else',
  228. 'calendars/whatever',
  229. false,
  230. CalDavBackend::PERSONAL_CALENDAR_URI,
  231. CalDavBackend::PERSONAL_CALENDAR_NAME,
  232. true
  233. ],
  234. ];
  235. }
  236. /**
  237. * @dataProvider propFindDefaultCalendarUrlProvider
  238. * @param string $principalUri
  239. * @param string|null $calendarHome
  240. * @param bool $isResource
  241. * @param string $calendarUri
  242. * @param string $displayName
  243. * @param bool $exists
  244. * @param bool $propertiesForPath
  245. */
  246. public function testPropFindDefaultCalendarUrl(string $principalUri, ?string $calendarHome, bool $isResource, string $calendarUri, string $displayName, bool $exists, bool $hasExistingCalendars = false, bool $propertiesForPath = true) {
  247. /** @var PropFind $propFind */
  248. $propFind = new PropFind(
  249. $principalUri,
  250. [
  251. Plugin::SCHEDULE_DEFAULT_CALENDAR_URL
  252. ],
  253. 0
  254. );
  255. /** @var IPrincipal|MockObject $node */
  256. $node = $this->getMockBuilder(IPrincipal::class)
  257. ->disableOriginalConstructor()
  258. ->getMock();
  259. $node->expects($this->once())
  260. ->method('getPrincipalUrl')
  261. ->with()
  262. ->willReturn($principalUri);
  263. $calDAVPlugin = $this->getMockBuilder(CalDAVPlugin::class)
  264. ->disableOriginalConstructor()
  265. ->getMock();
  266. $calDAVPlugin->expects($this->once())
  267. ->method('getCalendarHomeForPrincipal')
  268. ->willReturn($calendarHome);
  269. $this->server->expects($this->once())
  270. ->method('getPlugin')
  271. ->with('caldav')
  272. ->willReturn($calDAVPlugin);
  273. if (!$calendarHome) {
  274. $this->plugin->propFindDefaultCalendarUrl($propFind, $node);
  275. $this->assertNull($propFind->get(Plugin::SCHEDULE_DEFAULT_CALENDAR_URL));
  276. return;
  277. }
  278. if ($principalUri === 'principals/something-else') {
  279. $this->plugin->propFindDefaultCalendarUrl($propFind, $node);
  280. $this->assertNull($propFind->get(Plugin::SCHEDULE_DEFAULT_CALENDAR_URL));
  281. return;
  282. }
  283. if (!$isResource) {
  284. $this->config->expects($this->once())
  285. ->method('getUserValue')
  286. ->with('myuser', 'dav', 'defaultCalendar', CalDavBackend::PERSONAL_CALENDAR_URI)
  287. ->willReturn($calendarUri);
  288. }
  289. $calendarHomeObject = $this->createMock(CalendarHome::class);
  290. $calendarHomeObject->expects($this->once())
  291. ->method('childExists')
  292. ->with($calendarUri)
  293. ->willReturn($exists);
  294. $calendarBackend = $this->createMock(CalDavBackend::class);
  295. $calendarUri = $hasExistingCalendars ? 'custom' : $calendarUri;
  296. $displayName = $hasExistingCalendars ? 'Custom Calendar' : $displayName;
  297. $existingCalendars = $hasExistingCalendars ? [
  298. new Calendar(
  299. $calendarBackend,
  300. ['uri' => 'deleted', '{DAV:}displayname' => 'A deleted calendar', TrashbinPlugin::PROPERTY_DELETED_AT => 42],
  301. $this->createMock(IL10N::class),
  302. $this->config,
  303. $this->createMock(LoggerInterface::class)
  304. ),
  305. new Calendar(
  306. $calendarBackend,
  307. ['uri' => $calendarUri, '{DAV:}displayname' => $displayName],
  308. $this->createMock(IL10N::class),
  309. $this->config,
  310. $this->createMock(LoggerInterface::class)
  311. )
  312. ] : [];
  313. if (!$exists) {
  314. if (!$hasExistingCalendars) {
  315. $calendarBackend->expects($this->once())
  316. ->method('createCalendar')
  317. ->with($principalUri, $calendarUri, [
  318. '{DAV:}displayname' => $displayName,
  319. ]);
  320. $calendarHomeObject->expects($this->once())
  321. ->method('getCalDAVBackend')
  322. ->with()
  323. ->willReturn($calendarBackend);
  324. }
  325. if (!$isResource) {
  326. $calendarHomeObject->expects($this->once())
  327. ->method('getChildren')
  328. ->with()
  329. ->willReturn($existingCalendars);
  330. }
  331. }
  332. /** @var Tree|MockObject $tree */
  333. $tree = $this->createMock(Tree::class);
  334. $tree->expects($this->once())
  335. ->method('getNodeForPath')
  336. ->with($calendarHome)
  337. ->willReturn($calendarHomeObject);
  338. $this->server->tree = $tree;
  339. $properties = $propertiesForPath ? [
  340. ['href' => '/remote.php/dav/' . $calendarHome . '/' . $calendarUri]
  341. ] : [];
  342. $this->server->expects($this->once())
  343. ->method('getPropertiesForPath')
  344. ->with($calendarHome .'/' . $calendarUri, [], 1)
  345. ->willReturn($properties);
  346. $this->plugin->propFindDefaultCalendarUrl($propFind, $node);
  347. if (!$propertiesForPath) {
  348. $this->assertNull($propFind->get(Plugin::SCHEDULE_DEFAULT_CALENDAR_URL));
  349. return;
  350. }
  351. /** @var LocalHref $result */
  352. $result = $propFind->get(Plugin::SCHEDULE_DEFAULT_CALENDAR_URL);
  353. $this->assertEquals('/remote.php/dav/'. $calendarHome . '/' . $calendarUri, $result->getHref());
  354. }
  355. }