FakeLockerPluginTest.php 5.4 KB

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