2015-08-06 16:47:21 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use Behat\Behat\Context\BehatContext;
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use GuzzleHttp\Message\ResponseInterface;
|
|
|
|
|
2015-08-06 17:46:06 +03:00
|
|
|
require __DIR__ . '/../../vendor/autoload.php';
|
2015-08-06 16:47:21 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Features context.
|
|
|
|
*/
|
|
|
|
class FeatureContext extends BehatContext {
|
|
|
|
|
2015-08-06 18:16:24 +03:00
|
|
|
/** @var string */
|
|
|
|
private $baseUrl = '';
|
2015-08-06 16:47:21 +03:00
|
|
|
|
2015-08-06 18:16:24 +03:00
|
|
|
/** @var ResponseInterface */
|
|
|
|
private $response = null;
|
2015-08-06 16:47:21 +03:00
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $currentUser = '';
|
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
private $apiVersion = 1;
|
|
|
|
|
2015-08-06 18:16:24 +03:00
|
|
|
/**
|
|
|
|
* Initializes context.
|
|
|
|
* Every scenario gets it's own context object.
|
|
|
|
*
|
|
|
|
* @param array $parameters context parameters (set them up through behat.yml)
|
|
|
|
*/
|
|
|
|
public function __construct(array $parameters) {
|
2015-08-06 16:47:21 +03:00
|
|
|
|
2015-08-06 18:16:24 +03:00
|
|
|
// Initialize your context here
|
2015-08-06 16:47:21 +03:00
|
|
|
$this->baseUrl = $parameters['baseUrl'];
|
|
|
|
$this->adminUser = $parameters['admin'];
|
2015-10-14 19:30:18 +03:00
|
|
|
$this->regularUser = $parameters['regular_user_password'];
|
2015-09-29 14:56:56 +03:00
|
|
|
|
|
|
|
// in case of ci deployment we take the server url from the environment
|
|
|
|
$testServerUrl = getenv('TEST_SERVER_URL');
|
|
|
|
if ($testServerUrl !== false) {
|
|
|
|
$this->baseUrl = $testServerUrl;
|
|
|
|
}
|
2015-08-06 18:16:24 +03:00
|
|
|
}
|
2015-08-06 16:47:21 +03:00
|
|
|
|
2015-08-06 18:16:24 +03:00
|
|
|
/**
|
2015-10-08 17:21:31 +03:00
|
|
|
* @When /^sending "([^"]*)" to "([^"]*)"$/
|
|
|
|
*/
|
2015-08-06 18:16:24 +03:00
|
|
|
public function sendingTo($verb, $url) {
|
|
|
|
$this->sendingToWith($verb, $url, null);
|
|
|
|
}
|
|
|
|
|
2015-10-02 12:45:25 +03:00
|
|
|
/**
|
2015-10-08 17:21:31 +03:00
|
|
|
* Parses the xml answer to get ocs response which doesn't match with
|
|
|
|
* http one in v1 of the api.
|
|
|
|
*/
|
|
|
|
public function getOCSResponse($response) {
|
2015-10-02 12:45:25 +03:00
|
|
|
return $response->xml()->meta[0]->statuscode;
|
|
|
|
}
|
2015-09-30 18:50:18 +03:00
|
|
|
|
2015-10-06 19:37:55 +03:00
|
|
|
/**
|
2015-10-08 17:21:31 +03:00
|
|
|
* Parses the xml answer to get the array of users returned.
|
|
|
|
*/
|
2015-10-07 14:09:01 +03:00
|
|
|
public function getArrayOfUsersResponded($resp) {
|
|
|
|
$listCheckedElements = $resp->xml()->data[0]->users[0]->element;
|
2015-10-08 17:21:31 +03:00
|
|
|
$extractedElementsArray = json_decode(json_encode($listCheckedElements), 1);
|
2015-10-07 14:09:01 +03:00
|
|
|
return $extractedElementsArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-08 17:21:31 +03:00
|
|
|
* Parses the xml answer to get the array of groups returned.
|
|
|
|
*/
|
2015-10-07 14:09:01 +03:00
|
|
|
public function getArrayOfGroupsResponded($resp) {
|
|
|
|
$listCheckedElements = $resp->xml()->data[0]->groups[0]->element;
|
2015-10-08 17:21:31 +03:00
|
|
|
$extractedElementsArray = json_decode(json_encode($listCheckedElements), 1);
|
2015-10-07 14:09:01 +03:00
|
|
|
return $extractedElementsArray;
|
|
|
|
}
|
2015-10-06 19:37:55 +03:00
|
|
|
|
2015-10-14 19:30:18 +03:00
|
|
|
/**
|
|
|
|
* Parses the xml answer to get the array of subadmins returned.
|
|
|
|
*/
|
|
|
|
public function getArrayOfSubadminsResponded($resp) {
|
|
|
|
$listCheckedElements = $resp->xml()->data[0]->element;
|
|
|
|
$extractedElementsArray = json_decode(json_encode($listCheckedElements), 1);
|
|
|
|
return $extractedElementsArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function is needed to use a vertical fashion in the gherkin tables.
|
|
|
|
*/
|
|
|
|
public function simplifyArray($arrayOfArrays){
|
|
|
|
$a = array_map(function($subArray) { return $subArray[0]; }, $arrayOfArrays);
|
|
|
|
return $a;
|
|
|
|
}
|
|
|
|
|
2015-10-06 19:37:55 +03:00
|
|
|
/**
|
|
|
|
* @Then /^users returned are$/
|
|
|
|
* @param \Behat\Gherkin\Node\TableNode|null $formData
|
|
|
|
*/
|
|
|
|
public function theUsersShouldBe($usersList) {
|
|
|
|
if ($usersList instanceof \Behat\Gherkin\Node\TableNode) {
|
2015-10-14 19:30:18 +03:00
|
|
|
$users = $usersList->getRows();
|
|
|
|
$usersSimplified = $this->simplifyArray($users);
|
2015-10-07 14:09:01 +03:00
|
|
|
$respondedArray = $this->getArrayOfUsersResponded($this->response);
|
2015-10-14 19:30:18 +03:00
|
|
|
sort($usersSimplified);
|
|
|
|
sort($respondedArray);
|
|
|
|
PHPUnit_Framework_Assert::assertEquals($usersSimplified, $respondedArray);
|
2015-10-06 19:37:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-07 12:03:00 +03:00
|
|
|
/**
|
|
|
|
* @Then /^groups returned are$/
|
|
|
|
* @param \Behat\Gherkin\Node\TableNode|null $formData
|
|
|
|
*/
|
|
|
|
public function theGroupsShouldBe($groupsList) {
|
|
|
|
if ($groupsList instanceof \Behat\Gherkin\Node\TableNode) {
|
2015-10-14 19:30:18 +03:00
|
|
|
$groups = $groupsList->getRows();
|
|
|
|
$groupsSimplified = $this->simplifyArray($groups);
|
2015-10-07 14:09:01 +03:00
|
|
|
$respondedArray = $this->getArrayOfGroupsResponded($this->response);
|
2015-10-14 19:30:18 +03:00
|
|
|
sort($groups);
|
|
|
|
sort($respondedArray);
|
|
|
|
PHPUnit_Framework_Assert::assertEquals($groupsSimplified, $respondedArray);
|
2015-10-07 12:03:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-10-08 17:21:31 +03:00
|
|
|
|
2015-10-14 19:30:18 +03:00
|
|
|
/**
|
|
|
|
* @Then /^subadmin groups returned are$/
|
|
|
|
* @param \Behat\Gherkin\Node\TableNode|null $formData
|
|
|
|
*/
|
|
|
|
public function theSubadminGroupsShouldBe($groupsList) {
|
|
|
|
if ($groupsList instanceof \Behat\Gherkin\Node\TableNode) {
|
|
|
|
$groups = $groupsList->getRows();
|
|
|
|
$groupsSimplified = $this->simplifyArray($groups);
|
|
|
|
$respondedArray = $this->getArrayOfSubadminsResponded($this->response);
|
|
|
|
sort($groups);
|
|
|
|
sort($respondedArray);
|
|
|
|
PHPUnit_Framework_Assert::assertEquals($groupsSimplified, $respondedArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Then /^subadmin users returned are$/
|
|
|
|
* @param \Behat\Gherkin\Node\TableNode|null $formData
|
|
|
|
*/
|
|
|
|
public function theSubadminUsersShouldBe($groupsList) {
|
|
|
|
$this->theSubadminGroupsShouldBe($groupsList);
|
|
|
|
}
|
|
|
|
|
2015-10-02 12:45:25 +03:00
|
|
|
/**
|
2015-10-02 10:40:25 +03:00
|
|
|
* @Then /^the OCS status code should be "([^"]*)"$/
|
2015-08-06 18:16:24 +03:00
|
|
|
*/
|
2015-10-02 10:40:25 +03:00
|
|
|
public function theOCSStatusCodeShouldBe($statusCode) {
|
2015-09-30 18:50:18 +03:00
|
|
|
PHPUnit_Framework_Assert::assertEquals($statusCode, $this->getOCSResponse($this->response));
|
2015-08-06 18:16:24 +03:00
|
|
|
}
|
|
|
|
|
2015-10-02 12:45:25 +03:00
|
|
|
/**
|
2015-10-02 10:40:25 +03:00
|
|
|
* @Then /^the HTTP status code should be "([^"]*)"$/
|
|
|
|
*/
|
|
|
|
public function theHTTPStatusCodeShouldBe($statusCode) {
|
|
|
|
PHPUnit_Framework_Assert::assertEquals($statusCode, $this->response->getStatusCode());
|
|
|
|
}
|
|
|
|
|
2015-08-06 18:16:24 +03:00
|
|
|
/**
|
|
|
|
* @Given /^As an "([^"]*)"$/
|
|
|
|
*/
|
|
|
|
public function asAn($user) {
|
2015-08-06 16:47:21 +03:00
|
|
|
$this->currentUser = $user;
|
2015-08-06 18:16:24 +03:00
|
|
|
}
|
2015-08-06 16:47:21 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @Given /^using api version "([^"]*)"$/
|
|
|
|
*/
|
|
|
|
public function usingApiVersion($version) {
|
|
|
|
$this->apiVersion = $version;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Given /^user "([^"]*)" exists$/
|
|
|
|
*/
|
|
|
|
public function userExists($user) {
|
2015-10-02 10:40:25 +03:00
|
|
|
$fullUrl = $this->baseUrl . "v2.php/cloud/users/$user";
|
2015-08-06 18:16:24 +03:00
|
|
|
$client = new Client();
|
|
|
|
$options = [];
|
|
|
|
if ($this->currentUser === 'admin') {
|
|
|
|
$options['auth'] = $this->adminUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->response = $client->get($fullUrl, $options);
|
2015-10-02 10:40:25 +03:00
|
|
|
PHPUnit_Framework_Assert::assertEquals(200, $this->response->getStatusCode());
|
2015-08-06 18:16:24 +03:00
|
|
|
}
|
|
|
|
|
2015-10-14 19:30:18 +03:00
|
|
|
/**
|
|
|
|
* @Given /^user "([^"]*)" belongs to group "([^"]*)"$/
|
|
|
|
*/
|
|
|
|
public function userBelongsToGroup($user, $group) {
|
|
|
|
$fullUrl = $this->baseUrl . "v2.php/cloud/users/$user/groups";
|
|
|
|
$client = new Client();
|
|
|
|
$options = [];
|
|
|
|
if ($this->currentUser === 'admin') {
|
|
|
|
$options['auth'] = $this->adminUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->response = $client->get($fullUrl, $options);
|
|
|
|
$groups = array($group);
|
|
|
|
$respondedArray = $this->getArrayOfGroupsResponded($this->response);
|
|
|
|
sort($groups);
|
|
|
|
sort($respondedArray);
|
|
|
|
PHPUnit_Framework_Assert::assertEquals($groups, $respondedArray);
|
|
|
|
PHPUnit_Framework_Assert::assertEquals(200, $this->response->getStatusCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Given /^user "([^"]*)" does not belong to group "([^"]*)"$/
|
|
|
|
*/
|
|
|
|
public function userDoesNotBelongToGroup($user, $group) {
|
|
|
|
$fullUrl = $this->baseUrl . "v2.php/cloud/users/$user/groups";
|
|
|
|
$client = new Client();
|
|
|
|
$options = [];
|
|
|
|
if ($this->currentUser === 'admin') {
|
|
|
|
$options['auth'] = $this->adminUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->response = $client->get($fullUrl, $options);
|
|
|
|
$groups = array($group);
|
|
|
|
$respondedArray = $this->getArrayOfGroupsResponded($this->response);
|
|
|
|
sort($groups);
|
|
|
|
sort($respondedArray);
|
|
|
|
PHPUnit_Framework_Assert::assertNotEquals($groups, $respondedArray);
|
|
|
|
PHPUnit_Framework_Assert::assertEquals(200, $this->response->getStatusCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Given /^user "([^"]*)" is subadmin of group "([^"]*)"$/
|
|
|
|
*/
|
|
|
|
public function userIsSubadminOfGroup($user, $group) {
|
|
|
|
$fullUrl = $this->baseUrl . "v2.php/cloud/groups/$group/subadmins";
|
|
|
|
$client = new Client();
|
|
|
|
$options = [];
|
|
|
|
if ($this->currentUser === 'admin') {
|
|
|
|
$options['auth'] = $this->adminUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->response = $client->get($fullUrl, $options);
|
|
|
|
$subadmins = array($user);
|
|
|
|
$respondedArray = $this->getArrayOfSubadminsResponded($this->response);
|
|
|
|
sort($respondedArray);
|
|
|
|
PHPUnit_Framework_Assert::assertContains($user, $respondedArray);
|
|
|
|
PHPUnit_Framework_Assert::assertEquals(200, $this->response->getStatusCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Given /^user "([^"]*)" is not a subadmin of group "([^"]*)"$/
|
|
|
|
*/
|
|
|
|
public function userIsNotSubadminOfGroup($user, $group) {
|
|
|
|
$fullUrl = $this->baseUrl . "v2.php/cloud/groups/$group/subadmins";
|
|
|
|
$client = new Client();
|
|
|
|
$options = [];
|
|
|
|
if ($this->currentUser === 'admin') {
|
|
|
|
$options['auth'] = $this->adminUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->response = $client->get($fullUrl, $options);
|
|
|
|
$subadmins = array($user);
|
|
|
|
$respondedArray = $this->getArrayOfSubadminsResponded($this->response);
|
|
|
|
sort($respondedArray);
|
|
|
|
PHPUnit_Framework_Assert::assertNotContains($user, $respondedArray);
|
|
|
|
PHPUnit_Framework_Assert::assertEquals(200, $this->response->getStatusCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-06 18:16:24 +03:00
|
|
|
/**
|
|
|
|
* @Given /^user "([^"]*)" does not exist$/
|
|
|
|
*/
|
|
|
|
public function userDoesNotExist($user) {
|
|
|
|
try {
|
|
|
|
$this->userExists($user);
|
2015-10-06 11:29:33 +03:00
|
|
|
PHPUnit_Framework_Assert::fail('The user "' . $user . '" exists');
|
2015-08-06 18:16:24 +03:00
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $ex) {
|
2015-09-30 18:50:18 +03:00
|
|
|
$this->response = $ex->getResponse();
|
2015-10-02 12:45:25 +03:00
|
|
|
PHPUnit_Framework_Assert::assertEquals(404, $ex->getResponse()->getStatusCode());
|
2015-08-06 18:16:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @When /^creating the user "([^"]*)r"$/
|
|
|
|
*/
|
|
|
|
public function creatingTheUser($user) {
|
2015-10-08 17:21:31 +03:00
|
|
|
$fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/users/$user";
|
2015-08-06 18:16:24 +03:00
|
|
|
$client = new Client();
|
|
|
|
$options = [];
|
|
|
|
if ($this->currentUser === 'admin') {
|
|
|
|
$options['auth'] = $this->adminUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->response = $client->post($fullUrl, [
|
|
|
|
'form_params' => [
|
|
|
|
'userid' => $user,
|
|
|
|
'password' => '123456'
|
|
|
|
]
|
|
|
|
]);
|
2015-09-30 18:50:18 +03:00
|
|
|
|
2015-08-06 18:16:24 +03:00
|
|
|
}
|
|
|
|
|
2015-10-02 12:45:25 +03:00
|
|
|
/**
|
2015-09-29 17:52:19 +03:00
|
|
|
* @When /^creating the group "([^"]*)r"$/
|
|
|
|
*/
|
|
|
|
public function creatingTheGroup($group) {
|
2015-10-08 17:21:31 +03:00
|
|
|
$fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/groups/addgroup";
|
2015-09-29 17:52:19 +03:00
|
|
|
$client = new Client();
|
|
|
|
$options = [];
|
|
|
|
if ($this->currentUser === 'admin') {
|
|
|
|
$options['auth'] = $this->adminUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->response = $client->post($fullUrl, [
|
|
|
|
'form_params' => [
|
|
|
|
'groupid' => $user
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Given /^group "([^"]*)" exists$/
|
|
|
|
*/
|
|
|
|
public function groupExists($group) {
|
2015-10-02 10:40:25 +03:00
|
|
|
$fullUrl = $this->baseUrl . "v2.php/cloud/groups/$group";
|
2015-09-29 17:52:19 +03:00
|
|
|
$client = new Client();
|
|
|
|
$options = [];
|
|
|
|
if ($this->currentUser === 'admin') {
|
|
|
|
$options['auth'] = $this->adminUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->response = $client->get($fullUrl, $options);
|
2015-10-02 10:40:25 +03:00
|
|
|
PHPUnit_Framework_Assert::assertEquals(200, $this->response->getStatusCode());
|
2015-09-29 17:52:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Given /^group "([^"]*)" does not exist$/
|
|
|
|
*/
|
|
|
|
public function groupDoesNotExist($group) {
|
|
|
|
try {
|
|
|
|
$this->groupExists($group);
|
2015-10-06 11:29:33 +03:00
|
|
|
PHPUnit_Framework_Assert::fail('The group "' . $group . '" exists');
|
2015-09-29 17:52:19 +03:00
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $ex) {
|
2015-09-30 18:50:18 +03:00
|
|
|
$this->response = $ex->getResponse();
|
2015-10-02 12:45:25 +03:00
|
|
|
PHPUnit_Framework_Assert::assertEquals(404, $ex->getResponse()->getStatusCode());
|
2015-09-29 17:52:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-06 18:16:24 +03:00
|
|
|
/**
|
|
|
|
* @When /^sending "([^"]*)" to "([^"]*)" with$/
|
|
|
|
* @param \Behat\Gherkin\Node\TableNode|null $formData
|
|
|
|
*/
|
|
|
|
public function sendingToWith($verb, $url, $body) {
|
|
|
|
$fullUrl = $this->baseUrl . "v{$this->apiVersion}.php" . $url;
|
|
|
|
$client = new Client();
|
|
|
|
$options = [];
|
|
|
|
if ($this->currentUser === 'admin') {
|
|
|
|
$options['auth'] = $this->adminUser;
|
2015-10-14 19:30:18 +03:00
|
|
|
} else {
|
|
|
|
$options['auth'] = $this->regularUser;
|
2015-08-06 18:16:24 +03:00
|
|
|
}
|
|
|
|
if ($body instanceof \Behat\Gherkin\Node\TableNode) {
|
|
|
|
$fd = $body->getRowsHash();
|
|
|
|
$options['body'] = $fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->response = $client->send($client->createRequest($verb, $fullUrl, $options));
|
|
|
|
} catch (\GuzzleHttp\Exception\ClientException $ex) {
|
|
|
|
$this->response = $ex->getResponse();
|
|
|
|
}
|
2015-08-06 16:47:21 +03:00
|
|
|
}
|
|
|
|
}
|