CalendarObjectTest.php 5.0 KB

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