CalendarHomeTest.php 7.0 KB

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