PublicCalendarRootTest.php 5.2 KB

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