CalendarObjectTest.php 5.0 KB

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