Files

1012 lines
28 KiB
PHP
Raw Permalink Normal View History

2023-01-03 12:33:33 +05:45
<?php declare(strict_types=1);
/**
* ownCloud
*
* @author Artur Neumann <artur@jankaritech.com>
* @copyright Copyright (c) 2018 Artur Neumann artur@jankaritech.com
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License,
* as published by the Free Software Foundation;
* either version 3 of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\TableNode;
use GuzzleHttp\Exception\GuzzleException;
use PHPUnit\Framework\Assert;
2024-10-08 12:26:40 +05:45
use Psr\Http\Message\ResponseInterface;
2023-01-03 12:33:33 +05:45
use TestHelpers\HttpRequestHelper;
use TestHelpers\WebDavHelper;
2024-10-08 12:26:40 +05:45
use TestHelpers\BehatHelper;
2023-01-03 12:33:33 +05:45
require_once 'bootstrap.php';
/**
* context containing API steps needed for the locking mechanism of webdav
*/
class WebDavLockingContext implements Context {
2023-05-02 17:25:56 +05:45
private FeatureContext $featureContext;
private PublicWebDavContext $publicWebDavContext;
2023-10-27 14:45:15 +02:00
private SpacesContext $spacesContext;
2023-01-03 12:33:33 +05:45
/**
*
* @var string[][]
*/
2023-05-02 17:25:56 +05:45
private array $tokenOfLastLock = [];
2023-01-03 12:33:33 +05:45
/**
*
* @param string $user
* @param string $file
* @param TableNode $properties table with no heading with | property | value |
2024-04-26 15:27:00 +05:45
* @param string|null $fullUrl
2023-01-03 12:33:33 +05:45
* @param boolean $public if the file is in a public share or not
* @param boolean $expectToSucceed
2024-09-18 11:26:20 +05:45
* @param string|null $spaceId
2023-01-03 12:33:33 +05:45
*
* @return void
2024-04-26 15:27:00 +05:45
* @throws GuzzleException
* @throws JsonException
2023-01-03 12:33:33 +05:45
*/
private function lockFile(
2023-05-02 17:25:56 +05:45
string $user,
string $file,
2023-01-03 12:33:33 +05:45
TableNode $properties,
2025-06-24 11:39:50 +02:00
?string $fullUrl = null,
2023-05-02 17:25:56 +05:45
bool $public = false,
2024-09-18 11:26:20 +05:45
bool $expectToSucceed = true,
?string $spaceId = null,
2024-12-24 09:30:42 +05:45
): ResponseInterface {
2023-01-03 12:33:33 +05:45
$user = $this->featureContext->getActualUsername($user);
if ($public === true) {
2024-09-17 17:11:20 +05:45
$type = "public-files";
$password = $this->featureContext->getActualPassword("%public%");
2023-01-03 12:33:33 +05:45
} else {
$type = "files";
$password = $this->featureContext->getPasswordForUser($user);
}
$body
= "<?xml version='1.0' encoding='UTF-8'?>" .
"<d:lockinfo xmlns:d='DAV:'> ";
$headers = [];
2023-10-27 14:45:15 +02:00
// depth is only 0 or infinity. We don't need to set it more, as there is no lock for the folder
$this->featureContext->verifyTableNodeRows($properties, [], ['lockscope', 'timeout']);
2023-01-03 12:33:33 +05:45
$propertiesRows = $properties->getRowsHash();
2023-10-27 14:45:15 +02:00
2023-01-03 12:33:33 +05:45
foreach ($propertiesRows as $property => $value) {
2023-10-27 14:45:15 +02:00
if ($property === "timeout") {
2025-06-19 12:03:03 +05:45
// properties that are set in the header not in the xml
2023-01-03 12:33:33 +05:45
$headers[$property] = $value;
} else {
$body .= "<d:$property><d:$value/></d:$property>";
}
}
$body .= "</d:lockinfo>";
2023-11-01 06:02:08 +01:00
if (isset($fullUrl)) {
$response = HttpRequestHelper::sendRequest(
$fullUrl,
"LOCK",
$this->featureContext->getActualUsername($user),
$this->featureContext->getPasswordForUser($user),
$headers,
$body,
2023-11-01 06:02:08 +01:00
);
} else {
2024-09-17 17:11:20 +05:45
$baseUrl = $this->featureContext->getBaseUrl();
2023-11-01 06:02:08 +01:00
$response = WebDavHelper::makeDavRequest(
$baseUrl,
$user,
$password,
"LOCK",
$file,
$headers,
2024-09-18 11:26:20 +05:45
$spaceId,
2023-11-01 06:02:08 +01:00
$body,
$this->featureContext->getDavPathVersion(),
$type,
2023-11-01 06:02:08 +01:00
);
}
$responseXmlObject = HttpRequestHelper::getResponseXml($response, __METHOD__);
$xmlPart = $responseXmlObject->xpath("//d:locktoken/d:href");
2023-01-03 12:33:33 +05:45
if (isset($xmlPart[0])) {
$this->tokenOfLastLock[$user][$file] = (string) $xmlPart[0];
} else {
if ($expectToSucceed === true) {
Assert::fail("could not find lock token after trying to lock '$file'");
}
}
return $response;
2023-01-03 12:33:33 +05:45
}
/**
2023-11-01 06:02:08 +01:00
* @When user :user locks file :file using the WebDAV API setting the following properties
2023-10-27 14:45:15 +02:00
*
* @param string $user
2023-11-01 06:02:08 +01:00
* @param string $file
2023-10-27 14:45:15 +02:00
* @param TableNode $properties table with no heading with | property | value |
*
* @return void
*/
2025-06-19 12:15:41 +05:45
public function userLocksFileSettingPropertiesUsingWebDavAPI(
string $user,
string $file,
TableNode $properties,
2025-06-19 12:15:41 +05:45
): void {
2024-09-17 17:11:20 +05:45
$spaceId = null;
2024-12-24 09:30:42 +05:45
if (\str_starts_with($file, "Shares/")
&& $this->featureContext->getDavPathVersion() === WebDavHelper::DAV_VERSION_SPACES
) {
2024-09-17 17:11:20 +05:45
$spaceId = $this->spacesContext->getSpaceIdByName($user, "Shares");
$file = \str_replace("Shares/", "", $file);
}
$response = $this->lockFile($user, $file, $properties, null, false, true, $spaceId);
2023-11-01 06:02:08 +01:00
$this->featureContext->setResponse($response);
2023-10-27 14:45:15 +02:00
}
/**
* @When user :user tries to lock file/folder :file using the WebDAV API setting the following properties
2023-01-03 12:33:33 +05:45
*
* @param string $user
* @param string $file
* @param TableNode $properties table with no heading with | property | value |
*
* @return void
*/
2024-12-24 09:30:42 +05:45
public function userTriesToLockFileSettingPropertiesUsingWebDavAPI(
string $user,
string $file,
TableNode $properties,
2025-06-19 12:15:41 +05:45
): void {
2023-11-01 06:02:08 +01:00
$response = $this->lockFile($user, $file, $properties, null, false, false);
$this->featureContext->setResponse($response);
2023-01-03 12:33:33 +05:45
}
/**
2023-10-27 14:45:15 +02:00
* @When user :user locks file :file inside the space :space using the WebDAV API setting the following properties
*
* @param string $user
* @param string $file
* @param string $space
* @param TableNode $properties table with no heading with | property | value |
*
* @return void
* @throws GuzzleException
2023-10-27 14:45:15 +02:00
*/
2024-12-24 09:30:42 +05:45
public function userLocksFileInProjectSpaceUsingWebDavAPI(
string $user,
string $file,
string $space,
TableNode $properties,
2025-06-19 12:15:41 +05:45
): void {
$this->featureContext->setResponse($this->userLocksFileInProjectSpace($user, $file, $space, $properties));
}
/**
* @param string $user
* @param string $file
* @param string $space
* @param TableNode $properties
*
* @return ResponseInterface|null
*
* @throws GuzzleException
*/
2024-12-24 09:30:42 +05:45
public function userLocksFileInProjectSpace(
string $user,
string $file,
string $space,
TableNode $properties,
2024-12-24 09:30:42 +05:45
): ?ResponseInterface {
2023-11-01 06:02:08 +01:00
$spaceId = $this->spacesContext->getSpaceIdByName($user, $space);
2024-09-17 17:11:20 +05:45
$baseUrl = $this->featureContext->getBaseUrl();
$davPathVersion = $this->featureContext->getDavPathVersion();
2024-10-21 12:24:50 +05:45
$suffixPath = $user;
2024-09-17 17:11:20 +05:45
if ($davPathVersion === WebDavHelper::DAV_VERSION_SPACES) {
2024-10-21 12:24:50 +05:45
$suffixPath = $spaceId;
2024-09-17 17:11:20 +05:45
}
2024-10-21 12:24:50 +05:45
$davPath = WebDavHelper::getDavPath($davPathVersion, $suffixPath);
2024-09-17 17:11:20 +05:45
$fullUrl = "$baseUrl/$davPath/$file";
2024-09-18 11:26:20 +05:45
return $this->lockFile($user, $file, $properties, $fullUrl, false, true, $spaceId);
}
/**
* @Given user :user has locked file :file inside the space :space setting the following properties
*
* @param string $user
* @param string $file
* @param string $space
* @param TableNode $properties table with no heading with | property | value |
*
* @return void
* @throws GuzzleException
*/
2024-12-24 09:30:42 +05:45
public function userHasLockedFileInProjectSpaceUsingWebDavAPI(
string $user,
string $file,
string $space,
TableNode $properties,
2024-12-24 09:30:42 +05:45
): void {
$response = $this->userLocksFileInProjectSpace($user, $file, $space, $properties);
$this->featureContext->theHTTPStatusCodeShouldBe(200, '', $response);
2023-11-01 06:02:08 +01:00
}
/**
* @When user :user tries to lock file :file inside the space :space using the WebDAV API setting the following properties
*
* @param string $user
* @param string $file
* @param string $space
* @param TableNode $properties table with no heading with | property | value |
*
* @return void
*/
2024-12-24 09:30:42 +05:45
public function userTriesToLockFileInProjectSpaceUsingWebDavAPI(
string $user,
string $file,
string $space,
TableNode $properties,
2025-06-19 12:15:41 +05:45
): void {
2023-10-27 14:45:15 +02:00
$spaceId = $this->spacesContext->getSpaceIdByName($user, $space);
2024-09-17 17:11:20 +05:45
$davPathVersion = $this->featureContext->getDavPathVersion();
2024-10-21 12:24:50 +05:45
$suffixPath = $user;
2024-09-17 17:11:20 +05:45
if ($davPathVersion === WebDavHelper::DAV_VERSION_SPACES) {
2024-10-21 12:24:50 +05:45
$suffixPath = $spaceId;
2024-09-17 17:11:20 +05:45
}
2024-10-21 12:24:50 +05:45
$davPath = WebdavHelper::getDavPath($davPathVersion, $suffixPath);
2024-09-17 17:11:20 +05:45
$fullUrl = $this->featureContext->getBaseUrl() . "/$davPath/$file";
2024-09-18 11:26:20 +05:45
$response = $this->lockFile($user, $file, $properties, $fullUrl, false, false, $spaceId);
2023-10-27 14:45:15 +02:00
$this->featureContext->setResponse($response);
}
/**
2024-09-17 17:11:20 +05:45
* @When user :user locks file :file using file-id :fileId using the WebDAV API setting the following properties
2023-10-27 14:45:15 +02:00
*
* @param string $user
2023-11-01 06:02:08 +01:00
* @param string $file
2024-09-17 17:11:20 +05:45
* @param string $fileId
2023-10-27 14:45:15 +02:00
* @param TableNode $properties table with no heading with | property | value |
*
* @return void
*/
2024-12-24 09:30:42 +05:45
public function userLocksFileUsingFileIdUsingWebDavAPISettingFollowingProperties(
string $user,
string $file,
string $fileId,
TableNode $properties,
2025-06-19 12:15:41 +05:45
): void {
2024-09-17 17:11:20 +05:45
$davPath = WebdavHelper::getDavPath($this->featureContext->getDavPathVersion());
$davPath = \rtrim($davPath, '/');
$fullUrl = $this->featureContext->getBaseUrl() . "/$davPath/$fileId";
2023-11-01 06:02:08 +01:00
$response = $this->lockFile($user, $file, $properties, $fullUrl);
$this->featureContext->setResponse($response);
}
/**
2024-09-17 17:11:20 +05:45
* @When user :user tries to lock file :file using file-id :fileId using the WebDAV API setting the following properties
2023-11-01 06:02:08 +01:00
*
* @param string $user
* @param string $file
2024-09-17 17:11:20 +05:45
* @param string $fileId
2023-11-01 06:02:08 +01:00
* @param TableNode $properties table with no heading with | property | value |
*
* @return void
*/
2024-12-24 09:30:42 +05:45
public function userTriesToLockFileUsingFileIdUsingWebDavAPI(
string $user,
string $file,
string $fileId,
TableNode $properties,
2025-06-19 12:15:41 +05:45
): void {
2024-09-17 17:11:20 +05:45
$davPath = WebdavHelper::getDavPath($this->featureContext->getDavPathVersion());
$davPath = \rtrim($davPath, '/');
$fullUrl = $this->featureContext->getBaseUrl() . "/$davPath/$fileId";
2023-11-01 06:02:08 +01:00
$response = $this->lockFile($user, $file, $properties, $fullUrl, false, false);
2023-10-27 14:45:15 +02:00
$this->featureContext->setResponse($response);
}
/**
* @Given user :user has locked file :file setting the following properties
2023-01-03 12:33:33 +05:45
*
* @param string $user
* @param string $file
* @param TableNode $properties table with no heading with | property | value |
*
* @return void
*/
2025-06-19 12:15:41 +05:45
public function userHasLockedFile(string $user, string $file, TableNode $properties): void {
$response = $this->lockFile($user, $file, $properties);
$this->featureContext->theHTTPStatusCodeShouldBe(200, '', $response);
2023-01-03 12:33:33 +05:45
}
/**
* @Given user :user has locked file :file inside space :spaceName setting the following properties
*
* @param string $user
* @param string $file
* @param string $spaceName
* @param TableNode $properties table with no heading with | property | value |
*
* @return void
*/
2024-12-24 09:30:42 +05:45
public function userHasLockedFileInsideSpaceSettingTheFollowingProperties(
string $user,
string $file,
string $spaceName,
TableNode $properties,
2025-06-19 12:15:41 +05:45
): void {
2024-10-07 15:03:16 +05:45
$spaceId = $this->spacesContext->getSpaceIdByName($this->featureContext->getActualUsername($user), $spaceName);
2024-09-18 11:26:20 +05:45
$response = $this->lockFile($user, $file, $properties, null, false, true, $spaceId);
$this->featureContext->theHTTPStatusCodeShouldBe(200, '', $response);
}
2023-11-01 06:02:08 +01:00
/**
2024-09-17 17:11:20 +05:45
* @Given user :user has locked file :file using file-id :fileId setting the following properties
2023-11-01 06:02:08 +01:00
*
* @param string $user
* @param string $file
2024-09-17 17:11:20 +05:45
* @param string $fileId
2023-11-01 06:02:08 +01:00
* @param TableNode $properties table with no heading with | property | value |
*
* @return void
*/
2025-06-19 12:15:41 +05:45
public function userHasLockedFileUsingFileId(
string $user,
string $file,
string $fileId,
TableNode $properties,
2025-06-19 12:15:41 +05:45
): void {
2024-09-17 17:11:20 +05:45
$davPath = WebdavHelper::getDavPath($this->featureContext->getDavPathVersion());
$davPath = \rtrim($davPath, '/');
$fullUrl = $this->featureContext->getBaseUrl() . "/$davPath/$fileId";
2023-11-01 06:02:08 +01:00
$response = $this->lockFile($user, $file, $properties, $fullUrl);
$this->featureContext->theHTTPStatusCodeShouldBe(200, '', $response);
}
2023-01-03 12:33:33 +05:45
/**
* @Given the public has locked the last public link shared file/folder setting the following properties
*
* @param TableNode $properties
*
* @return void
*/
2025-06-19 12:15:41 +05:45
public function publicHasLockedLastSharedFile(TableNode $properties): void {
$response = $this->lockFile(
$this->featureContext->getLastCreatedPublicShareToken(),
2023-01-03 12:33:33 +05:45
"/",
$properties,
2023-11-01 06:02:08 +01:00
null,
true,
2023-01-03 12:33:33 +05:45
);
$this->featureContext->theHTTPStatusCodeShouldBe(200, '', $response);
2023-01-03 12:33:33 +05:45
}
/**
* @When the public locks the last public link shared file using the WebDAV API setting the following properties
* @When the public tries to lock the last public link shared file using the WebDAV API setting the following properties
2023-01-03 12:33:33 +05:45
*
* @param TableNode $properties
*
* @return void
*/
2025-06-19 12:15:41 +05:45
public function publicLocksLastSharedFile(TableNode $properties): void {
2024-12-24 09:30:42 +05:45
$token = ($this->featureContext->isUsingSharingNG())
? $this->featureContext->shareNgGetLastCreatedLinkShareToken()
: $this->featureContext->getLastCreatedPublicShareToken();
$response = $this->lockFile(
$token,
2023-01-03 12:33:33 +05:45
"/",
$properties,
2023-11-01 06:02:08 +01:00
null,
2023-01-03 12:33:33 +05:45
true,
false,
2023-01-03 12:33:33 +05:45
);
$this->featureContext->setResponse($response);
2023-01-03 12:33:33 +05:45
}
/**
* @Given the public has locked :file in the last public link shared folder setting the following properties
*
* @param string $file
* @param TableNode $properties
*
* @return void
*/
public function publicHasLockedFileLastSharedFolder(
2025-06-19 12:12:17 +05:45
string $file,
TableNode $properties,
2025-06-19 12:15:41 +05:45
): void {
2024-12-24 09:30:42 +05:45
$token = ($this->featureContext->isUsingSharingNG())
? $this->featureContext->shareNgGetLastCreatedLinkShareToken()
: $this->featureContext->getLastCreatedPublicShareToken();
$response = $this->lockFile(
$token,
2023-01-03 12:33:33 +05:45
$file,
$properties,
2023-11-01 06:02:08 +01:00
null,
true,
2023-01-03 12:33:33 +05:45
);
$this->featureContext->theHTTPStatusCodeShouldBe(200, '', $response);
2023-01-03 12:33:33 +05:45
}
/**
2024-11-11 14:28:23 +05:45
* @When /^the public locks "([^"]*)" in the last public link shared folder using the public WebDAV API setting the following properties$/
* @When /^the public tries to lock "([^"]*)" in the last public link shared folder using the public WebDAV API setting the following properties$/
2023-01-03 12:33:33 +05:45
*
* @param string $file
* @param TableNode $properties
*
* @return void
*/
public function publicLocksFileLastSharedFolder(
2023-05-02 17:25:56 +05:45
string $file,
TableNode $properties,
2025-06-19 12:15:41 +05:45
): void {
2024-12-24 09:30:42 +05:45
$token = ($this->featureContext->isUsingSharingNG())
? $this->featureContext->shareNgGetLastCreatedLinkShareToken()
: $this->featureContext->getLastCreatedPublicShareToken();
$response = $this->lockFile(
$token,
2023-01-03 12:33:33 +05:45
$file,
$properties,
2023-11-01 06:02:08 +01:00
null,
2023-01-03 12:33:33 +05:45
true,
false,
2023-01-03 12:33:33 +05:45
);
$this->featureContext->setResponse($response);
2023-01-03 12:33:33 +05:45
}
/**
2023-10-27 14:45:15 +02:00
* @When user :user unlocks the last created lock of file :file using the WebDAV API
2023-01-03 12:33:33 +05:45
*
* @param string $user
* @param string $file
*
* @return void
*/
2025-06-19 12:15:41 +05:45
public function unlockLastLockUsingWebDavAPI(string $user, string $file): void {
$response = $this->unlockItemWithLastLockOfUserAndItemUsingWebDavAPI(
$user,
$file,
$user,
$file,
);
$this->featureContext->setResponse($response);
}
/**
* @When user :user unlocks the last created lock of file :file inside space :spaceName using the WebDAV API
*
* @param string $user
* @param string $spaceName
* @param string $file
*
* @return void
*/
2024-12-24 09:30:42 +05:45
public function userUnlocksTheLastCreatedLockOfFileInsideSpaceUsingTheWebdavApi(
string $user,
string $spaceName,
string $file,
2025-06-19 12:15:41 +05:45
): void {
2024-10-07 15:03:16 +05:45
$spaceId = $this->spacesContext->getSpaceIdByName($this->featureContext->getActualUsername($user), $spaceName);
$response = $this->unlockItemWithLastLockOfUserAndItemUsingWebDavAPI(
2023-01-03 12:33:33 +05:45
$user,
$file,
$user,
2024-09-18 11:26:20 +05:45
$file,
false,
null,
$spaceId,
2023-01-03 12:33:33 +05:45
);
$this->featureContext->setResponse($response);
2023-01-03 12:33:33 +05:45
}
/**
2024-09-17 17:11:20 +05:45
* @When user :user unlocks the last created lock of file :itemToUnlock using file-id :fileId using the WebDAV API
*
* @param string $user
* @param string $itemToUnlock
2024-09-17 17:11:20 +05:45
* @param string $fileId
*
* @return void
*/
2024-12-24 09:30:42 +05:45
public function userUnlocksTheLastCreatedLockOfFileWithFileIdPathUsingTheWebdavApi(
string $user,
string $itemToUnlock,
string $fileId,
2025-06-19 12:15:41 +05:45
): void {
2024-09-17 17:11:20 +05:45
$davPath = WebdavHelper::getDavPath($this->featureContext->getDavPathVersion());
$davPath = \rtrim($davPath, '/');
$fullUrl = $this->featureContext->getBaseUrl() . "/$davPath/$fileId";
2024-12-24 09:30:42 +05:45
$response = $this->unlockItemWithLastLockOfUserAndItemUsingWebDavAPI(
$user,
$itemToUnlock,
$user,
$itemToUnlock,
false,
$fullUrl,
2024-12-24 09:30:42 +05:45
);
$this->featureContext->setResponse($response);
}
2023-01-03 12:33:33 +05:45
/**
2023-10-27 14:45:15 +02:00
* @When user :user unlocks file :itemToUnlock with the last created lock of file :itemToUseLockOf using the WebDAV API
2023-01-03 12:33:33 +05:45
*
* @param string $user
* @param string $itemToUnlock
* @param string $itemToUseLockOf
*
* @return void
*/
public function unlockItemWithLastLockOfOtherItemUsingWebDavAPI(
2023-05-02 17:25:56 +05:45
string $user,
string $itemToUnlock,
string $itemToUseLockOf,
2025-06-19 12:15:41 +05:45
): void {
$response = $this->unlockItemWithLastLockOfUserAndItemUsingWebDavAPI(
2023-01-03 12:33:33 +05:45
$user,
$itemToUnlock,
$user,
$itemToUseLockOf,
2023-01-03 12:33:33 +05:45
);
$this->featureContext->setResponse($response);
2023-01-03 12:33:33 +05:45
}
/**
2023-10-27 14:45:15 +02:00
* @When user :user unlocks file :itemToUnlock with the last created public lock of file :itemToUseLockOf using the WebDAV API
2023-01-03 12:33:33 +05:45
*
* @param string $user
* @param string $itemToUnlock
* @param string $itemToUseLockOf
*
* @return void
*/
public function unlockItemWithLastPublicLockOfOtherItemUsingWebDavAPI(
2023-05-02 17:25:56 +05:45
string $user,
string $itemToUnlock,
string $itemToUseLockOf,
2025-06-19 12:15:41 +05:45
): void {
$lockOwner = $this->featureContext->getLastCreatedPublicShareToken();
$response = $this->unlockItemWithLastLockOfUserAndItemUsingWebDavAPI(
2023-01-03 12:33:33 +05:45
$user,
$itemToUnlock,
$lockOwner,
$itemToUseLockOf,
2023-01-03 12:33:33 +05:45
);
$this->featureContext->setResponse($response);
2023-01-03 12:33:33 +05:45
}
/**
*
* @param string $user
* @param string $itemToUnlock
*
2023-05-02 17:25:56 +05:45
* @return int
2023-01-03 12:33:33 +05:45
*
* @throws Exception|GuzzleException
*/
private function countLockOfResources(
string $user,
string $itemToUnlock,
2024-12-24 09:30:42 +05:45
): int {
2023-01-03 12:33:33 +05:45
$user = $this->featureContext->getActualUsername($user);
$baseUrl = $this->featureContext->getBaseUrl();
$password = $this->featureContext->getPasswordForUser($user);
$body
= "<?xml version='1.0' encoding='UTF-8'?>" .
"<d:propfind xmlns:d='DAV:'> " .
"<d:prop><d:lockdiscovery/></d:prop>" .
"</d:propfind>";
$response = WebDavHelper::makeDavRequest(
$baseUrl,
$user,
$password,
"PROPFIND",
$itemToUnlock,
null,
2024-09-18 11:26:20 +05:45
null,
2023-01-03 12:33:33 +05:45
$body,
$this->featureContext->getDavPathVersion(),
2023-01-03 12:33:33 +05:45
);
$responseXmlObject = HttpRequestHelper::getResponseXml($response, __METHOD__);
$xmlPart = $responseXmlObject->xpath("//d:response//d:lockdiscovery/d:activelock");
2023-01-03 12:33:33 +05:45
if (\is_array($xmlPart)) {
return \count($xmlPart);
}
2025-06-19 10:58:37 +05:45
throw new Exception("xmlPart for 'd:activelock' was expected to be array but found: $xmlPart");
2023-01-03 12:33:33 +05:45
}
/**
2023-10-27 14:45:15 +02:00
* @Given user :user has unlocked file :itemToUnlock with the last created lock of file :itemToUseLockOf of user :lockOwner using the WebDAV API
2023-01-03 12:33:33 +05:45
*
* @param string $user
* @param string $itemToUnlock
* @param string $lockOwner
* @param string $itemToUseLockOf
* @param boolean $public
*
* @return void
* @throws Exception|GuzzleException
*/
public function hasUnlockItemWithTheLastCreatedLock(
2023-05-02 17:25:56 +05:45
string $user,
string $itemToUnlock,
string $lockOwner,
string $itemToUseLockOf,
bool $public = false,
2025-06-19 12:15:41 +05:45
): void {
2023-01-03 12:33:33 +05:45
$lockCount = $this->countLockOfResources($user, $itemToUnlock);
$response = $this->unlockItemWithLastLockOfUserAndItemUsingWebDavAPI(
2023-01-03 12:33:33 +05:45
$user,
$itemToUnlock,
$lockOwner,
$itemToUseLockOf,
$public,
2023-01-03 12:33:33 +05:45
);
$this->featureContext->theHTTPStatusCodeShouldBe(204, "", $response);
2023-01-03 12:33:33 +05:45
$lockCountAfterUnlock = $this->countLockOfResources($user, $itemToUnlock);
Assert::assertEquals(
$lockCount - 1,
$lockCountAfterUnlock,
"Expected $lockCount lock(s) for '$itemToUnlock' but found '$lockCount'",
);
2023-01-03 12:33:33 +05:45
}
/**
* @param string $user
* @param string $itemToUnlock
* @param string $lockOwner
* @param string $itemToUseLockOf
* @param boolean $public
* @param string|null $fullUrl
2024-09-18 11:26:20 +05:45
* @param string|null $spaceId
2023-01-03 12:33:33 +05:45
*
* @return ResponseInterface
* @throws GuzzleException
* @throws JsonException
2023-01-03 12:33:33 +05:45
*/
public function unlockItemWithLastLockOfUserAndItemUsingWebDavAPI(
string $user,
string $itemToUnlock,
string $lockOwner,
string $itemToUseLockOf,
bool $public = false,
2025-06-24 11:39:50 +02:00
?string $fullUrl = null,
2024-09-18 11:26:20 +05:45
?string $spaceId = null,
2024-12-24 09:30:42 +05:45
): ResponseInterface {
2023-01-03 12:33:33 +05:45
$user = $this->featureContext->getActualUsername($user);
$lockOwner = $this->featureContext->getActualUsername($lockOwner);
if ($public === true) {
2024-09-17 17:11:20 +05:45
$type = "public-files";
$password = $this->featureContext->getActualPassword("%public%");
2023-01-03 12:33:33 +05:45
} else {
$type = "files";
$password = $this->featureContext->getPasswordForUser($user);
}
$baseUrl = $this->featureContext->getBaseUrl();
if (!isset($this->tokenOfLastLock[$lockOwner][$itemToUseLockOf])) {
Assert::fail(
"could not find saved token of '$itemToUseLockOf' " .
"owned by user '$lockOwner'",
2023-01-03 12:33:33 +05:45
);
}
$headers = [
2025-06-19 10:46:56 +05:45
"Lock-Token" => $this->tokenOfLastLock[$lockOwner][$itemToUseLockOf],
2023-01-03 12:33:33 +05:45
];
if (isset($fullUrl)) {
$response = HttpRequestHelper::sendRequest(
$fullUrl,
"UNLOCK",
$this->featureContext->getActualUsername($user),
$this->featureContext->getPasswordForUser($user),
$headers,
);
} else {
$response = WebDavHelper::makeDavRequest(
$baseUrl,
$user,
$password,
"UNLOCK",
$itemToUnlock,
$headers,
2024-09-18 11:26:20 +05:45
$spaceId,
null,
$this->featureContext->getDavPathVersion(),
$type,
);
}
return $response;
}
/**
2023-10-27 14:45:15 +02:00
* @When user :user unlocks file :itemToUnlock with the last created lock of file :itemToUseLockOf of user :lockOwner using the WebDAV API
*
* @param string $user
* @param string $itemToUnlock
* @param string $lockOwner
* @param string $itemToUseLockOf
*
* @return void
*/
public function userUnlocksItemWithLastLockOfUserAndItemUsingWebDavAPI(
string $user,
string $itemToUnlock,
string $lockOwner,
string $itemToUseLockOf,
2025-06-19 12:15:41 +05:45
): void {
$response = $this->unlockItemWithLastLockOfUserAndItemUsingWebDavAPI(
$user,
$itemToUnlock,
$lockOwner,
$itemToUseLockOf,
);
$this->featureContext->setResponse($response);
2023-01-03 12:33:33 +05:45
}
/**
2023-10-27 14:45:15 +02:00
* @When the public unlocks file :itemToUnlock with the last created lock of file :itemToUseLockOf of user :lockOwner using the WebDAV API
2023-01-03 12:33:33 +05:45
*
* @param string $itemToUnlock
* @param string $lockOwner
* @param string $itemToUseLockOf
*
* @return void
*/
public function unlockItemAsPublicWithLastLockOfUserAndItemUsingWebDavAPI(
2023-05-02 17:25:56 +05:45
string $itemToUnlock,
string $lockOwner,
string $itemToUseLockOf,
2025-06-19 12:15:41 +05:45
): void {
2024-12-24 09:30:42 +05:45
$token = ($this->featureContext->isUsingSharingNG())
? $this->featureContext->shareNgGetLastCreatedLinkShareToken()
: $this->featureContext->getLastCreatedPublicShareToken();
$response = $this->unlockItemWithLastLockOfUserAndItemUsingWebDavAPI(
$token,
2023-01-03 12:33:33 +05:45
$itemToUnlock,
$lockOwner,
$itemToUseLockOf,
true,
2023-01-03 12:33:33 +05:45
);
$this->featureContext->setResponse($response);
2023-01-03 12:33:33 +05:45
}
/**
2023-10-27 14:45:15 +02:00
* @When the public unlocks file :itemToUnlock using the WebDAV API
2023-01-03 12:33:33 +05:45
*
* @param string $itemToUnlock
*
* @return void
*/
2025-06-19 12:15:41 +05:45
public function unlockItemAsPublicUsingWebDavAPI(string $itemToUnlock): void {
2024-12-24 09:30:42 +05:45
$token = ($this->featureContext->isUsingSharingNG())
? $this->featureContext->shareNgGetLastCreatedLinkShareToken()
: $this->featureContext->getLastCreatedPublicShareToken();
$response = $this->unlockItemWithLastLockOfUserAndItemUsingWebDavAPI(
$token,
2023-01-03 12:33:33 +05:45
$itemToUnlock,
$token,
2023-01-03 12:33:33 +05:45
$itemToUnlock,
true,
2023-01-03 12:33:33 +05:45
);
$this->featureContext->setResponse($response);
2023-01-03 12:33:33 +05:45
}
/**
* @When /^user "([^"]*)" moves (?:file|folder|entry) "([^"]*)" to "([^"]*)" sending the locktoken of (?:file|folder|entry) "([^"]*)" using the WebDAV API$/
*
* @param string $user
* @param string $fileSource
* @param string $fileDestination
* @param string $itemToUseLockOf
*
* @return void
*/
public function moveItemSendingLockToken(
2023-05-02 17:25:56 +05:45
string $user,
string $fileSource,
string $fileDestination,
string $itemToUseLockOf,
2025-06-19 12:15:41 +05:45
): void {
$response = $this->moveItemSendingLockTokenOfUser(
2023-01-03 12:33:33 +05:45
$user,
$fileSource,
$fileDestination,
$itemToUseLockOf,
$user,
2023-01-03 12:33:33 +05:45
);
$this->featureContext->setResponse($response);
2023-01-03 12:33:33 +05:45
}
/**
* @param string $user
* @param string $fileSource
* @param string $fileDestination
* @param string $itemToUseLockOf
* @param string $lockOwner
*
* @return ResponseInterface
2023-01-03 12:33:33 +05:45
*/
public function moveItemSendingLockTokenOfUser(
2023-05-02 17:25:56 +05:45
string $user,
string $fileSource,
string $fileDestination,
string $itemToUseLockOf,
string $lockOwner,
2024-12-24 09:30:42 +05:45
): ResponseInterface {
2023-01-03 12:33:33 +05:45
$user = $this->featureContext->getActualUsername($user);
$lockOwner = $this->featureContext->getActualUsername($lockOwner);
$destination = $this->featureContext->destinationHeaderValue(
$user,
$fileDestination,
2023-01-03 12:33:33 +05:45
);
$token = $this->tokenOfLastLock[$lockOwner][$itemToUseLockOf];
$headers = [
"Destination" => $destination,
2025-06-19 10:46:56 +05:45
"If" => "(<$token>)",
2023-01-03 12:33:33 +05:45
];
return $this->featureContext->makeDavRequest(
$user,
"MOVE",
$fileSource,
$headers,
);
}
/**
* @When /^user "([^"]*)" moves (?:file|folder|entry) "([^"]*)" to "([^"]*)" sending the locktoken of (?:file|folder|entry) "([^"]*)" of user "([^"]*)" using the WebDAV API$/
*
* @param string $user
* @param string $fileSource
* @param string $fileDestination
* @param string $itemToUseLockOf
* @param string $lockOwner
*
* @return void
*/
public function userMovesItemSendingLockTokenOfUser(
string $user,
string $fileSource,
string $fileDestination,
string $itemToUseLockOf,
string $lockOwner,
2025-06-19 12:15:41 +05:45
): void {
$response = $this->moveItemSendingLockTokenOfUser(
$user,
$fileSource,
$fileDestination,
$itemToUseLockOf,
$lockOwner,
);
$this->featureContext->setResponse($response);
2023-01-03 12:33:33 +05:45
}
/**
* @When /^user "([^"]*)" uploads file with content "([^"]*)" to "([^"]*)" sending the locktoken of (?:file|folder|entry) "([^"]*)" using the WebDAV API$/
*
* @param string $user
* @param string $content
* @param string $destination
* @param string $itemToUseLockOf
*
* @return void
*/
public function userUploadsAFileWithContentTo(
2023-05-02 17:25:56 +05:45
string $user,
string $content,
string $destination,
string $itemToUseLockOf,
2025-06-19 12:15:41 +05:45
): void {
2023-01-03 12:33:33 +05:45
$user = $this->featureContext->getActualUsername($user);
$token = $this->tokenOfLastLock[$user][$itemToUseLockOf];
$this->featureContext->pauseUploadDelete();
$response = $this->featureContext->makeDavRequest(
$user,
"PUT",
$destination,
["If" => "(<$token>)"],
$content,
2023-01-03 12:33:33 +05:45
);
$this->featureContext->setResponse($response);
$this->featureContext->setLastUploadDeleteTime(\time());
}
/**
2023-10-27 14:45:15 +02:00
* @Then :count locks should be reported for file :file of user :user by the WebDAV API
2023-01-03 12:33:33 +05:45
*
* @param int $count
* @param string $file
* @param string $user
*
* @return void
* @throws GuzzleException
*/
2025-06-19 12:15:41 +05:45
public function numberOfLockShouldBeReported(int $count, string $file, string $user): void {
2023-01-03 12:33:33 +05:45
$lockCount = $this->countLockOfResources($user, $file);
Assert::assertEquals(
$count,
$lockCount,
"Expected $count lock(s) for '$file' but found '$lockCount'",
2023-01-03 12:33:33 +05:45
);
}
/**
* @When the user waits for :time seconds to expire the lock
*
* @param int $time
*
* @return void
*/
public function waitForCertainSecondsToExpireTheLock(int $time): void {
\sleep($time);
}
/**
* @Then :count locks should be reported for file :file inside the space :space of user :user
*
* @param int $count
* @param string $file
* @param string $spaceName
* @param string $user
*
* @return void
* @throws GuzzleException
*/
2024-12-24 09:30:42 +05:45
public function numberOfLockShouldBeReportedInProjectSpace(
int $count,
string $file,
string $spaceName,
string $user,
2025-06-19 12:15:41 +05:45
): void {
2024-03-13 14:26:37 +05:45
$response = $this->spacesContext->sendPropfindRequestToSpace($user, $spaceName, $file, null, '0');
$this->featureContext->theHTTPStatusCodeShouldBe(207, "", $response);
$responseXmlObject = HttpRequestHelper::getResponseXml($response, __METHOD__);
$xmlPart = $responseXmlObject->xpath("//d:response//d:lockdiscovery/d:activelock");
if (\is_array($xmlPart)) {
$lockCount = \count($xmlPart);
} else {
throw new Exception("xmlPart for 'd:activelock' was expected to be array but found: $xmlPart");
}
Assert::assertEquals(
$count,
$lockCount,
"Expected $count lock(s) for '$file' inside space '$spaceName' but found '$lockCount'",
);
}
2023-01-03 12:33:33 +05:45
/**
* This will run before EVERY scenario.
* It will set the properties for this object.
*
* @BeforeScenario
*
* @param BeforeScenarioScope $scope
*
* @return void
*/
2025-06-19 12:15:41 +05:45
public function before(BeforeScenarioScope $scope): void {
2023-01-03 12:33:33 +05:45
// Get the environment
$environment = $scope->getEnvironment();
// Get all the contexts you need in this context
2024-10-08 12:26:40 +05:45
$this->featureContext = BehatHelper::getContext($scope, $environment, 'FeatureContext');
$this->publicWebDavContext = BehatHelper::getContext($scope, $environment, 'PublicWebDavContext');
$this->spacesContext = BehatHelper::getContext($scope, $environment, 'SpacesContext');
2023-01-03 12:33:33 +05:45
}
}