FakeLockerPluginTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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\Connector\Sabre;
  8. use OCA\DAV\Connector\Sabre\FakeLockerPlugin;
  9. use Sabre\DAV\INode;
  10. use Sabre\DAV\PropFind;
  11. use Sabre\DAV\Server;
  12. use Sabre\HTTP\RequestInterface;
  13. use Sabre\HTTP\Response;
  14. use Sabre\HTTP\ResponseInterface;
  15. use Test\TestCase;
  16. /**
  17. * Class FakeLockerPluginTest
  18. *
  19. * @package OCA\DAV\Tests\unit\Connector\Sabre
  20. */
  21. class FakeLockerPluginTest extends TestCase {
  22. /** @var FakeLockerPlugin */
  23. private $fakeLockerPlugin;
  24. protected function setUp(): void {
  25. parent::setUp();
  26. $this->fakeLockerPlugin = new FakeLockerPlugin();
  27. }
  28. public function testInitialize(): void {
  29. /** @var Server $server */
  30. $server = $this->getMockBuilder(Server::class)
  31. ->disableOriginalConstructor()
  32. ->getMock();
  33. $server
  34. ->expects($this->exactly(4))
  35. ->method('on')
  36. ->withConsecutive(
  37. ['method:LOCK', [$this->fakeLockerPlugin, 'fakeLockProvider'], 1],
  38. ['method:UNLOCK', [$this->fakeLockerPlugin, 'fakeUnlockProvider'], 1],
  39. ['propFind', [$this->fakeLockerPlugin, 'propFind']],
  40. ['validateTokens', [$this->fakeLockerPlugin, 'validateTokens']],
  41. );
  42. $this->fakeLockerPlugin->initialize($server);
  43. }
  44. public function testGetHTTPMethods(): void {
  45. $expected = [
  46. 'LOCK',
  47. 'UNLOCK',
  48. ];
  49. $this->assertSame($expected, $this->fakeLockerPlugin->getHTTPMethods('Test'));
  50. }
  51. public function testGetFeatures(): void {
  52. $expected = [
  53. 2,
  54. ];
  55. $this->assertSame($expected, $this->fakeLockerPlugin->getFeatures());
  56. }
  57. public function testPropFind(): void {
  58. $propFind = $this->getMockBuilder(PropFind::class)
  59. ->disableOriginalConstructor()
  60. ->getMock();
  61. $node = $this->getMockBuilder(INode::class)
  62. ->disableOriginalConstructor()
  63. ->getMock();
  64. $propFind->expects($this->exactly(2))
  65. ->method('handle')
  66. ->withConsecutive(
  67. ['{DAV:}supportedlock'],
  68. ['{DAV:}lockdiscovery'],
  69. );
  70. $this->fakeLockerPlugin->propFind($propFind, $node);
  71. }
  72. public function tokenDataProvider() {
  73. return [
  74. [
  75. [
  76. [
  77. 'tokens' => [
  78. [
  79. 'token' => 'aToken',
  80. 'validToken' => false,
  81. ],
  82. [],
  83. [
  84. 'token' => 'opaquelocktoken:asdf',
  85. 'validToken' => false,
  86. ]
  87. ],
  88. ]
  89. ],
  90. [
  91. [
  92. 'tokens' => [
  93. [
  94. 'token' => 'aToken',
  95. 'validToken' => false,
  96. ],
  97. [],
  98. [
  99. 'token' => 'opaquelocktoken:asdf',
  100. 'validToken' => true,
  101. ]
  102. ],
  103. ]
  104. ],
  105. ]
  106. ];
  107. }
  108. /**
  109. * @dataProvider tokenDataProvider
  110. * @param array $input
  111. * @param array $expected
  112. */
  113. public function testValidateTokens(array $input, array $expected): void {
  114. $request = $this->getMockBuilder(RequestInterface::class)
  115. ->disableOriginalConstructor()
  116. ->getMock();
  117. $this->fakeLockerPlugin->validateTokens($request, $input);
  118. $this->assertSame($expected, $input);
  119. }
  120. public function testFakeLockProvider(): void {
  121. $request = $this->getMockBuilder(RequestInterface::class)
  122. ->disableOriginalConstructor()
  123. ->getMock();
  124. $response = new Response();
  125. $server = $this->getMockBuilder(Server::class)
  126. ->getMock();
  127. $this->fakeLockerPlugin->initialize($server);
  128. $request->expects($this->exactly(2))
  129. ->method('getPath')
  130. ->willReturn('MyPath');
  131. $this->assertSame(false, $this->fakeLockerPlugin->fakeLockProvider($request, $response));
  132. $expectedXml = '<?xml version="1.0" encoding="utf-8"?><d:prop xmlns:d="DAV:" xmlns:s="http://sabredav.org/ns"><d:lockdiscovery><d:activelock><d:lockscope><d:exclusive/></d:lockscope><d:locktype><d:write/></d:locktype><d:lockroot><d:href>MyPath</d:href></d:lockroot><d:depth>infinity</d:depth><d:timeout>Second-1800</d:timeout><d:locktoken><d:href>opaquelocktoken:fe4f7f2437b151fbcb4e9f5c8118c6b1</d:href></d:locktoken></d:activelock></d:lockdiscovery></d:prop>';
  133. $this->assertXmlStringEqualsXmlString($expectedXml, $response->getBody());
  134. }
  135. public function testFakeUnlockProvider(): void {
  136. $request = $this->getMockBuilder(RequestInterface::class)
  137. ->disableOriginalConstructor()
  138. ->getMock();
  139. $response = $this->getMockBuilder(ResponseInterface::class)
  140. ->disableOriginalConstructor()
  141. ->getMock();
  142. $response->expects($this->once())
  143. ->method('setStatus')
  144. ->with('204');
  145. $response->expects($this->once())
  146. ->method('setHeader')
  147. ->with('Content-Length', '0');
  148. $this->assertSame(false, $this->fakeLockerPlugin->fakeUnlockProvider($request, $response));
  149. }
  150. }