CalendarHomeTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2017, Georg Ehrke
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\DAV\Tests\unit\CalDAV;
  27. use OCA\DAV\AppInfo\PluginManager;
  28. use OCA\DAV\CalDAV\CalDavBackend;
  29. use OCA\DAV\CalDAV\CalendarHome;
  30. use OCA\DAV\CalDAV\Integration\ExternalCalendar;
  31. use OCA\DAV\CalDAV\Integration\ICalendarProvider;
  32. use OCA\DAV\CalDAV\Outbox;
  33. use OCA\DAV\CalDAV\Trashbin\TrashbinHome;
  34. use PHPUnit\Framework\MockObject\MockObject;
  35. use Psr\Log\LoggerInterface;
  36. use Sabre\CalDAV\Schedule\Inbox;
  37. use Sabre\DAV\MkCol;
  38. use Test\TestCase;
  39. class CalendarHomeTest extends TestCase {
  40. /** @var CalDavBackend | MockObject */
  41. private $backend;
  42. /** @var array */
  43. private $principalInfo = [];
  44. /** @var PluginManager */
  45. private $pluginManager;
  46. /** @var CalendarHome */
  47. private $calendarHome;
  48. /** @var MockObject|LoggerInterface */
  49. private $logger;
  50. protected function setUp(): void {
  51. parent::setUp();
  52. $this->backend = $this->createMock(CalDavBackend::class);
  53. $this->principalInfo = [
  54. 'uri' => 'user-principal-123',
  55. ];
  56. $this->pluginManager = $this->createMock(PluginManager::class);
  57. $this->logger = $this->createMock(LoggerInterface::class);
  58. $this->calendarHome = new CalendarHome(
  59. $this->backend,
  60. $this->principalInfo,
  61. $this->logger
  62. );
  63. // Replace PluginManager with our mock
  64. $reflection = new \ReflectionClass($this->calendarHome);
  65. $reflectionProperty = $reflection->getProperty('pluginManager');
  66. $reflectionProperty->setAccessible(true);
  67. $reflectionProperty->setValue($this->calendarHome, $this->pluginManager);
  68. }
  69. public function testCreateCalendarValidName(): void {
  70. /** @var MkCol | MockObject $mkCol */
  71. $mkCol = $this->createMock(MkCol::class);
  72. $mkCol->method('getResourceType')
  73. ->willReturn(['{DAV:}collection',
  74. '{urn:ietf:params:xml:ns:caldav}calendar']);
  75. $mkCol->method('getRemainingValues')
  76. ->willReturn(['... properties ...']);
  77. $this->backend->expects(self::once())
  78. ->method('createCalendar')
  79. ->with('user-principal-123', 'name123', ['... properties ...']);
  80. $this->calendarHome->createExtendedCollection('name123', $mkCol);
  81. }
  82. public function testCreateCalendarReservedName(): void {
  83. $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
  84. $this->expectExceptionMessage('The resource you tried to create has a reserved name');
  85. /** @var MkCol | MockObject $mkCol */
  86. $mkCol = $this->createMock(MkCol::class);
  87. $this->calendarHome->createExtendedCollection('contact_birthdays', $mkCol);
  88. }
  89. public function testCreateCalendarReservedNameAppGenerated(): void {
  90. $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
  91. $this->expectExceptionMessage('The resource you tried to create has a reserved name');
  92. /** @var MkCol | MockObject $mkCol */
  93. $mkCol = $this->createMock(MkCol::class);
  94. $this->calendarHome->createExtendedCollection('app-generated--example--foo-1', $mkCol);
  95. }
  96. public function testGetChildren():void {
  97. $this->backend
  98. ->expects(self::once())
  99. ->method('getCalendarsForUser')
  100. ->with('user-principal-123')
  101. ->willReturn([]);
  102. $this->backend
  103. ->expects(self::once())
  104. ->method('getSubscriptionsForUser')
  105. ->with('user-principal-123')
  106. ->willReturn([]);
  107. $calendarPlugin1 = $this->createMock(ICalendarProvider::class);
  108. $calendarPlugin1
  109. ->expects(self::once())
  110. ->method('fetchAllForCalendarHome')
  111. ->with('user-principal-123')
  112. ->willReturn(['plugin1calendar1', 'plugin1calendar2']);
  113. $calendarPlugin2 = $this->createMock(ICalendarProvider::class);
  114. $calendarPlugin2
  115. ->expects(self::once())
  116. ->method('fetchAllForCalendarHome')
  117. ->with('user-principal-123')
  118. ->willReturn(['plugin2calendar1', 'plugin2calendar2']);
  119. $this->pluginManager
  120. ->expects(self::once())
  121. ->method('getCalendarPlugins')
  122. ->with()
  123. ->willReturn([$calendarPlugin1, $calendarPlugin2]);
  124. $actual = $this->calendarHome->getChildren();
  125. $this->assertCount(7, $actual);
  126. $this->assertInstanceOf(Inbox::class, $actual[0]);
  127. $this->assertInstanceOf(Outbox::class, $actual[1]);
  128. $this->assertInstanceOf(TrashbinHome::class, $actual[2]);
  129. $this->assertEquals('plugin1calendar1', $actual[3]);
  130. $this->assertEquals('plugin1calendar2', $actual[4]);
  131. $this->assertEquals('plugin2calendar1', $actual[5]);
  132. $this->assertEquals('plugin2calendar2', $actual[6]);
  133. }
  134. public function testGetChildNonAppGenerated():void {
  135. $this->backend
  136. ->expects(self::once())
  137. ->method('getCalendarByUri')
  138. ->with('user-principal-123')
  139. ->willReturn([]);
  140. $this->backend
  141. ->expects(self::once())
  142. ->method('getCalendarsForUser')
  143. ->with('user-principal-123')
  144. ->willReturn([]);
  145. $this->backend
  146. ->expects(self::once())
  147. ->method('getSubscriptionsForUser')
  148. ->with('user-principal-123')
  149. ->willReturn([]);
  150. $this->pluginManager
  151. ->expects(self::never())
  152. ->method('getCalendarPlugins');
  153. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  154. $this->expectExceptionMessage('Node with name \'personal\' could not be found');
  155. $this->calendarHome->getChild('personal');
  156. }
  157. public function testGetChildAppGenerated():void {
  158. $this->backend
  159. ->expects(self::once())
  160. ->method('getCalendarByUri')
  161. ->with('user-principal-123')
  162. ->willReturn([]);
  163. $this->backend
  164. ->expects(self::once())
  165. ->method('getCalendarsForUser')
  166. ->with('user-principal-123')
  167. ->willReturn([]);
  168. $this->backend
  169. ->expects(self::once())
  170. ->method('getSubscriptionsForUser')
  171. ->with('user-principal-123')
  172. ->willReturn([]);
  173. $calendarPlugin1 = $this->createMock(ICalendarProvider::class);
  174. $calendarPlugin1
  175. ->expects(self::once())
  176. ->method('getAppId')
  177. ->with()
  178. ->willReturn('calendar_plugin_1');
  179. $calendarPlugin1
  180. ->expects(self::never())
  181. ->method('hasCalendarInCalendarHome');
  182. $calendarPlugin1
  183. ->expects(self::never())
  184. ->method('getCalendarInCalendarHome');
  185. $externalCalendarMock = $this->createMock(ExternalCalendar::class);
  186. $calendarPlugin2 = $this->createMock(ICalendarProvider::class);
  187. $calendarPlugin2
  188. ->expects(self::once())
  189. ->method('getAppId')
  190. ->with()
  191. ->willReturn('calendar_plugin_2');
  192. $calendarPlugin2
  193. ->expects(self::once())
  194. ->method('hasCalendarInCalendarHome')
  195. ->with('user-principal-123', 'calendar-uri-from-backend')
  196. ->willReturn(true);
  197. $calendarPlugin2
  198. ->expects(self::once())
  199. ->method('getCalendarInCalendarHome')
  200. ->with('user-principal-123', 'calendar-uri-from-backend')
  201. ->willReturn($externalCalendarMock);
  202. $this->pluginManager
  203. ->expects(self::once())
  204. ->method('getCalendarPlugins')
  205. ->with()
  206. ->willReturn([$calendarPlugin1, $calendarPlugin2]);
  207. $actual = $this->calendarHome->getChild('app-generated--calendar_plugin_2--calendar-uri-from-backend');
  208. $this->assertEquals($externalCalendarMock, $actual);
  209. }
  210. }