PluginTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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\DAV\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)->
  38. disableOriginalConstructor()->
  39. getMock();
  40. $this->book->method('getName')->willReturn('addressbook1.vcf');
  41. $root->addChild($this->book);
  42. $this->plugin->initialize($this->server);
  43. }
  44. public function testSharing(): void {
  45. $this->book->expects($this->once())->method('updateShares')->with([[
  46. 'href' => 'principal:principals/admin',
  47. 'commonName' => null,
  48. 'summary' => null,
  49. 'readOnly' => false
  50. ]], ['mailto:wilfredo@example.com']);
  51. // setup request
  52. $request = new Request('POST', 'addressbook1.vcf');
  53. $request->addHeader('Content-Type', 'application/xml');
  54. $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>');
  55. $response = new Response();
  56. $this->plugin->httpPost($request, $response);
  57. }
  58. }