CalendarObjectTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Tests\unit\CalDAV\AppCalendar;
  7. use OCA\DAV\CalDAV\AppCalendar\AppCalendar;
  8. use OCA\DAV\CalDAV\AppCalendar\CalendarObject;
  9. use OCP\Calendar\ICalendar;
  10. use OCP\Calendar\ICreateFromString;
  11. use OCP\Constants;
  12. use PHPUnit\Framework\MockObject\MockObject;
  13. use Sabre\VObject\Component\VCalendar;
  14. use Test\TestCase;
  15. class CalendarObjectTest extends TestCase {
  16. private CalendarObject $calendarObject;
  17. private AppCalendar|MockObject $calendar;
  18. private ICalendar|MockObject $backend;
  19. private VCalendar|MockObject $vobject;
  20. protected function setUp(): void {
  21. parent::setUp();
  22. $this->calendar = $this->createMock(AppCalendar::class);
  23. $this->calendar->method('getOwner')->willReturn('owner');
  24. $this->calendar->method('getGroup')->willReturn('group');
  25. $this->backend = $this->createMock(ICalendar::class);
  26. $this->vobject = $this->createMock(VCalendar::class);
  27. $this->calendarObject = new CalendarObject($this->calendar, $this->backend, $this->vobject);
  28. }
  29. public function testGetOwner() {
  30. $this->assertEquals($this->calendarObject->getOwner(), 'owner');
  31. }
  32. public function testGetGroup() {
  33. $this->assertEquals($this->calendarObject->getGroup(), 'group');
  34. }
  35. public function testGetACL() {
  36. $this->calendar->expects($this->exactly(2))
  37. ->method('getPermissions')
  38. ->willReturnOnConsecutiveCalls(Constants::PERMISSION_READ, Constants::PERMISSION_ALL);
  39. // read only
  40. $this->assertEquals($this->calendarObject->getACL(), [
  41. [
  42. 'privilege' => '{DAV:}read',
  43. 'principal' => 'owner',
  44. 'protected' => true,
  45. ]
  46. ]);
  47. // write permissions
  48. $this->assertEquals($this->calendarObject->getACL(), [
  49. [
  50. 'privilege' => '{DAV:}read',
  51. 'principal' => 'owner',
  52. 'protected' => true,
  53. ],
  54. [
  55. 'privilege' => '{DAV:}write-content',
  56. 'principal' => 'owner',
  57. 'protected' => true,
  58. ]
  59. ]);
  60. }
  61. public function testSetACL() {
  62. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  63. $this->calendarObject->setACL([]);
  64. }
  65. public function testPut_readOnlyBackend() {
  66. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  67. $this->calendarObject->put('foo');
  68. }
  69. public function testPut_noPermissions() {
  70. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  71. $backend = $this->createMock(ICreateFromString::class);
  72. $calendarObject = new CalendarObject($this->calendar, $backend, $this->vobject);
  73. $this->calendar->expects($this->once())
  74. ->method('getPermissions')
  75. ->willReturn(Constants::PERMISSION_READ);
  76. $calendarObject->put('foo');
  77. }
  78. public function testPut() {
  79. $backend = $this->createMock(ICreateFromString::class);
  80. $calendarObject = new CalendarObject($this->calendar, $backend, $this->vobject);
  81. $this->vobject->expects($this->once())
  82. ->method('getBaseComponent')
  83. ->willReturn((object)['UID' => 'someid']);
  84. $this->calendar->expects($this->once())
  85. ->method('getPermissions')
  86. ->willReturn(Constants::PERMISSION_ALL);
  87. $backend->expects($this->once())
  88. ->method('createFromString')
  89. ->with('someid.ics', 'foo');
  90. $calendarObject->put('foo');
  91. }
  92. public function testGet() {
  93. $this->vobject->expects($this->once())
  94. ->method('serialize')
  95. ->willReturn('foo');
  96. $this->assertEquals($this->calendarObject->get(), 'foo');
  97. }
  98. public function testDelete_notWriteable() {
  99. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  100. $this->calendarObject->delete();
  101. }
  102. public function testDelete_noPermission() {
  103. $backend = $this->createMock(ICreateFromString::class);
  104. $calendarObject = new CalendarObject($this->calendar, $backend, $this->vobject);
  105. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  106. $calendarObject->delete();
  107. }
  108. public function testDelete() {
  109. $backend = $this->createMock(ICreateFromString::class);
  110. $calendarObject = new CalendarObject($this->calendar, $backend, $this->vobject);
  111. $components = [(new VCalendar(['VEVENT' => ['UID' => 'someid']]))->getBaseComponent()];
  112. $this->calendar->expects($this->once())
  113. ->method('getPermissions')
  114. ->willReturn(Constants::PERMISSION_DELETE);
  115. $this->vobject->expects($this->once())
  116. ->method('getBaseComponents')
  117. ->willReturn($components);
  118. $this->vobject->expects($this->once())
  119. ->method('getBaseComponent')
  120. ->willReturn($components[0]);
  121. $backend->expects($this->once())
  122. ->method('createFromString')
  123. ->with('someid.ics', self::callback(fn ($data): bool => preg_match('/BEGIN:VEVENT(.|\r\n)+STATUS:CANCELLED/', $data) === 1));
  124. $calendarObject->delete();
  125. }
  126. public function testGetName() {
  127. $this->vobject->expects($this->exactly(2))
  128. ->method('getBaseComponent')
  129. ->willReturnOnConsecutiveCalls((object)['UID' => 'someid'], (object)['UID' => 'someid', 'X-FILENAME' => 'real-filename.ics']);
  130. $this->assertEquals($this->calendarObject->getName(), 'someid.ics');
  131. $this->assertEquals($this->calendarObject->getName(), 'real-filename.ics');
  132. }
  133. public function testSetName() {
  134. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  135. $this->calendarObject->setName('Some name');
  136. }
  137. }