EventTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2020 Thomas Citharel <nextcloud@tcit.fr>
  4. *
  5. * @author Thomas Citharel <nextcloud@tcit.fr>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\DAV\Tests\unit\CalDAV\Activity\Provider;
  24. use InvalidArgumentException;
  25. use OCA\DAV\CalDAV\Activity\Provider\Base;
  26. use OCA\DAV\CalDAV\Activity\Provider\Event;
  27. use OCP\Activity\IEventMerger;
  28. use OCP\Activity\IManager;
  29. use OCP\Activity\IProvider;
  30. use OCP\App\IAppManager;
  31. use OCP\IGroupManager;
  32. use OCP\IURLGenerator;
  33. use OCP\IUserManager;
  34. use OCP\L10N\IFactory;
  35. use PHPUnit\Framework\MockObject\MockObject;
  36. use Test\TestCase;
  37. use TypeError;
  38. class EventTest extends TestCase {
  39. /** @var IUserManager|MockObject */
  40. protected $userManager;
  41. /** @var IGroupManager|MockObject */
  42. protected $groupManager;
  43. /** @var IURLGenerator|MockObject */
  44. protected $url;
  45. /** @var IProvider|Base|MockObject */
  46. protected $provider;
  47. /** @var IAppManager|MockObject */
  48. protected $appManager;
  49. /** @var IFactory|MockObject */
  50. protected $i10nFactory;
  51. /** @var IManager|MockObject */
  52. protected $activityManager;
  53. /** @var IEventMerger|MockObject */
  54. protected $eventMerger;
  55. protected function setUp(): void {
  56. parent::setUp();
  57. $this->i10nFactory = $this->createMock(IFactory::class);
  58. $this->userManager = $this->createMock(IUserManager::class);
  59. $this->groupManager = $this->createMock(IGroupManager::class);
  60. $this->activityManager = $this->createMock(IManager::class);
  61. $this->url = $this->createMock(IURLGenerator::class);
  62. $this->appManager = $this->createMock(IAppManager::class);
  63. $this->eventMerger = $this->createMock(IEventMerger::class);
  64. $this->provider = $this->getMockBuilder(Event::class)
  65. ->setConstructorArgs([
  66. $this->i10nFactory,
  67. $this->url,
  68. $this->activityManager,
  69. $this->userManager,
  70. $this->groupManager,
  71. $this->eventMerger,
  72. $this->appManager
  73. ])
  74. ->setMethods(['parse'])
  75. ->getMock();
  76. }
  77. public function dataGenerateObjectParameter() {
  78. $link = [
  79. 'object_uri' => 'someuuid.ics',
  80. 'calendar_uri' => 'personal',
  81. 'owner' => 'someuser'
  82. ];
  83. return [
  84. [23, 'c1', $link, true],
  85. [23, 'c1', $link, false],
  86. [42, 'c2', null],
  87. ];
  88. }
  89. /**
  90. * @dataProvider dataGenerateObjectParameter
  91. * @param int $id
  92. * @param string $name
  93. * @param array|null $link
  94. * @param bool $calendarAppEnabled
  95. */
  96. public function testGenerateObjectParameter(int $id, string $name, ?array $link, bool $calendarAppEnabled = true): void {
  97. if ($link) {
  98. $generatedLink = [
  99. 'view' => 'dayGridMonth',
  100. 'timeRange' => 'now',
  101. 'mode' => 'sidebar',
  102. 'objectId' => base64_encode('/remote.php/dav/calendars/' . $link['owner'] . '/' . $link['calendar_uri'] . '/' . $link['object_uri']),
  103. 'recurrenceId' => 'next'
  104. ];
  105. $this->appManager->expects($this->once())
  106. ->method('isEnabledForUser')
  107. ->with('calendar')
  108. ->willReturn($calendarAppEnabled);
  109. if ($calendarAppEnabled) {
  110. $this->url->expects($this->once())
  111. ->method('getWebroot');
  112. $this->url->expects($this->once())
  113. ->method('linkToRouteAbsolute')
  114. ->with('calendar.view.indexview.timerange.edit', $generatedLink)
  115. ->willReturn('fullLink');
  116. }
  117. }
  118. $objectParameter = ['id' => $id, 'name' => $name];
  119. if ($link) {
  120. $objectParameter['link'] = $link;
  121. }
  122. $result = [
  123. 'type' => 'calendar-event',
  124. 'id' => $id,
  125. 'name' => $name,
  126. ];
  127. if ($link && $calendarAppEnabled) {
  128. $result['link'] = 'fullLink';
  129. }
  130. $this->assertEquals($result, $this->invokePrivate($this->provider, 'generateObjectParameter', [$objectParameter]));
  131. }
  132. public function dataGenerateObjectParameterThrows() {
  133. return [
  134. ['event', TypeError::class],
  135. [['name' => 'event']],
  136. [['id' => 42]],
  137. ];
  138. }
  139. /**
  140. * @dataProvider dataGenerateObjectParameterThrows
  141. * @param mixed $eventData
  142. * @param string $exception
  143. */
  144. public function testGenerateObjectParameterThrows($eventData, string $exception = InvalidArgumentException::class): void {
  145. $this->expectException($exception);
  146. $this->invokePrivate($this->provider, 'generateObjectParameter', [$eventData]);
  147. }
  148. }