-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
FileDownloadTrait.php
387 lines (325 loc) · 11.5 KB
/
FileDownloadTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
declare(strict_types=1);
namespace DrevOps\BehatSteps;
use Behat\Behat\Hook\Scope\AfterScenarioScope;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\Mink\Driver\Selenium2Driver;
use Behat\Mink\Element\NodeElement;
use Symfony\Component\Filesystem\Filesystem;
/**
* Trait FileDownloadTrait.
*
* Steps to work with file downloads.
*
* @package DrevOps\BehatSteps
*/
trait FileDownloadTrait {
/**
* Information about downloaded file.
*
* @var array<string, mixed>
*/
protected $fileDownloadDownloadedFileInfo;
/**
* Prepare scenario to work with this trait.
*
* @BeforeScenario
*/
public function fileDownloadBeforeScenario(BeforeScenarioScope $scope): void {
// Allow to skip this by adding a tag.
if ($scope->getScenario()->hasTag('behat-steps-skip:' . __FUNCTION__)) {
return;
}
if ($scope->getScenario()->hasTag('download')) {
$this->fileDownloadRemoveTempDir();
$this->fileDownloadPrepareTempDir();
}
}
/**
* Cleanup after scenario run.
*
* @AfterScenario
*/
public function fileDownloadAfterScenario(AfterScenarioScope $scope): void {
// Allow to skip this by adding a tag.
if ($scope->getScenario()->hasTag('behat-steps-skip:' . __FUNCTION__)) {
return;
}
if ($scope->getScenario()->hasTag('download')) {
$this->fileDownloadRemoveTempDir();
}
}
/**
* Download a file from the specified URL.
*
* @Then I download file from :url
*/
public function fileDownloadFrom(string $url): void {
if (empty(parse_url($url, PHP_URL_HOST))) {
$url = rtrim($this->getMinkParameter('base_url'), '/') . '/' . ltrim($url, '/');
}
$cookie_list = [];
/** @var \Behat\Mink\Driver\CoreDriver $driver */
$driver = $this->getSession()->getDriver();
if ($driver instanceof Selenium2Driver) {
$cookies = $driver->getWebDriverSession()->getAllCookies();
foreach ($cookies as $cookie) {
$cookie_list[] = $cookie['name'] . '=' . $cookie['value'];
}
}
else {
/** @var \Behat\Mink\Driver\BrowserKitDriver $driver */
$cookies = $driver->getClient()->getCookieJar()->allValues($driver->getCurrentUrl());
foreach ($cookies as $cookie_name => $cookie_value) {
$cookie_list[] = $cookie_name . '=' . $cookie_value;
}
}
$this->fileDownloadDownloadedFileInfo = $this->fileDownloadProcess($url, [
CURLOPT_COOKIE => implode('; ', $cookie_list),
]);
if (!$this->fileDownloadDownloadedFileInfo['file_path']) {
throw new \RuntimeException('Unable to download file from URL ' . $url);
}
$file_data = file_get_contents($this->fileDownloadDownloadedFileInfo['file_path']);
if ($file_data === FALSE) {
throw new \RuntimeException('Unable to load content for downloaded file from temporary local file');
}
$this->fileDownloadDownloadedFileInfo['content'] = $file_data;
}
/**
* Download the file from the specified HTML link.
*
* @Then I download file from link :link
*/
public function fileDownloadFromLink(string $link): void {
$link_element = $this->fileDownloadAssertLinkPresence($link, 'present');
$url = $link_element->getAttribute('href');
$this->fileDownloadFrom($url);
}
/**
* Assert that an HTML link is present or absent on the page.
*
* @Then I see download :link link :presence(on the page)
*/
public function fileDownloadAssertLinkPresence(string $link, string $presence): NodeElement {
$should_be_present = $presence === 'present';
$page = $this->getSession()->getPage();
$link_element = $page->findLink($link);
if ($should_be_present && !$link_element) {
throw new \Exception(sprintf('No link "%s" is present on the page, but expected to be present', $link));
}
elseif (!$should_be_present && $link_element) {
throw new \Exception(sprintf('Link "%s" is present on the page, but expected to be absent', $link));
}
return $link_element;
}
/**
* Assert the contents of the download file.
*
* @Then downloaded file contains:
*/
public function fileDownloadAssertFileContains(PyStringNode $string): void {
$string = strval($string);
if (!$this->fileDownloadDownloadedFileInfo) {
throw new \RuntimeException('Downloaded file content has no data.');
}
$lines = preg_split('/\R/', (string) $this->fileDownloadDownloadedFileInfo['content']);
if (is_array($lines)) {
foreach ($lines as $line) {
if (preg_match('/^\/.+\/[a-z]*$/i', $string)) {
if (preg_match($string, $line)) {
return;
}
}
elseif (str_contains($line, $string)) {
return;
}
}
}
throw new \Exception('Unable to find a content line with searched string.');
}
/**
* Assert the file name of the downloaded file.
*
* @Then downloaded file name is :name
*/
public function fileDownloadAssertFileName(string $name): void {
if (!$this->fileDownloadDownloadedFileInfo || empty($this->fileDownloadDownloadedFileInfo['file_name'])) {
throw new \RuntimeException('Downloaded file name content has no data.');
}
if ($name != $this->fileDownloadDownloadedFileInfo['file_name']) {
throw new \Exception(sprintf('Downloaded file %s, but expected %s', $this->fileDownloadDownloadedFileInfo['file_name'], $name));
}
}
/**
* Assert downloaded file is a ZIP archive and it contains files.
*
* @Then downloaded file is zip archive that contains files:
*/
public function fileDownloadAssertZipContains(TableNode $files): void {
$zip = $this->fileDownloadOpenZip();
$errors = [];
foreach ($files->getColumn(0) as $line) {
if ($zip->locateName($line) === FALSE) {
$errors[] = sprintf('Unable to find file "%s" in archive', $line);
}
}
if (!empty($errors)) {
throw new \Exception(implode(PHP_EOL, $errors));
}
}
/**
* Assert downloaded file is a ZIP archive and it does not contain files.
*
* @Then downloaded file is zip archive that does not contain files:
*/
public function fileDownloadAssertNoZipContains(TableNode $files): void {
$zip = $this->fileDownloadOpenZip();
$errors = [];
foreach ($files->getColumn(0) as $line) {
if ($zip->locateName($line) !== FALSE) {
$errors[] = sprintf('Found file "%s" in archive but should not', $line);
}
}
if (!empty($errors)) {
throw new \Exception(implode(PHP_EOL, $errors));
}
}
/**
* Open downloaded ZIP archive and validate contents.
*/
protected function fileDownloadOpenZip(): \ZipArchive {
if (!class_exists('\ZipArchive')) {
throw new \RuntimeException('ZIP extension is not enabled for PHP');
}
if (empty($this->fileDownloadDownloadedFileInfo) || empty($this->fileDownloadDownloadedFileInfo['file_path'])) {
throw new \RuntimeException('Downloaded file path data is not available.');
}
if (empty($this->fileDownloadDownloadedFileInfo) || empty($this->fileDownloadDownloadedFileInfo['content_type'])) {
throw new \Exception('Downloaded file information does not have content type data.');
}
if (!in_array($this->fileDownloadDownloadedFileInfo['content_type'], [
'application/octet-stream', 'application/zip',
])) {
throw new \Exception('Downloaded file does not have correct headers set for ZIP.');
}
$zip = new \ZipArchive();
$result = $zip->open($this->fileDownloadDownloadedFileInfo['file_path']);
if ($result !== TRUE) {
if ($result == \ZipArchive::ER_NOZIP) {
throw new \Exception('Downloaded file is not valid ZIP file.');
}
else {
throw new \Exception('Downloaded file cannot be read.');
}
}
return $zip;
}
/**
* Download file.
*
* @param string $url
* URL to download file from.
* @param array<int, mixed> $options
* CURL options.
*
* @return array<string, string>
* Array of downloaded file information.
*/
protected function fileDownloadProcess(string $url, array $options = []): array {
$response_headers = [];
$options += [
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HEADER => FALSE,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_MAXREDIRS => 10,
CURLOPT_ENCODING => '',
CURLOPT_USERAGENT => 'test',
CURLOPT_AUTOREFERER => TRUE,
CURLOPT_CONNECTTIMEOUT => 120,
CURLOPT_TIMEOUT => 120,
CURLOPT_HEADERFUNCTION => function ($ch, $header) use (&$response_headers): int {
$response_headers[] = $header;
return strlen($header);
},
];
if (!filter_var($url, FILTER_VALIDATE_URL)) {
throw new \RuntimeException('Invalid download URL provided: ' . $url);
}
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
curl_close($ch);
if (!$content) {
throw new \RuntimeException('Unable to save temp file from URL ' . $url);
}
// Extract meta information from headers.
$headers = $this->fileDownloadParseHeaders($response_headers);
// Resolve file path and name.
$dir = $this->fileDownloadGetTempDir();
// Try to extract name from the download string.
$url_file_name = parse_url($url, PHP_URL_PATH);
$url_file_name = $url_file_name ? basename($url_file_name) : $url_file_name;
$headers['file_name'] = empty($headers['file_name']) && !empty($url_file_name) ? $url_file_name : $headers['file_name'];
$file_path = empty($headers['file_name']) ? tempnam($dir, 'behat') : $dir . DIRECTORY_SEPARATOR . $headers['file_name'];
if (!$file_path) {
throw new \RuntimeException('Unable to create temp file for downloaded content');
}
$file_name = basename($file_path);
// Write file contents.
$written = file_put_contents($file_path, $content);
if ($written === FALSE) {
throw new \RuntimeException('Unable to write downloaded content into file ' . $file_path);
}
print $file_path;
return ['file_name' => $file_name, 'file_path' => $file_path] + $headers;
}
/**
* Extract downloaded file information from the response headers.
*
* @param array<int,string> $headers
* Array of headers from CURL.
*
* @return array<string, string>
* Array of parsed headers, if any.
*/
protected function fileDownloadParseHeaders(array $headers): array {
$parsed_headers = [];
foreach ($headers as $header) {
if (preg_match('/Content-Disposition:\s*attachment;\s*filename\s*=\s*\"([^"]+)"/', (string) $header, $matches) && !empty($matches[1])) {
$parsed_headers['file_name'] = trim($matches[1]);
continue;
}
if (preg_match('/Content-Type:\s*(.+)/', (string) $header, $matches) && !empty($matches[1])) {
$parsed_headers['content_type'] = trim($matches[1]);
}
}
return $parsed_headers;
}
/**
* Prepare temporary directory for file downloads.
*/
protected function fileDownloadPrepareTempDir(): void {
$fs = new Filesystem();
if (!$fs->exists($this->fileDownloadGetTempDir())) {
$fs->mkdir($this->fileDownloadGetTempDir());
}
}
/**
* Remove temporary directory for file downloads.
*/
protected function fileDownloadRemoveTempDir(): void {
$fs = new Filesystem();
if (!$fs->exists($this->fileDownloadGetTempDir())) {
$fs->remove($this->fileDownloadGetTempDir());
}
}
/**
* Get temp download dir.
*/
protected function fileDownloadGetTempDir(): string {
return '/tmp/behat_downloads';
}
}