FeatureContext.php 30 KB
Newer Older
Thomas Müller's avatar
Thomas Müller committed
1
2
<?php

3
4
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
Thomas Müller's avatar
Thomas Müller committed
5
6
7
use GuzzleHttp\Client;
use GuzzleHttp\Message\ResponseInterface;

Thomas Müller's avatar
Thomas Müller committed
8
require __DIR__ . '/../../vendor/autoload.php';
Thomas Müller's avatar
Thomas Müller committed
9
10
11
12

/**
 * Features context.
 */
13
class FeatureContext implements Context, SnippetAcceptingContext {
Thomas Müller's avatar
Thomas Müller committed
14

Thomas Müller's avatar
Thomas Müller committed
15
16
	/** @var string */
	private $baseUrl = '';
Thomas Müller's avatar
Thomas Müller committed
17

Thomas Müller's avatar
Thomas Müller committed
18
19
	/** @var ResponseInterface */
	private $response = null;
Thomas Müller's avatar
Thomas Müller committed
20
21
22
23
24
25
26

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

	/** @var int */
	private $apiVersion = 1;

27
28
29
	/** @var int */
	private $sharingApiVersion = 1;

Sergio Bertolin's avatar
Sergio Bertolin committed
30
31
32
	/** @var SimpleXMLElement */
	private $lastShareData = null;

33
34
35
	/** @var array */
	private $createdUsers = [];

36
37
38
	/** @var array */
	private $createdGroups = [];

39
	public function __construct($baseUrl, $admin, $regular_user_password) {
Thomas Müller's avatar
Thomas Müller committed
40

Thomas Müller's avatar
Thomas Müller committed
41
		// Initialize your context here
42
43
44
		$this->baseUrl = $baseUrl;
		$this->adminUser = $admin;
		$this->regularUser = $regular_user_password;
45
46
47
48
49
50

		// in case of ci deployment we take the server url from the environment
		$testServerUrl = getenv('TEST_SERVER_URL');
		if ($testServerUrl !== false) {
			$this->baseUrl = $testServerUrl;
		}
Thomas Müller's avatar
Thomas Müller committed
51
	}
Thomas Müller's avatar
Thomas Müller committed
52

Thomas Müller's avatar
Thomas Müller committed
53
	/**
54
55
	 * @When /^sending "([^"]*)" to "([^"]*)"$/
	 */
Thomas Müller's avatar
Thomas Müller committed
56
57
58
59
	public function sendingTo($verb, $url) {
		$this->sendingToWith($verb, $url, null);
	}

60
	/**
61
62
	 * Parses the xml answer to get ocs response which doesn't match with
	 * http one in v1 of the api.
Joas Schilling's avatar
Joas Schilling committed
63
64
	 * @param ResponseInterface $response
	 * @return string
65
66
	 */
	public function getOCSResponse($response) {
67
68
		return $response->xml()->meta[0]->statuscode;
	}
69

Sergio Bertolin's avatar
Sergio Bertolin committed
70
	/**
71
	 * Parses the xml answer to get the array of users returned.
Joas Schilling's avatar
Joas Schilling committed
72
73
	 * @param ResponseInterface $resp
	 * @return array
74
	 */
75
76
	public function getArrayOfUsersResponded($resp) {
		$listCheckedElements = $resp->xml()->data[0]->users[0]->element;
77
		$extractedElementsArray = json_decode(json_encode($listCheckedElements), 1);
78
79
80
81
		return $extractedElementsArray;
	}

	/**
82
	 * Parses the xml answer to get the array of groups returned.
Joas Schilling's avatar
Joas Schilling committed
83
84
	 * @param ResponseInterface $resp
	 * @return array
85
	 */
86
87
	public function getArrayOfGroupsResponded($resp) {
		$listCheckedElements = $resp->xml()->data[0]->groups[0]->element;
88
		$extractedElementsArray = json_decode(json_encode($listCheckedElements), 1);
89
90
		return $extractedElementsArray;
	}
Sergio Bertolin's avatar
Sergio Bertolin committed
91

92
93
	/**
	 * Parses the xml answer to get the array of subadmins returned.
Joas Schilling's avatar
Joas Schilling committed
94
95
	 * @param ResponseInterface $resp
	 * @return array
96
97
98
99
100
101
102
	 */
	public function getArrayOfSubadminsResponded($resp) {
		$listCheckedElements = $resp->xml()->data[0]->element;
		$extractedElementsArray = json_decode(json_encode($listCheckedElements), 1);
		return $extractedElementsArray;
	}

Sergio Bertolin's avatar
Sergio Bertolin committed
103
104
	/**
	 * Parses the xml answer to get the array of apps returned.
Joas Schilling's avatar
Joas Schilling committed
105
106
	 * @param ResponseInterface $resp
	 * @return array
Sergio Bertolin's avatar
Sergio Bertolin committed
107
108
109
110
111
112
113
	 */
	public function getArrayOfAppsResponded($resp) {
		$listCheckedElements = $resp->xml()->data[0]->apps[0]->element;
		$extractedElementsArray = json_decode(json_encode($listCheckedElements), 1);
		return $extractedElementsArray;
	}

114
115
116
117
118
119
120
	/**
	 * 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;
	}
Joas Schilling's avatar
Joas Schilling committed
121

Sergio Bertolin's avatar
Sergio Bertolin committed
122
123
	/**
	 * @Then /^users returned are$/
Joas Schilling's avatar
Joas Schilling committed
124
	 * @param \Behat\Gherkin\Node\TableNode|null $usersList
Sergio Bertolin's avatar
Sergio Bertolin committed
125
126
127
	 */
	public function theUsersShouldBe($usersList) {
		if ($usersList instanceof \Behat\Gherkin\Node\TableNode) {
128
129
			$users = $usersList->getRows();
			$usersSimplified = $this->simplifyArray($users);
130
			$respondedArray = $this->getArrayOfUsersResponded($this->response);
131
			PHPUnit_Framework_Assert::assertEquals($usersSimplified, $respondedArray, "", 0.0, 10, true);
Sergio Bertolin's avatar
Sergio Bertolin committed
132
133
134
135
		}

	}

Sergio Bertolin's avatar
Sergio Bertolin committed
136
137
	/**
	 * @Then /^groups returned are$/
Joas Schilling's avatar
Joas Schilling committed
138
	 * @param \Behat\Gherkin\Node\TableNode|null $groupsList
Sergio Bertolin's avatar
Sergio Bertolin committed
139
140
141
	 */
	public function theGroupsShouldBe($groupsList) {
		if ($groupsList instanceof \Behat\Gherkin\Node\TableNode) {
142
143
			$groups = $groupsList->getRows();
			$groupsSimplified = $this->simplifyArray($groups);
144
			$respondedArray = $this->getArrayOfGroupsResponded($this->response);
145
			PHPUnit_Framework_Assert::assertEquals($groupsSimplified, $respondedArray, "", 0.0, 10, true);
Sergio Bertolin's avatar
Sergio Bertolin committed
146
147
148
		}

	}
149

150
151
	/**
	 * @Then /^subadmin groups returned are$/
Joas Schilling's avatar
Joas Schilling committed
152
	 * @param \Behat\Gherkin\Node\TableNode|null $groupsList
153
154
155
156
157
158
	 */
	public function theSubadminGroupsShouldBe($groupsList) {
		if ($groupsList instanceof \Behat\Gherkin\Node\TableNode) {
			$groups = $groupsList->getRows();
			$groupsSimplified = $this->simplifyArray($groups);
			$respondedArray = $this->getArrayOfSubadminsResponded($this->response);
159
			PHPUnit_Framework_Assert::assertEquals($groupsSimplified, $respondedArray, "", 0.0, 10, true);
160
161
162
163
164
165
		}

	}

	/**
	 * @Then /^subadmin users returned are$/
Joas Schilling's avatar
Joas Schilling committed
166
	 * @param \Behat\Gherkin\Node\TableNode|null $groupsList
167
168
169
170
171
	 */
	public function theSubadminUsersShouldBe($groupsList) {
		$this->theSubadminGroupsShouldBe($groupsList);
	}

Sergio Bertolin's avatar
Sergio Bertolin committed
172
173
	/**
	 * @Then /^apps returned are$/
Joas Schilling's avatar
Joas Schilling committed
174
	 * @param \Behat\Gherkin\Node\TableNode|null $appList
Sergio Bertolin's avatar
Sergio Bertolin committed
175
176
177
178
179
180
181
182
183
184
185
	 */
	public function theAppsShouldBe($appList) {
		if ($appList instanceof \Behat\Gherkin\Node\TableNode) {
			$apps = $appList->getRows();
			$appsSimplified = $this->simplifyArray($apps);
			$respondedArray = $this->getArrayOfAppsResponded($this->response);
			PHPUnit_Framework_Assert::assertEquals($appsSimplified, $respondedArray, "", 0.0, 10, true);
		}

	}

186
	/**
187
	 * @Then /^the OCS status code should be "([^"]*)"$/
Thomas Müller's avatar
Thomas Müller committed
188
	 */
189
	public function theOCSStatusCodeShouldBe($statusCode) {
190
		PHPUnit_Framework_Assert::assertEquals($statusCode, $this->getOCSResponse($this->response));
Thomas Müller's avatar
Thomas Müller committed
191
192
	}

193
	/**
194
195
196
197
198
199
	 * @Then /^the HTTP status code should be "([^"]*)"$/
	 */
	public function theHTTPStatusCodeShouldBe($statusCode) {
		PHPUnit_Framework_Assert::assertEquals($statusCode, $this->response->getStatusCode());
	}

Thomas Müller's avatar
Thomas Müller committed
200
201
202
203
	/**
	 * @Given /^As an "([^"]*)"$/
	 */
	public function asAn($user) {
Thomas Müller's avatar
Thomas Müller committed
204
		$this->currentUser = $user;
Thomas Müller's avatar
Thomas Müller committed
205
	}
Thomas Müller's avatar
Thomas Müller committed
206
207
208
209
210
211
212
213
214
215
216

	/**
	 * @Given /^using api version "([^"]*)"$/
	 */
	public function usingApiVersion($version) {
		$this->apiVersion = $version;
	}

	/**
	 * @Given /^user "([^"]*)" exists$/
	 */
Sergio Bertolin's avatar
Sergio Bertolin committed
217
218
	public function assureUserExists($user) {
		try {
Joas Schilling's avatar
Joas Schilling committed
219
			$this->userExists($user);
Sergio Bertolin's avatar
Sergio Bertolin committed
220
221
222
223
224
225
226
227
228
229
230
231
		} catch (\GuzzleHttp\Exception\ClientException $ex) {
			$previous_user = $this->currentUser;
			$this->currentUser = "admin";
			$this->creatingTheUser($user);
			$this->currentUser = $previous_user;
		}
		$this->userExists($user);
		PHPUnit_Framework_Assert::assertEquals(200, $this->response->getStatusCode());

	}

	public function userExists($user){
232
		$fullUrl = $this->baseUrl . "v2.php/cloud/users/$user";
Thomas Müller's avatar
Thomas Müller committed
233
234
		$client = new Client();
		$options = [];
Sergio Bertolin's avatar
Sergio Bertolin committed
235
		$options['auth'] = $this->adminUser;
Thomas Müller's avatar
Thomas Müller committed
236
237
238
239

		$this->response = $client->get($fullUrl, $options);
	}

240
	/**
241
	 * @Then /^check that user "([^"]*)" belongs to group "([^"]*)"$/
242
	 */
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
	public function checkThatUserBelongsToGroup($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);
		$respondedArray = $this->getArrayOfGroupsResponded($this->response);
		sort($respondedArray);
		PHPUnit_Framework_Assert::assertContains($group, $respondedArray);
		PHPUnit_Framework_Assert::assertEquals(200, $this->response->getStatusCode());
	}

258
259
260
261
262
263
264
265
266
267
268
	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);
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288

		if (array_key_exists($group, $respondedArray)) {
			return True;
		} else{
			return False;
		}
	}

	/**
	 * @Given /^user "([^"]*)" belongs to group "([^"]*)"$/
	 */
	public function assureUserBelongsToGroup($user, $group){
		if (!$this->userBelongsToGroup($user, $group)){			
			$previous_user = $this->currentUser;
			$this->currentUser = "admin";
			$this->addingUserToGroup($user, $group);
			$this->currentUser = $previous_user;
		}
		$this->checkThatUserBelongsToGroup($user, $group);

289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
	}

	/**
	 * @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);
305
		PHPUnit_Framework_Assert::assertNotEquals($groups, $respondedArray, "", 0.0, 10, true);
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
		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);
		$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);
		$respondedArray = $this->getArrayOfSubadminsResponded($this->response);
		sort($respondedArray);
		PHPUnit_Framework_Assert::assertNotContains($user, $respondedArray);
		PHPUnit_Framework_Assert::assertEquals(200, $this->response->getStatusCode());
	}

Thomas Müller's avatar
Thomas Müller committed
346
347
348
349
350
351
	/**
	 * @Given /^user "([^"]*)" does not exist$/
	 */
	public function userDoesNotExist($user) {
		try {
			$this->userExists($user);
Sergio Bertolin's avatar
Sergio Bertolin committed
352
353
354
355
356
357
358
359
360
361
362
		} catch (\GuzzleHttp\Exception\ClientException $ex) {
			$this->response = $ex->getResponse();
			PHPUnit_Framework_Assert::assertEquals(404, $ex->getResponse()->getStatusCode());
			return;
		}
		$previous_user = $this->currentUser;
		$this->currentUser = "admin";
		$this->deletingTheUser($user);
		$this->currentUser = $previous_user;
		try {
			$this->userExists($user);
Thomas Müller's avatar
Thomas Müller committed
363
		} catch (\GuzzleHttp\Exception\ClientException $ex) {
364
			$this->response = $ex->getResponse();
365
			PHPUnit_Framework_Assert::assertEquals(404, $ex->getResponse()->getStatusCode());
Thomas Müller's avatar
Thomas Müller committed
366
367
368
		}
	}

Sergio Bertolin's avatar
Sergio Bertolin committed
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
	/**
	 * @Given /^app "([^"]*)" is disabled$/
	 */
	public function appIsDisabled($app) {
		$fullUrl = $this->baseUrl . "v2.php/cloud/apps?filter=disabled";
		$client = new Client();
		$options = [];
		if ($this->currentUser === 'admin') {
			$options['auth'] = $this->adminUser;
		}

		$this->response = $client->get($fullUrl, $options);
		$respondedArray = $this->getArrayOfAppsResponded($this->response);
		PHPUnit_Framework_Assert::assertContains($app, $respondedArray);
		PHPUnit_Framework_Assert::assertEquals(200, $this->response->getStatusCode());
	}

	/**
	 * @Given /^app "([^"]*)" is enabled$/
	 */
	public function appIsEnabled($app) {
		$fullUrl = $this->baseUrl . "v2.php/cloud/apps?filter=enabled";
		$client = new Client();
		$options = [];
		if ($this->currentUser === 'admin') {
			$options['auth'] = $this->adminUser;
		}

		$this->response = $client->get($fullUrl, $options);
		$respondedArray = $this->getArrayOfAppsResponded($this->response);
		PHPUnit_Framework_Assert::assertContains($app, $respondedArray);
		PHPUnit_Framework_Assert::assertEquals(200, $this->response->getStatusCode());
	}

Sergio Bertolin's avatar
Sergio Bertolin committed
403
	public function createUser($user) {
404
405
		$previous_user = $this->currentUser;
		$this->currentUser = "admin";
Sergio Bertolin's avatar
Sergio Bertolin committed
406
407
		$this->creatingTheUser($user);
		$this->userExists($user);
408
		$this->currentUser = $previous_user;
Sergio Bertolin's avatar
Sergio Bertolin committed
409
410
411
	}

	public function deleteUser($user) {
412
413
		$previous_user = $this->currentUser;
		$this->currentUser = "admin";
Sergio Bertolin's avatar
Sergio Bertolin committed
414
415
		$this->deletingTheUser($user);
		$this->userDoesNotExist($user);
416
		$this->currentUser = $previous_user;
Sergio Bertolin's avatar
Sergio Bertolin committed
417
418
419
	}

	public function createGroup($group) {
420
421
		$previous_user = $this->currentUser;
		$this->currentUser = "admin";
Sergio Bertolin's avatar
Sergio Bertolin committed
422
423
		$this->creatingTheGroup($group);
		$this->groupExists($group);
424
		$this->currentUser = $previous_user;
Sergio Bertolin's avatar
Sergio Bertolin committed
425
426
427
	}

	public function deleteGroup($group) {
428
429
		$previous_user = $this->currentUser;
		$this->currentUser = "admin";
Sergio Bertolin's avatar
Sergio Bertolin committed
430
431
		$this->deletingTheGroup($group);
		$this->groupDoesNotExist($group);
432
		$this->currentUser = $previous_user;
Sergio Bertolin's avatar
Sergio Bertolin committed
433
434
	}

Thomas Müller's avatar
Thomas Müller committed
435
	public function creatingTheUser($user) {
Sergio Bertolin's avatar
Sergio Bertolin committed
436
		$fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/users";
Thomas Müller's avatar
Thomas Müller committed
437
438
439
440
441
442
		$client = new Client();
		$options = [];
		if ($this->currentUser === 'admin') {
			$options['auth'] = $this->adminUser;
		}

Sergio Bertolin's avatar
Sergio Bertolin committed
443
444
445
446
447
448
		$options['body'] = [
							'userid' => $user,
							'password' => '123456'
							];

		$this->response = $client->send($client->createRequest("POST", $fullUrl, $options));
449
		$this->createdUsers[$user] = $user;
Thomas Müller's avatar
Thomas Müller committed
450
451
	}

452
	/**
Sergio Bertolin's avatar
Sergio Bertolin committed
453
	 * @When /^creating the group "([^"]*)"$/
Sergio Bertolin's avatar
Sergio Bertolin committed
454
455
	 */
	public function creatingTheGroup($group) {
Sergio Bertolin's avatar
Sergio Bertolin committed
456
		$fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/groups";
Sergio Bertolin's avatar
Sergio Bertolin committed
457
458
459
460
461
462
		$client = new Client();
		$options = [];
		if ($this->currentUser === 'admin') {
			$options['auth'] = $this->adminUser;
		}

Sergio Bertolin's avatar
Sergio Bertolin committed
463
464
465
466
467
		$options['body'] = [
							'groupid' => $group,
							];

		$this->response = $client->send($client->createRequest("POST", $fullUrl, $options));
468
		$this->createdGroups[$group] = $group;
Sergio Bertolin's avatar
Sergio Bertolin committed
469
470
471
	}

	/**
Sergio Bertolin's avatar
Sergio Bertolin committed
472
	 * @When /^Deleting the user "([^"]*)"$/
Sergio Bertolin's avatar
Sergio Bertolin committed
473
	 */
Sergio Bertolin's avatar
Sergio Bertolin committed
474
475
	public function deletingTheUser($user) {
		$fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/users/$user";
Sergio Bertolin's avatar
Sergio Bertolin committed
476
477
478
479
480
481
		$client = new Client();
		$options = [];
		if ($this->currentUser === 'admin') {
			$options['auth'] = $this->adminUser;
		}

Sergio Bertolin's avatar
Sergio Bertolin committed
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
		$this->response = $client->send($client->createRequest("DELETE", $fullUrl, $options));
	}

	/**
	 * @When /^Deleting the group "([^"]*)"$/
	 */
	public function deletingTheGroup($group) {
		$fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/groups/$group";
		$client = new Client();
		$options = [];
		if ($this->currentUser === 'admin') {
			$options['auth'] = $this->adminUser;
		}

		$this->response = $client->send($client->createRequest("DELETE", $fullUrl, $options));
	}

	/**
	 * @Given /^Add user "([^"]*)" to the group "([^"]*)"$/
	 */
	public function addUserToGroup($user, $group) {
		$this->userExists($user);
		$this->groupExists($group);
		$this->addingUserToGroup($user, $group);

	}

	/**
	 * @When /^User "([^"]*)" is added to the group "([^"]*)"$/
	 */
	public function addingUserToGroup($user, $group) {
		$fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/cloud/users/$user/groups";
		$client = new Client();
		$options = [];
		if ($this->currentUser === 'admin') {
			$options['auth'] = $this->adminUser;
		}

		$options['body'] = [
							'groupid' => $group,
							];

		$this->response = $client->send($client->createRequest("POST", $fullUrl, $options));
	}


	public function groupExists($group) {
		$fullUrl = $this->baseUrl . "v2.php/cloud/groups/$group";
		$client = new Client();
		$options = [];
		$options['auth'] = $this->adminUser;

Sergio Bertolin's avatar
Sergio Bertolin committed
534
		$this->response = $client->get($fullUrl, $options);
Sergio Bertolin's avatar
Sergio Bertolin committed
535
536
537
538
539
540
541
	}

	/**
	 * @Given /^group "([^"]*)" exists$/
	 */
	public function assureGroupExists($group) {
		try {
Joas Schilling's avatar
Joas Schilling committed
542
			$this->groupExists($group);
Sergio Bertolin's avatar
Sergio Bertolin committed
543
544
545
546
547
548
549
		} catch (\GuzzleHttp\Exception\ClientException $ex) {
			$previous_user = $this->currentUser;
			$this->currentUser = "admin";
			$this->creatingTheGroup($group);
			$this->currentUser = $previous_user;
		}
		$this->groupExists($group);
550
		PHPUnit_Framework_Assert::assertEquals(200, $this->response->getStatusCode());
Sergio Bertolin's avatar
Sergio Bertolin committed
551
552
553
554
555
556
557
558
	}

	/**
	 * @Given /^group "([^"]*)" does not exist$/
	 */
	public function groupDoesNotExist($group) {
		try {
			$this->groupExists($group);
Sergio Bertolin's avatar
Sergio Bertolin committed
559
560
561
562
563
564
565
566
567
568
569
		} catch (\GuzzleHttp\Exception\ClientException $ex) {
			$this->response = $ex->getResponse();
			PHPUnit_Framework_Assert::assertEquals(404, $ex->getResponse()->getStatusCode());
			return;
		}
		$previous_user = $this->currentUser;
		$this->currentUser = "admin";
		$this->deletingTheGroup($group);
		$this->currentUser = $previous_user;
		try {
			$this->groupExists($group);
Sergio Bertolin's avatar
Sergio Bertolin committed
570
		} catch (\GuzzleHttp\Exception\ClientException $ex) {
571
			$this->response = $ex->getResponse();
572
			PHPUnit_Framework_Assert::assertEquals(404, $ex->getResponse()->getStatusCode());
Sergio Bertolin's avatar
Sergio Bertolin committed
573
574
575
		}
	}

Thomas Müller's avatar
Thomas Müller committed
576
577
578
579
580
581
582
583
584
585
	/**
	 * @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;
586
		} else {
587
			$options['auth'] = [$this->currentUser, $this->regularUser];
Thomas Müller's avatar
Thomas Müller committed
588
589
590
591
592
593
594
595
596
597
598
		}
		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();
		}
Thomas Müller's avatar
Thomas Müller committed
599
	}
Sergio Bertolin's avatar
Sergio Bertolin committed
600
601
602
603
604
605

	/**
	 * @When /^creating a public share with$/
	 * @param \Behat\Gherkin\Node\TableNode|null $formData
	 */
	public function createPublicShare($body) {
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
		$fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/files_sharing/api/v1/shares";
		$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();
			if (array_key_exists('expireDate', $fd)){
				$dateModification = $fd['expireDate'];
				$fd['expireDate'] = date('Y-m-d', strtotime($dateModification));
			}
			$options['body'] = $fd;
		}

		try {
			$this->response = $client->send($client->createRequest("POST", $fullUrl, $options));
		} catch (\GuzzleHttp\Exception\ClientException $ex) {
			$this->response = $ex->getResponse();
		}

Sergio Bertolin's avatar
Sergio Bertolin committed
630
631
632
633
634
635
636
637
638
		$this->lastShareData = $this->response->xml();
	}

	/**
	 * @Then /^Public shared file "([^"]*)" can be downloaded$/
	 */
	public function checkPublicSharedFile($filename) {
		$client = new Client();
		$options = [];
639
640
641
642
643
644
		if (count($this->lastShareData->data->element) > 0){
			$url = $this->lastShareData->data[0]->url;
		}
		else{
			$url = $this->lastShareData->data->url;
		}
Sergio Bertolin's avatar
Sergio Bertolin committed
645
646
647
648
649
650
651
		$fullUrl = $url . "/download";
		$options['save_to'] = "./$filename";
		$this->response = $client->get($fullUrl, $options);
		$finfo = new finfo;
		$fileinfo = $finfo->file("./$filename", FILEINFO_MIME_TYPE);
		PHPUnit_Framework_Assert::assertEquals($fileinfo, "text/plain");
		if (file_exists("./$filename")) {
Joas Schilling's avatar
Joas Schilling committed
652
653
			unlink("./$filename");
		}
Sergio Bertolin's avatar
Sergio Bertolin committed
654
655
656
657
658
659
660
661
	}

	/**
	 * @Then /^Public shared file "([^"]*)" with password "([^"]*)" can be downloaded$/
	 */
	public function checkPublicSharedFileWithPassword($filename, $password) {
		$client = new Client();
		$options = [];
662
663
664
665
666
667
		if (count($this->lastShareData->data->element) > 0){
			$token = $this->lastShareData->data[0]->token;
		}
		else{
			$token = $this->lastShareData->data->token;
		}
Joas Schilling's avatar
Joas Schilling committed
668

Sergio Bertolin's avatar
Sergio Bertolin committed
669
670
671
672
673
674
675
676
		$fullUrl = substr($this->baseUrl, 0, -4) . "public.php/webdav";
		$options['auth'] = [$token, $password];
		$options['save_to'] = "./$filename";
		$this->response = $client->get($fullUrl, $options);
		$finfo = new finfo;
		$fileinfo = $finfo->file("./$filename", FILEINFO_MIME_TYPE);
		PHPUnit_Framework_Assert::assertEquals($fileinfo, "text/plain");
		if (file_exists("./$filename")) {
Joas Schilling's avatar
Joas Schilling committed
677
678
			unlink("./$filename");
		}
Sergio Bertolin's avatar
Sergio Bertolin committed
679
680
681
682
683
684
685
	}

	/**
	 * @When /^Adding expiration date to last share$/
	 */
	public function addingExpirationDate() {
		$share_id = $this->lastShareData->data[0]->id;
686
		$fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/files_sharing/api/v{$this->sharingApiVersion}/shares/$share_id";
Sergio Bertolin's avatar
Sergio Bertolin committed
687
688
689
690
691
692
693
694
695
696
697
698
		$client = new Client();
		$options = [];
		if ($this->currentUser === 'admin') {
			$options['auth'] = $this->adminUser;
		} else {
			$options['auth'] = [$this->currentUser, $this->regularUser];
		}
		$date = date('Y-m-d', strtotime("+3 days"));
		$options['body'] = ['expireDate' => $date];
		$this->response = $client->send($client->createRequest("PUT", $fullUrl, $options));
		PHPUnit_Framework_Assert::assertEquals(200, $this->response->getStatusCode());
	}
699

700
701
	/**
	 * @When /^Updating last share with$/
Joas Schilling's avatar
Joas Schilling committed
702
	 * @param \Behat\Gherkin\Node\TableNode|null $body
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
	 */
	public function updatingLastShare($body) {
		$share_id = $this->lastShareData->data[0]->id;
		$fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/files_sharing/api/v{$this->sharingApiVersion}/shares/$share_id";
		$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();
			if (array_key_exists('expireDate', $fd)){
				$dateModification = $fd['expireDate'];
				$fd['expireDate'] = date('Y-m-d', strtotime($dateModification));
			}
			$options['body'] = $fd;
		}

		try {
			$this->response = $client->send($client->createRequest("PUT", $fullUrl, $options));
		} catch (\GuzzleHttp\Exception\ClientException $ex) {
			$this->response = $ex->getResponse();
		}

		PHPUnit_Framework_Assert::assertEquals(200, $this->response->getStatusCode());
	}


734
	public function createShare($user,
Joas Schilling's avatar
Joas Schilling committed
735
736
737
738
739
								$path = null,
								$shareType = null,
								$shareWith = null,
								$publicUpload = null,
								$password = null,
740
								$permissions = null){
741
		$fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/files_sharing/api/v{$this->sharingApiVersion}/shares";
742
743
744
745
746
747
748
749
750
751
		$client = new Client();
		$options = [];

		if ($user === 'admin') {
			$options['auth'] = $this->adminUser;
		} else {
			$options['auth'] = [$user, $this->regularUser];
		}
		$fd = [];
		if (!is_null($path)){
Joas Schilling's avatar
Joas Schilling committed
752
			$fd['path'] = $path;
753
754
		}
		if (!is_null($shareType)){
Joas Schilling's avatar
Joas Schilling committed
755
			$fd['shareType'] = $shareType;
756
757
		}
		if (!is_null($shareWith)){
Joas Schilling's avatar
Joas Schilling committed
758
			$fd['shareWith'] = $shareWith;
759
760
		}
		if (!is_null($publicUpload)){
Joas Schilling's avatar
Joas Schilling committed
761
			$fd['publicUpload'] = $publicUpload;
762
763
		}
		if (!is_null($password)){
Joas Schilling's avatar
Joas Schilling committed
764
			$fd['password'] = $password;
765
766
		}
		if (!is_null($permissions)){
Joas Schilling's avatar
Joas Schilling committed
767
			$fd['permissions'] = $permissions;
768
769
770
		}

		$options['body'] = $fd;
Joas Schilling's avatar
Joas Schilling committed
771

772
773
		try {
			$this->response = $client->send($client->createRequest("POST", $fullUrl, $options));
774
			$this->lastShareData = $this->response->xml();
775
776
777
778
779
780
		} catch (\GuzzleHttp\Exception\ClientException $ex) {
			$this->response = $ex->getResponse();
		}

	}

781
782
783
	public function isExpectedUrl($possibleUrl, $finalPart){
		$baseUrlChopped = substr($this->baseUrl, 0, -4);
		$endCharacter = strlen($baseUrlChopped) + strlen($finalPart);
Joas Schilling's avatar
Joas Schilling committed
784
		return (substr($possibleUrl,0,$endCharacter) == "$baseUrlChopped" . "$finalPart");
785
786
787
	}

	public function isFieldInResponse($field, $contentExpected){
788
		$data = $this->response->xml()->data[0];
789
790
791
792
793
794
795
796
797
798
799
800
801
802
		if ((string)$field == 'expiration'){
			$contentExpected = date('Y-m-d', strtotime($contentExpected)) . " 00:00:00";
		}
		if (count($data->element) > 0){
			foreach($data as $element) {
				if ($contentExpected == "A_TOKEN"){
					return (strlen((string)$element->$field) == 15);
				}
				elseif ($contentExpected == "A_NUMBER"){
					return is_numeric((string)$element->$field);
				}
				elseif($contentExpected == "AN_URL"){
					return $this->isExpectedUrl((string)$element->$field, "index.php/s/");
				}
803
				elseif ((string)$element->$field == $contentExpected){
804
805
					return True;
				}
806
			}
807

808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
			return False;
		} else {
			if ($contentExpected == "A_TOKEN"){
					return (strlen((string)$data->$field) == 15);
			}
			elseif ($contentExpected == "A_NUMBER"){
					return is_numeric((string)$data->$field);
			}
			elseif($contentExpected == "AN_URL"){
					return $this->isExpectedUrl((string)$data->$field, "index.php/s/");
			}
			elseif ($data->$field == $contentExpected){
					return True;
			}
			return False;
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
		}
	}

	/**
	 * @Then /^File "([^"]*)" should be included in the response$/
	 */
	public function checkSharedFileInResponse($filename){
		PHPUnit_Framework_Assert::assertEquals(True, $this->isFieldInResponse('file_target', "/$filename"));
	}

	/**
	 * @Then /^File "([^"]*)" should not be included in the response$/
	 */
	public function checkSharedFileNotInResponse($filename){
		PHPUnit_Framework_Assert::assertEquals(False, $this->isFieldInResponse('file_target', "/$filename"));
	}

840
841
842
843
844
845
846
847
848
849
850
851
852
853
	/**
	 * @Then /^User "([^"]*)" should be included in the response$/
	 */
	public function checkSharedUserInResponse($user){
		PHPUnit_Framework_Assert::assertEquals(True, $this->isFieldInResponse('share_with', "$user"));
	}

	/**
	 * @Then /^User "([^"]*)" should not be included in the response$/
	 */
	public function checkSharedUserNotInResponse($user){
		PHPUnit_Framework_Assert::assertEquals(False, $this->isFieldInResponse('share_with', "$user"));
	}

854
	public function isUserOrGroupInSharedData($userOrGroup){
855
856
		$data = $this->response->xml()->data[0];
		foreach($data as $element) {
857
			if ($element->share_with == $userOrGroup){
858
859
860
861
862
863
864
865
866
867
				return True;
			}
		}
		return False;
	}

	/**
	 * @Given /^file "([^"]*)" from user "([^"]*)" is shared with user "([^"]*)"$/
	 */
	public function assureFileIsShared($filepath, $user1, $user2){
868
		$fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/files_sharing/api/v{$this->sharingApiVersion}/shares" . "?path=$filepath";
869
870
871
872
873
874
875
876
		$client = new Client();
		$options = [];
		if ($user1 === 'admin') {
			$options['auth'] = $this->adminUser;
		} else {
			$options['auth'] = [$user1, $this->regularUser];
		}
		$this->response = $client->get($fullUrl, $options);
877
		if ($this->isUserOrGroupInSharedData($user2)){
878
879
880
881
882
			return;
		} else {
			$this->createShare($user1, $filepath, 0, $user2, null, null, null);
		}
		$this->response = $client->get($fullUrl, $options);
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
		PHPUnit_Framework_Assert::assertEquals(True, $this->isUserOrGroupInSharedData($user2));
	}

	/**
	 * @Given /^file "([^"]*)" from user "([^"]*)" is shared with group "([^"]*)"$/
	 */
	public function assureFileIsSharedWithGroup($filepath, $user, $group){
		$fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/files_sharing/api/v{$this->sharingApiVersion}/shares" . "?path=$filepath";
		$client = new Client();
		$options = [];
		if ($user === 'admin') {
			$options['auth'] = $this->adminUser;
		} else {
			$options['auth'] = [$user, $this->regularUser];
		}
		$this->response = $client->get($fullUrl, $options);
		if ($this->isUserOrGroupInSharedData($group)){
			return;
		} else {
			$this->createShare($user, $filepath, 1, $group, null, null, null);
		}
		$this->response = $client->get($fullUrl, $options);
		PHPUnit_Framework_Assert::assertEquals(True, $this->isUserOrGroupInSharedData($group));
	}

	public function makeDavRequest($user, $method, $path, $headers){
		$fullUrl = substr($this->baseUrl, 0, -4) . "remote.php/webdav" . "$path";
		$client = new Client();
		$options = [];
		if ($user === 'admin') {
			$options['auth'] = $this->adminUser;
		} else {
			$options['auth'] = [$user, $this->regularUser];
		}
		$request = $client->createRequest($method, $fullUrl, $options);
		foreach ($headers as $key => $value) {
			$request->addHeader($key, $value);	
		}
		$this->response = $client->send($request);
	}

	/**
	 * @Given /^User "([^"]*)" moved file "([^"]*)" to "([^"]*)"$/
	 */
	public function userMovedFile($user, $fileSource, $fileDestination){
		$fullUrl = substr($this->baseUrl, 0, -4) . "remote.php/webdav";
		$headers['Destination'] = $fullUrl . $fileDestination;
		$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) . "remote.php/webdav";
		$headers['Destination'] = $fullUrl . $fileDestination;
		$this->makeDavRequest($user, "MOVE", $fileSource, $headers);
941
942
943
944
945
946
947
	}

	/**
	 * @When /^Deleting last share$/
	 */
	public function deletingLastShare(){
		$share_id = $this->lastShareData->data[0]->id;
948
		$url = "/apps/files_sharing/api/v{$this->sharingApiVersion}/shares/$share_id";
949
		$this->sendingToWith("DELETE", $url, null);
950
951
	}

952
953
954
955
956
957
958
959
960
	/**
	 * @When /^Getting info of last share$/
	 */
	public function gettingInfoOfLastShare(){
		$share_id = $this->lastShareData->data[0]->id;
		$url = "/apps/files_sharing/api/v{$this->sharingApiVersion}/shares/$share_id";
		$this->sendingToWith("GET", $url, null);
	}

961
962
963
964
965
966
967
968
969
970
971
	/**
	 * @Then /^last share_id is included in the answer$/
	 */
	public function findingLastShareID(){
		print_r($this->response->xml());
		$share_id = $this->lastShareData->data[0]->id;
		if (!$this->isFieldInResponse('id', $share_id)){
			PHPUnit_Framework_Assert::fail("Share id $share_id not found in response");
		}
	}

972
973
974
975
976
977
978
	/**
	 * @Then /^Share fields of last share match with$/
	 * @param \Behat\Gherkin\Node\TableNode|null $formData
	 */
	public function checkShareFields($body){
		if ($body instanceof \Behat\Gherkin\Node\TableNode) {
			$fd = $body->getRowsHash();
979

980
			foreach($fd as $field => $value) {
981
982
983
				if (!$this->isFieldInResponse($field, $value)){
					PHPUnit_Framework_Assert::fail("$field" . " doesn't have value " . "$value");
				}
984
985
986
987
			}
		}
	}

988
989
	public static function removeFile($path, $filename){
		if (file_exists("$path" . "$filename")) {
990
			unlink("$path" . "$filename");
Joas Schilling's avatar
Joas Schilling committed
991
		}
992
993
994
995
996
997
998
	}

	/**
	 * @BeforeSuite
	 */
	public static function addFilesToSkeleton(){
		for ($i=0; $i<5; $i++){
999
			file_put_contents("../../core/skeleton/" . "textfile" . "$i" . ".txt", "ownCloud test text file\n");
1000
		}
1001
		if (!file_exists("../../core/skeleton/FOLDER")) {
Joas Schilling's avatar
Joas Schilling committed
1002
			mkdir("../../core/skeleton/FOLDER", 0777, true);
1003
		}
1004
1005
1006
1007
1008
1009
1010
1011
1012

	}

	/**
	 * @AfterSuite
	 */
	public static function removeFilesFromSkeleton(){
		for ($i=0; $i<5; $i++){
			self::removeFile("../../core/skeleton/", "textfile" . "$i" . ".txt");
Joas Schilling's avatar
Joas Schilling committed
1013
		}
1014
		if (is_dir("../../core/skeleton/FOLDER")) {
Joas Schilling's avatar
Joas Schilling committed
1015
			rmdir("../../core/skeleton/FOLDER");
1016
		}
1017
1018
	}

1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
	/**
	 * @BeforeScenario
	 * @AfterScenario
	 */
	public function cleanupUsers()
	{
		foreach($this->createdUsers as $user) {
			$this->deleteUser($user);
		}
	}

1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040

	/**
	 * @BeforeScenario
	 * @AfterScenario
	 */
	public function cleanupGroups()
	{
		foreach($this->createdGroups as $group) {
			$this->deleteGroup($group);
		}
	}
Thomas Müller's avatar
Thomas Müller committed
1041
}