2015-11-24 15:48:06 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use Behat\Behat\Context\Context;
|
|
|
|
use Behat\Behat\Context\SnippetAcceptingContext;
|
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';
|
|
|
|
|
|
|
|
|
|
|
|
trait WebDav{
|
|
|
|
|
|
|
|
/** @var string*/
|
|
|
|
private $davPath = "remote.php/webdav";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Given /^using dav path "([^"]*)"$/
|
|
|
|
*/
|
|
|
|
public function usingDavPath($davPath) {
|
|
|
|
$this->davPath = $davPath;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
$request->addHeader($key, $value);
|
|
|
|
}
|
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 "([^"]*)"$/
|
|
|
|
*/
|
|
|
|
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 "([^"]*)"$/
|
|
|
|
*/
|
|
|
|
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 "([^"]*)"$/
|
|
|
|
*/
|
|
|
|
public function downloadFileWithRange($fileSource, $range){
|
|
|
|
$fullUrl = substr($this->baseUrl, 0, -4) . $this->davPath;
|
|
|
|
$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 "([^"]*)"$/
|
|
|
|
*/
|
|
|
|
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, ""];
|
|
|
|
|
|
|
|
$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 "([^"]*)"$/
|
|
|
|
*/
|
|
|
|
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 "([^"]*)"$/
|
|
|
|
*/
|
|
|
|
public function downloadedContentWhenDownloadindShouldBe($fileSource, $range, $content){
|
|
|
|
$this->downloadFileWithRange($fileSource, $range);
|
|
|
|
$this->downloadedContentShouldBe($content);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-30 15:20:05 +03:00
|
|
|
/*Returns the elements of a propfind, $folderDepth requires 1 to see elements without children*/
|
|
|
|
public function listFolder($user, $path, $folderDepth){
|
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);
|
|
|
|
|
|
|
|
$response = $client->propfind($this->davPath . "/", array(
|
2015-11-30 15:20:05 +03:00
|
|
|
'{DAV:}getetag'
|
|
|
|
), $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$/
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
2015-12-02 14:44:03 +03:00
|
|
|
/**
|
|
|
|
* @Given User :user created a folder :destination
|
|
|
|
*/
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-24 15:48:06 +03:00
|
|
|
}
|
|
|
|
|