PluginTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Citharel <nextcloud@tcit.fr>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\DAV\Tests\unit\CalDAV\Schedule;
  28. use OCA\DAV\CalDAV\CalDavBackend;
  29. use OCA\DAV\CalDAV\CalendarHome;
  30. use OCA\DAV\CalDAV\Plugin as CalDAVPlugin;
  31. use OCA\DAV\CalDAV\Schedule\Plugin;
  32. use OCP\IConfig;
  33. use PHPUnit\Framework\MockObject\MockObject;
  34. use Sabre\DAV\PropFind;
  35. use Sabre\DAV\Server;
  36. use Sabre\DAV\Tree;
  37. use Sabre\DAV\Xml\Property\Href;
  38. use Sabre\DAV\Xml\Property\LocalHref;
  39. use Sabre\DAVACL\IPrincipal;
  40. use Sabre\HTTP\ResponseInterface;
  41. use Sabre\VObject\Parameter;
  42. use Sabre\VObject\Property\ICalendar\CalAddress;
  43. use Sabre\Xml\Service;
  44. use Test\TestCase;
  45. class PluginTest extends TestCase {
  46. /** @var Plugin */
  47. private $plugin;
  48. /** @var Server|MockObject */
  49. private $server;
  50. /** @var IConfig|MockObject */
  51. private $config;
  52. protected function setUp(): void {
  53. parent::setUp();
  54. $this->server = $this->createMock(Server::class);
  55. $this->config = $this->createMock(IConfig::class);
  56. $response = $this->getMockBuilder(ResponseInterface::class)
  57. ->disableOriginalConstructor()
  58. ->getMock();
  59. $this->server->httpResponse = $response;
  60. $this->server->xml = new Service();
  61. $this->plugin = new Plugin($this->config);
  62. $this->plugin->initialize($this->server);
  63. }
  64. public function testInitialize() {
  65. $plugin = new Plugin($this->config);
  66. $this->server->expects($this->at(7))
  67. ->method('on')
  68. ->with('propFind', [$plugin, 'propFindDefaultCalendarUrl'], 90);
  69. $this->server->expects($this->at(8))
  70. ->method('on')
  71. ->with('afterWriteContent', [$plugin, 'dispatchSchedulingResponses']);
  72. $this->server->expects($this->at(9))
  73. ->method('on')
  74. ->with('afterCreateFile', [$plugin, 'dispatchSchedulingResponses']);
  75. $plugin->initialize($this->server);
  76. }
  77. public function testGetAddressesForPrincipal() {
  78. $href = $this->createMock(Href::class);
  79. $href
  80. ->expects($this->once())
  81. ->method('getHrefs')
  82. ->willReturn(['lukas@nextcloud.com', 'rullzer@nextcloud.com']);
  83. $this->server
  84. ->expects($this->once())
  85. ->method('getProperties')
  86. ->with(
  87. 'MyPrincipal',
  88. [
  89. '{urn:ietf:params:xml:ns:caldav}calendar-user-address-set',
  90. ]
  91. )
  92. ->willReturn([
  93. '{urn:ietf:params:xml:ns:caldav}calendar-user-address-set' => $href
  94. ]);
  95. $result = $this->invokePrivate($this->plugin, 'getAddressesForPrincipal', ['MyPrincipal']);
  96. $this->assertSame(['lukas@nextcloud.com', 'rullzer@nextcloud.com'], $result);
  97. }
  98. public function testGetAddressesForPrincipalEmpty() {
  99. $this->server
  100. ->expects($this->once())
  101. ->method('getProperties')
  102. ->with(
  103. 'MyPrincipal',
  104. [
  105. '{urn:ietf:params:xml:ns:caldav}calendar-user-address-set',
  106. ]
  107. )
  108. ->willReturn(null);
  109. $result = $this->invokePrivate($this->plugin, 'getAddressesForPrincipal', ['MyPrincipal']);
  110. $this->assertSame([], $result);
  111. }
  112. public function testStripOffMailTo() {
  113. $this->assertEquals('test@example.com', $this->invokePrivate($this->plugin, 'stripOffMailTo', ['test@example.com']));
  114. $this->assertEquals('test@example.com', $this->invokePrivate($this->plugin, 'stripOffMailTo', ['mailto:test@example.com']));
  115. }
  116. public function testGetAttendeeRSVP() {
  117. $property1 = $this->createMock(CalAddress::class);
  118. $parameter1 = $this->createMock(Parameter::class);
  119. $property1->expects($this->once())
  120. ->method('offsetGet')
  121. ->with('RSVP')
  122. ->willReturn($parameter1);
  123. $parameter1->expects($this->once())
  124. ->method('getValue')
  125. ->with()
  126. ->willReturn('TRUE');
  127. $property2 = $this->createMock(CalAddress::class);
  128. $parameter2 = $this->createMock(Parameter::class);
  129. $property2->expects($this->once())
  130. ->method('offsetGet')
  131. ->with('RSVP')
  132. ->willReturn($parameter2);
  133. $parameter2->expects($this->once())
  134. ->method('getValue')
  135. ->with()
  136. ->willReturn('FALSE');
  137. $property3 = $this->createMock(CalAddress::class);
  138. $property3->expects($this->once())
  139. ->method('offsetGet')
  140. ->with('RSVP')
  141. ->willReturn(null);
  142. $this->assertTrue($this->invokePrivate($this->plugin, 'getAttendeeRSVP', [$property1]));
  143. $this->assertFalse($this->invokePrivate($this->plugin, 'getAttendeeRSVP', [$property2]));
  144. $this->assertFalse($this->invokePrivate($this->plugin, 'getAttendeeRSVP', [$property3]));
  145. }
  146. public function propFindDefaultCalendarUrlProvider(): array {
  147. return [
  148. [
  149. 'principals/users/myuser',
  150. 'calendars/myuser',
  151. false,
  152. CalDavBackend::PERSONAL_CALENDAR_URI,
  153. CalDavBackend::PERSONAL_CALENDAR_NAME,
  154. true
  155. ],
  156. [
  157. 'principals/users/myuser',
  158. 'calendars/myuser',
  159. false,
  160. CalDavBackend::PERSONAL_CALENDAR_URI,
  161. CalDavBackend::PERSONAL_CALENDAR_NAME,
  162. false
  163. ],
  164. [
  165. 'principals/users/myuser',
  166. null,
  167. false,
  168. CalDavBackend::PERSONAL_CALENDAR_URI,
  169. CalDavBackend::PERSONAL_CALENDAR_NAME,
  170. true
  171. ],
  172. [
  173. 'principals/users/myuser',
  174. 'calendars/myuser',
  175. false,
  176. CalDavBackend::PERSONAL_CALENDAR_URI,
  177. CalDavBackend::PERSONAL_CALENDAR_NAME,
  178. true,
  179. false,
  180. ],
  181. [
  182. 'principals/users/myuser',
  183. 'calendars/myuser',
  184. false,
  185. 'my_other_calendar',
  186. 'My Other Calendar',
  187. true
  188. ],
  189. [
  190. 'principals/calendar-resources',
  191. 'system-calendars/calendar-resources/myuser',
  192. true,
  193. CalDavBackend::RESOURCE_BOOKING_CALENDAR_URI,
  194. CalDavBackend::RESOURCE_BOOKING_CALENDAR_NAME,
  195. true
  196. ],
  197. [
  198. 'principals/calendar-resources',
  199. 'system-calendars/calendar-resources/myuser',
  200. true,
  201. CalDavBackend::RESOURCE_BOOKING_CALENDAR_URI,
  202. CalDavBackend::RESOURCE_BOOKING_CALENDAR_NAME,
  203. false
  204. ],
  205. [
  206. 'principals/something-else',
  207. 'calendars/whatever',
  208. false,
  209. CalDavBackend::PERSONAL_CALENDAR_URI,
  210. CalDavBackend::PERSONAL_CALENDAR_NAME,
  211. true
  212. ],
  213. ];
  214. }
  215. /**
  216. * @dataProvider propFindDefaultCalendarUrlProvider
  217. * @param string $principalUri
  218. * @param string $calendarHome
  219. * @param bool $isResource
  220. * @param string $calendarUri
  221. * @param string $displayName
  222. * @param bool $exists
  223. * @param bool $propertiesForPath
  224. */
  225. public function testPropFindDefaultCalendarUrl(string $principalUri, ?string $calendarHome, bool $isResource, string $calendarUri, string $displayName, bool $exists, bool $propertiesForPath = true) {
  226. /** @var PropFind $propFind */
  227. $propFind = new PropFind(
  228. $principalUri,
  229. [
  230. Plugin::SCHEDULE_DEFAULT_CALENDAR_URL
  231. ],
  232. 0
  233. );
  234. /** @var IPrincipal|MockObject $node */
  235. $node = $this->getMockBuilder(IPrincipal::class)
  236. ->disableOriginalConstructor()
  237. ->getMock();
  238. $node->expects($this->once())
  239. ->method('getPrincipalUrl')
  240. ->with()
  241. ->willReturn($principalUri);
  242. $calDAVPlugin = $this->getMockBuilder(CalDAVPlugin::class)
  243. ->disableOriginalConstructor()
  244. ->getMock();
  245. $calDAVPlugin->expects($this->once())
  246. ->method('getCalendarHomeForPrincipal')
  247. ->willReturn($calendarHome);
  248. $this->server->expects($this->once())
  249. ->method('getPlugin')
  250. ->with('caldav')
  251. ->willReturn($calDAVPlugin);
  252. if (!$calendarHome) {
  253. $this->plugin->propFindDefaultCalendarUrl($propFind, $node);
  254. $this->assertNull($propFind->get(Plugin::SCHEDULE_DEFAULT_CALENDAR_URL));
  255. return;
  256. }
  257. if ($principalUri === 'principals/something-else') {
  258. $this->plugin->propFindDefaultCalendarUrl($propFind, $node);
  259. $this->assertNull($propFind->get(Plugin::SCHEDULE_DEFAULT_CALENDAR_URL));
  260. return;
  261. }
  262. if (!$isResource) {
  263. $this->config->expects($this->once())
  264. ->method('getUserValue')
  265. ->with('myuser', 'dav', 'defaultCalendar', CalDavBackend::PERSONAL_CALENDAR_URI)
  266. ->willReturn($calendarUri);
  267. }
  268. $calendarHomeObject = $this->createMock(CalendarHome::class);
  269. $calendarHomeObject->expects($this->once())
  270. ->method('childExists')
  271. ->with($calendarUri)
  272. ->willReturn($exists);
  273. if (!$exists) {
  274. $calendarBackend = $this->createMock(CalDavBackend::class);
  275. $calendarBackend->expects($this->once())
  276. ->method('createCalendar')
  277. ->with($principalUri, $calendarUri, [
  278. '{DAV:}displayname' => $displayName,
  279. ]);
  280. $calendarHomeObject->expects($this->once())
  281. ->method('getCalDAVBackend')
  282. ->with()
  283. ->willReturn($calendarBackend);
  284. }
  285. /** @var Tree|MockObject $tree */
  286. $tree = $this->createMock(Tree::class);
  287. $tree->expects($this->once())
  288. ->method('getNodeForPath')
  289. ->with($calendarHome)
  290. ->willReturn($calendarHomeObject);
  291. $this->server->tree = $tree;
  292. $properties = $propertiesForPath ? [
  293. ['href' => '/remote.php/dav/' . $calendarHome . '/' . $calendarUri]
  294. ] : [];
  295. $this->server->expects($this->once())
  296. ->method('getPropertiesForPath')
  297. ->with($calendarHome .'/' . $calendarUri, [], 1)
  298. ->willReturn($properties);
  299. $this->plugin->propFindDefaultCalendarUrl($propFind, $node);
  300. if (!$propertiesForPath) {
  301. $this->assertNull($propFind->get(Plugin::SCHEDULE_DEFAULT_CALENDAR_URL));
  302. return;
  303. }
  304. /** @var LocalHref $result */
  305. $result = $propFind->get(Plugin::SCHEDULE_DEFAULT_CALENDAR_URL);
  306. $this->assertEquals('/remote.php/dav/'. $calendarHome . '/' . $calendarUri, $result->getHref());
  307. }
  308. }