You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PublicCalendarRootTest.php 5.1KB

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