CalendarImplTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * @copyright 2017, Georg Ehrke <oc.list@georgehrke.com>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  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
  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\CalDAV\Auth\CustomPrincipalPlugin;
  28. use OCA\DAV\CalDAV\CalDavBackend;
  29. use OCA\DAV\CalDAV\Calendar;
  30. use OCA\DAV\CalDAV\CalendarImpl;
  31. use OCA\DAV\CalDAV\InvitationResponse\InvitationResponseServer;
  32. use OCA\DAV\CalDAV\Schedule\Plugin;
  33. use PHPUnit\Framework\MockObject\MockObject;
  34. class CalendarImplTest extends \Test\TestCase {
  35. /** @var CalendarImpl */
  36. private $calendarImpl;
  37. /** @var Calendar | \PHPUnit\Framework\MockObject\MockObject */
  38. private $calendar;
  39. /** @var array */
  40. private $calendarInfo;
  41. /** @var CalDavBackend | \PHPUnit\Framework\MockObject\MockObject */
  42. private $backend;
  43. protected function setUp(): void {
  44. parent::setUp();
  45. $this->calendar = $this->createMock(Calendar::class);
  46. $this->calendarInfo = [
  47. 'id' => 'fancy_id_123',
  48. '{DAV:}displayname' => 'user readable name 123',
  49. '{http://apple.com/ns/ical/}calendar-color' => '#AABBCC',
  50. 'uri' => '/this/is/a/uri'
  51. ];
  52. $this->backend = $this->createMock(CalDavBackend::class);
  53. $this->calendarImpl = new CalendarImpl($this->calendar,
  54. $this->calendarInfo, $this->backend);
  55. }
  56. public function testGetKey() {
  57. $this->assertEquals($this->calendarImpl->getKey(), 'fancy_id_123');
  58. }
  59. public function testGetDisplayname() {
  60. $this->assertEquals($this->calendarImpl->getDisplayName(),'user readable name 123');
  61. }
  62. public function testGetDisplayColor() {
  63. $this->assertEquals($this->calendarImpl->getDisplayColor(), '#AABBCC');
  64. }
  65. public function testSearch() {
  66. $this->backend->expects($this->once())
  67. ->method('search')
  68. ->with($this->calendarInfo, 'abc', ['def'], ['ghi'], 42, 1337)
  69. ->willReturn(['SEARCHRESULTS']);
  70. $result = $this->calendarImpl->search('abc', ['def'], ['ghi'], 42, 1337);
  71. $this->assertEquals($result, ['SEARCHRESULTS']);
  72. }
  73. public function testGetPermissionRead() {
  74. $this->calendar->expects($this->once())
  75. ->method('getACL')
  76. ->with()
  77. ->willReturn([
  78. ['privilege' => '{DAV:}read']
  79. ]);
  80. $this->assertEquals(1, $this->calendarImpl->getPermissions());
  81. }
  82. public function testGetPermissionWrite() {
  83. $this->calendar->expects($this->once())
  84. ->method('getACL')
  85. ->with()
  86. ->willReturn([
  87. ['privilege' => '{DAV:}write']
  88. ]);
  89. $this->assertEquals(6, $this->calendarImpl->getPermissions());
  90. }
  91. public function testGetPermissionReadWrite() {
  92. $this->calendar->expects($this->once())
  93. ->method('getACL')
  94. ->with()
  95. ->willReturn([
  96. ['privilege' => '{DAV:}read'],
  97. ['privilege' => '{DAV:}write']
  98. ]);
  99. $this->assertEquals(7, $this->calendarImpl->getPermissions());
  100. }
  101. public function testGetPermissionAll() {
  102. $this->calendar->expects($this->once())
  103. ->method('getACL')
  104. ->with()
  105. ->willReturn([
  106. ['privilege' => '{DAV:}all']
  107. ]);
  108. $this->assertEquals(31, $this->calendarImpl->getPermissions());
  109. }
  110. public function testHandleImipMessage(): void {
  111. $invitationResponseServer = $this->createConfiguredMock(InvitationResponseServer::class, [
  112. 'server' => $this->createConfiguredMock(CalDavBackend::class, [
  113. 'getPlugin' => [
  114. 'auth' => $this->createMock(CustomPrincipalPlugin::class),
  115. 'schedule' => $this->createMock(Plugin::class)
  116. ]
  117. ])
  118. ]);
  119. $message = <<<EOF
  120. BEGIN:VCALENDAR
  121. PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
  122. METHOD:REPLY
  123. VERSION:2.0
  124. BEGIN:VEVENT
  125. ATTENDEE;PARTSTAT=mailto:lewis@stardew-tent-living.com:ACCEPTED
  126. ORGANIZER:mailto:pierre@generalstore.com
  127. UID:aUniqueUid
  128. SEQUENCE:2
  129. REQUEST-STATUS:2.0;Success
  130. %sEND:VEVENT
  131. END:VCALENDAR
  132. EOF;
  133. /** @var CustomPrincipalPlugin|MockObject $authPlugin */
  134. $authPlugin = $invitationResponseServer->server->getPlugin('auth');
  135. $authPlugin->expects(self::once())
  136. ->method('setPrincipalUri')
  137. ->with($this->calendar->getPrincipalURI());
  138. /** @var Plugin|MockObject $schedulingPlugin */
  139. $schedulingPlugin = $invitationResponseServer->server->getPlugin('caldav-schedule');
  140. $schedulingPlugin->expects(self::once())
  141. ->method('setPathOfCalendarObjectChange')
  142. ->with('fullcalendarname');
  143. }
  144. public function testHandleImipMessageNoCalendarUri(): void {
  145. $invitationResponseServer = $this->createConfiguredMock(InvitationResponseServer::class, [
  146. 'server' => $this->createConfiguredMock(CalDavBackend::class, [
  147. 'getPlugin' => [
  148. 'auth' => $this->createMock(CustomPrincipalPlugin::class),
  149. 'schedule' => $this->createMock(Plugin::class)
  150. ]
  151. ])
  152. ]);
  153. $message = <<<EOF
  154. BEGIN:VCALENDAR
  155. PRODID:-//Nextcloud/Nextcloud CalDAV Server//EN
  156. METHOD:REPLY
  157. VERSION:2.0
  158. BEGIN:VEVENT
  159. ATTENDEE;PARTSTAT=mailto:lewis@stardew-tent-living.com:ACCEPTED
  160. ORGANIZER:mailto:pierre@generalstore.com
  161. UID:aUniqueUid
  162. SEQUENCE:2
  163. REQUEST-STATUS:2.0;Success
  164. %sEND:VEVENT
  165. END:VCALENDAR
  166. EOF;
  167. /** @var CustomPrincipalPlugin|MockObject $authPlugin */
  168. $authPlugin = $invitationResponseServer->server->getPlugin('auth');
  169. $authPlugin->expects(self::once())
  170. ->method('setPrincipalUri')
  171. ->with($this->calendar->getPrincipalURI());
  172. unset($this->calendarInfo['uri']);
  173. $this->expectException('CalendarException');
  174. $this->calendarImpl->handleIMipMessage('filename.ics', $message);
  175. }
  176. }