ExternalCalendarTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Tests\unit\CalDAV\Integration;
  7. use OCA\DAV\CalDAV\Integration\ExternalCalendar;
  8. use Test\TestCase;
  9. class ExternalCalendarTest extends TestCase {
  10. private $abstractExternalCalendar;
  11. protected function setUp(): void {
  12. parent::setUp();
  13. $this->abstractExternalCalendar =
  14. $this->getMockForAbstractClass(ExternalCalendar::class, ['example-app-id', 'calendar-uri-in-backend']);
  15. }
  16. public function testGetName():void {
  17. // Check that the correct name is returned
  18. $this->assertEquals('app-generated--example-app-id--calendar-uri-in-backend',
  19. $this->abstractExternalCalendar->getName());
  20. // Check that the method is final and can't be overridden by other classes
  21. $reflectionMethod = new \ReflectionMethod(ExternalCalendar::class, 'getName');
  22. $this->assertTrue($reflectionMethod->isFinal());
  23. }
  24. public function testSetName():void {
  25. // Check that the method is final and can't be overridden by other classes
  26. $reflectionMethod = new \ReflectionMethod(ExternalCalendar::class, 'setName');
  27. $this->assertTrue($reflectionMethod->isFinal());
  28. $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
  29. $this->expectExceptionMessage('Renaming calendars is not yet supported');
  30. $this->abstractExternalCalendar->setName('other-name');
  31. }
  32. public function createDirectory():void {
  33. // Check that the method is final and can't be overridden by other classes
  34. $reflectionMethod = new \ReflectionMethod(ExternalCalendar::class, 'createDirectory');
  35. $this->assertTrue($reflectionMethod->isFinal());
  36. $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
  37. $this->expectExceptionMessage('Creating collections in calendar objects is not allowed');
  38. $this->abstractExternalCalendar->createDirectory('other-name');
  39. }
  40. public function testIsAppGeneratedCalendar():void {
  41. $this->assertFalse(ExternalCalendar::isAppGeneratedCalendar('personal'));
  42. $this->assertFalse(ExternalCalendar::isAppGeneratedCalendar('work'));
  43. $this->assertFalse(ExternalCalendar::isAppGeneratedCalendar('contact_birthdays'));
  44. $this->assertFalse(ExternalCalendar::isAppGeneratedCalendar('company'));
  45. $this->assertFalse(ExternalCalendar::isAppGeneratedCalendar('app-generated'));
  46. $this->assertFalse(ExternalCalendar::isAppGeneratedCalendar('app-generated--example'));
  47. $this->assertTrue(ExternalCalendar::isAppGeneratedCalendar('app-generated--deck--board-1'));
  48. $this->assertTrue(ExternalCalendar::isAppGeneratedCalendar('app-generated--example--foo-2'));
  49. $this->assertTrue(ExternalCalendar::isAppGeneratedCalendar('app-generated--example--foo--2'));
  50. }
  51. /**
  52. * @dataProvider splitAppGeneratedCalendarUriDataProvider
  53. */
  54. public function testSplitAppGeneratedCalendarUriInvalid(string $name):void {
  55. $this->expectException(\InvalidArgumentException::class);
  56. $this->expectExceptionMessage('Provided calendar uri was not app-generated');
  57. ExternalCalendar::splitAppGeneratedCalendarUri($name);
  58. }
  59. public function splitAppGeneratedCalendarUriDataProvider():array {
  60. return [
  61. ['personal'],
  62. ['foo_shared_by_admin'],
  63. ['contact_birthdays'],
  64. ];
  65. }
  66. public function testSplitAppGeneratedCalendarUri():void {
  67. $this->assertEquals(['deck', 'board-1'], ExternalCalendar::splitAppGeneratedCalendarUri('app-generated--deck--board-1'));
  68. $this->assertEquals(['example', 'foo-2'], ExternalCalendar::splitAppGeneratedCalendarUri('app-generated--example--foo-2'));
  69. $this->assertEquals(['example', 'foo--2'], ExternalCalendar::splitAppGeneratedCalendarUri('app-generated--example--foo--2'));
  70. }
  71. public function testDoesViolateReservedName():void {
  72. $this->assertFalse(ExternalCalendar::doesViolateReservedName('personal'));
  73. $this->assertFalse(ExternalCalendar::doesViolateReservedName('work'));
  74. $this->assertFalse(ExternalCalendar::doesViolateReservedName('contact_birthdays'));
  75. $this->assertFalse(ExternalCalendar::doesViolateReservedName('company'));
  76. $this->assertTrue(ExternalCalendar::doesViolateReservedName('app-generated'));
  77. $this->assertTrue(ExternalCalendar::doesViolateReservedName('app-generated-calendar'));
  78. $this->assertTrue(ExternalCalendar::doesViolateReservedName('app-generated--deck-123'));
  79. }
  80. }