EventTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 static function generateObjectParameterLinkEncodingDataProvider(): array {
  118. return [
  119. [ // Shared calendar
  120. [
  121. 'object_uri' => 'someuuid.ics',
  122. 'calendar_uri' => 'personal',
  123. 'owner' => 'sharer'
  124. ],
  125. base64_encode('/remote.php/dav/calendars/sharee/personal_shared_by_sharer/someuuid.ics'),
  126. ],
  127. [ // Shared calendar with umlauts
  128. [
  129. 'object_uri' => 'someuuid.ics',
  130. 'calendar_uri' => 'umlaut_äüöß',
  131. 'owner' => 'sharer'
  132. ],
  133. base64_encode('/remote.php/dav/calendars/sharee/umlaut_%c3%a4%c3%bc%c3%b6%c3%9f_shared_by_sharer/someuuid.ics'),
  134. ],
  135. [ // Shared calendar with umlauts and mixed casing
  136. [
  137. 'object_uri' => 'someuuid.ics',
  138. 'calendar_uri' => 'Umlaut_äüöß',
  139. 'owner' => 'sharer'
  140. ],
  141. base64_encode('/remote.php/dav/calendars/sharee/Umlaut_%c3%a4%c3%bc%c3%b6%c3%9f_shared_by_sharer/someuuid.ics'),
  142. ],
  143. [ // Owned calendar with umlauts
  144. [
  145. 'object_uri' => 'someuuid.ics',
  146. 'calendar_uri' => 'umlaut_äüöß',
  147. 'owner' => 'sharee'
  148. ],
  149. base64_encode('/remote.php/dav/calendars/sharee/umlaut_%c3%a4%c3%bc%c3%b6%c3%9f/someuuid.ics'),
  150. ],
  151. [ // Owned calendar with umlauts and mixed casing
  152. [
  153. 'object_uri' => 'someuuid.ics',
  154. 'calendar_uri' => 'Umlaut_äüöß',
  155. 'owner' => 'sharee'
  156. ],
  157. base64_encode('/remote.php/dav/calendars/sharee/Umlaut_%c3%a4%c3%bc%c3%b6%c3%9f/someuuid.ics'),
  158. ],
  159. ];
  160. }
  161. /** @dataProvider generateObjectParameterLinkEncodingDataProvider */
  162. public function testGenerateObjectParameterLinkEncoding(array $link, string $objectId): void {
  163. $generatedLink = [
  164. 'view' => 'dayGridMonth',
  165. 'timeRange' => 'now',
  166. 'mode' => 'sidebar',
  167. 'objectId' => $objectId,
  168. 'recurrenceId' => 'next'
  169. ];
  170. $this->appManager->expects($this->once())
  171. ->method('isEnabledForUser')
  172. ->with('calendar')
  173. ->willReturn(true);
  174. $this->url->expects($this->once())
  175. ->method('getWebroot');
  176. $this->url->expects($this->once())
  177. ->method('linkToRouteAbsolute')
  178. ->with('calendar.view.indexview.timerange.edit', $generatedLink)
  179. ->willReturn('fullLink');
  180. $objectParameter = ['id' => 42, 'name' => 'calendar', 'link' => $link];
  181. $result = [
  182. 'type' => 'calendar-event',
  183. 'id' => 42,
  184. 'name' => 'calendar',
  185. 'link' => 'fullLink',
  186. ];
  187. $this->assertEquals($result, $this->invokePrivate($this->provider, 'generateObjectParameter', [$objectParameter, 'sharee']));
  188. }
  189. public function dataGenerateObjectParameterThrows() {
  190. return [
  191. ['event', TypeError::class],
  192. [['name' => 'event']],
  193. [['id' => 42]],
  194. ];
  195. }
  196. /**
  197. * @dataProvider dataGenerateObjectParameterThrows
  198. * @param mixed $eventData
  199. * @param string $exception
  200. */
  201. public function testGenerateObjectParameterThrows($eventData, string $exception = InvalidArgumentException::class): void {
  202. $this->expectException($exception);
  203. $this->invokePrivate($this->provider, 'generateObjectParameter', [$eventData, 'no_user']);
  204. }
  205. }