You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CalendarHomeTest.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 Sabre\CalDAV\Schedule\Inbox;
  34. use Sabre\DAV\MkCol;
  35. use Test\TestCase;
  36. class CalendarHomeTest extends TestCase {
  37. /** @var CalDavBackend | \PHPUnit\Framework\MockObject\MockObject */
  38. private $backend;
  39. /** @var array */
  40. private $principalInfo = [];
  41. /** @var PluginManager */
  42. private $pluginManager;
  43. /** @var CalendarHome */
  44. private $calendarHome;
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->backend = $this->createMock(CalDavBackend::class);
  48. $this->principalInfo = [
  49. 'uri' => 'user-principal-123',
  50. ];
  51. $this->pluginManager = $this->createMock(PluginManager::class);
  52. $this->calendarHome = new CalendarHome($this->backend,
  53. $this->principalInfo);
  54. // Replace PluginManager with our mock
  55. $reflection = new \ReflectionClass($this->calendarHome);
  56. $reflectionProperty = $reflection->getProperty('pluginManager');
  57. $reflectionProperty->setAccessible(true);
  58. $reflectionProperty->setValue($this->calendarHome, $this->pluginManager);
  59. }
  60. public function testCreateCalendarValidName() {
  61. /** @var MkCol | \PHPUnit\Framework\MockObject\MockObject $mkCol */
  62. $mkCol = $this->createMock(MkCol::class);
  63. $mkCol->method('getResourceType')
  64. ->willReturn(['{DAV:}collection',
  65. '{urn:ietf:params:xml:ns:caldav}calendar']);
  66. $mkCol->method('getRemainingValues')
  67. ->willReturn(['... properties ...']);
  68. $this->backend->expects($this->once())
  69. ->method('createCalendar')
  70. ->with('user-principal-123', 'name123', ['... properties ...']);
  71. $this->calendarHome->createExtendedCollection('name123', $mkCol);
  72. }
  73. public function testCreateCalendarReservedName() {
  74. $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
  75. $this->expectExceptionMessage('The resource you tried to create has a reserved name');
  76. /** @var MkCol | \PHPUnit\Framework\MockObject\MockObject $mkCol */
  77. $mkCol = $this->createMock(MkCol::class);
  78. $this->calendarHome->createExtendedCollection('contact_birthdays', $mkCol);
  79. }
  80. public function testCreateCalendarReservedNameAppGenerated() {
  81. $this->expectException(\Sabre\DAV\Exception\MethodNotAllowed::class);
  82. $this->expectExceptionMessage('The resource you tried to create has a reserved name');
  83. /** @var MkCol | \PHPUnit\Framework\MockObject\MockObject $mkCol */
  84. $mkCol = $this->createMock(MkCol::class);
  85. $this->calendarHome->createExtendedCollection('app-generated--example--foo-1', $mkCol);
  86. }
  87. public function testGetChildren():void {
  88. $this->backend
  89. ->expects($this->at(0))
  90. ->method('getCalendarsForUser')
  91. ->with('user-principal-123')
  92. ->willReturn([]);
  93. $this->backend
  94. ->expects($this->at(1))
  95. ->method('getSubscriptionsForUser')
  96. ->with('user-principal-123')
  97. ->willReturn([]);
  98. $calendarPlugin1 = $this->createMock(ICalendarProvider::class);
  99. $calendarPlugin1
  100. ->expects($this->once())
  101. ->method('fetchAllForCalendarHome')
  102. ->with('user-principal-123')
  103. ->willReturn(['plugin1calendar1', 'plugin1calendar2']);
  104. $calendarPlugin2 = $this->createMock(ICalendarProvider::class);
  105. $calendarPlugin2
  106. ->expects($this->once())
  107. ->method('fetchAllForCalendarHome')
  108. ->with('user-principal-123')
  109. ->willReturn(['plugin2calendar1', 'plugin2calendar2']);
  110. $this->pluginManager
  111. ->expects($this->once())
  112. ->method('getCalendarPlugins')
  113. ->with()
  114. ->willReturn([$calendarPlugin1, $calendarPlugin2]);
  115. $actual = $this->calendarHome->getChildren();
  116. $this->assertCount(6, $actual);
  117. $this->assertInstanceOf(Inbox::class, $actual[0]);
  118. $this->assertInstanceOf(Outbox::class, $actual[1]);
  119. $this->assertEquals('plugin1calendar1', $actual[2]);
  120. $this->assertEquals('plugin1calendar2', $actual[3]);
  121. $this->assertEquals('plugin2calendar1', $actual[4]);
  122. $this->assertEquals('plugin2calendar2', $actual[5]);
  123. }
  124. public function testGetChildNonAppGenerated():void {
  125. $this->backend
  126. ->expects($this->at(0))
  127. ->method('getCalendarsForUser')
  128. ->with('user-principal-123')
  129. ->willReturn([]);
  130. $this->backend
  131. ->expects($this->at(1))
  132. ->method('getSubscriptionsForUser')
  133. ->with('user-principal-123')
  134. ->willReturn([]);
  135. $this->pluginManager
  136. ->expects($this->never())
  137. ->method('getCalendarPlugins');
  138. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  139. $this->expectExceptionMessage('Node with name \'personal\' could not be found');
  140. $this->calendarHome->getChild('personal');
  141. }
  142. public function testGetChildAppGenerated():void {
  143. $this->backend
  144. ->expects($this->at(0))
  145. ->method('getCalendarsForUser')
  146. ->with('user-principal-123')
  147. ->willReturn([]);
  148. $this->backend
  149. ->expects($this->at(1))
  150. ->method('getSubscriptionsForUser')
  151. ->with('user-principal-123')
  152. ->willReturn([]);
  153. $calendarPlugin1 = $this->createMock(ICalendarProvider::class);
  154. $calendarPlugin1
  155. ->expects($this->once())
  156. ->method('getAppId')
  157. ->with()
  158. ->willReturn('calendar_plugin_1');
  159. $calendarPlugin1
  160. ->expects($this->never())
  161. ->method('hasCalendarInCalendarHome');
  162. $calendarPlugin1
  163. ->expects($this->never())
  164. ->method('getCalendarInCalendarHome');
  165. $externalCalendarMock = $this->createMock(ExternalCalendar::class);
  166. $calendarPlugin2 = $this->createMock(ICalendarProvider::class);
  167. $calendarPlugin2
  168. ->expects($this->once())
  169. ->method('getAppId')
  170. ->with()
  171. ->willReturn('calendar_plugin_2');
  172. $calendarPlugin2
  173. ->expects($this->once())
  174. ->method('hasCalendarInCalendarHome')
  175. ->with('user-principal-123', 'calendar-uri-from-backend')
  176. ->willReturn(true);
  177. $calendarPlugin2
  178. ->expects($this->once())
  179. ->method('getCalendarInCalendarHome')
  180. ->with('user-principal-123', 'calendar-uri-from-backend')
  181. ->willReturn($externalCalendarMock);
  182. $this->pluginManager
  183. ->expects($this->once())
  184. ->method('getCalendarPlugins')
  185. ->with()
  186. ->willReturn([$calendarPlugin1, $calendarPlugin2]);
  187. $actual = $this->calendarHome->getChild('app-generated--calendar_plugin_2--calendar-uri-from-backend');
  188. $this->assertEquals($externalCalendarMock, $actual);
  189. }
  190. }