FakeLockerPluginTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  29. use OCA\DAV\Connector\Sabre\FakeLockerPlugin;
  30. use Sabre\DAV\INode;
  31. use Sabre\DAV\PropFind;
  32. use Sabre\DAV\Server;
  33. use Sabre\HTTP\RequestInterface;
  34. use Sabre\HTTP\Response;
  35. use Sabre\HTTP\ResponseInterface;
  36. use Test\TestCase;
  37. /**
  38. * Class FakeLockerPluginTest
  39. *
  40. * @package OCA\DAV\Tests\unit\Connector\Sabre
  41. */
  42. class FakeLockerPluginTest extends TestCase {
  43. /** @var FakeLockerPlugin */
  44. private $fakeLockerPlugin;
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->fakeLockerPlugin = new FakeLockerPlugin();
  48. }
  49. public function testInitialize(): void {
  50. /** @var Server $server */
  51. $server = $this->getMockBuilder(Server::class)
  52. ->disableOriginalConstructor()
  53. ->getMock();
  54. $server
  55. ->expects($this->exactly(4))
  56. ->method('on')
  57. ->withConsecutive(
  58. ['method:LOCK', [$this->fakeLockerPlugin, 'fakeLockProvider'], 1],
  59. ['method:UNLOCK', [$this->fakeLockerPlugin, 'fakeUnlockProvider'], 1],
  60. ['propFind', [$this->fakeLockerPlugin, 'propFind']],
  61. ['validateTokens', [$this->fakeLockerPlugin, 'validateTokens']],
  62. );
  63. $this->fakeLockerPlugin->initialize($server);
  64. }
  65. public function testGetHTTPMethods(): void {
  66. $expected = [
  67. 'LOCK',
  68. 'UNLOCK',
  69. ];
  70. $this->assertSame($expected, $this->fakeLockerPlugin->getHTTPMethods('Test'));
  71. }
  72. public function testGetFeatures(): void {
  73. $expected = [
  74. 2,
  75. ];
  76. $this->assertSame($expected, $this->fakeLockerPlugin->getFeatures());
  77. }
  78. public function testPropFind(): void {
  79. $propFind = $this->getMockBuilder(PropFind::class)
  80. ->disableOriginalConstructor()
  81. ->getMock();
  82. $node = $this->getMockBuilder(INode::class)
  83. ->disableOriginalConstructor()
  84. ->getMock();
  85. $propFind->expects($this->exactly(2))
  86. ->method('handle')
  87. ->withConsecutive(
  88. ['{DAV:}supportedlock'],
  89. ['{DAV:}lockdiscovery'],
  90. );
  91. $this->fakeLockerPlugin->propFind($propFind, $node);
  92. }
  93. public function tokenDataProvider() {
  94. return [
  95. [
  96. [
  97. [
  98. 'tokens' => [
  99. [
  100. 'token' => 'aToken',
  101. 'validToken' => false,
  102. ],
  103. [],
  104. [
  105. 'token' => 'opaquelocktoken:asdf',
  106. 'validToken' => false,
  107. ]
  108. ],
  109. ]
  110. ],
  111. [
  112. [
  113. 'tokens' => [
  114. [
  115. 'token' => 'aToken',
  116. 'validToken' => false,
  117. ],
  118. [],
  119. [
  120. 'token' => 'opaquelocktoken:asdf',
  121. 'validToken' => true,
  122. ]
  123. ],
  124. ]
  125. ],
  126. ]
  127. ];
  128. }
  129. /**
  130. * @dataProvider tokenDataProvider
  131. * @param array $input
  132. * @param array $expected
  133. */
  134. public function testValidateTokens(array $input, array $expected): void {
  135. $request = $this->getMockBuilder(RequestInterface::class)
  136. ->disableOriginalConstructor()
  137. ->getMock();
  138. $this->fakeLockerPlugin->validateTokens($request, $input);
  139. $this->assertSame($expected, $input);
  140. }
  141. public function testFakeLockProvider(): void {
  142. $request = $this->getMockBuilder(RequestInterface::class)
  143. ->disableOriginalConstructor()
  144. ->getMock();
  145. $response = new Response();
  146. $server = $this->getMockBuilder(Server::class)
  147. ->getMock();
  148. $this->fakeLockerPlugin->initialize($server);
  149. $request->expects($this->exactly(2))
  150. ->method('getPath')
  151. ->willReturn('MyPath');
  152. $this->assertSame(false, $this->fakeLockerPlugin->fakeLockProvider($request, $response));
  153. $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>';
  154. $this->assertXmlStringEqualsXmlString($expectedXml, $response->getBody());
  155. }
  156. public function testFakeUnlockProvider(): void {
  157. $request = $this->getMockBuilder(RequestInterface::class)
  158. ->disableOriginalConstructor()
  159. ->getMock();
  160. $response = $this->getMockBuilder(ResponseInterface::class)
  161. ->disableOriginalConstructor()
  162. ->getMock();
  163. $response->expects($this->once())
  164. ->method('setStatus')
  165. ->with('204');
  166. $response->expects($this->once())
  167. ->method('setHeader')
  168. ->with('Content-Length', '0');
  169. $this->assertSame(false, $this->fakeLockerPlugin->fakeUnlockProvider($request, $response));
  170. }
  171. }