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) { |
|
|
|
|
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){ |
|
|
|
|
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) { |
|
|
|
|
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){ |
|
|
|
|
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) { |
|
|
|
|
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) { |
|
|
|
|
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
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
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:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.