PluginTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. /** @var MockObject|LoggerInterface */
  57. private $logger;
  58. protected function setUp(): void {
  59. parent::setUp();
  60. $this->server = $this->createMock(Server::class);
  61. $this->config = $this->createMock(IConfig::class);
  62. $response = $this->getMockBuilder(ResponseInterface::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $this->server->httpResponse = $response;
  66. $this->server->xml = new Service();
  67. $this->logger = $this->createMock(LoggerInterface::class);
  68. $this->plugin = new Plugin($this->config, $this->logger);
  69. $this->plugin->initialize($this->server);
  70. }
  71. public function testInitialize(): void {
  72. $plugin = new Plugin($this->config, $this->logger);
  73. $this->server->expects($this->exactly(10))
  74. ->method('on')
  75. ->withConsecutive(
  76. // Sabre\CalDAV\Schedule\Plugin events
  77. ['method:POST', [$plugin, 'httpPost']],
  78. ['propFind', [$plugin, 'propFind']],
  79. ['propPatch', [$plugin, 'propPatch']],
  80. ['calendarObjectChange', [$plugin, 'calendarObjectChange']],
  81. ['beforeUnbind', [$plugin, 'beforeUnbind']],
  82. ['schedule', [$plugin, 'scheduleLocalDelivery']],
  83. ['getSupportedPrivilegeSet', [$plugin, 'getSupportedPrivilegeSet']],
  84. // OCA\DAV\CalDAV\Schedule\Plugin events
  85. ['propFind', [$plugin, 'propFindDefaultCalendarUrl'], 90],
  86. ['afterWriteContent', [$plugin, 'dispatchSchedulingResponses']],
  87. ['afterCreateFile', [$plugin, 'dispatchSchedulingResponses']]
  88. );
  89. $plugin->initialize($this->server);
  90. }
  91. public function testGetAddressesForPrincipal(): void {
  92. $href = $this->createMock(Href::class);
  93. $href
  94. ->expects($this->once())
  95. ->method('getHrefs')
  96. ->willReturn(['lukas@nextcloud.com', 'rullzer@nextcloud.com']);
  97. $this->server
  98. ->expects($this->once())
  99. ->method('getProperties')
  100. ->with(
  101. 'MyPrincipal',
  102. [
  103. '{urn:ietf:params:xml:ns:caldav}calendar-user-address-set',
  104. ]
  105. )
  106. ->willReturn([
  107. '{urn:ietf:params:xml:ns:caldav}calendar-user-address-set' => $href
  108. ]);
  109. $result = $this->invokePrivate($this->plugin, 'getAddressesForPrincipal', ['MyPrincipal']);
  110. $this->assertSame(['lukas@nextcloud.com', 'rullzer@nextcloud.com'], $result);
  111. }
  112. public function testGetAddressesForPrincipalEmpty(): void {
  113. $this->server
  114. ->expects($this->once())
  115. ->method('getProperties')
  116. ->with(
  117. 'MyPrincipal',
  118. [
  119. '{urn:ietf:params:xml:ns:caldav}calendar-user-address-set',
  120. ]
  121. )
  122. ->willReturn(null);
  123. $result = $this->invokePrivate($this->plugin, 'getAddressesForPrincipal', ['MyPrincipal']);
  124. $this->assertSame([], $result);
  125. }
  126. public function testStripOffMailTo(): void {
  127. $this->assertEquals('test@example.com', $this->invokePrivate($this->plugin, 'stripOffMailTo', ['test@example.com']));
  128. $this->assertEquals('test@example.com', $this->invokePrivate($this->plugin, 'stripOffMailTo', ['mailto:test@example.com']));
  129. }
  130. public function testGetAttendeeRSVP(): void {
  131. $property1 = $this->createMock(CalAddress::class);
  132. $parameter1 = $this->createMock(Parameter::class);
  133. $property1->expects($this->once())
  134. ->method('offsetGet')
  135. ->with('RSVP')
  136. ->willReturn($parameter1);
  137. $parameter1->expects($this->once())
  138. ->method('getValue')
  139. ->with()
  140. ->willReturn('TRUE');
  141. $property2 = $this->createMock(CalAddress::class);
  142. $parameter2 = $this->createMock(Parameter::class);
  143. $property2->expects($this->once())
  144. ->method('offsetGet')
  145. ->with('RSVP')
  146. ->willReturn($parameter2);
  147. $parameter2->expects($this->once())
  148. ->method('getValue')
  149. ->with()
  150. ->willReturn('FALSE');
  151. $property3 = $this->createMock(CalAddress::class);
  152. $property3->expects($this->once())
  153. ->method('offsetGet')
  154. ->with('RSVP')
  155. ->willReturn(null);
  156. $this->assertTrue($this->invokePrivate($this->plugin, 'getAttendeeRSVP', [$property1]));
  157. $this->assertFalse($this->invokePrivate($this->plugin, 'getAttendeeRSVP', [$property2]));
  158. $this->assertFalse($this->invokePrivate($this->plugin, 'getAttendeeRSVP', [$property3]));
  159. }
  160. public function propFindDefaultCalendarUrlProvider(): array {
  161. return [
  162. [
  163. 'principals/users/myuser',
  164. 'calendars/myuser',
  165. false,
  166. CalDavBackend::PERSONAL_CALENDAR_URI,
  167. CalDavBackend::PERSONAL_CALENDAR_NAME,
  168. true
  169. ],
  170. [
  171. 'principals/users/myuser',
  172. 'calendars/myuser',
  173. false,
  174. CalDavBackend::PERSONAL_CALENDAR_URI,
  175. CalDavBackend::PERSONAL_CALENDAR_NAME,
  176. true,
  177. true
  178. ],
  179. [
  180. 'principals/users/myuser',
  181. 'calendars/myuser',
  182. false,
  183. CalDavBackend::PERSONAL_CALENDAR_URI,
  184. CalDavBackend::PERSONAL_CALENDAR_NAME,
  185. false,
  186. false,
  187. true
  188. ],
  189. [
  190. 'principals/users/myuser',
  191. 'calendars/myuser',
  192. false,
  193. CalDavBackend::PERSONAL_CALENDAR_URI,
  194. CalDavBackend::PERSONAL_CALENDAR_NAME,
  195. false
  196. ],
  197. [
  198. 'principals/users/myuser',
  199. null,
  200. false,
  201. CalDavBackend::PERSONAL_CALENDAR_URI,
  202. CalDavBackend::PERSONAL_CALENDAR_NAME,
  203. true
  204. ],
  205. [
  206. 'principals/users/myuser',
  207. 'calendars/myuser',
  208. false,
  209. CalDavBackend::PERSONAL_CALENDAR_URI,
  210. CalDavBackend::PERSONAL_CALENDAR_NAME,
  211. true,
  212. false,
  213. false,
  214. false,
  215. ],
  216. [
  217. 'principals/users/myuser',
  218. 'calendars/myuser',
  219. false,
  220. 'my_other_calendar',
  221. 'My Other Calendar',
  222. true
  223. ],
  224. [
  225. 'principals/calendar-resources',
  226. 'system-calendars/calendar-resources/myuser',
  227. true,
  228. CalDavBackend::RESOURCE_BOOKING_CALENDAR_URI,
  229. CalDavBackend::RESOURCE_BOOKING_CALENDAR_NAME,
  230. true
  231. ],
  232. [
  233. 'principals/calendar-resources',
  234. 'system-calendars/calendar-resources/myuser',
  235. true,
  236. CalDavBackend::RESOURCE_BOOKING_CALENDAR_URI,
  237. CalDavBackend::RESOURCE_BOOKING_CALENDAR_NAME,
  238. false
  239. ],
  240. [
  241. 'principals/something-else',
  242. 'calendars/whatever',
  243. false,
  244. CalDavBackend::PERSONAL_CALENDAR_URI,
  245. CalDavBackend::PERSONAL_CALENDAR_NAME,
  246. true
  247. ],
  248. ];
  249. }
  250. /**
  251. * @dataProvider propFindDefaultCalendarUrlProvider
  252. */
  253. public function testPropFindDefaultCalendarUrl(string $principalUri, ?string $calendarHome, bool $isResource, string $calendarUri, string $displayName, bool $exists, bool $deleted = false, bool $hasExistingCalendars = false, bool $propertiesForPath = true): void {
  254. $propFind = new PropFind(
  255. $principalUri,
  256. [
  257. Plugin::SCHEDULE_DEFAULT_CALENDAR_URL
  258. ],
  259. 0
  260. );
  261. /** @var IPrincipal|MockObject $node */
  262. $node = $this->getMockBuilder(IPrincipal::class)
  263. ->disableOriginalConstructor()
  264. ->getMock();
  265. $node->expects($this->once())
  266. ->method('getPrincipalUrl')
  267. ->with()
  268. ->willReturn($principalUri);
  269. $calDAVPlugin = $this->getMockBuilder(CalDAVPlugin::class)
  270. ->disableOriginalConstructor()
  271. ->getMock();
  272. $calDAVPlugin->expects($this->once())
  273. ->method('getCalendarHomeForPrincipal')
  274. ->willReturn($calendarHome);
  275. $this->server->expects($this->once())
  276. ->method('getPlugin')
  277. ->with('caldav')
  278. ->willReturn($calDAVPlugin);
  279. if (!$calendarHome) {
  280. $this->plugin->propFindDefaultCalendarUrl($propFind, $node);
  281. $this->assertNull($propFind->get(Plugin::SCHEDULE_DEFAULT_CALENDAR_URL));
  282. return;
  283. }
  284. if ($principalUri === 'principals/something-else') {
  285. $this->plugin->propFindDefaultCalendarUrl($propFind, $node);
  286. $this->assertNull($propFind->get(Plugin::SCHEDULE_DEFAULT_CALENDAR_URL));
  287. return;
  288. }
  289. if (!$isResource) {
  290. $this->config->expects($this->once())
  291. ->method('getUserValue')
  292. ->with('myuser', 'dav', 'defaultCalendar', CalDavBackend::PERSONAL_CALENDAR_URI)
  293. ->willReturn($calendarUri);
  294. }
  295. $calendarHomeObject = $this->createMock(CalendarHome::class);
  296. $calendarHomeObject->expects($this->once())
  297. ->method('childExists')
  298. ->with($calendarUri)
  299. ->willReturn($exists);
  300. if ($exists) {
  301. $calendar = $this->createMock(Calendar::class);
  302. $calendar->expects($this->once())->method('isDeleted')->willReturn($deleted);
  303. $calendarHomeObject->expects($deleted && !$hasExistingCalendars ? $this->exactly(2) : $this->once())->method('getChild')->with($calendarUri)->willReturn($calendar);
  304. }
  305. $calendarBackend = $this->createMock(CalDavBackend::class);
  306. $calendarUri = $hasExistingCalendars ? 'custom' : $calendarUri;
  307. $displayName = $hasExistingCalendars ? 'Custom Calendar' : $displayName;
  308. $existingCalendars = $hasExistingCalendars ? [
  309. new Calendar(
  310. $calendarBackend,
  311. ['uri' => 'deleted', '{DAV:}displayname' => 'A deleted calendar', TrashbinPlugin::PROPERTY_DELETED_AT => 42],
  312. $this->createMock(IL10N::class),
  313. $this->config,
  314. $this->createMock(LoggerInterface::class)
  315. ),
  316. new Calendar(
  317. $calendarBackend,
  318. ['uri' => $calendarUri, '{DAV:}displayname' => $displayName],
  319. $this->createMock(IL10N::class),
  320. $this->config,
  321. $this->createMock(LoggerInterface::class)
  322. )
  323. ] : [];
  324. if (!$exists || $deleted) {
  325. if (!$hasExistingCalendars) {
  326. $calendarBackend->expects($this->once())
  327. ->method('createCalendar')
  328. ->with($principalUri, $calendarUri, [
  329. '{DAV:}displayname' => $displayName,
  330. ]);
  331. $calendarHomeObject->expects($this->once())
  332. ->method('getCalDAVBackend')
  333. ->with()
  334. ->willReturn($calendarBackend);
  335. }
  336. if (!$isResource) {
  337. $calendarHomeObject->expects($this->once())
  338. ->method('getChildren')
  339. ->with()
  340. ->willReturn($existingCalendars);
  341. }
  342. }
  343. /** @var Tree|MockObject $tree */
  344. $tree = $this->createMock(Tree::class);
  345. $tree->expects($this->once())
  346. ->method('getNodeForPath')
  347. ->with($calendarHome)
  348. ->willReturn($calendarHomeObject);
  349. $this->server->tree = $tree;
  350. $properties = $propertiesForPath ? [
  351. ['href' => '/remote.php/dav/' . $calendarHome . '/' . $calendarUri]
  352. ] : [];
  353. $this->server->expects($this->once())
  354. ->method('getPropertiesForPath')
  355. ->with($calendarHome .'/' . $calendarUri, [], 1)
  356. ->willReturn($properties);
  357. $this->plugin->propFindDefaultCalendarUrl($propFind, $node);
  358. if (!$propertiesForPath) {
  359. $this->assertNull($propFind->get(Plugin::SCHEDULE_DEFAULT_CALENDAR_URL));
  360. return;
  361. }
  362. /** @var LocalHref $result */
  363. $result = $propFind->get(Plugin::SCHEDULE_DEFAULT_CALENDAR_URL);
  364. $this->assertEquals('/remote.php/dav/'. $calendarHome . '/' . $calendarUri, $result->getHref());
  365. }
  366. }