PublicCalendarRootTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Tests\unit\CalDAV;
  7. use OCA\DAV\CalDAV\CalDavBackend;
  8. use OCA\DAV\CalDAV\Calendar;
  9. use OCA\DAV\CalDAV\PublicCalendar;
  10. use OCA\DAV\CalDAV\PublicCalendarRoot;
  11. use OCA\DAV\Connector\Sabre\Principal;
  12. use OCP\EventDispatcher\IEventDispatcher;
  13. use OCP\IConfig;
  14. use OCP\IGroupManager;
  15. use OCP\IL10N;
  16. use OCP\IUserManager;
  17. use OCP\Security\ISecureRandom;
  18. use Psr\Log\LoggerInterface;
  19. use Test\TestCase;
  20. /**
  21. * Class PublicCalendarRootTest
  22. *
  23. * @group DB
  24. *
  25. * @package OCA\DAV\Tests\unit\CalDAV
  26. */
  27. class PublicCalendarRootTest extends TestCase {
  28. public const UNIT_TEST_USER = '';
  29. /** @var CalDavBackend */
  30. private $backend;
  31. /** @var PublicCalendarRoot */
  32. private $publicCalendarRoot;
  33. /** @var IL10N */
  34. private $l10n;
  35. /** @var Principal|\PHPUnit\Framework\MockObject\MockObject */
  36. private $principal;
  37. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  38. protected $userManager;
  39. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
  40. protected $groupManager;
  41. /** @var IConfig */
  42. protected $config;
  43. /** @var ISecureRandom */
  44. private $random;
  45. /** @var LoggerInterface */
  46. private $logger;
  47. protected function setUp(): void {
  48. parent::setUp();
  49. $db = \OC::$server->getDatabaseConnection();
  50. $this->principal = $this->createMock('OCA\DAV\Connector\Sabre\Principal');
  51. $this->userManager = $this->createMock(IUserManager::class);
  52. $this->groupManager = $this->createMock(IGroupManager::class);
  53. $this->random = \OC::$server->getSecureRandom();
  54. $this->logger = $this->createMock(LoggerInterface::class);
  55. $dispatcher = $this->createMock(IEventDispatcher::class);
  56. $config = $this->createMock(IConfig::class);
  57. $sharingBackend = $this->createMock(\OCA\DAV\CalDAV\Sharing\Backend::class);
  58. $this->principal->expects($this->any())->method('getGroupMembership')
  59. ->withAnyParameters()
  60. ->willReturn([]);
  61. $this->principal->expects($this->any())->method('getCircleMembership')
  62. ->withAnyParameters()
  63. ->willReturn([]);
  64. $this->backend = new CalDavBackend(
  65. $db,
  66. $this->principal,
  67. $this->userManager,
  68. $this->random,
  69. $this->logger,
  70. $dispatcher,
  71. $config,
  72. $sharingBackend,
  73. false,
  74. );
  75. $this->l10n = $this->getMockBuilder(IL10N::class)
  76. ->disableOriginalConstructor()->getMock();
  77. $this->config = $this->createMock(IConfig::class);
  78. $this->publicCalendarRoot = new PublicCalendarRoot($this->backend,
  79. $this->l10n, $this->config, $this->logger);
  80. }
  81. protected function tearDown(): void {
  82. parent::tearDown();
  83. if (is_null($this->backend)) {
  84. return;
  85. }
  86. $this->principal->expects($this->any())->method('getGroupMembership')
  87. ->withAnyParameters()
  88. ->willReturn([]);
  89. $this->principal->expects($this->any())->method('getCircleMembership')
  90. ->withAnyParameters()
  91. ->willReturn([]);
  92. $books = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
  93. foreach ($books as $book) {
  94. $this->backend->deleteCalendar($book['id'], true);
  95. }
  96. }
  97. public function testGetName(): void {
  98. $name = $this->publicCalendarRoot->getName();
  99. $this->assertEquals('public-calendars', $name);
  100. }
  101. public function testGetChild(): void {
  102. $calendar = $this->createPublicCalendar();
  103. $publicCalendars = $this->backend->getPublicCalendars();
  104. $this->assertEquals(1, count($publicCalendars));
  105. $this->assertEquals(true, $publicCalendars[0]['{http://owncloud.org/ns}public']);
  106. $publicCalendarURI = $publicCalendars[0]['uri'];
  107. $calendarResult = $this->publicCalendarRoot->getChild($publicCalendarURI);
  108. $this->assertEquals($calendar, $calendarResult);
  109. }
  110. public function testGetChildren(): void {
  111. $this->createPublicCalendar();
  112. $calendarResults = $this->publicCalendarRoot->getChildren();
  113. $this->assertSame([], $calendarResults);
  114. }
  115. /**
  116. * @return Calendar
  117. */
  118. protected function createPublicCalendar() {
  119. $this->backend->createCalendar(self::UNIT_TEST_USER, 'Example', []);
  120. $calendarInfo = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER)[0];
  121. $calendar = new PublicCalendar($this->backend, $calendarInfo, $this->l10n, $this->config, $this->logger);
  122. $publicUri = $calendar->setPublishStatus(true);
  123. $calendarInfo = $this->backend->getPublicCalendar($publicUri);
  124. $calendar = new PublicCalendar($this->backend, $calendarInfo, $this->l10n, $this->config, $this->logger);
  125. return $calendar;
  126. }
  127. }