EventTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\Tests\unit\CalDAV\Activity\Provider;
  7. use InvalidArgumentException;
  8. use OCA\DAV\CalDAV\Activity\Provider\Base;
  9. use OCA\DAV\CalDAV\Activity\Provider\Event;
  10. use OCP\Activity\IEventMerger;
  11. use OCP\Activity\IManager;
  12. use OCP\Activity\IProvider;
  13. use OCP\App\IAppManager;
  14. use OCP\IGroupManager;
  15. use OCP\IURLGenerator;
  16. use OCP\IUserManager;
  17. use OCP\L10N\IFactory;
  18. use PHPUnit\Framework\MockObject\MockObject;
  19. use Test\TestCase;
  20. use TypeError;
  21. class EventTest extends TestCase {
  22. /** @var IUserManager|MockObject */
  23. protected $userManager;
  24. /** @var IGroupManager|MockObject */
  25. protected $groupManager;
  26. /** @var IURLGenerator|MockObject */
  27. protected $url;
  28. /** @var IProvider|Base|MockObject */
  29. protected $provider;
  30. /** @var IAppManager|MockObject */
  31. protected $appManager;
  32. /** @var IFactory|MockObject */
  33. protected $i10nFactory;
  34. /** @var IManager|MockObject */
  35. protected $activityManager;
  36. /** @var IEventMerger|MockObject */
  37. protected $eventMerger;
  38. protected function setUp(): void {
  39. parent::setUp();
  40. $this->i10nFactory = $this->createMock(IFactory::class);
  41. $this->userManager = $this->createMock(IUserManager::class);
  42. $this->groupManager = $this->createMock(IGroupManager::class);
  43. $this->activityManager = $this->createMock(IManager::class);
  44. $this->url = $this->createMock(IURLGenerator::class);
  45. $this->appManager = $this->createMock(IAppManager::class);
  46. $this->eventMerger = $this->createMock(IEventMerger::class);
  47. $this->provider = $this->getMockBuilder(Event::class)
  48. ->setConstructorArgs([
  49. $this->i10nFactory,
  50. $this->url,
  51. $this->activityManager,
  52. $this->userManager,
  53. $this->groupManager,
  54. $this->eventMerger,
  55. $this->appManager
  56. ])
  57. ->setMethods(['parse'])
  58. ->getMock();
  59. }
  60. public function dataGenerateObjectParameter() {
  61. $link = [
  62. 'object_uri' => 'someuuid.ics',
  63. 'calendar_uri' => 'personal',
  64. 'owner' => 'someuser'
  65. ];
  66. return [
  67. [23, 'c1', $link, true],
  68. [23, 'c1', $link, false],
  69. [42, 'c2', null],
  70. ];
  71. }
  72. /**
  73. * @dataProvider dataGenerateObjectParameter
  74. * @param int $id
  75. * @param string $name
  76. * @param array|null $link
  77. * @param bool $calendarAppEnabled
  78. */
  79. public function testGenerateObjectParameter(int $id, string $name, ?array $link, bool $calendarAppEnabled = true): void {
  80. $affectedUser = 'otheruser';
  81. if ($link) {
  82. $affectedUser = $link['owner'];
  83. $generatedLink = [
  84. 'view' => 'dayGridMonth',
  85. 'timeRange' => 'now',
  86. 'mode' => 'sidebar',
  87. 'objectId' => base64_encode('/remote.php/dav/calendars/' . $link['owner'] . '/' . $link['calendar_uri'] . '/' . $link['object_uri']),
  88. 'recurrenceId' => 'next'
  89. ];
  90. $this->appManager->expects($this->once())
  91. ->method('isEnabledForUser')
  92. ->with('calendar')
  93. ->willReturn($calendarAppEnabled);
  94. if ($calendarAppEnabled) {
  95. $this->url->expects($this->once())
  96. ->method('getWebroot');
  97. $this->url->expects($this->once())
  98. ->method('linkToRouteAbsolute')
  99. ->with('calendar.view.indexview.timerange.edit', $generatedLink)
  100. ->willReturn('fullLink');
  101. }
  102. }
  103. $objectParameter = ['id' => $id, 'name' => $name];
  104. if ($link) {
  105. $objectParameter['link'] = $link;
  106. }
  107. $result = [
  108. 'type' => 'calendar-event',
  109. 'id' => $id,
  110. 'name' => $name,
  111. ];
  112. if ($link && $calendarAppEnabled) {
  113. $result['link'] = 'fullLink';
  114. }
  115. $this->assertEquals($result, $this->invokePrivate($this->provider, 'generateObjectParameter', [$objectParameter, $affectedUser]));
  116. }
  117. public function testGenerateObjectParameterWithSharedCalendar(): void {
  118. $link = [
  119. 'object_uri' => 'someuuid.ics',
  120. 'calendar_uri' => 'personal',
  121. 'owner' => 'sharer'
  122. ];
  123. $generatedLink = [
  124. 'view' => 'dayGridMonth',
  125. 'timeRange' => 'now',
  126. 'mode' => 'sidebar',
  127. 'objectId' => base64_encode('/remote.php/dav/calendars/sharee/' . $link['calendar_uri'] . '_shared_by_sharer/' . $link['object_uri']),
  128. 'recurrenceId' => 'next'
  129. ];
  130. $this->appManager->expects($this->once())
  131. ->method('isEnabledForUser')
  132. ->with('calendar')
  133. ->willReturn(true);
  134. $this->url->expects($this->once())
  135. ->method('getWebroot');
  136. $this->url->expects($this->once())
  137. ->method('linkToRouteAbsolute')
  138. ->with('calendar.view.indexview.timerange.edit', $generatedLink)
  139. ->willReturn('fullLink');
  140. $objectParameter = ['id' => 42, 'name' => 'calendar', 'link' => $link];
  141. $result = [
  142. 'type' => 'calendar-event',
  143. 'id' => 42,
  144. 'name' => 'calendar',
  145. 'link' => 'fullLink',
  146. ];
  147. $this->assertEquals($result, $this->invokePrivate($this->provider, 'generateObjectParameter', [$objectParameter, 'sharee']));
  148. }
  149. public function dataGenerateObjectParameterThrows() {
  150. return [
  151. ['event', TypeError::class],
  152. [['name' => 'event']],
  153. [['id' => 42]],
  154. ];
  155. }
  156. /**
  157. * @dataProvider dataGenerateObjectParameterThrows
  158. * @param mixed $eventData
  159. * @param string $exception
  160. */
  161. public function testGenerateObjectParameterThrows($eventData, string $exception = InvalidArgumentException::class): void {
  162. $this->expectException($exception);
  163. $this->invokePrivate($this->provider, 'generateObjectParameter', [$eventData, 'no_user']);
  164. }
  165. }