AppCalendarTest.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 OCP\Calendar\ICalendar;
  9. use OCP\Calendar\ICreateFromString;
  10. use OCP\Constants;
  11. use PHPUnit\Framework\MockObject\MockObject;
  12. use Test\TestCase;
  13. use function rewind;
  14. class AppCalendarTest extends TestCase {
  15. private $principal = 'principals/users/foo';
  16. private AppCalendar $appCalendar;
  17. private AppCalendar $writeableAppCalendar;
  18. private ICalendar|MockObject $calendar;
  19. private ICalendar|MockObject $writeableCalendar;
  20. protected function setUp(): void {
  21. parent::setUp();
  22. $this->calendar = $this->getMockBuilder(ICalendar::class)->getMock();
  23. $this->calendar->method('getPermissions')
  24. ->willReturn(Constants::PERMISSION_READ);
  25. $this->writeableCalendar = $this->getMockBuilder(ICreateFromString::class)->getMock();
  26. $this->writeableCalendar->method('getPermissions')
  27. ->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_CREATE);
  28. $this->appCalendar = new AppCalendar('dav-wrapper', $this->calendar, $this->principal);
  29. $this->writeableAppCalendar = new AppCalendar('dav-wrapper', $this->writeableCalendar, $this->principal);
  30. }
  31. public function testGetPrincipal():void {
  32. // Check that the correct name is returned
  33. $this->assertEquals($this->principal, $this->appCalendar->getOwner());
  34. $this->assertEquals($this->principal, $this->writeableAppCalendar->getOwner());
  35. }
  36. public function testDelete(): void {
  37. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  38. $this->expectExceptionMessage('Deleting an entry is not implemented');
  39. $this->appCalendar->delete();
  40. }
  41. public function testCreateFile() {
  42. $this->writeableCalendar->expects($this->exactly(3))
  43. ->method('createFromString')
  44. ->withConsecutive(['some-name', 'data'], ['other-name', ''], ['name', 'some data']);
  45. // pass data
  46. $this->assertNull($this->writeableAppCalendar->createFile('some-name', 'data'));
  47. // null is empty string
  48. $this->assertNull($this->writeableAppCalendar->createFile('other-name', null));
  49. // resource to data
  50. $fp = fopen('php://memory', 'r+');
  51. fwrite($fp, 'some data');
  52. rewind($fp);
  53. $this->assertNull($this->writeableAppCalendar->createFile('name', $fp));
  54. fclose($fp);
  55. }
  56. public function testCreateFile_readOnly() {
  57. // If writing is not supported
  58. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  59. $this->expectExceptionMessage('Creating a new entry is not allowed');
  60. $this->appCalendar->createFile('some-name', 'data');
  61. }
  62. public function testSetACL(): void {
  63. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  64. $this->expectExceptionMessage('Setting ACL is not supported on this node');
  65. $this->appCalendar->setACL([]);
  66. }
  67. public function testGetACL():void {
  68. $expectedRO = [
  69. [
  70. 'privilege' => '{DAV:}read',
  71. 'principal' => $this->principal,
  72. 'protected' => true,
  73. ],
  74. [
  75. 'privilege' => '{DAV:}write-properties',
  76. 'principal' => $this->principal,
  77. 'protected' => true,
  78. ]
  79. ];
  80. $expectedRW = $expectedRO;
  81. $expectedRW[] = [
  82. 'privilege' => '{DAV:}write',
  83. 'principal' => $this->principal,
  84. 'protected' => true,
  85. ];
  86. // Check that the correct ACL is returned (default be only readable)
  87. $this->assertEquals($expectedRO, $this->appCalendar->getACL());
  88. $this->assertEquals($expectedRW, $this->writeableAppCalendar->getACL());
  89. }
  90. }