1
0

PublicCalendarRootTest.php 5.0 KB

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