1
0

ManagerTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\Calendar;
  7. use OC\AppFramework\Bootstrap\Coordinator;
  8. use OC\Calendar\Manager;
  9. use OCP\AppFramework\Utility\ITimeFactory;
  10. use OCP\Calendar\ICalendar;
  11. use OCP\Calendar\ICreateFromString;
  12. use OCP\Calendar\IHandleImipMessage;
  13. use PHPUnit\Framework\MockObject\MockObject;
  14. use Psr\Container\ContainerInterface;
  15. use Psr\Log\LoggerInterface;
  16. use Sabre\VObject\Document;
  17. use Sabre\VObject\Reader;
  18. use Test\TestCase;
  19. /*
  20. * This allows us to create Mock object supporting both interfaces
  21. */
  22. interface ICreateFromStringAndHandleImipMessage extends ICreateFromString, IHandleImipMessage {
  23. }
  24. class ManagerTest extends TestCase {
  25. /** @var Coordinator|MockObject */
  26. private $coordinator;
  27. /** @var MockObject|ContainerInterface */
  28. private $container;
  29. /** @var MockObject|LoggerInterface */
  30. private $logger;
  31. /** @var Manager */
  32. private $manager;
  33. /** @var ITimeFactory|ITimeFactory&MockObject|MockObject */
  34. private $time;
  35. protected function setUp(): void {
  36. parent::setUp();
  37. $this->coordinator = $this->createMock(Coordinator::class);
  38. $this->container = $this->createMock(ContainerInterface::class);
  39. $this->logger = $this->createMock(LoggerInterface::class);
  40. $this->time = $this->createMock(ITimeFactory::class);
  41. $this->manager = new Manager(
  42. $this->coordinator,
  43. $this->container,
  44. $this->logger,
  45. $this->time,
  46. );
  47. }
  48. /**
  49. * @dataProvider searchProvider
  50. */
  51. public function testSearch($search1, $search2, $expected) {
  52. /** @var ICalendar | MockObject $calendar1 */
  53. $calendar1 = $this->createMock(ICalendar::class);
  54. $calendar1->method('getKey')->willReturn('simple:1');
  55. $calendar1->expects($this->once())
  56. ->method('search')
  57. ->with('', [], [], null, null)
  58. ->willReturn($search1);
  59. /** @var ICalendar | MockObject $calendar2 */
  60. $calendar2 = $this->createMock(ICalendar::class);
  61. $calendar2->method('getKey')->willReturn('simple:2');
  62. $calendar2->expects($this->once())
  63. ->method('search')
  64. ->with('', [], [], null, null)
  65. ->willReturn($search2);
  66. $this->manager->registerCalendar($calendar1);
  67. $this->manager->registerCalendar($calendar2);
  68. $result = $this->manager->search('');
  69. $this->assertEquals($expected, $result);
  70. }
  71. /**
  72. * @dataProvider searchProvider
  73. */
  74. public function testSearchOptions($search1, $search2, $expected) {
  75. /** @var ICalendar | MockObject $calendar1 */
  76. $calendar1 = $this->createMock(ICalendar::class);
  77. $calendar1->method('getKey')->willReturn('simple:1');
  78. $calendar1->expects($this->once())
  79. ->method('search')
  80. ->with('searchTerm', ['SUMMARY', 'DESCRIPTION'],
  81. ['timerange' => ['start' => null, 'end' => null]], 5, 20)
  82. ->willReturn($search1);
  83. /** @var ICalendar | MockObject $calendar2 */
  84. $calendar2 = $this->createMock(ICalendar::class);
  85. $calendar2->method('getKey')->willReturn('simple:2');
  86. $calendar2->expects($this->once())
  87. ->method('search')
  88. ->with('searchTerm', ['SUMMARY', 'DESCRIPTION'],
  89. ['timerange' => ['start' => null, 'end' => null]], 5, 20)
  90. ->willReturn($search2);
  91. $this->manager->registerCalendar($calendar1);
  92. $this->manager->registerCalendar($calendar2);
  93. $result = $this->manager->search('searchTerm', ['SUMMARY', 'DESCRIPTION'],
  94. ['timerange' => ['start' => null, 'end' => null]], 5, 20);
  95. $this->assertEquals($expected, $result);
  96. }
  97. public function searchProvider() {
  98. $search1 = [
  99. [
  100. 'id' => 1,
  101. 'data' => 'foobar',
  102. ],
  103. [
  104. 'id' => 2,
  105. 'data' => 'barfoo',
  106. ]
  107. ];
  108. $search2 = [
  109. [
  110. 'id' => 3,
  111. 'data' => 'blablub',
  112. ],
  113. [
  114. 'id' => 4,
  115. 'data' => 'blubbla',
  116. ]
  117. ];
  118. $expected = [
  119. [
  120. 'id' => 1,
  121. 'data' => 'foobar',
  122. 'calendar-key' => 'simple:1',
  123. ],
  124. [
  125. 'id' => 2,
  126. 'data' => 'barfoo',
  127. 'calendar-key' => 'simple:1',
  128. ],
  129. [
  130. 'id' => 3,
  131. 'data' => 'blablub',
  132. 'calendar-key' => 'simple:2',
  133. ],
  134. [
  135. 'id' => 4,
  136. 'data' => 'blubbla',
  137. 'calendar-key' => 'simple:2',
  138. ]
  139. ];
  140. return [
  141. [
  142. $search1,
  143. $search2,
  144. $expected
  145. ]
  146. ];
  147. }
  148. public function testRegisterUnregister() {
  149. /** @var ICalendar | MockObject $calendar1 */
  150. $calendar1 = $this->createMock(ICalendar::class);
  151. $calendar1->method('getKey')->willReturn('key1');
  152. /** @var ICalendar | MockObject $calendar2 */
  153. $calendar2 = $this->createMock(ICalendar::class);
  154. $calendar2->method('getKey')->willReturn('key2');
  155. $this->manager->registerCalendar($calendar1);
  156. $this->manager->registerCalendar($calendar2);
  157. $result = $this->manager->getCalendars();
  158. $this->assertCount(2, $result);
  159. $this->assertContains($calendar1, $result);
  160. $this->assertContains($calendar2, $result);
  161. $this->manager->unregisterCalendar($calendar1);
  162. $result = $this->manager->getCalendars();
  163. $this->assertCount(1, $result);
  164. $this->assertContains($calendar2, $result);
  165. }
  166. public function testGetCalendars() {
  167. /** @var ICalendar | MockObject $calendar1 */
  168. $calendar1 = $this->createMock(ICalendar::class);
  169. $calendar1->method('getKey')->willReturn('key1');
  170. /** @var ICalendar | MockObject $calendar2 */
  171. $calendar2 = $this->createMock(ICalendar::class);
  172. $calendar2->method('getKey')->willReturn('key2');
  173. $this->manager->registerCalendar($calendar1);
  174. $this->manager->registerCalendar($calendar2);
  175. $result = $this->manager->getCalendars();
  176. $this->assertCount(2, $result);
  177. $this->assertContains($calendar1, $result);
  178. $this->assertContains($calendar2, $result);
  179. $this->manager->clear();
  180. $result = $this->manager->getCalendars();
  181. $this->assertCount(0, $result);
  182. }
  183. public function testEnabledIfNot() {
  184. $isEnabled = $this->manager->isEnabled();
  185. $this->assertFalse($isEnabled);
  186. }
  187. public function testIfEnabledIfSo() {
  188. /** @var ICalendar | MockObject $calendar */
  189. $calendar = $this->createMock(ICalendar::class);
  190. $this->manager->registerCalendar($calendar);
  191. $isEnabled = $this->manager->isEnabled();
  192. $this->assertTrue($isEnabled);
  193. }
  194. public function testHandleImipReplyWrongMethod(): void {
  195. $principalUri = 'principals/user/linus';
  196. $sender = 'pierre@general-store.com';
  197. $recipient = 'linus@stardew-tent-living.com';
  198. $calendarData = $this->getVCalendarReply();
  199. $calendarData->METHOD = 'REQUEST';
  200. $this->logger->expects(self::once())
  201. ->method('warning');
  202. $this->time->expects(self::never())
  203. ->method('getTime');
  204. $result = $this->manager->handleIMipReply($principalUri, $sender, $recipient, $calendarData->serialize());
  205. $this->assertFalse($result);
  206. }
  207. public function testHandleImipReplyOrganizerNotRecipient(): void {
  208. $principalUri = 'principals/user/linus';
  209. $recipient = 'pierre@general-store.com';
  210. $sender = 'linus@stardew-tent-living.com';
  211. $calendarData = $this->getVCalendarReply();
  212. $this->logger->expects(self::once())
  213. ->method('warning');
  214. $this->time->expects(self::never())
  215. ->method('getTime');
  216. $result = $this->manager->handleIMipReply($principalUri, $sender, $recipient, $calendarData->serialize());
  217. $this->assertFalse($result);
  218. }
  219. public function testHandleImipReplyDateInThePast(): void {
  220. $principalUri = 'principals/user/linus';
  221. $sender = 'pierre@general-store.com';
  222. $recipient = 'linus@stardew-tent-living.com';
  223. $calendarData = $this->getVCalendarReply();
  224. $calendarData->VEVENT->DTSTART = new \DateTime('2013-04-07'); // set to in the past
  225. $this->time->expects(self::once())
  226. ->method('getTime')
  227. ->willReturn(time());
  228. $this->logger->expects(self::once())
  229. ->method('warning');
  230. $result = $this->manager->handleIMipReply($principalUri, $sender, $recipient, $calendarData->serialize());
  231. $this->assertFalse($result);
  232. }
  233. public function testHandleImipReplyNoCalendars(): void {
  234. /** @var Manager | \PHPUnit\Framework\MockObject\MockObject $manager */
  235. $manager = $this->getMockBuilder(Manager::class)
  236. ->setConstructorArgs([
  237. $this->coordinator,
  238. $this->container,
  239. $this->logger,
  240. $this->time
  241. ])
  242. ->setMethods([
  243. 'getCalendarsForPrincipal'
  244. ])
  245. ->getMock();
  246. $principalUri = 'principals/user/linus';
  247. $sender = 'pierre@general-store.com';
  248. $recipient = 'linus@stardew-tent-living.com';
  249. $calendarData = $this->getVCalendarReply();
  250. $this->time->expects(self::once())
  251. ->method('getTime')
  252. ->willReturn(1628374233);
  253. $manager->expects(self::once())
  254. ->method('getCalendarsForPrincipal')
  255. ->willReturn([]);
  256. $this->logger->expects(self::once())
  257. ->method('warning');
  258. $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendarData->serialize());
  259. $this->assertFalse($result);
  260. }
  261. public function testHandleImipReplyEventNotFound(): void {
  262. /** @var Manager | \PHPUnit\Framework\MockObject\MockObject $manager */
  263. $manager = $this->getMockBuilder(Manager::class)
  264. ->setConstructorArgs([
  265. $this->coordinator,
  266. $this->container,
  267. $this->logger,
  268. $this->time
  269. ])
  270. ->setMethods([
  271. 'getCalendarsForPrincipal'
  272. ])
  273. ->getMock();
  274. $calendar = $this->createMock(ICreateFromStringAndHandleImipMessage::class);
  275. $principalUri = 'principals/user/linus';
  276. $sender = 'pierre@general-store.com';
  277. $recipient = 'linus@stardew-tent-living.com';
  278. $calendarData = $this->getVCalendarReply();
  279. $this->time->expects(self::once())
  280. ->method('getTime')
  281. ->willReturn(1628374233);
  282. $manager->expects(self::once())
  283. ->method('getCalendarsForPrincipal')
  284. ->willReturn([$calendar]);
  285. $calendar->expects(self::once())
  286. ->method('search')
  287. ->willReturn([]);
  288. $this->logger->expects(self::once())
  289. ->method('info');
  290. $calendar->expects(self::never())
  291. ->method('handleIMipMessage');
  292. $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendarData->serialize());
  293. $this->assertFalse($result);
  294. }
  295. public function testHandleImipReply(): void {
  296. /** @var Manager | \PHPUnit\Framework\MockObject\MockObject $manager */
  297. $manager = $this->getMockBuilder(Manager::class)
  298. ->setConstructorArgs([
  299. $this->coordinator,
  300. $this->container,
  301. $this->logger,
  302. $this->time
  303. ])
  304. ->setMethods([
  305. 'getCalendarsForPrincipal'
  306. ])
  307. ->getMock();
  308. $calendar = $this->createMock(ICreateFromStringAndHandleImipMessage::class);
  309. $principalUri = 'principals/user/linus';
  310. $sender = 'pierre@general-store.com';
  311. $recipient = 'linus@stardew-tent-living.com';
  312. $calendarData = $this->getVCalendarReply();
  313. $this->time->expects(self::once())
  314. ->method('getTime')
  315. ->willReturn(1628374233);
  316. $manager->expects(self::once())
  317. ->method('getCalendarsForPrincipal')
  318. ->willReturn([$calendar]);
  319. $calendar->expects(self::once())
  320. ->method('search')
  321. ->willReturn([['uri' => 'testname.ics']]);
  322. $calendar->expects(self::once())
  323. ->method('handleIMipMessage')
  324. ->with('testname.ics', $calendarData->serialize());
  325. $result = $manager->handleIMipReply($principalUri, $sender, $recipient, $calendarData->serialize());
  326. $this->assertTrue($result);
  327. }
  328. public function testHandleImipCancelWrongMethod(): void {
  329. $principalUri = 'principals/user/pierre';
  330. $sender = 'linus@stardew-tent-living.com';
  331. $recipient = 'pierre@general-store.com';
  332. $replyTo = null;
  333. $calendarData = $this->getVCalendarCancel();
  334. $calendarData->METHOD = 'REQUEST';
  335. $this->logger->expects(self::once())
  336. ->method('warning');
  337. $this->time->expects(self::never())
  338. ->method('getTime');
  339. $result = $this->manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize());
  340. $this->assertFalse($result);
  341. }
  342. public function testHandleImipCancelAttendeeNotRecipient(): void {
  343. $principalUri = '/user/admin';
  344. $sender = 'linus@stardew-tent-living.com';
  345. $recipient = 'leah@general-store.com';
  346. $replyTo = null;
  347. $calendarData = $this->getVCalendarCancel();
  348. $this->logger->expects(self::once())
  349. ->method('warning');
  350. $this->time->expects(self::never())
  351. ->method('getTime');
  352. $result = $this->manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize());
  353. $this->assertFalse($result);
  354. }
  355. public function testHandleImipCancelDateInThePast(): void {
  356. $principalUri = 'principals/user/pierre';
  357. $sender = 'linus@stardew-tent-living.com';
  358. $recipient = 'pierre@general-store.com';
  359. $replyTo = null;
  360. $calendarData = $this->getVCalendarCancel();
  361. $calendarData->VEVENT->DTSTART = new \DateTime('2013-04-07'); // set to in the past
  362. $this->time->expects(self::once())
  363. ->method('getTime')
  364. ->willReturn(time());
  365. $this->logger->expects(self::once())
  366. ->method('warning');
  367. $result = $this->manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize());
  368. $this->assertFalse($result);
  369. }
  370. public function testHandleImipCancelNoCalendars(): void {
  371. /** @var Manager | \PHPUnit\Framework\MockObject\MockObject $manager */
  372. $manager = $this->getMockBuilder(Manager::class)
  373. ->setConstructorArgs([
  374. $this->coordinator,
  375. $this->container,
  376. $this->logger,
  377. $this->time
  378. ])
  379. ->setMethods([
  380. 'getCalendarsForPrincipal'
  381. ])
  382. ->getMock();
  383. $principalUri = 'principals/user/pierre';
  384. $sender = 'linus@stardew-tent-living.com';
  385. $recipient = 'pierre@general-store.com';
  386. $replyTo = null;
  387. $calendarData = $this->getVCalendarCancel();
  388. $this->time->expects(self::once())
  389. ->method('getTime')
  390. ->willReturn(1628374233);
  391. $manager->expects(self::once())
  392. ->method('getCalendarsForPrincipal')
  393. ->with($principalUri)
  394. ->willReturn([]);
  395. $this->logger->expects(self::once())
  396. ->method('warning');
  397. $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize());
  398. $this->assertFalse($result);
  399. }
  400. public function testHandleImipCancelOrganiserInReplyTo(): void {
  401. /** @var Manager | \PHPUnit\Framework\MockObject\MockObject $manager */
  402. $manager = $this->getMockBuilder(Manager::class)
  403. ->setConstructorArgs([
  404. $this->coordinator,
  405. $this->container,
  406. $this->logger,
  407. $this->time
  408. ])
  409. ->setMethods([
  410. 'getCalendarsForPrincipal'
  411. ])
  412. ->getMock();
  413. $principalUri = 'principals/user/pierre';
  414. $sender = 'clint@stardew-blacksmiths.com';
  415. $recipient = 'pierre@general-store.com';
  416. $replyTo = 'linus@stardew-tent-living.com';
  417. $calendar = $this->createMock(ICreateFromStringAndHandleImipMessage::class);
  418. $calendarData = $this->getVCalendarCancel();
  419. $this->time->expects(self::once())
  420. ->method('getTime')
  421. ->willReturn(1628374233);
  422. $manager->expects(self::once())
  423. ->method('getCalendarsForPrincipal')
  424. ->with($principalUri)
  425. ->willReturn([$calendar]);
  426. $calendar->expects(self::once())
  427. ->method('search')
  428. ->willReturn([['uri' => 'testname.ics']]);
  429. $calendar->expects(self::once())
  430. ->method('handleIMipMessage')
  431. ->with('testname.ics', $calendarData->serialize());
  432. $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize());
  433. $this->assertTrue($result);
  434. }
  435. public function testHandleImipCancel(): void {
  436. /** @var Manager | \PHPUnit\Framework\MockObject\MockObject $manager */
  437. $manager = $this->getMockBuilder(Manager::class)
  438. ->setConstructorArgs([
  439. $this->coordinator,
  440. $this->container,
  441. $this->logger,
  442. $this->time
  443. ])
  444. ->setMethods([
  445. 'getCalendarsForPrincipal'
  446. ])
  447. ->getMock();
  448. $principalUri = 'principals/user/pierre';
  449. $sender = 'linus@stardew-tent-living.com';
  450. $recipient = 'pierre@general-store.com';
  451. $replyTo = null;
  452. $calendar = $this->createMock(ICreateFromStringAndHandleImipMessage::class);
  453. $calendarData = $this->getVCalendarCancel();
  454. $this->time->expects(self::once())
  455. ->method('getTime')
  456. ->willReturn(1628374233);
  457. $manager->expects(self::once())
  458. ->method('getCalendarsForPrincipal')
  459. ->with($principalUri)
  460. ->willReturn([$calendar]);
  461. $calendar->expects(self::once())
  462. ->method('search')
  463. ->willReturn([['uri' => 'testname.ics']]);
  464. $calendar->expects(self::once())
  465. ->method('handleIMipMessage')
  466. ->with('testname.ics', $calendarData->serialize());
  467. $result = $manager->handleIMipCancel($principalUri, $sender, $replyTo, $recipient, $calendarData->serialize());
  468. $this->assertTrue($result);
  469. }
  470. private function getVCalendarReply(): Document {
  471. $data = <<<EOF
  472. BEGIN:VCALENDAR
  473. PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
  474. VERSION:2.0
  475. CALSCALE:GREGORIAN
  476. METHOD:REPLY
  477. BEGIN:VEVENT
  478. DTSTART;VALUE=DATE:20210820
  479. DTEND;VALUE=DATE:20220821
  480. DTSTAMP:20210812T100040Z
  481. ORGANIZER;CN=admin:mailto:linus@stardew-tent-living.com
  482. UID:dcc733bf-b2b2-41f2-a8cf-550ae4b67aff
  483. ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;CN=pierr
  484. e@general-store.com;X-NUM-GUESTS=0:mailto:pierre@general-store.com
  485. CREATED:20220812T100021Z
  486. DESCRIPTION:
  487. LAST-MODIFIED:20220812T100040Z
  488. LOCATION:
  489. SEQUENCE:3
  490. STATUS:CONFIRMED
  491. SUMMARY:berry basket
  492. TRANSP:OPAQUE
  493. END:VEVENT
  494. END:VCALENDAR
  495. EOF;
  496. return Reader::read($data);
  497. }
  498. private function getVCalendarCancel(): Document {
  499. $data = <<<EOF
  500. BEGIN:VCALENDAR
  501. PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
  502. VERSION:2.0
  503. CALSCALE:GREGORIAN
  504. METHOD:CANCEL
  505. BEGIN:VEVENT
  506. DTSTART;VALUE=DATE:20210820
  507. DTEND;VALUE=DATE:20220821
  508. DTSTAMP:20210812T100040Z
  509. ORGANIZER;CN=admin:mailto:linus@stardew-tent-living.com
  510. UID:dcc733bf-b2b2-41f2-a8cf-550ae4b67aff
  511. ATTENDEE;CUTYPE=INDIVIDUAL;ROLE=REQ-PARTICIPANT;PARTSTAT=ACCEPTED;CN=pierr
  512. e@general-store.com;X-NUM-GUESTS=0:mailto:pierre@general-store.com
  513. CREATED:20220812T100021Z
  514. DESCRIPTION:
  515. LAST-MODIFIED:20220812T100040Z
  516. LOCATION:
  517. SEQUENCE:3
  518. STATUS:CANCELLED
  519. SUMMARY:berry basket
  520. TRANSP:OPAQUE
  521. END:VEVENT
  522. END:VCALENDAR
  523. EOF;
  524. return Reader::read($data);
  525. }
  526. }