1
0

ExternalCalendarTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * @copyright 2020, Georg Ehrke <oc.list@georgehrke.com>
  4. *
  5. * @author Georg Ehrke <oc.list@georgehrke.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\DAV\Tests\unit\CalDAV\Integration;
  24. use OCA\DAV\CalDAV\Integration\ExternalCalendar;
  25. use Test\TestCase;
  26. class ExternalCalendarTest extends TestCase {
  27. private $abstractExternalCalendar;
  28. protected function setUp(): void {
  29. parent::setUp();
  30. $this->abstractExternalCalendar =
  31. $this->getMockForAbstractClass(ExternalCalendar::class, ['example-app-id', 'calendar-uri-in-backend']);
  32. }
  33. public function testGetName():void {
  34. // Check that the correct name is returned
  35. $this->assertEquals('app-generated--example-app-id--calendar-uri-in-backend',
  36. $this->abstractExternalCalendar->getName());
  37. // Check that the method is final and can't be overriden by other classes
  38. $reflectionMethod = new \ReflectionMethod(ExternalCalendar::class, 'getName');
  39. $this->assertTrue($reflectionMethod->isFinal());
  40. }
  41. public function testSetName():void {
  42. // Check that the method is final and can't be overriden by other classes
  43. $reflectionMethod = new \ReflectionMethod(ExternalCalendar::class, 'setName');
  44. $this->assertTrue($reflectionMethod->isFinal());
  45. $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
  46. $this->expectExceptionMessage('Renaming calendars is not yet supported');
  47. $this->abstractExternalCalendar->setName('other-name');
  48. }
  49. public function createDirectory():void {
  50. // Check that the method is final and can't be overriden by other classes
  51. $reflectionMethod = new \ReflectionMethod(ExternalCalendar::class, 'createDirectory');
  52. $this->assertTrue($reflectionMethod->isFinal());
  53. $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
  54. $this->expectExceptionMessage('Creating collections in calendar objects is not allowed');
  55. $this->abstractExternalCalendar->createDirectory('other-name');
  56. }
  57. public function testIsAppGeneratedCalendar():void {
  58. $this->assertFalse(ExternalCalendar::isAppGeneratedCalendar('personal'));
  59. $this->assertFalse(ExternalCalendar::isAppGeneratedCalendar('work'));
  60. $this->assertFalse(ExternalCalendar::isAppGeneratedCalendar('contact_birthdays'));
  61. $this->assertFalse(ExternalCalendar::isAppGeneratedCalendar('company'));
  62. $this->assertFalse(ExternalCalendar::isAppGeneratedCalendar('app-generated'));
  63. $this->assertFalse(ExternalCalendar::isAppGeneratedCalendar('app-generated--example'));
  64. $this->assertTrue(ExternalCalendar::isAppGeneratedCalendar('app-generated--deck--board-1'));
  65. $this->assertTrue(ExternalCalendar::isAppGeneratedCalendar('app-generated--example--foo-2'));
  66. $this->assertTrue(ExternalCalendar::isAppGeneratedCalendar('app-generated--example--foo--2'));
  67. }
  68. /**
  69. * @dataProvider splitAppGeneratedCalendarUriDataProvider
  70. */
  71. public function testSplitAppGeneratedCalendarUriInvalid(string $name):void {
  72. $this->expectException(\InvalidArgumentException::class);
  73. $this->expectExceptionMessage('Provided calendar uri was not app-generated');
  74. ExternalCalendar::splitAppGeneratedCalendarUri($name);
  75. }
  76. public function splitAppGeneratedCalendarUriDataProvider():array {
  77. return [
  78. ['personal'],
  79. ['foo_shared_by_admin'],
  80. ['contact_birthdays'],
  81. ];
  82. }
  83. public function testSplitAppGeneratedCalendarUri():void {
  84. $this->assertEquals(['deck', 'board-1'], ExternalCalendar::splitAppGeneratedCalendarUri('app-generated--deck--board-1'));
  85. $this->assertEquals(['example', 'foo-2'], ExternalCalendar::splitAppGeneratedCalendarUri('app-generated--example--foo-2'));
  86. $this->assertEquals(['example', 'foo--2'], ExternalCalendar::splitAppGeneratedCalendarUri('app-generated--example--foo--2'));
  87. }
  88. public function testDoesViolateReservedName():void {
  89. $this->assertFalse(ExternalCalendar::doesViolateReservedName('personal'));
  90. $this->assertFalse(ExternalCalendar::doesViolateReservedName('work'));
  91. $this->assertFalse(ExternalCalendar::doesViolateReservedName('contact_birthdays'));
  92. $this->assertFalse(ExternalCalendar::doesViolateReservedName('company'));
  93. $this->assertTrue(ExternalCalendar::doesViolateReservedName('app-generated'));
  94. $this->assertTrue(ExternalCalendar::doesViolateReservedName('app-generated-calendar'));
  95. $this->assertTrue(ExternalCalendar::doesViolateReservedName('app-generated--deck-123'));
  96. }
  97. }