AppCalendarTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * @license GNU AGPL version 3 or any later version
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. namespace OCA\DAV\Tests\unit\CalDAV\AppCalendar;
  20. use OCA\DAV\CalDAV\AppCalendar\AppCalendar;
  21. use OCP\Calendar\ICalendar;
  22. use OCP\Calendar\ICreateFromString;
  23. use OCP\Constants;
  24. use PHPUnit\Framework\MockObject\MockObject;
  25. use Test\TestCase;
  26. use function Safe\rewind;
  27. class AppCalendarTest extends TestCase {
  28. private $principal = 'principals/users/foo';
  29. private AppCalendar $appCalendar;
  30. private AppCalendar $writeableAppCalendar;
  31. private ICalendar|MockObject $calendar;
  32. private ICalendar|MockObject $writeableCalendar;
  33. protected function setUp(): void {
  34. parent::setUp();
  35. $this->calendar = $this->getMockBuilder(ICalendar::class)->getMock();
  36. $this->calendar->method('getPermissions')
  37. ->willReturn(Constants::PERMISSION_READ);
  38. $this->writeableCalendar = $this->getMockBuilder(ICreateFromString::class)->getMock();
  39. $this->writeableCalendar->method('getPermissions')
  40. ->willReturn(Constants::PERMISSION_READ | Constants::PERMISSION_CREATE);
  41. $this->appCalendar = new AppCalendar('dav-wrapper', $this->calendar, $this->principal);
  42. $this->writeableAppCalendar = new AppCalendar('dav-wrapper', $this->writeableCalendar, $this->principal);
  43. }
  44. public function testGetPrincipal():void {
  45. // Check that the correct name is returned
  46. $this->assertEquals($this->principal, $this->appCalendar->getOwner());
  47. $this->assertEquals($this->principal, $this->writeableAppCalendar->getOwner());
  48. }
  49. public function testDelete(): void {
  50. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  51. $this->expectExceptionMessage('Deleting an entry is not implemented');
  52. $this->appCalendar->delete();
  53. }
  54. public function testCreateFile() {
  55. $this->writeableCalendar->expects($this->exactly(3))
  56. ->method('createFromString')
  57. ->withConsecutive(['some-name', 'data'], ['other-name', ''], ['name', 'some data']);
  58. // pass data
  59. $this->assertNull($this->writeableAppCalendar->createFile('some-name', 'data'));
  60. // null is empty string
  61. $this->assertNull($this->writeableAppCalendar->createFile('other-name', null));
  62. // resource to data
  63. $fp = fopen('php://memory', 'r+');
  64. fwrite($fp, 'some data');
  65. rewind($fp);
  66. $this->assertNull($this->writeableAppCalendar->createFile('name', $fp));
  67. fclose($fp);
  68. }
  69. public function testCreateFile_readOnly() {
  70. // If writing is not supported
  71. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  72. $this->expectExceptionMessage('Creating a new entry is not allowed');
  73. $this->appCalendar->createFile('some-name', 'data');
  74. }
  75. public function testSetACL(): void {
  76. $this->expectException(\Sabre\DAV\Exception\Forbidden::class);
  77. $this->expectExceptionMessage('Setting ACL is not supported on this node');
  78. $this->appCalendar->setACL([]);
  79. }
  80. public function testGetACL():void {
  81. $expectedRO = [
  82. [
  83. 'privilege' => '{DAV:}read',
  84. 'principal' => $this->principal,
  85. 'protected' => true,
  86. ],
  87. [
  88. 'privilege' => '{DAV:}write-properties',
  89. 'principal' => $this->principal,
  90. 'protected' => true,
  91. ]
  92. ];
  93. $expectedRW = $expectedRO;
  94. $expectedRW[] = [
  95. 'privilege' => '{DAV:}write',
  96. 'principal' => $this->principal,
  97. 'protected' => true,
  98. ];
  99. // Check that the correct ACL is returned (default be only readable)
  100. $this->assertEquals($expectedRO, $this->appCalendar->getACL());
  101. $this->assertEquals($expectedRW, $this->writeableAppCalendar->getACL());
  102. }
  103. }