1
0

PublicCalendarRootTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Thomas Citharel <nextcloud@tcit.fr>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Citharel <nextcloud@tcit.fr>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. * @author Vinicius Cubas Brand <vinicius@eita.org.br>
  14. *
  15. * @license GNU AGPL version 3 or any later version
  16. *
  17. * This program is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License as
  19. * published by the Free Software Foundation, either version 3 of the
  20. * License, or (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. *
  30. */
  31. namespace OCA\DAV\Tests\unit\CalDAV;
  32. use OCA\DAV\CalDAV\CalDavBackend;
  33. use OCA\DAV\CalDAV\Calendar;
  34. use OCA\DAV\CalDAV\PublicCalendar;
  35. use OCA\DAV\CalDAV\PublicCalendarRoot;
  36. use OCA\DAV\Connector\Sabre\Principal;
  37. use OCP\EventDispatcher\IEventDispatcher;
  38. use OCP\IConfig;
  39. use OCP\IGroupManager;
  40. use OCP\IL10N;
  41. use OCP\IUserManager;
  42. use OCP\Security\ISecureRandom;
  43. use Psr\Log\LoggerInterface;
  44. use Test\TestCase;
  45. /**
  46. * Class PublicCalendarRootTest
  47. *
  48. * @group DB
  49. *
  50. * @package OCA\DAV\Tests\unit\CalDAV
  51. */
  52. class PublicCalendarRootTest extends TestCase {
  53. public const UNIT_TEST_USER = '';
  54. /** @var CalDavBackend */
  55. private $backend;
  56. /** @var PublicCalendarRoot */
  57. private $publicCalendarRoot;
  58. /** @var IL10N */
  59. private $l10n;
  60. /** @var Principal|\PHPUnit\Framework\MockObject\MockObject */
  61. private $principal;
  62. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  63. protected $userManager;
  64. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
  65. protected $groupManager;
  66. /** @var IConfig */
  67. protected $config;
  68. /** @var ISecureRandom */
  69. private $random;
  70. /** @var LoggerInterface */
  71. private $logger;
  72. protected function setUp(): void {
  73. parent::setUp();
  74. $db = \OC::$server->getDatabaseConnection();
  75. $this->principal = $this->createMock('OCA\DAV\Connector\Sabre\Principal');
  76. $this->userManager = $this->createMock(IUserManager::class);
  77. $this->groupManager = $this->createMock(IGroupManager::class);
  78. $this->random = \OC::$server->getSecureRandom();
  79. $this->logger = $this->createMock(LoggerInterface::class);
  80. $dispatcher = $this->createMock(IEventDispatcher::class);
  81. $config = $this->createMock(IConfig::class);
  82. $sharingBackend = $this->createMock(\OCA\DAV\CalDAV\Sharing\Backend::class);
  83. $this->principal->expects($this->any())->method('getGroupMembership')
  84. ->withAnyParameters()
  85. ->willReturn([]);
  86. $this->principal->expects($this->any())->method('getCircleMembership')
  87. ->withAnyParameters()
  88. ->willReturn([]);
  89. $this->backend = new CalDavBackend(
  90. $db,
  91. $this->principal,
  92. $this->userManager,
  93. $this->random,
  94. $this->logger,
  95. $dispatcher,
  96. $config,
  97. $sharingBackend,
  98. false,
  99. );
  100. $this->l10n = $this->getMockBuilder(IL10N::class)
  101. ->disableOriginalConstructor()->getMock();
  102. $this->config = $this->createMock(IConfig::class);
  103. $this->publicCalendarRoot = new PublicCalendarRoot($this->backend,
  104. $this->l10n, $this->config, $this->logger);
  105. }
  106. protected function tearDown(): void {
  107. parent::tearDown();
  108. if (is_null($this->backend)) {
  109. return;
  110. }
  111. $this->principal->expects($this->any())->method('getGroupMembership')
  112. ->withAnyParameters()
  113. ->willReturn([]);
  114. $this->principal->expects($this->any())->method('getCircleMembership')
  115. ->withAnyParameters()
  116. ->willReturn([]);
  117. $books = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
  118. foreach ($books as $book) {
  119. $this->backend->deleteCalendar($book['id'], true);
  120. }
  121. }
  122. public function testGetName(): void {
  123. $name = $this->publicCalendarRoot->getName();
  124. $this->assertEquals('public-calendars', $name);
  125. }
  126. public function testGetChild(): void {
  127. $calendar = $this->createPublicCalendar();
  128. $publicCalendars = $this->backend->getPublicCalendars();
  129. $this->assertEquals(1, count($publicCalendars));
  130. $this->assertEquals(true, $publicCalendars[0]['{http://owncloud.org/ns}public']);
  131. $publicCalendarURI = $publicCalendars[0]['uri'];
  132. $calendarResult = $this->publicCalendarRoot->getChild($publicCalendarURI);
  133. $this->assertEquals($calendar, $calendarResult);
  134. }
  135. public function testGetChildren(): void {
  136. $this->createPublicCalendar();
  137. $calendarResults = $this->publicCalendarRoot->getChildren();
  138. $this->assertSame([], $calendarResults);
  139. }
  140. /**
  141. * @return Calendar
  142. */
  143. protected function createPublicCalendar() {
  144. $this->backend->createCalendar(self::UNIT_TEST_USER, 'Example', []);
  145. $calendarInfo = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER)[0];
  146. $calendar = new PublicCalendar($this->backend, $calendarInfo, $this->l10n, $this->config, $this->logger);
  147. $publicUri = $calendar->setPublishStatus(true);
  148. $calendarInfo = $this->backend->getPublicCalendar($publicUri);
  149. $calendar = new PublicCalendar($this->backend, $calendarInfo, $this->l10n, $this->config, $this->logger);
  150. return $calendar;
  151. }
  152. }