Completed
Push — master ( c4929a...3bd4fb )
by Julius
19:34 queued 18:15
created

FeatureContext::aGuestOpensTheShareLinkAs()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 29

Duplication

Lines 5
Ratio 17.24 %

Importance

Changes 0
Metric Value
dl 5
loc 29
rs 9.456
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
require __DIR__ . '/../../vendor/autoload.php';
3
4
use Behat\Behat\Context\Context;
5
use GuzzleHttp\Client;
6
use GuzzleHttp\Psr7\Response;
7
use PHPUnit\Framework\Assert;
8
9
class FeatureContext implements Context
10
{
11
12
	use \JuliusHaertl\NextcloudBehat\FilesSharingContextTrait;
13
	use \JuliusHaertl\NextcloudBehat\FilesDavContextTrait;
14
	use \JuliusHaertl\NextcloudBehat\UserContextTrait;
15
	use \JuliusHaertl\NextcloudBehat\UserWebContextTrait;
16
17
	/**
18
	 * Initializes context.
19
	 *
20
	 * Every scenario gets its own context instance.
21
	 * You can also pass arbitrary arguments to the
22
	 * context constructor through behat.yml.
23
	 */
24
	public function __construct($baseUrl)
25
	{
26
		$this->setBaseUrl($baseUrl);
27
	}
28
29
	/**
30
	 * @When User :user opens :file
31
	 */
32
	public function userOpens($user, $file)
33
	{
34
		// get file id
35
		$davClient = $this->getSabreClient($user);
36
		$path = $this->makeSabrePath($user, $file);
37
		$result = $davClient->propFind($path, ['{http://owncloud.org/ns}fileid']);
38
		$fileId = $result['{http://owncloud.org/ns}fileid'];
39
40
		$this->usingWebAsUser($user);
41
42
		$client = new Client();
43
		$result = $client->get($this->baseUrl . 'index.php/apps/richdocuments/index?fileId=' . $fileId, $this->getWebOptions());
44
		$contents =$result->getBody()->getContents();
45
		$re = '/var richdocuments_([A-z]+) = (.*);/m';
46
		preg_match_all($re, $contents, $matches, PREG_SET_ORDER, 0);
47
		$result = [];
48
		foreach ($matches as $match) {
0 ignored issues
show
Bug introduced by
The expression $matches of type null|array<integer,array<integer,string>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
49
			$result[$match[1]] = str_replace("'", "", $match[2]);
50
		}
51
52
		$this->fileId = $result['fileId'];
53
		$this->wopiToken = $result['token'];
54
	}
55
56
57
	/**
58
	 * @Then a guest opens the share link
59
	 */
60
	public function aGuestOpensTheShareLink()
61
	{
62 View Code Duplication
		if (count($this->lastShareData->data->element) > 0){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
63
			$token = $this->lastShareData->data[0]->token;
64
		} else {
65
			$token = $this->lastShareData->data->token;
66
		}
67
68
69
		// 	public function publicPage($shareToken, $fileName, $fileId) {
70
		$client = new Client();
71
		$result = $client->get($this->baseUrl . 'index.php/apps/richdocuments/public?shareToken=' . $token, $this->getWebOptions());
72
		$contents =$result->getBody()->getContents();
73
		$re = '/var richdocuments_([A-z]+) = (.*);/m';
74
		preg_match_all($re, $contents, $matches, PREG_SET_ORDER, 0);
75
		$result = [];
76
		foreach ($matches as $match) {
0 ignored issues
show
Bug introduced by
The expression $matches of type null|array<integer,array<integer,string>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
77
			$result[$match[1]] = str_replace("'", "", $match[2]);
78
		}
79
80
		$this->fileId = $result['fileId'];
81
		$this->wopiToken = $result['token'];
82
	}
83
84
	/**
85
	 * @Then a guest opens the share link as :user
86
	 */
87
	public function aGuestOpensTheShareLinkAs($user)
88
	{
89 View Code Duplication
		if (count($this->lastShareData->data->element) > 0){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
90
			$token = $this->lastShareData->data[0]->token;
91
		} else {
92
			$token = $this->lastShareData->data->token;
93
		}
94
95
		$cookieJar = \GuzzleHttp\Cookie\CookieJar::fromArray([
96
			'guestUser' => $user
97
		], 'localhost');
98
99
100
		// 	public function publicPage($shareToken, $fileName, $fileId) {
101
		$client = new Client();
102
		$result = $client->get($this->baseUrl . 'index.php/apps/richdocuments/public?shareToken=' . $token, array_merge($this->getWebOptions(), [
103
			'cookies' => $cookieJar
104
		]));
105
		$contents = $result->getBody()->getContents();
106
		$re = '/var richdocuments_([A-z]+) = (.*);/m';
107
		preg_match_all($re, $contents, $matches, PREG_SET_ORDER, 0);
108
		$result = [];
109
		foreach ($matches as $match) {
0 ignored issues
show
Bug introduced by
The expression $matches of type null|array<integer,array<integer,string>> is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
110
			$result[$match[1]] = str_replace("'", "", $match[2]);
111
		}
112
113
		$this->fileId = $result['fileId'];
114
		$this->wopiToken = $result['token'];
115
	}
116
117
	/**
118
	 * @Then Collabora fetches checkFileInfo
119
	 */
120
	public function collaboraFetchesCheckfileinfo() {
121
		$client = new Client();
122
		$options = [];
123
		$result = $client->get($this->baseUrl . 'index.php/apps/richdocuments/wopi/files/' . $this->fileId . '?access_token=' . $this->wopiToken, $options);
124
		$this->checkFileInfoResult = json_decode($result->getBody()->getContents(), true);
125
	}
126
127
	/**
128
	 * @Then Collabora puts :source
129
	 */
130
	public function collaboraPuts($source)
131
	{
132
		$file = \GuzzleHttp\Psr7\stream_for(fopen($source, 'r'));
133
		$client = new Client();
134
		$options = [
135
			'data' => $file,
136
			'headers' => [
137
				'X-LOOL-WOPI-Timestamp' => $this->checkFileInfoResult['LastModifiedTime']
138
			]
139
		];
140
		try {
141
			$result = $client->post($this->baseUrl . 'index.php/apps/richdocuments/wopi/files/' . $this->fileId . '/contents?access_token=' . $this->wopiToken, $options);
142
			$this->checkFileInfoResult = json_decode($result->getBody()->getContents(), true);
143
		} catch (\GuzzleHttp\Exception\ClientException $e) {
0 ignored issues
show
Bug introduced by
The class GuzzleHttp\Exception\ClientException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
144
			$this->response = $e->getResponse();
145
		}
146
	}
147
148
	/**
149
	 * @Then /^the HTTP status code should be "([^"]*)"$/
150
	 * @param int $statusCode
151
	 */
152
	public function theHTTPStatusCodeShouldBe($statusCode) {
153
		Assert::assertEquals($statusCode, $this->response->getStatusCode());
154
	}
155
156
157
	/**
158
	 * @Then checkFileInfo :arg1 is ":arg2"
159
	 */
160
	public function checkfileinfoIs($arg1, $arg2)
161
	{
162
		\PHPUnit\Framework\Assert::assertEquals($arg2, $this->checkFileInfoResult[$arg1]);
163
	}
164
165
166
	/**
167
	 * @Then checkFileInfo :arg1 matches ":arg2"
168
	 */
169
	public function checkfileinfoMatches($arg1, $arg2)
170
	{
171
		\PHPUnit\Framework\Assert::assertRegExp($arg2, $this->checkFileInfoResult[$arg1]);
172
	}
173
174
	/**
175
	 * @Then checkFileInfo :arg1 is true
176
	 */
177
	public function checkfileinfoIsTrue($arg1)
178
	{
179
		\PHPUnit\Framework\Assert::assertTrue($this->checkFileInfoResult[$arg1]);
180
	}
181
182
	/**
183
	 * @Then checkFileInfo :arg1 is false
184
	 */
185
	public function checkfileinfoIsFalse($arg1)
186
	{
187
		\PHPUnit\Framework\Assert::assertFalse($this->checkFileInfoResult[$arg1]);
188
	}
189
190
}
191