PluginTest.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Tests\unit\CardDAV\Sharing;
  8. use OCA\DAV\Connector\Sabre\Auth;
  9. use OCA\DAV\DAV\Sharing\IShareable;
  10. use OCA\DAV\DAV\Sharing\Plugin;
  11. use OCP\IConfig;
  12. use OCP\IRequest;
  13. use Sabre\DAV\Server;
  14. use Sabre\DAV\SimpleCollection;
  15. use Sabre\HTTP\Request;
  16. use Sabre\HTTP\Response;
  17. use Test\TestCase;
  18. class PluginTest extends TestCase {
  19. /** @var Plugin */
  20. private $plugin;
  21. /** @var Server */
  22. private $server;
  23. /** @var IShareable | \PHPUnit\Framework\MockObject\MockObject */
  24. private $book;
  25. protected function setUp(): void {
  26. parent::setUp();
  27. /** @var Auth | \PHPUnit\Framework\MockObject\MockObject $authBackend */
  28. $authBackend = $this->getMockBuilder(Auth::class)->disableOriginalConstructor()->getMock();
  29. $authBackend->method('isDavAuthenticated')->willReturn(true);
  30. /** @var IRequest $request */
  31. $request = $this->getMockBuilder(IRequest::class)->disableOriginalConstructor()->getMock();
  32. $config = $this->createMock(IConfig::class);
  33. $this->plugin = new Plugin($authBackend, $request, $config);
  34. $root = new SimpleCollection('root');
  35. $this->server = new \Sabre\DAV\Server($root);
  36. /** @var SimpleCollection $node */
  37. $this->book = $this->getMockBuilder(IShareable::class)->disableOriginalConstructor()->getMock();
  38. $this->book->method('getName')->willReturn('addressbook1.vcf');
  39. $root->addChild($this->book);
  40. $this->plugin->initialize($this->server);
  41. }
  42. public function testSharing(): void {
  43. $this->book->expects($this->once())->method('updateShares')->with([[
  44. 'href' => 'principal:principals/admin',
  45. 'commonName' => null,
  46. 'summary' => null,
  47. 'readOnly' => false
  48. ]], ['mailto:wilfredo@example.com']);
  49. // setup request
  50. $request = new Request('POST', 'addressbook1.vcf');
  51. $request->addHeader('Content-Type', 'application/xml');
  52. $request->setBody('<?xml version="1.0" encoding="utf-8" ?><CS:share xmlns:D="DAV:" xmlns:CS="http://owncloud.org/ns"><CS:set><D:href>principal:principals/admin</D:href><CS:read-write/></CS:set> <CS:remove><D:href>mailto:wilfredo@example.com</D:href></CS:remove></CS:share>');
  53. $response = new Response();
  54. $this->plugin->httpPost($request, $response);
  55. }
  56. }