CalendarHomeTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Tests\unit\CalDAV;
  8. use OCA\DAV\AppInfo\PluginManager;
  9. use OCA\DAV\CalDAV\CachedSubscription;
  10. use OCA\DAV\CalDAV\CalDavBackend;
  11. use OCA\DAV\CalDAV\CalendarHome;
  12. use OCA\DAV\CalDAV\Integration\ExternalCalendar;
  13. use OCA\DAV\CalDAV\Integration\ICalendarProvider;
  14. use OCA\DAV\CalDAV\Outbox;
  15. use OCA\DAV\CalDAV\Trashbin\TrashbinHome;
  16. use PHPUnit\Framework\MockObject\MockObject;
  17. use Psr\Log\LoggerInterface;
  18. use Sabre\CalDAV\Schedule\Inbox;
  19. use Sabre\CalDAV\Subscriptions\Subscription;
  20. use Sabre\DAV\MkCol;
  21. use Test\TestCase;
  22. class CalendarHomeTest extends TestCase {
  23. /** @var CalDavBackend | MockObject */
  24. private $backend;
  25. /** @var array */
  26. private $principalInfo = [];
  27. /** @var PluginManager */
  28. private $pluginManager;
  29. /** @var CalendarHome */
  30. private $calendarHome;
  31. /** @var MockObject|LoggerInterface */
  32. private $logger;
  33. protected function setUp(): void {
  34. parent::setUp();
  35. $this->backend = $this->createMock(CalDavBackend::class);
  36. $this->principalInfo = [
  37. 'uri' => 'user-principal-123',
  38. ];
  39. $this->pluginManager = $this->createMock(PluginManager::class);
  40. $this->logger = $this->createMock(LoggerInterface::class);
  41. $this->calendarHome = new CalendarHome(
  42. $this->backend,
  43. $this->principalInfo,
  44. $this->logger,
  45. false
  46. );
  47. // Replace PluginManager with our mock
  48. $reflection = new \ReflectionClass($this->calendarHome);
  49. $reflectionProperty = $reflection->getProperty('pluginManager');
  50. $reflectionProperty->setValue($this->calendarHome, $this->pluginManager);
  51. }
  52. public function testCreateCalendarValidName(): void {
  53. /** @var MkCol | MockObject $mkCol */
  54. $mkCol = $this->createMock(MkCol::class);
  55. $mkCol->method('getResourceType')
  56. ->willReturn(['{DAV:}collection',
  57. '{urn:ietf:params:xml:ns:caldav}calendar']);
  58. $mkCol->method('getRemainingValues')
  59. ->willReturn(['... properties ...']);
  60. $this->backend->expects(self::once())
  61. ->method('createCalendar')
  62. ->with('user-principal-123', 'name123', ['... properties ...']);
  63. $this->calendarHome->createExtendedCollection('name123', $mkCol);
  64. }
  65. public function testCreateCalendarReservedName(): void {
  66. $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
  67. $this->expectExceptionMessage('The resource you tried to create has a reserved name');
  68. /** @var MkCol | MockObject $mkCol */
  69. $mkCol = $this->createMock(MkCol::class);
  70. $this->calendarHome->createExtendedCollection('contact_birthdays', $mkCol);
  71. }
  72. public function testCreateCalendarReservedNameAppGenerated(): void {
  73. $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
  74. $this->expectExceptionMessage('The resource you tried to create has a reserved name');
  75. /** @var MkCol | MockObject $mkCol */
  76. $mkCol = $this->createMock(MkCol::class);
  77. $this->calendarHome->createExtendedCollection('app-generated--example--foo-1', $mkCol);
  78. }
  79. public function testGetChildren():void {
  80. $this->backend
  81. ->expects(self::once())
  82. ->method('getCalendarsForUser')
  83. ->with('user-principal-123')
  84. ->willReturn([]);
  85. $this->backend
  86. ->expects(self::once())
  87. ->method('getSubscriptionsForUser')
  88. ->with('user-principal-123')
  89. ->willReturn([]);
  90. $calendarPlugin1 = $this->createMock(ICalendarProvider::class);
  91. $calendarPlugin1
  92. ->expects(self::once())
  93. ->method('fetchAllForCalendarHome')
  94. ->with('user-principal-123')
  95. ->willReturn(['plugin1calendar1', 'plugin1calendar2']);
  96. $calendarPlugin2 = $this->createMock(ICalendarProvider::class);
  97. $calendarPlugin2
  98. ->expects(self::once())
  99. ->method('fetchAllForCalendarHome')
  100. ->with('user-principal-123')
  101. ->willReturn(['plugin2calendar1', 'plugin2calendar2']);
  102. $this->pluginManager
  103. ->expects(self::once())
  104. ->method('getCalendarPlugins')
  105. ->with()
  106. ->willReturn([$calendarPlugin1, $calendarPlugin2]);
  107. $actual = $this->calendarHome->getChildren();
  108. $this->assertCount(7, $actual);
  109. $this->assertInstanceOf(Inbox::class, $actual[0]);
  110. $this->assertInstanceOf(Outbox::class, $actual[1]);
  111. $this->assertInstanceOf(TrashbinHome::class, $actual[2]);
  112. $this->assertEquals('plugin1calendar1', $actual[3]);
  113. $this->assertEquals('plugin1calendar2', $actual[4]);
  114. $this->assertEquals('plugin2calendar1', $actual[5]);
  115. $this->assertEquals('plugin2calendar2', $actual[6]);
  116. }
  117. public function testGetChildNonAppGenerated():void {
  118. $this->backend
  119. ->expects(self::once())
  120. ->method('getCalendarByUri')
  121. ->with('user-principal-123')
  122. ->willReturn([]);
  123. $this->backend
  124. ->expects(self::once())
  125. ->method('getCalendarsForUser')
  126. ->with('user-principal-123')
  127. ->willReturn([]);
  128. $this->backend
  129. ->expects(self::once())
  130. ->method('getSubscriptionsForUser')
  131. ->with('user-principal-123')
  132. ->willReturn([]);
  133. $this->pluginManager
  134. ->expects(self::never())
  135. ->method('getCalendarPlugins');
  136. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  137. $this->expectExceptionMessage('Node with name \'personal\' could not be found');
  138. $this->calendarHome->getChild('personal');
  139. }
  140. public function testGetChildAppGenerated():void {
  141. $this->backend
  142. ->expects(self::once())
  143. ->method('getCalendarByUri')
  144. ->with('user-principal-123')
  145. ->willReturn([]);
  146. $this->backend
  147. ->expects(self::once())
  148. ->method('getCalendarsForUser')
  149. ->with('user-principal-123')
  150. ->willReturn([]);
  151. $this->backend
  152. ->expects(self::once())
  153. ->method('getSubscriptionsForUser')
  154. ->with('user-principal-123')
  155. ->willReturn([]);
  156. $calendarPlugin1 = $this->createMock(ICalendarProvider::class);
  157. $calendarPlugin1
  158. ->expects(self::once())
  159. ->method('getAppId')
  160. ->with()
  161. ->willReturn('calendar_plugin_1');
  162. $calendarPlugin1
  163. ->expects(self::never())
  164. ->method('hasCalendarInCalendarHome');
  165. $calendarPlugin1
  166. ->expects(self::never())
  167. ->method('getCalendarInCalendarHome');
  168. $externalCalendarMock = $this->createMock(ExternalCalendar::class);
  169. $calendarPlugin2 = $this->createMock(ICalendarProvider::class);
  170. $calendarPlugin2
  171. ->expects(self::once())
  172. ->method('getAppId')
  173. ->with()
  174. ->willReturn('calendar_plugin_2');
  175. $calendarPlugin2
  176. ->expects(self::once())
  177. ->method('hasCalendarInCalendarHome')
  178. ->with('user-principal-123', 'calendar-uri-from-backend')
  179. ->willReturn(true);
  180. $calendarPlugin2
  181. ->expects(self::once())
  182. ->method('getCalendarInCalendarHome')
  183. ->with('user-principal-123', 'calendar-uri-from-backend')
  184. ->willReturn($externalCalendarMock);
  185. $this->pluginManager
  186. ->expects(self::once())
  187. ->method('getCalendarPlugins')
  188. ->with()
  189. ->willReturn([$calendarPlugin1, $calendarPlugin2]);
  190. $actual = $this->calendarHome->getChild('app-generated--calendar_plugin_2--calendar-uri-from-backend');
  191. $this->assertEquals($externalCalendarMock, $actual);
  192. }
  193. public function testGetChildrenSubscriptions(): void {
  194. $this->backend
  195. ->expects(self::once())
  196. ->method('getCalendarsForUser')
  197. ->with('user-principal-123')
  198. ->willReturn([]);
  199. $this->backend
  200. ->expects(self::once())
  201. ->method('getSubscriptionsForUser')
  202. ->with('user-principal-123')
  203. ->willReturn([
  204. [
  205. 'id' => 'subscription-1',
  206. 'uri' => 'subscription-1',
  207. 'principaluri' => 'user-principal-123',
  208. 'source' => 'https://localhost/subscription-1',
  209. // A subscription array has actually more properties.
  210. ],
  211. [
  212. 'id' => 'subscription-2',
  213. 'uri' => 'subscription-2',
  214. 'principaluri' => 'user-principal-123',
  215. 'source' => 'https://localhost/subscription-2',
  216. // A subscription array has actually more properties.
  217. ]
  218. ]);
  219. /*
  220. * @FIXME: PluginManager should be injected via constructor.
  221. */
  222. $pluginManager = $this->createMock(PluginManager::class);
  223. $pluginManager
  224. ->expects(self::once())
  225. ->method('getCalendarPlugins')
  226. ->with()
  227. ->willReturn([]);
  228. $calendarHome = new CalendarHome(
  229. $this->backend,
  230. $this->principalInfo,
  231. $this->logger,
  232. false
  233. );
  234. $reflection = new \ReflectionClass($calendarHome);
  235. $reflectionProperty = $reflection->getProperty('pluginManager');
  236. $reflectionProperty->setValue($calendarHome, $pluginManager);
  237. $actual = $calendarHome->getChildren();
  238. $this->assertCount(5, $actual);
  239. $this->assertInstanceOf(Inbox::class, $actual[0]);
  240. $this->assertInstanceOf(Outbox::class, $actual[1]);
  241. $this->assertInstanceOf(TrashbinHome::class, $actual[2]);
  242. $this->assertInstanceOf(Subscription::class, $actual[3]);
  243. $this->assertInstanceOf(Subscription::class, $actual[4]);
  244. }
  245. public function testGetChildrenCachedSubscriptions(): void {
  246. $this->backend
  247. ->expects(self::once())
  248. ->method('getCalendarsForUser')
  249. ->with('user-principal-123')
  250. ->willReturn([]);
  251. $this->backend
  252. ->expects(self::once())
  253. ->method('getSubscriptionsForUser')
  254. ->with('user-principal-123')
  255. ->willReturn([
  256. [
  257. 'id' => 'subscription-1',
  258. 'uri' => 'subscription-1',
  259. 'principaluris' => 'user-principal-123',
  260. 'source' => 'https://localhost/subscription-1',
  261. // A subscription array has actually more properties.
  262. ],
  263. [
  264. 'id' => 'subscription-2',
  265. 'uri' => 'subscription-2',
  266. 'principaluri' => 'user-principal-123',
  267. 'source' => 'https://localhost/subscription-2',
  268. // A subscription array has actually more properties.
  269. ]
  270. ]);
  271. /*
  272. * @FIXME: PluginManager should be injected via constructor.
  273. */
  274. $pluginManager = $this->createMock(PluginManager::class);
  275. $pluginManager
  276. ->expects(self::once())
  277. ->method('getCalendarPlugins')
  278. ->with()
  279. ->willReturn([]);
  280. $calendarHome = new CalendarHome(
  281. $this->backend,
  282. $this->principalInfo,
  283. $this->logger,
  284. true
  285. );
  286. $reflection = new \ReflectionClass($calendarHome);
  287. $reflectionProperty = $reflection->getProperty('pluginManager');
  288. $reflectionProperty->setValue($calendarHome, $pluginManager);
  289. $actual = $calendarHome->getChildren();
  290. $this->assertCount(5, $actual);
  291. $this->assertInstanceOf(Inbox::class, $actual[0]);
  292. $this->assertInstanceOf(Outbox::class, $actual[1]);
  293. $this->assertInstanceOf(TrashbinHome::class, $actual[2]);
  294. $this->assertInstanceOf(CachedSubscription::class, $actual[3]);
  295. $this->assertInstanceOf(CachedSubscription::class, $actual[4]);
  296. }
  297. }