2015-11-24 15:48:06 +03:00
|
|
|
<?php
|
|
|
|
|
2015-11-27 15:55:48 +03:00
|
|
|
use GuzzleHttp\Client as GClient;
|
2015-11-24 15:48:06 +03:00
|
|
|
use GuzzleHttp\Message\ResponseInterface;
|
2015-11-27 15:55:48 +03:00
|
|
|
use Sabre\DAV\Client as SClient;
|
2015-11-24 15:48:06 +03:00
|
|
|
|
|
|
|
require __DIR__ . '/../../vendor/autoload.php';
|
|
|
|
|
|
|
|
|
2016-02-19 01:07:41 +03:00
|
|
|
trait WebDav {
|
2016-03-11 13:14:49 +03:00
|
|
|
use Sharing;
|
|
|
|
|
2015-11-24 15:48:06 +03:00
|
|
|
/** @var string*/
|
|
|
|
private $davPath = "remote.php/webdav";
|
2016-02-23 13:54:22 +03:00
|
|
|
/** @var ResponseInterface */
|
|
|
|
private $response;
|
2015-11-24 15:48:06 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @Given /^using dav path "([^"]*)"$/
|
|
|
|
*/
|
|
|
|
public function usingDavPath($davPath) {
|
|
|
|
$this->davPath = $davPath;
|
2016-02-24 12:39:04 +03:00
|
|
|
}
|
2015-11-24 15:48:06 +03:00
|
|
|
|
2015-12-01 15:36:55 +03:00
|
|
|
public function makeDavRequest($user, $method, $path, $headers, $body = null){
|
2015-11-24 15:48:06 +03:00
|
|
|
$fullUrl = substr($this->baseUrl, 0, -4) . $this->davPath . "$path";
|
2015-11-27 15:55:48 +03:00
|
|
|
$client = new GClient();
|
2015-11-24 15:48:06 +03:00
|
|
|
$options = [];
|
|
|
|
if ($user === 'admin') {
|
|
|
|
$options['auth'] = $this->adminUser;
|
|
|
|
} else {
|
|
|
|
$options['auth'] = [$user, $this->regularUser];
|
|
|
|
}
|
|
|
|
$request = $client->createRequest($method, $fullUrl, $options);
|
2015-11-27 15:55:48 +03:00
|
|
|
if (!is_null($headers)){
|
|
|
|
foreach ($headers as $key => $value) {
|
2016-02-24 12:39:04 +03:00
|
|
|
$request->addHeader($key, $value);
|
2015-11-27 15:55:48 +03:00
|
|
|
}
|
2015-11-24 15:48:06 +03:00
|
|
|
}
|
2015-12-01 15:36:55 +03:00
|
|
|
|
|
|
|
if (!is_null($body)) {
|
|
|
|
$request->setBody($body);
|
|
|
|
}
|
|
|
|
|
2015-11-24 15:48:06 +03:00
|
|
|
return $client->send($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Given /^User "([^"]*)" moved file "([^"]*)" to "([^"]*)"$/
|
2016-03-11 13:14:49 +03:00
|
|
|
* @param string $user
|
|
|
|
* @param string $fileSource
|
|
|
|
* @param string $fileDestination
|
2015-11-24 15:48:06 +03:00
|
|
|
*/
|
|
|
|
public function userMovedFile($user, $fileSource, $fileDestination){
|
|
|
|
$fullUrl = substr($this->baseUrl, 0, -4) . $this->davPath;
|
|
|
|
$headers['Destination'] = $fullUrl . $fileDestination;
|
|
|
|
$this->response = $this->makeDavRequest($user, "MOVE", $fileSource, $headers);
|
|
|
|
PHPUnit_Framework_Assert::assertEquals(201, $this->response->getStatusCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @When /^User "([^"]*)" moves file "([^"]*)" to "([^"]*)"$/
|
2016-03-11 13:14:49 +03:00
|
|
|
* @param string $user
|
|
|
|
* @param string $fileSource
|
|
|
|
* @param string $fileDestination
|
2015-11-24 15:48:06 +03:00
|
|
|
*/
|
|
|
|
public function userMovesFile($user, $fileSource, $fileDestination){
|
|
|
|
$fullUrl = substr($this->baseUrl, 0, -4) . $this->davPath;
|
|
|
|
$headers['Destination'] = $fullUrl . $fileDestination;
|
|
|
|
$this->response = $this->makeDavRequest($user, "MOVE", $fileSource, $headers);
|
|
|
|
}
|
|
|
|
|
2015-11-30 17:07:02 +03:00
|
|
|
/**
|
|
|
|
* @When /^Downloading file "([^"]*)" with range "([^"]*)"$/
|
2016-03-11 13:14:49 +03:00
|
|
|
* @param string $fileSource
|
|
|
|
* @param string $range
|
2015-11-30 17:07:02 +03:00
|
|
|
*/
|
|
|
|
public function downloadFileWithRange($fileSource, $range){
|
|
|
|
$headers['Range'] = $range;
|
|
|
|
$this->response = $this->makeDavRequest($this->currentUser, "GET", $fileSource, $headers);
|
|
|
|
}
|
|
|
|
|
2015-12-03 12:46:00 +03:00
|
|
|
/**
|
|
|
|
* @When /^Downloading last public shared file with range "([^"]*)"$/
|
2016-03-11 13:14:49 +03:00
|
|
|
* @param string $range
|
2015-12-03 12:46:00 +03:00
|
|
|
*/
|
|
|
|
public function downloadPublicFileWithRange($range){
|
|
|
|
$token = $this->lastShareData->data->token;
|
|
|
|
$fullUrl = substr($this->baseUrl, 0, -4) . "public.php/webdav";
|
|
|
|
$headers['Range'] = $range;
|
|
|
|
|
|
|
|
$client = new GClient();
|
|
|
|
$options = [];
|
|
|
|
$options['auth'] = [$token, ""];
|
2016-02-24 12:39:04 +03:00
|
|
|
|
2015-12-03 12:46:00 +03:00
|
|
|
$request = $client->createRequest("GET", $fullUrl, $options);
|
|
|
|
$request->addHeader('Range', $range);
|
|
|
|
|
|
|
|
$this->response = $client->send($request);
|
|
|
|
}
|
|
|
|
|
2015-11-30 17:07:02 +03:00
|
|
|
/**
|
|
|
|
* @Then /^Downloaded content should be "([^"]*)"$/
|
2016-03-11 13:14:49 +03:00
|
|
|
* @param string $content
|
2015-11-30 17:07:02 +03:00
|
|
|
*/
|
|
|
|
public function downloadedContentShouldBe($content){
|
|
|
|
PHPUnit_Framework_Assert::assertEquals($content, (string)$this->response->getBody());
|
|
|
|
}
|
|
|
|
|
2015-12-09 15:56:03 +03:00
|
|
|
/**
|
|
|
|
* @Then /^Downloaded content when downloading file "([^"]*)" with range "([^"]*)" should be "([^"]*)"$/
|
2016-03-11 13:14:49 +03:00
|
|
|
* @param string $fileSource
|
|
|
|
* @param string $range
|
|
|
|
* @param string $content
|
2015-12-09 15:56:03 +03:00
|
|
|
*/
|
|
|
|
public function downloadedContentWhenDownloadindShouldBe($fileSource, $range, $content){
|
|
|
|
$this->downloadFileWithRange($fileSource, $range);
|
|
|
|
$this->downloadedContentShouldBe($content);
|
|
|
|
}
|
|
|
|
|
2016-02-23 13:54:22 +03:00
|
|
|
/**
|
|
|
|
* @When Downloading file :fileName
|
2016-03-11 13:14:49 +03:00
|
|
|
* @param string $fileName
|
2016-02-23 13:54:22 +03:00
|
|
|
*/
|
|
|
|
public function downloadingFile($fileName) {
|
|
|
|
$this->response = $this->makeDavRequest($this->currentUser, 'GET', $fileName, []);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Then The following headers should be set
|
2016-03-11 13:14:49 +03:00
|
|
|
* @param \Behat\Gherkin\Node\TableNode $table
|
|
|
|
* @throws \Exception
|
2016-02-23 13:54:22 +03:00
|
|
|
*/
|
|
|
|
public function theFollowingHeadersShouldBeSet(\Behat\Gherkin\Node\TableNode $table) {
|
|
|
|
foreach($table->getTable() as $header) {
|
|
|
|
$headerName = $header[0];
|
|
|
|
$expectedHeaderValue = $header[1];
|
|
|
|
$returnedHeader = $this->response->getHeader($headerName);
|
|
|
|
if($returnedHeader !== $expectedHeaderValue) {
|
|
|
|
throw new \Exception(
|
|
|
|
sprintf(
|
|
|
|
"Expected value '%s' for header '%s', got '%s'",
|
|
|
|
$expectedHeaderValue,
|
|
|
|
$headerName,
|
|
|
|
$returnedHeader
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Then Downloaded content should start with :start
|
2016-03-11 13:14:49 +03:00
|
|
|
* @param int $start
|
|
|
|
* @throws \Exception
|
2016-02-23 13:54:22 +03:00
|
|
|
*/
|
|
|
|
public function downloadedContentShouldStartWith($start) {
|
|
|
|
if(strpos($this->response->getBody()->getContents(), $start) !== 0) {
|
|
|
|
throw new \Exception(
|
|
|
|
sprintf(
|
|
|
|
"Expected '%s', got '%s'",
|
|
|
|
$start,
|
|
|
|
$this->response->getBody()->getContents()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2015-12-09 15:56:03 +03:00
|
|
|
|
2016-02-24 12:39:04 +03:00
|
|
|
/**
|
|
|
|
* @Then /^as "([^"]*)" gets properties of folder "([^"]*)" with$/
|
2016-03-11 13:14:49 +03:00
|
|
|
* @param string $user
|
|
|
|
* @param string $path
|
2016-02-24 12:39:04 +03:00
|
|
|
* @param \Behat\Gherkin\Node\TableNode|null $propertiesTable
|
|
|
|
*/
|
|
|
|
public function asGetsPropertiesOfFolderWith($user, $path, $propertiesTable) {
|
|
|
|
$properties = null;
|
|
|
|
if ($propertiesTable instanceof \Behat\Gherkin\Node\TableNode) {
|
|
|
|
foreach ($propertiesTable->getRows() as $row) {
|
|
|
|
$properties[] = $row[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->response = $this->listFolder($user, $path, 0, $properties);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Then the single response should contain a property :key with value :value
|
2016-03-11 13:14:49 +03:00
|
|
|
* @param string $key
|
|
|
|
* @param string $expectedValue
|
|
|
|
* @throws \Exception
|
2016-02-24 12:39:04 +03:00
|
|
|
*/
|
|
|
|
public function theSingleResponseShouldContainAPropertyWithValue($key, $expectedValue) {
|
|
|
|
$keys = $this->response;
|
2016-03-17 17:50:33 +03:00
|
|
|
if (!array_key_exists($key, $keys)) {
|
2016-03-11 13:14:49 +03:00
|
|
|
throw new \Exception("Cannot find property \"$key\" with \"$expectedValue\"");
|
2016-02-24 12:39:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$value = $keys[$key];
|
|
|
|
if ($value !== $expectedValue) {
|
|
|
|
throw new \Exception("Property \"$key\" found with value \"$value\", expected \"$expectedValue\"");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-17 17:50:33 +03:00
|
|
|
/**
|
|
|
|
* @Then the response should contain a share-types property with
|
|
|
|
*/
|
|
|
|
public function theResponseShouldContainAShareTypesPropertyWith($table)
|
|
|
|
{
|
|
|
|
$keys = $this->response;
|
|
|
|
if (!array_key_exists('{http://owncloud.org/ns}share-types', $keys)) {
|
|
|
|
throw new \Exception("Cannot find property \"{http://owncloud.org/ns}share-types\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
$foundTypes = [];
|
|
|
|
$data = $keys['{http://owncloud.org/ns}share-types'];
|
|
|
|
foreach ($data as $item) {
|
|
|
|
if ($item['name'] !== '{http://owncloud.org/ns}share-type') {
|
|
|
|
throw new \Exception('Invalid property found: "' . $item['name'] . '"');
|
|
|
|
}
|
|
|
|
|
|
|
|
$foundTypes[] = $item['value'];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($table->getRows() as $row) {
|
|
|
|
$key = array_search($row[0], $foundTypes);
|
|
|
|
if ($key === false) {
|
|
|
|
throw new \Exception('Expected type ' . $row[0] . ' not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
unset($foundTypes[$key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($foundTypes !== []) {
|
|
|
|
throw new \Exception('Found more share types then specified: ' . $foundTypes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Then the response should contain an empty property :property
|
|
|
|
* @param string $property
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function theResponseShouldContainAnEmptyProperty($property) {
|
|
|
|
$properties = $this->response;
|
|
|
|
if (!array_key_exists($property, $properties)) {
|
|
|
|
throw new \Exception("Cannot find property \"$property\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($properties[$property] !== null) {
|
|
|
|
throw new \Exception("Property \"$property\" is not empty");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-30 15:20:05 +03:00
|
|
|
/*Returns the elements of a propfind, $folderDepth requires 1 to see elements without children*/
|
2016-02-24 12:39:04 +03:00
|
|
|
public function listFolder($user, $path, $folderDepth, $properties = null){
|
2015-11-27 15:55:48 +03:00
|
|
|
$fullUrl = substr($this->baseUrl, 0, -4);
|
|
|
|
|
|
|
|
$settings = array(
|
|
|
|
'baseUri' => $fullUrl,
|
|
|
|
'userName' => $user,
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($user === 'admin') {
|
|
|
|
$settings['password'] = $this->adminUser[1];
|
|
|
|
} else {
|
|
|
|
$settings['password'] = $this->regularUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
$client = new SClient($settings);
|
|
|
|
|
2016-02-24 12:39:04 +03:00
|
|
|
if (!$properties) {
|
|
|
|
$properties = [
|
|
|
|
'{DAV:}getetag'
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = $client->propfind($this->davPath . '/' . ltrim($path, '/'), $properties, $folderDepth);
|
2015-11-27 15:55:48 +03:00
|
|
|
|
2015-11-30 15:20:05 +03:00
|
|
|
return $response;
|
2015-11-27 15:55:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-30 15:20:05 +03:00
|
|
|
* @Then /^user "([^"]*)" should see following elements$/
|
2016-03-11 13:14:49 +03:00
|
|
|
* @param string $user
|
2015-11-30 15:20:05 +03:00
|
|
|
* @param \Behat\Gherkin\Node\TableNode|null $expectedElements
|
2015-11-27 15:55:48 +03:00
|
|
|
*/
|
2015-11-30 15:20:05 +03:00
|
|
|
public function checkElementList($user, $expectedElements){
|
2015-12-07 17:03:40 +03:00
|
|
|
$elementList = $this->listFolder($user, '/', 3);
|
2015-11-30 15:20:05 +03:00
|
|
|
if ($expectedElements instanceof \Behat\Gherkin\Node\TableNode) {
|
|
|
|
$elementRows = $expectedElements->getRows();
|
|
|
|
$elementsSimplified = $this->simplifyArray($elementRows);
|
|
|
|
foreach($elementsSimplified as $expectedElement) {
|
|
|
|
$webdavPath = "/" . $this->davPath . $expectedElement;
|
|
|
|
if (!array_key_exists($webdavPath,$elementList)){
|
|
|
|
PHPUnit_Framework_Assert::fail("$webdavPath" . " is not in propfind answer");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-27 15:55:48 +03:00
|
|
|
}
|
2015-12-01 15:36:55 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @When User :user uploads file :source to :destination
|
2016-03-11 13:14:49 +03:00
|
|
|
* @param string $user
|
|
|
|
* @param string $source
|
|
|
|
* @param string $destination
|
2015-12-01 15:36:55 +03:00
|
|
|
*/
|
|
|
|
public function userUploadsAFileTo($user, $source, $destination)
|
|
|
|
{
|
|
|
|
$file = \GuzzleHttp\Stream\Stream::factory(fopen($source, 'r'));
|
|
|
|
try {
|
|
|
|
$this->response = $this->makeDavRequest($user, "PUT", $destination, [], $file);
|
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) {
|
|
|
|
// 4xx and 5xx responses cause an exception
|
|
|
|
$this->response = $e->getResponse();
|
|
|
|
}
|
|
|
|
}
|
2015-11-27 15:55:48 +03:00
|
|
|
|
2016-02-19 01:07:41 +03:00
|
|
|
/**
|
|
|
|
* @When User :user deletes file :file
|
2016-03-11 13:14:49 +03:00
|
|
|
* @param string $user
|
|
|
|
* @param string $file
|
2016-02-19 01:07:41 +03:00
|
|
|
*/
|
|
|
|
public function userDeletesFile($user, $file) {
|
|
|
|
try {
|
|
|
|
$this->response = $this->makeDavRequest($user, 'DELETE', $file, []);
|
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) {
|
|
|
|
// 4xx and 5xx responses cause an exception
|
|
|
|
$this->response = $e->getResponse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-02 14:44:03 +03:00
|
|
|
/**
|
|
|
|
* @Given User :user created a folder :destination
|
2016-03-11 13:14:49 +03:00
|
|
|
* @param string $user
|
|
|
|
* @param string $destination
|
2015-12-02 14:44:03 +03:00
|
|
|
*/
|
|
|
|
public function userCreatedAFolder($user, $destination){
|
|
|
|
try {
|
|
|
|
$this->response = $this->makeDavRequest($user, "MKCOL", $destination, []);
|
|
|
|
} catch (\GuzzleHttp\Exception\ServerException $e) {
|
|
|
|
// 4xx and 5xx responses cause an exception
|
|
|
|
$this->response = $e->getResponse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-07 22:55:49 +03:00
|
|
|
/**
|
|
|
|
* @Given user :user uploads chunk file :num of :total with :data to :destination
|
2016-03-11 13:14:49 +03:00
|
|
|
* @param string $user
|
|
|
|
* @param int $num
|
|
|
|
* @param int $total
|
|
|
|
* @param string $data
|
|
|
|
* @param string $destination
|
2016-03-07 22:55:49 +03:00
|
|
|
*/
|
|
|
|
public function userUploadsChunkFileOfWithToWithChecksum($user, $num, $total, $data, $destination)
|
|
|
|
{
|
|
|
|
$num -= 1;
|
|
|
|
$data = \GuzzleHttp\Stream\Stream::factory($data);
|
|
|
|
$file = $destination . '-chunking-42-'.$total.'-'.$num;
|
|
|
|
$this->makeDavRequest($user, 'PUT', $file, ['OC-Chunked' => '1'], $data);
|
|
|
|
}
|
|
|
|
|
2015-11-24 15:48:06 +03:00
|
|
|
}
|
|
|
|
|