BasicStructure.php 9.46 KB
Newer Older
1 2 3 4 5
<?php

use GuzzleHttp\Client;
use GuzzleHttp\Message\ResponseInterface;

Sergio Bertolin's avatar
Sergio Bertolin committed
6
require __DIR__ . '/../../../../lib/composer/autoload.php';
7

8
trait BasicStructure {
Christoph Wurst's avatar
Christoph Wurst committed
9 10

	use Auth;
11
	use Trashbin;
12
	use Checksums;
Sergio Bertolin's avatar
Sergio Bertolin committed
13
	use Comments;
Christoph Wurst's avatar
Christoph Wurst committed
14

15 16 17 18 19 20 21 22 23
	/** @var string */
	private $currentUser = '';

	/** @var string */
	private $currentServer = '';

	/** @var string */
	private $baseUrl = '';

24 25 26
	/** @var int */
	private $apiVersion = 1;

27 28 29
	/** @var ResponseInterface */
	private $response = null;

30 31 32 33
	/** @var \GuzzleHttp\Cookie\CookieJar */
	private $cookieJar;

	/** @var string */
Thomas Müller's avatar
Thomas Müller committed
34
	private $requestToken;
35

36 37 38 39 40 41
	public function __construct($baseUrl, $admin, $regular_user_password) {

		// Initialize your context here
		$this->baseUrl = $baseUrl;
		$this->adminUser = $admin;
		$this->regularUser = $regular_user_password;
Thomas Müller's avatar
Thomas Müller committed
42 43
		$this->localBaseUrl = $this->baseUrl;
		$this->remoteBaseUrl = $this->baseUrl;
44
		$this->currentServer = 'LOCAL';
45
		$this->cookieJar = new \GuzzleHttp\Cookie\CookieJar();
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

		// in case of ci deployment we take the server url from the environment
		$testServerUrl = getenv('TEST_SERVER_URL');
		if ($testServerUrl !== false) {
			$this->baseUrl = $testServerUrl;
			$this->localBaseUrl = $testServerUrl;
		}

		// federated server url from the environment
		$testRemoteServerUrl = getenv('TEST_SERVER_FED_URL');
		if ($testRemoteServerUrl !== false) {
			$this->remoteBaseUrl = $testRemoteServerUrl;
		}
	}

61 62 63 64 65 66 67 68
	/**
	 * @Given /^using api version "([^"]*)"$/
	 * @param string $version
	 */
	public function usingApiVersion($version) {
		$this->apiVersion = $version;
	}

69 70
	/**
	 * @Given /^As an "([^"]*)"$/
Joas Schilling's avatar
Joas Schilling committed
71
	 * @param string $user
72 73 74 75 76 77
	 */
	public function asAn($user) {
		$this->currentUser = $user;
	}

	/**
78
	 * @Given /^Using server "(LOCAL|REMOTE)"$/
Joas Schilling's avatar
Joas Schilling committed
79
	 * @param string $server
80
	 * @return string Previous used server
81 82
	 */
	public function usingServer($server) {
83
		$previousServer = $this->currentServer;
84 85 86
		if ($server === 'LOCAL'){
			$this->baseUrl = $this->localBaseUrl;
			$this->currentServer = 'LOCAL';
87 88
			return $previousServer;
		} else {
89 90
			$this->baseUrl = $this->remoteBaseUrl;
			$this->currentServer = 'REMOTE';
91
			return $previousServer;
92 93 94 95 96
		}
	}

	/**
	 * @When /^sending "([^"]*)" to "([^"]*)"$/
Joas Schilling's avatar
Joas Schilling committed
97 98
	 * @param string $verb
	 * @param string $url
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
	 */
	public function sendingTo($verb, $url) {
		$this->sendingToWith($verb, $url, null);
	}

	/**
	 * Parses the xml answer to get ocs response which doesn't match with
	 * http one in v1 of the api.
	 * @param ResponseInterface $response
	 * @return string
	 */
	public function getOCSResponse($response) {
		return $response->xml()->meta[0]->statuscode;
	}

	/**
	 * This function is needed to use a vertical fashion in the gherkin tables.
Joas Schilling's avatar
Joas Schilling committed
116 117
	 * @param array $arrayOfArrays
	 * @return array
118 119 120 121 122 123 124 125
	 */
	public function simplifyArray($arrayOfArrays){
		$a = array_map(function($subArray) { return $subArray[0]; }, $arrayOfArrays);
		return $a;
	}

	/**
	 * @When /^sending "([^"]*)" to "([^"]*)" with$/
Joas Schilling's avatar
Joas Schilling committed
126 127 128
	 * @param string $verb
	 * @param string $url
	 * @param \Behat\Gherkin\Node\TableNode $body
129 130 131 132 133
	 */
	public function sendingToWith($verb, $url, $body) {
		$fullUrl = $this->baseUrl . "v{$this->apiVersion}.php" . $url;
		$client = new Client();
		$options = [];
134 135 136 137 138 139
		if ($this->currentUser !== 'UNAUTHORIZED_USER') {
			if ($this->currentUser === 'admin') {
				$options['auth'] = $this->adminUser;
			} else {
				$options['auth'] = [$this->currentUser, $this->regularUser];
			}
140 141 142 143 144 145 146 147 148 149 150 151 152
		}
		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();
		}
	}

153 154 155 156 157 158 159 160 161
	/**
	 * @When /^sending "([^"]*)" with exact url to "([^"]*)"$/
	 * @param string $verb
	 * @param string $url
	 */
	public function sendingToDirectUrl($verb, $url) {
		$this->sendingToWithDirectUrl($verb, $url, null);
	}

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	public function sendingToWithDirectUrl($verb, $url, $body) {
		$fullUrl = substr($this->baseUrl, 0, -5) . $url;
		$client = new Client();
		$options = [];
		if ($this->currentUser === 'admin') {
			$options['auth'] = $this->adminUser;
		} else {
			$options['auth'] = [$this->currentUser, $this->regularUser];
		}
		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();
		}
	}

183 184 185 186 187 188 189 190
	public function isExpectedUrl($possibleUrl, $finalPart){
		$baseUrlChopped = substr($this->baseUrl, 0, -4);
		$endCharacter = strlen($baseUrlChopped) + strlen($finalPart);
		return (substr($possibleUrl,0,$endCharacter) == "$baseUrlChopped" . "$finalPart");
	}

	/**
	 * @Then /^the OCS status code should be "([^"]*)"$/
Joas Schilling's avatar
Joas Schilling committed
191
	 * @param int $statusCode
192 193 194 195 196 197 198
	 */
	public function theOCSStatusCodeShouldBe($statusCode) {
		PHPUnit_Framework_Assert::assertEquals($statusCode, $this->getOCSResponse($this->response));
	}

	/**
	 * @Then /^the HTTP status code should be "([^"]*)"$/
Joas Schilling's avatar
Joas Schilling committed
199
	 * @param int $statusCode
200 201 202 203 204
	 */
	public function theHTTPStatusCodeShouldBe($statusCode) {
		PHPUnit_Framework_Assert::assertEquals($statusCode, $this->response->getStatusCode());
	}

205 206 207 208
	/**
	 * @param ResponseInterface $response
	 */
	private function extracRequestTokenFromResponse(ResponseInterface $response) {
Thomas Müller's avatar
Thomas Müller committed
209
		$this->requestToken = substr(preg_replace('/(.*)data-requesttoken="(.*)">(.*)/sm', '\2', $response->getBody()->getContents()), 0, 89);
210 211 212 213
	}

	/**
	 * @Given Logging in using web as :user
Joas Schilling's avatar
Joas Schilling committed
214
	 * @param string $user
215 216
	 */
	public function loggingInUsingWebAs($user) {
Christoph Wurst's avatar
Christoph Wurst committed
217
		$loginUrl = substr($this->baseUrl, 0, -5) . '/login';
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
		// Request a new session and extract CSRF token
		$client = new Client();
		$response = $client->get(
			$loginUrl,
			[
				'cookies' => $this->cookieJar,
			]
		);
		$this->extracRequestTokenFromResponse($response);

		// Login and extract new token
		$password = ($user === 'admin') ? 'admin' : '123456';
		$client = new Client();
		$response = $client->post(
			$loginUrl,
			[
				'body' => [
					'user' => $user,
					'password' => $password,
Thomas Müller's avatar
Thomas Müller committed
237
					'requesttoken' => $this->requestToken,
238 239 240 241 242 243 244 245 246
				],
				'cookies' => $this->cookieJar,
			]
		);
		$this->extracRequestTokenFromResponse($response);
	}

	/**
	 * @When Sending a :method to :url with requesttoken
Joas Schilling's avatar
Joas Schilling committed
247 248
	 * @param string $method
	 * @param string $url
249 250 251 252 253 254 255 256 257 258 259 260
	 */
	public function sendingAToWithRequesttoken($method, $url) {
		$baseUrl = substr($this->baseUrl, 0, -5);

		$client = new Client();
		$request = $client->createRequest(
			$method,
			$baseUrl . $url,
			[
				'cookies' => $this->cookieJar,
			]
		);
Thomas Müller's avatar
Thomas Müller committed
261
		$request->addHeader('requesttoken', $this->requestToken);
262 263 264 265 266 267 268 269 270
		try {
			$this->response = $client->send($request);
		} catch (\GuzzleHttp\Exception\ClientException $e) {
			$this->response = $e->getResponse();
		}
	}

	/**
	 * @When Sending a :method to :url without requesttoken
Joas Schilling's avatar
Joas Schilling committed
271 272
	 * @param string $method
	 * @param string $url
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
	 */
	public function sendingAToWithoutRequesttoken($method, $url) {
		$baseUrl = substr($this->baseUrl, 0, -5);

		$client = new Client();
		$request = $client->createRequest(
			$method,
			$baseUrl . $url,
			[
				'cookies' => $this->cookieJar,
			]
		);
		try {
			$this->response = $client->send($request);
		} catch (\GuzzleHttp\Exception\ClientException $e) {
			$this->response = $e->getResponse();
		}
	}

292 293 294 295 296 297
	public static function removeFile($path, $filename){
		if (file_exists("$path" . "$filename")) {
			unlink("$path" . "$filename");
		}
	}

298 299 300 301 302 303 304
	/**
	 * @Given User :user modifies text of :filename with text :text
	 * @param string $user
	 * @param string $filename
	 * @param string $text
	 */
	public function modifyTextOfFile($user, $filename, $text) {
Sergio Bertolin's avatar
Sergio Bertolin committed
305 306
		self::removeFile($this->getUserHome($user) . "/files", "$filename");
		file_put_contents($this->getUserHome($user) . "/files" . "$filename", "$text");
307 308
	}

309
	public function createFileSpecificSize($name, $size){
310
		$file = fopen("work/" . "$name", 'w');
311 312 313 314 315
		fseek($file, $size - 1 ,SEEK_CUR);
		fwrite($file,'a'); // write a dummy char at SIZE position
		fclose($file);
	}

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
	public function createFileWithText($name, $text){
		$file = fopen("work/" . "$name", 'w');
		fwrite($file, $text);
		fclose($file);
	}

	/**
	 * @Given file :filename of size :size is created in local storage
	 * @param string $filename
	 * @param string $size
	 */
	public function fileIsCreatedInLocalStorageWithSize($filename, $size) {
		$this->createFileSpecificSize("local_storage/$filename", $size);
	}

	/**
	 * @Given file :filename with text :text is created in local storage
	 * @param string $filename
	 * @param string $text
	 */
	public function fileIsCreatedInLocalStorageWithText($filename, $text) {
		$this->createFileWithText("local_storage/$filename", $text);
	}

340 341 342 343
	/**
	 * @BeforeScenario @local_storage
	 */
	public static function removeFilesFromLocalStorageBefore(){
344
		$dir = "./work/local_storage/";
345 346 347 348 349 350 351
		$di = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS);
		$ri = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST);
		foreach ( $ri as $file ) {
			$file->isDir() ?  rmdir($file) : unlink($file);
		}
	}

352 353 354
	/**
	 * @AfterScenario @local_storage
	 */
355
	public static function removeFilesFromLocalStorageAfter(){
356
		$dir = "./work/local_storage/";
357 358 359 360 361 362
		$di = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS);
		$ri = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST);
		foreach ( $ri as $file ) {
			$file->isDir() ?  rmdir($file) : unlink($file);
		}
	}
363 364
}