CalendarTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2020, Gary Kim <gary@garykim.dev>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Gary Kim <gary@garykim.dev>
  8. * @author Georg Ehrke <oc.list@georgehrke.com>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\DAV\Tests\unit\CalDAV;
  30. use OCA\DAV\CalDAV\BirthdayService;
  31. use OCA\DAV\CalDAV\CalDavBackend;
  32. use OCA\DAV\CalDAV\Calendar;
  33. use OCP\IConfig;
  34. use OCP\IL10N;
  35. use Sabre\DAV\PropPatch;
  36. use Sabre\VObject\Reader;
  37. use Test\TestCase;
  38. class CalendarTest extends TestCase {
  39. /** @var IL10N */
  40. protected $l10n;
  41. /** @var IConfig */
  42. protected $config;
  43. protected function setUp(): void {
  44. parent::setUp();
  45. $this->l10n = $this->getMockBuilder(IL10N::class)
  46. ->disableOriginalConstructor()->getMock();
  47. $this->config = $this->createMock(IConfig::class);
  48. $this->l10n
  49. ->expects($this->any())
  50. ->method('t')
  51. ->willReturnCallback(function ($text, $parameters = []) {
  52. return vsprintf($text, $parameters);
  53. });
  54. }
  55. public function testDelete() {
  56. /** @var \PHPUnit_Framework_MockObject_MockObject | CalDavBackend $backend */
  57. $backend = $this->getMockBuilder(CalDavBackend::class)->disableOriginalConstructor()->getMock();
  58. $backend->expects($this->once())->method('updateShares');
  59. $backend->expects($this->any())->method('getShares')->willReturn([
  60. ['href' => 'principal:user2']
  61. ]);
  62. $calendarInfo = [
  63. '{http://owncloud.org/ns}owner-principal' => 'user1',
  64. 'principaluri' => 'user2',
  65. 'id' => 666,
  66. 'uri' => 'cal',
  67. ];
  68. $c = new Calendar($backend, $calendarInfo, $this->l10n, $this->config);
  69. $c->delete();
  70. }
  71. public function testDeleteFromGroup() {
  72. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  73. /** @var \PHPUnit_Framework_MockObject_MockObject | CalDavBackend $backend */
  74. $backend = $this->getMockBuilder(CalDavBackend::class)->disableOriginalConstructor()->getMock();
  75. $backend->expects($this->never())->method('updateShares');
  76. $backend->expects($this->any())->method('getShares')->willReturn([
  77. ['href' => 'principal:group2']
  78. ]);
  79. $calendarInfo = [
  80. '{http://owncloud.org/ns}owner-principal' => 'user1',
  81. 'principaluri' => 'user2',
  82. 'id' => 666,
  83. 'uri' => 'cal',
  84. ];
  85. $c = new Calendar($backend, $calendarInfo, $this->l10n, $this->config);
  86. $c->delete();
  87. }
  88. public function testDeleteOwn() {
  89. /** @var \PHPUnit_Framework_MockObject_MockObject | CalDavBackend $backend */
  90. $backend = $this->createMock(CalDavBackend::class);
  91. $backend->expects($this->never())->method('updateShares');
  92. $backend->expects($this->never())->method('getShares');
  93. $this->config->expects($this->never())->method('setUserValue');
  94. $backend->expects($this->once())->method('deleteCalendar')
  95. ->with(666);
  96. $calendarInfo = [
  97. '{http://owncloud.org/ns}owner-principal' => 'user1',
  98. 'principaluri' => 'user1',
  99. 'id' => 666,
  100. 'uri' => 'cal',
  101. ];
  102. $c = new Calendar($backend, $calendarInfo, $this->l10n, $this->config);
  103. $c->delete();
  104. }
  105. public function testDeleteBirthdayCalendar() {
  106. /** @var \PHPUnit_Framework_MockObject_MockObject | CalDavBackend $backend */
  107. $backend = $this->createMock(CalDavBackend::class);
  108. $backend->expects($this->once())->method('deleteCalendar')
  109. ->with(666);
  110. $this->config->expects($this->once())
  111. ->method('setUserValue')
  112. ->with('user1', 'dav', 'generateBirthdayCalendar', 'no');
  113. $calendarInfo = [
  114. '{http://owncloud.org/ns}owner-principal' => 'principals/users/user1',
  115. 'principaluri' => 'principals/users/user1',
  116. 'id' => 666,
  117. 'uri' => 'contact_birthdays',
  118. ];
  119. $c = new Calendar($backend, $calendarInfo, $this->l10n, $this->config);
  120. $c->delete();
  121. }
  122. public function dataPropPatch() {
  123. return [
  124. ['user1', 'user2', [], true],
  125. ['user1', 'user2', [
  126. '{http://owncloud.org/ns}calendar-enabled' => true,
  127. ], true],
  128. ['user1', 'user2', [
  129. '{DAV:}displayname' => true,
  130. ], true],
  131. ['user1', 'user2', [
  132. '{DAV:}displayname' => true,
  133. '{http://owncloud.org/ns}calendar-enabled' => true,
  134. ], true],
  135. ['user1', 'user1', [], false],
  136. ['user1', 'user1', [
  137. '{http://owncloud.org/ns}calendar-enabled' => true,
  138. ], false],
  139. ['user1', 'user1', [
  140. '{DAV:}displayname' => true,
  141. ], false],
  142. ['user1', 'user1', [
  143. '{DAV:}displayname' => true,
  144. '{http://owncloud.org/ns}calendar-enabled' => true,
  145. ], false],
  146. ];
  147. }
  148. /**
  149. * @dataProvider dataPropPatch
  150. */
  151. public function testPropPatch($ownerPrincipal, $principalUri, $mutations, $shared) {
  152. /** @var \PHPUnit_Framework_MockObject_MockObject | CalDavBackend $backend */
  153. $backend = $this->getMockBuilder(CalDavBackend::class)->disableOriginalConstructor()->getMock();
  154. $calendarInfo = [
  155. '{http://owncloud.org/ns}owner-principal' => $ownerPrincipal,
  156. 'principaluri' => $principalUri,
  157. 'id' => 666,
  158. 'uri' => 'default'
  159. ];
  160. $c = new Calendar($backend, $calendarInfo, $this->l10n, $this->config);
  161. $propPatch = new PropPatch($mutations);
  162. if (!$shared) {
  163. $backend->expects($this->once())
  164. ->method('updateCalendar')
  165. ->with(666, $propPatch);
  166. }
  167. $c->propPatch($propPatch);
  168. $this->addToAssertionCount(1);
  169. }
  170. /**
  171. * @dataProvider providesReadOnlyInfo
  172. */
  173. public function testAcl($expectsWrite, $readOnlyValue, $hasOwnerSet, $uri = 'default') {
  174. /** @var \PHPUnit_Framework_MockObject_MockObject | CalDavBackend $backend */
  175. $backend = $this->getMockBuilder(CalDavBackend::class)->disableOriginalConstructor()->getMock();
  176. $backend->expects($this->any())->method('applyShareAcl')->willReturnArgument(1);
  177. $calendarInfo = [
  178. 'principaluri' => 'user2',
  179. 'id' => 666,
  180. 'uri' => $uri
  181. ];
  182. if (!is_null($readOnlyValue)) {
  183. $calendarInfo['{http://owncloud.org/ns}read-only'] = $readOnlyValue;
  184. }
  185. if ($hasOwnerSet) {
  186. $calendarInfo['{http://owncloud.org/ns}owner-principal'] = 'user1';
  187. }
  188. $c = new Calendar($backend, $calendarInfo, $this->l10n, $this->config);
  189. $acl = $c->getACL();
  190. $childAcl = $c->getChildACL();
  191. $expectedAcl = [[
  192. 'privilege' => '{DAV:}read',
  193. 'principal' => $hasOwnerSet ? 'user1' : 'user2',
  194. 'protected' => true
  195. ], [
  196. 'privilege' => '{DAV:}read',
  197. 'principal' => ($hasOwnerSet ? 'user1' : 'user2') . '/calendar-proxy-write',
  198. 'protected' => true,
  199. ], [
  200. 'privilege' => '{DAV:}read',
  201. 'principal' => ($hasOwnerSet ? 'user1' : 'user2') . '/calendar-proxy-read',
  202. 'protected' => true,
  203. ]];
  204. if ($uri === BirthdayService::BIRTHDAY_CALENDAR_URI) {
  205. $expectedAcl[] = [
  206. 'privilege' => '{DAV:}write-properties',
  207. 'principal' => $hasOwnerSet ? 'user1' : 'user2',
  208. 'protected' => true
  209. ];
  210. $expectedAcl[] = [
  211. 'privilege' => '{DAV:}write-properties',
  212. 'principal' => ($hasOwnerSet ? 'user1' : 'user2') . '/calendar-proxy-write',
  213. 'protected' => true
  214. ];
  215. } else {
  216. $expectedAcl[] = [
  217. 'privilege' => '{DAV:}write',
  218. 'principal' => $hasOwnerSet ? 'user1' : 'user2',
  219. 'protected' => true
  220. ];
  221. $expectedAcl[] = [
  222. 'privilege' => '{DAV:}write',
  223. 'principal' => ($hasOwnerSet ? 'user1' : 'user2') . '/calendar-proxy-write',
  224. 'protected' => true
  225. ];
  226. }
  227. $expectedAcl[] = [
  228. 'privilege' => '{DAV:}write-properties',
  229. 'principal' => ($hasOwnerSet ? 'user1' : 'user2') . '/calendar-proxy-read',
  230. 'protected' => true
  231. ];
  232. if ($hasOwnerSet) {
  233. $expectedAcl[] = [
  234. 'privilege' => '{DAV:}read',
  235. 'principal' => 'user2',
  236. 'protected' => true
  237. ];
  238. if ($expectsWrite) {
  239. $expectedAcl[] = [
  240. 'privilege' => '{DAV:}write',
  241. 'principal' => 'user2',
  242. 'protected' => true
  243. ];
  244. } else {
  245. $expectedAcl[] = [
  246. 'privilege' => '{DAV:}write-properties',
  247. 'principal' => 'user2',
  248. 'protected' => true
  249. ];
  250. }
  251. }
  252. $this->assertEquals($expectedAcl, $acl);
  253. $this->assertEquals($expectedAcl, $childAcl);
  254. }
  255. public function providesReadOnlyInfo() {
  256. return [
  257. 'read-only property not set' => [true, null, true],
  258. 'read-only property is false' => [true, false, true],
  259. 'read-only property is true' => [false, true, true],
  260. 'read-only property not set and no owner' => [true, null, false],
  261. 'read-only property is false and no owner' => [true, false, false],
  262. 'read-only property is true and no owner' => [false, true, false],
  263. 'birthday calendar' => [false, false, false, BirthdayService::BIRTHDAY_CALENDAR_URI]
  264. ];
  265. }
  266. /**
  267. * @dataProvider providesConfidentialClassificationData
  268. * @param int $expectedChildren
  269. * @param bool $isShared
  270. */
  271. public function testPrivateClassification($expectedChildren, $isShared) {
  272. $calObject0 = ['uri' => 'event-0', 'classification' => CalDavBackend::CLASSIFICATION_PUBLIC];
  273. $calObject1 = ['uri' => 'event-1', 'classification' => CalDavBackend::CLASSIFICATION_CONFIDENTIAL];
  274. $calObject2 = ['uri' => 'event-2', 'classification' => CalDavBackend::CLASSIFICATION_PRIVATE];
  275. /** @var \PHPUnit_Framework_MockObject_MockObject | CalDavBackend $backend */
  276. $backend = $this->getMockBuilder(CalDavBackend::class)->disableOriginalConstructor()->getMock();
  277. $backend->expects($this->any())->method('getCalendarObjects')->willReturn([
  278. $calObject0, $calObject1, $calObject2
  279. ]);
  280. $backend->expects($this->any())->method('getMultipleCalendarObjects')
  281. ->with(666, ['event-0', 'event-1', 'event-2'])
  282. ->willReturn([
  283. $calObject0, $calObject1, $calObject2
  284. ]);
  285. $backend->expects($this->any())->method('getCalendarObject')
  286. ->willReturn($calObject2)->with(666, 'event-2');
  287. $backend->expects($this->any())->method('applyShareAcl')->willReturnArgument(1);
  288. $calendarInfo = [
  289. 'principaluri' => 'user2',
  290. 'id' => 666,
  291. 'uri' => 'cal',
  292. ];
  293. if ($isShared) {
  294. $calendarInfo['{http://owncloud.org/ns}owner-principal'] = 'user1';
  295. }
  296. $c = new Calendar($backend, $calendarInfo, $this->l10n, $this->config);
  297. $children = $c->getChildren();
  298. $this->assertEquals($expectedChildren, count($children));
  299. $children = $c->getMultipleChildren(['event-0', 'event-1', 'event-2']);
  300. $this->assertEquals($expectedChildren, count($children));
  301. $this->assertEquals(!$isShared, $c->childExists('event-2'));
  302. }
  303. /**
  304. * @dataProvider providesConfidentialClassificationData
  305. * @param int $expectedChildren
  306. * @param bool $isShared
  307. */
  308. public function testConfidentialClassification($expectedChildren, $isShared) {
  309. $start = '20160609';
  310. $end = '20160610';
  311. $calData = <<<EOD
  312. BEGIN:VCALENDAR
  313. PRODID:-//ownCloud calendar v1.2.2
  314. BEGIN:VEVENT
  315. CREATED:20160602T133732
  316. DTSTAMP:20160602T133732
  317. LAST-MODIFIED:20160602T133732
  318. UID:wej2z68l9h
  319. SUMMARY:Test Event
  320. LOCATION:Somewhere ...
  321. ATTENDEE;ROLE=REQ-PARTICIPANT;PARTSTAT=NEEDS-ACTION;CUTYPE=INDIVIDUAL;CN=de
  322. epdiver:MAILTO:thomas.mueller@tmit.eu
  323. ORGANIZER;CN=deepdiver:MAILTO:thomas.mueller@tmit.eu
  324. DESCRIPTION:maybe ....
  325. DTSTART;TZID=Europe/Berlin;VALUE=DATE:$start
  326. DTEND;TZID=Europe/Berlin;VALUE=DATE:$end
  327. RRULE:FREQ=DAILY
  328. BEGIN:VALARM
  329. ACTION:AUDIO
  330. TRIGGER:-PT15M
  331. END:VALARM
  332. END:VEVENT
  333. BEGIN:VTIMEZONE
  334. TZID:Europe/Berlin
  335. BEGIN:DAYLIGHT
  336. DTSTART:19810329T020000
  337. RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
  338. TZNAME:MESZ
  339. TZOFFSETFROM:+0100
  340. TZOFFSETTO:+0200
  341. END:DAYLIGHT
  342. BEGIN:STANDARD
  343. DTSTART:19961027T030000
  344. RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
  345. TZNAME:MEZ
  346. TZOFFSETFROM:+0200
  347. TZOFFSETTO:+0100
  348. END:STANDARD
  349. END:VTIMEZONE
  350. END:VCALENDAR
  351. EOD;
  352. $calObject0 = ['uri' => 'event-0', 'classification' => CalDavBackend::CLASSIFICATION_PUBLIC];
  353. $calObject1 = ['uri' => 'event-1', 'classification' => CalDavBackend::CLASSIFICATION_CONFIDENTIAL, 'calendardata' => $calData];
  354. $calObject2 = ['uri' => 'event-2', 'classification' => CalDavBackend::CLASSIFICATION_PRIVATE];
  355. /** @var \PHPUnit_Framework_MockObject_MockObject | CalDavBackend $backend */
  356. $backend = $this->getMockBuilder(CalDavBackend::class)->disableOriginalConstructor()->getMock();
  357. $backend->expects($this->any())->method('getCalendarObjects')->willReturn([
  358. $calObject0, $calObject1, $calObject2
  359. ]);
  360. $backend->expects($this->any())->method('getMultipleCalendarObjects')
  361. ->with(666, ['event-0', 'event-1', 'event-2'])
  362. ->willReturn([
  363. $calObject0, $calObject1, $calObject2
  364. ]);
  365. $backend->expects($this->any())->method('getCalendarObject')
  366. ->willReturn($calObject1)->with(666, 'event-1');
  367. $backend->expects($this->any())->method('applyShareAcl')->willReturnArgument(1);
  368. $calendarInfo = [
  369. '{http://owncloud.org/ns}owner-principal' => $isShared ? 'user1' : 'user2',
  370. 'principaluri' => 'user2',
  371. 'id' => 666,
  372. 'uri' => 'cal',
  373. ];
  374. $c = new Calendar($backend, $calendarInfo, $this->l10n, $this->config);
  375. $this->assertEquals(count($c->getChildren()), $expectedChildren);
  376. // test private event
  377. $privateEvent = $c->getChild('event-1');
  378. $calData = $privateEvent->get();
  379. $event = Reader::read($calData);
  380. $this->assertEquals($start, $event->VEVENT->DTSTART->getValue());
  381. $this->assertEquals($end, $event->VEVENT->DTEND->getValue());
  382. if ($isShared) {
  383. $this->assertEquals('Busy', $event->VEVENT->SUMMARY->getValue());
  384. $this->assertArrayNotHasKey('ATTENDEE', $event->VEVENT);
  385. $this->assertArrayNotHasKey('LOCATION', $event->VEVENT);
  386. $this->assertArrayNotHasKey('DESCRIPTION', $event->VEVENT);
  387. $this->assertArrayNotHasKey('ORGANIZER', $event->VEVENT);
  388. } else {
  389. $this->assertEquals('Test Event', $event->VEVENT->SUMMARY->getValue());
  390. }
  391. // Test l10n
  392. $l10n = $this->createMock(IL10N::class);
  393. if ($isShared) {
  394. $l10n->expects($this->once())
  395. ->method('t')
  396. ->with('Busy')
  397. ->willReturn("Translated busy");
  398. } else {
  399. $l10n->expects($this->never());
  400. }
  401. $c = new Calendar($backend, $calendarInfo, $l10n, $this->config);
  402. $calData = $c->getChild('event-1')->get();
  403. $event = Reader::read($calData);
  404. if ($isShared) {
  405. $this->assertEquals('Translated busy', $event->VEVENT->SUMMARY->getValue());
  406. } else {
  407. $this->assertEquals('Test Event', $event->VEVENT->SUMMARY->getValue());
  408. }
  409. }
  410. public function providesConfidentialClassificationData() {
  411. return [
  412. [3, false],
  413. [2, true]
  414. ];
  415. }
  416. public function testRemoveVAlarms() {
  417. $publicObjectData = <<<EOD
  418. BEGIN:VCALENDAR
  419. VERSION:2.0
  420. PRODID:-//Nextcloud calendar v1.5.6
  421. CALSCALE:GREGORIAN
  422. BEGIN:VEVENT
  423. CREATED:20171022T125130
  424. DTSTAMP:20171022T125130
  425. LAST-MODIFIED:20171022T125130
  426. UID:PPL24TH8UGOWE94XET87ER
  427. SUMMARY:Foo bar blub
  428. CLASS:PUBLIC
  429. STATUS:CONFIRMED
  430. DTSTART;VALUE=DATE:20171024
  431. DTEND;VALUE=DATE:20171025
  432. BEGIN:VALARM
  433. ACTION:AUDIO
  434. TRIGGER:-PT15M
  435. END:VALARM
  436. END:VEVENT
  437. END:VCALENDAR
  438. EOD;
  439. $confidentialObjectData = <<<EOD
  440. BEGIN:VCALENDAR
  441. VERSION:2.0
  442. PRODID:-//Nextcloud calendar v1.5.6
  443. CALSCALE:GREGORIAN
  444. BEGIN:VEVENT
  445. CREATED:20171022T125130
  446. DTSTAMP:20171022T125130
  447. LAST-MODIFIED:20171022T125130
  448. UID:PPL24TH8UGOWE94XET87ER
  449. SUMMARY:Foo bar blub
  450. CLASS:CONFIDENTIAL
  451. STATUS:CONFIRMED
  452. DTSTART;VALUE=DATE:20171024
  453. DTEND;VALUE=DATE:20171025
  454. BEGIN:VALARM
  455. ACTION:AUDIO
  456. TRIGGER:-PT15M
  457. END:VALARM
  458. END:VEVENT
  459. END:VCALENDAR
  460. EOD;
  461. $publicObjectDataWithoutVAlarm = <<<EOD
  462. BEGIN:VCALENDAR
  463. VERSION:2.0
  464. PRODID:-//Nextcloud calendar v1.5.6
  465. CALSCALE:GREGORIAN
  466. BEGIN:VEVENT
  467. CREATED:20171022T125130
  468. DTSTAMP:20171022T125130
  469. LAST-MODIFIED:20171022T125130
  470. UID:PPL24TH8UGOWE94XET87ER
  471. SUMMARY:Foo bar blub
  472. CLASS:PUBLIC
  473. STATUS:CONFIRMED
  474. DTSTART;VALUE=DATE:20171024
  475. DTEND;VALUE=DATE:20171025
  476. END:VEVENT
  477. END:VCALENDAR
  478. EOD;
  479. $confidentialObjectCleaned = <<<EOD
  480. BEGIN:VCALENDAR
  481. VERSION:2.0
  482. PRODID:-//Nextcloud calendar v1.5.6
  483. CALSCALE:GREGORIAN
  484. BEGIN:VEVENT
  485. CREATED:20171022T125130
  486. UID:PPL24TH8UGOWE94XET87ER
  487. SUMMARY:Busy
  488. CLASS:CONFIDENTIAL
  489. DTSTART;VALUE=DATE:20171024
  490. DTEND;VALUE=DATE:20171025
  491. END:VEVENT
  492. END:VCALENDAR
  493. EOD;
  494. $publicObject = ['uri' => 'event-0',
  495. 'classification' => CalDavBackend::CLASSIFICATION_PUBLIC,
  496. 'calendardata' => $publicObjectData];
  497. $confidentialObject = ['uri' => 'event-1',
  498. 'classification' => CalDavBackend::CLASSIFICATION_CONFIDENTIAL,
  499. 'calendardata' => $confidentialObjectData];
  500. /** @var \PHPUnit_Framework_MockObject_MockObject | CalDavBackend $backend */
  501. $backend = $this->createMock(CalDavBackend::class);
  502. $backend->expects($this->any())
  503. ->method('getCalendarObjects')
  504. ->willReturn([$publicObject, $confidentialObject]);
  505. $backend->expects($this->any())
  506. ->method('getMultipleCalendarObjects')
  507. ->with(666, ['event-0', 'event-1'])
  508. ->willReturn([$publicObject, $confidentialObject]);
  509. $backend->expects($this->any())
  510. ->method('getCalendarObject')
  511. ->willReturnCallback(function ($cId, $uri) use ($publicObject, $confidentialObject) {
  512. switch ($uri) {
  513. case 'event-0':
  514. return $publicObject;
  515. case 'event-1':
  516. return $confidentialObject;
  517. default:
  518. throw new \Exception('unexpected uri');
  519. }
  520. });
  521. $backend->expects($this->any())
  522. ->method('applyShareAcl')
  523. ->willReturnArgument(1);
  524. $calendarInfoOwner = [
  525. '{http://owncloud.org/ns}owner-principal' => 'user1',
  526. 'principaluri' => 'user1',
  527. 'id' => 666,
  528. 'uri' => 'cal',
  529. ];
  530. $calendarInfoSharedRW = [
  531. '{http://owncloud.org/ns}owner-principal' => 'user1',
  532. 'principaluri' => 'user2',
  533. 'id' => 666,
  534. 'uri' => 'cal',
  535. ];
  536. $calendarInfoSharedRO = [
  537. '{http://owncloud.org/ns}owner-principal' => 'user1',
  538. '{http://owncloud.org/ns}read-only' => true,
  539. 'principaluri' => 'user2',
  540. 'id' => 666,
  541. 'uri' => 'cal',
  542. ];
  543. $ownerCalendar = new Calendar($backend, $calendarInfoOwner, $this->l10n, $this->config);
  544. $rwCalendar = new Calendar($backend, $calendarInfoSharedRW, $this->l10n, $this->config);
  545. $roCalendar = new Calendar($backend, $calendarInfoSharedRO, $this->l10n, $this->config);
  546. $this->assertEquals(count($ownerCalendar->getChildren()), 2);
  547. $this->assertEquals(count($rwCalendar->getChildren()), 2);
  548. $this->assertEquals(count($roCalendar->getChildren()), 2);
  549. // calendar data shall not be altered for the owner
  550. $this->assertEquals($ownerCalendar->getChild('event-0')->get(), $publicObjectData);
  551. $this->assertEquals($ownerCalendar->getChild('event-1')->get(), $confidentialObjectData);
  552. // valarms shall not be removed for read-write shares
  553. $this->assertEquals(
  554. $this->fixLinebreak($rwCalendar->getChild('event-0')->get()),
  555. $this->fixLinebreak($publicObjectData));
  556. $this->assertEquals(
  557. $this->fixLinebreak($rwCalendar->getChild('event-1')->get()),
  558. $this->fixLinebreak($confidentialObjectCleaned));
  559. // valarms shall be removed for read-only shares
  560. $this->assertEquals(
  561. $this->fixLinebreak($roCalendar->getChild('event-0')->get()),
  562. $this->fixLinebreak($publicObjectDataWithoutVAlarm));
  563. $this->assertEquals(
  564. $this->fixLinebreak($roCalendar->getChild('event-1')->get()),
  565. $this->fixLinebreak($confidentialObjectCleaned));
  566. }
  567. private function fixLinebreak($str) {
  568. return preg_replace('~(*BSR_ANYCRLF)\R~', "\r\n", $str);
  569. }
  570. }