1
0

PublicCalendarRootTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. $this->principal->expects($this->any())->method('getGroupMembership')
  83. ->withAnyParameters()
  84. ->willReturn([]);
  85. $this->principal->expects($this->any())->method('getCircleMembership')
  86. ->withAnyParameters()
  87. ->willReturn([]);
  88. $this->backend = new CalDavBackend(
  89. $db,
  90. $this->principal,
  91. $this->userManager,
  92. $this->groupManager,
  93. $this->random,
  94. $this->logger,
  95. $dispatcher,
  96. $config
  97. );
  98. $this->l10n = $this->getMockBuilder(IL10N::class)
  99. ->disableOriginalConstructor()->getMock();
  100. $this->config = $this->createMock(IConfig::class);
  101. $this->publicCalendarRoot = new PublicCalendarRoot($this->backend,
  102. $this->l10n, $this->config, $this->logger);
  103. }
  104. protected function tearDown(): void {
  105. parent::tearDown();
  106. if (is_null($this->backend)) {
  107. return;
  108. }
  109. $this->principal->expects($this->any())->method('getGroupMembership')
  110. ->withAnyParameters()
  111. ->willReturn([]);
  112. $this->principal->expects($this->any())->method('getCircleMembership')
  113. ->withAnyParameters()
  114. ->willReturn([]);
  115. $books = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER);
  116. foreach ($books as $book) {
  117. $this->backend->deleteCalendar($book['id'], true);
  118. }
  119. }
  120. public function testGetName(): void {
  121. $name = $this->publicCalendarRoot->getName();
  122. $this->assertEquals('public-calendars', $name);
  123. }
  124. public function testGetChild(): void {
  125. $calendar = $this->createPublicCalendar();
  126. $publicCalendars = $this->backend->getPublicCalendars();
  127. $this->assertEquals(1, count($publicCalendars));
  128. $this->assertEquals(true, $publicCalendars[0]['{http://owncloud.org/ns}public']);
  129. $publicCalendarURI = $publicCalendars[0]['uri'];
  130. $calendarResult = $this->publicCalendarRoot->getChild($publicCalendarURI);
  131. $this->assertEquals($calendar, $calendarResult);
  132. }
  133. public function testGetChildren(): void {
  134. $this->createPublicCalendar();
  135. $calendarResults = $this->publicCalendarRoot->getChildren();
  136. $this->assertSame([], $calendarResults);
  137. }
  138. /**
  139. * @return Calendar
  140. */
  141. protected function createPublicCalendar() {
  142. $this->backend->createCalendar(self::UNIT_TEST_USER, 'Example', []);
  143. $calendarInfo = $this->backend->getCalendarsForUser(self::UNIT_TEST_USER)[0];
  144. $calendar = new PublicCalendar($this->backend, $calendarInfo, $this->l10n, $this->config, $this->logger);
  145. $publicUri = $calendar->setPublishStatus(true);
  146. $calendarInfo = $this->backend->getPublicCalendar($publicUri);
  147. $calendar = new PublicCalendar($this->backend, $calendarInfo, $this->l10n, $this->config, $this->logger);
  148. return $calendar;
  149. }
  150. }