Skip to content

Commit d45efea

Browse files
committed
test: fix archive tests
1 parent 085a1a4 commit d45efea

File tree

4 files changed

+94
-50
lines changed

4 files changed

+94
-50
lines changed

tests/acceptance/expected-failures-localAPI-on-OCIS-storage.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ The expected failures in this file are from features in the owncloud/ocis repo.
1313
- [apiArchiver/downloadByPath.feature:171](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiArchiver/downloadByPath.feature#L171)
1414
- [apiArchiver/downloadByPath.feature:172](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiArchiver/downloadByPath.feature#L172)
1515

16-
#### [Downloaded /Shares tar contains resource (files|folder) with leading / in Response](https://github.com/owncloud/ocis/issues/4636)
17-
18-
- [apiArchiver/downloadById.feature:173](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiArchiver/downloadById.feature#L173)
19-
- [apiArchiver/downloadById.feature:174](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiArchiver/downloadById.feature#L174)
20-
2116
#### [Shared mount folder gets deleted when overwritten by a file from personal space](https://github.com/owncloud/ocis/issues/7208)
2217

2318
- [apiSpacesShares/copySpaces.feature:696](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiSpacesShares/copySpaces.feature#L696)

tests/acceptance/features/apiArchiver/downloadById.feature

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Feature: download multiple resources bundled into an archive
1313

1414
Scenario Outline: download a single file
1515
Given user "Alice" has uploaded file with content "some data" to "/textfile0.txt"
16-
When user "Alice" downloads the archive of "/textfile0.txt" using the resource id and setting these headers
16+
When user "Alice" downloads the <archive-type> archive of "/textfile0.txt" using the resource id and setting these headers:
1717
| header | value |
1818
| User-Agent | <user-agent> |
1919
Then the HTTP status code should be "200"
@@ -22,15 +22,15 @@ Feature: download multiple resources bundled into an archive
2222
| textfile0.txt | some data |
2323
Examples:
2424
| user-agent | archive-type |
25-
| Linux | zip |
25+
| Linux | tar |
2626
| Windows NT | zip |
2727

2828

2929
Scenario Outline: download a single folder
3030
Given user "Alice" has created folder "my_data"
3131
And user "Alice" has uploaded file with content "some data" to "/my_data/textfile0.txt"
3232
And user "Alice" has uploaded file with content "more data" to "/my_data/an_other_file.txt"
33-
When user "Alice" downloads the archive of "/my_data" using the resource id and setting these headers
33+
When user "Alice" downloads the <archive-type> archive of "/my_data" using the resource id and setting these headers:
3434
| header | value |
3535
| User-Agent | <user-agent> |
3636
Then the HTTP status code should be "200"
@@ -41,7 +41,7 @@ Feature: download multiple resources bundled into an archive
4141
Examples:
4242
| user-agent | archive-type |
4343
| Linux | zip |
44-
| Windows NT | zip |
44+
| Windows NT | tar |
4545

4646

4747
Scenario: download multiple files and folders
@@ -158,16 +158,16 @@ Feature: download multiple resources bundled into an archive
158158
| shareType | user |
159159
| permissionsRole | Viewer |
160160
And user "Brian" has a share "more_data" synced
161-
When user "Brian" downloads the archive of "/Shares" using the resource id and setting these headers
161+
When user "Brian" downloads the <archive-type> archive of "/Shares" using the resource id and setting these headers:
162162
| header | value |
163163
| User-Agent | <user-agent> |
164164
Then the HTTP status code should be "200"
165165
And the downloaded <archive-type> archive should contain these files:
166-
| name | content |
167-
| Shares/textfile0.txt | some data |
168-
| Shares/textfile1.txt | other data |
169-
| Shares/my_data/textfile2.txt | some data |
170-
| Shares/more_data/an_other_file.txt | more data |
166+
| name | content |
167+
| textfile0.txt | some data |
168+
| textfile1.txt | other data |
169+
| my_data/textfile2.txt | some data |
170+
| more_data/an_other_file.txt | more data |
171171
Examples:
172172
| user-agent | archive-type |
173173
| Linux | tar |

tests/acceptance/features/bootstrap/ArchiverContext.php

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
use TestHelpers\SetupHelper;
2929
use PHPUnit\Framework\Assert;
3030
use Psr\Http\Message\ResponseInterface;
31+
use splitbrain\PHPArchive\Tar;
32+
use splitbrain\PHPArchive\Zip;
33+
use splitbrain\PHPArchive\Archive;
3134

3235
require_once 'bootstrap.php';
3336

@@ -62,6 +65,38 @@ public function setUpScenario(BeforeScenarioScope $scope): void {
6265
);
6366
}
6467

68+
/**
69+
* @param string $type
70+
*
71+
* @return Archive
72+
*/
73+
public function getArchiveClass(string $type): Archive {
74+
if ($type === 'zip') {
75+
return new Zip();
76+
} elseif ($type === 'tar') {
77+
return new Tar();
78+
} else {
79+
throw new Exception('Unknown archive type: ' . $type);
80+
}
81+
}
82+
83+
/**
84+
* @param string $dir
85+
*
86+
* @return void
87+
*/
88+
public function removeDir(string $dir): void {
89+
$items = \glob("$dir/*");
90+
foreach ($items as $item) {
91+
if (\is_dir($item)) {
92+
$this->removeDir($item);
93+
} else {
94+
\unlink($item);
95+
}
96+
}
97+
\rmdir($dir);
98+
}
99+
65100
/**
66101
* @param string $user
67102
* @param string $resource
@@ -92,9 +127,10 @@ private function getArchiverQueryString(
92127
}
93128

94129
/**
95-
* @When user :user downloads the archive of :resource using the resource :addressType and setting these headers
130+
* @When /^user "([^"]*)" downloads the (zip|tar) archive of "([^"]*)" using the resource (id|ids|path|paths) and setting these headers:$/
96131
*
97132
* @param string $user
133+
* @param string $archiveType
98134
* @param string $resource
99135
* @param string $addressType id|path
100136
* @param TableNode $headersTable
@@ -104,8 +140,9 @@ private function getArchiverQueryString(
104140
* @throws GuzzleException
105141
* @throws Exception
106142
*/
107-
public function userDownloadsTheArchive(
143+
public function userDownloadsTheZipOrTarArchiveOfResourceUsingResourceIdOrPathAndSettingTheseHeaders(
108144
string $user,
145+
string $archiveType,
109146
string $resource,
110147
string $addressType,
111148
TableNode $headersTable
@@ -118,7 +155,7 @@ public function userDownloadsTheArchive(
118155
foreach ($headersTable as $row) {
119156
$headers[$row['header']] = $row ['value'];
120157
}
121-
$this->featureContext->setResponse($this->downloadArchive($user, $resource, $addressType, null, $headers));
158+
$this->featureContext->setResponse($this->downloadArchive($user, $resource, $addressType, $archiveType, null, $headers));
122159
}
123160

124161
/**
@@ -140,13 +177,14 @@ public function userDownloadsTheArchiveOfItemOfUser(
140177
string $owner,
141178
string $addressType
142179
): void {
143-
$this->featureContext->setResponse($this->downloadArchive($downloader, $resource, $addressType, $owner));
180+
$this->featureContext->setResponse($this->downloadArchive($downloader, $resource, $addressType, null, $owner));
144181
}
145182

146183
/**
147184
* @param string $downloader
148185
* @param string $resource
149186
* @param string $addressType
187+
* @param string|null $archiveType
150188
* @param string|null $owner
151189
* @param array|null $headers
152190
*
@@ -158,12 +196,16 @@ public function downloadArchive(
158196
string $downloader,
159197
string $resource,
160198
string $addressType,
199+
?string $archiveType = null,
161200
?string $owner = null,
162201
?array $headers = null
163202
): ResponseInterface {
164203
$owner = $owner ?? $downloader;
165204
$downloader = $this->featureContext->getActualUsername($downloader);
166205
$queryString = $this->getArchiverQueryString($owner, $resource, $addressType);
206+
if ($archiveType !== null) {
207+
$queryString .= '&output-format=' . $archiveType;
208+
}
167209
return HttpRequestHelper::get(
168210
$this->featureContext->getBaseUrl() . '/archiver?' . $queryString,
169211
$this->featureContext->getStepLineRef(),
@@ -220,28 +262,34 @@ public function theDownloadedArchiveShouldContainTheseFiles(string $type, TableN
220262
$this->featureContext->verifyTableNodeColumns($expectedFiles, ['name', 'content']);
221263
$contents = $this->featureContext->getResponse()->getBody()->getContents();
222264
$tempFile = \tempnam(\sys_get_temp_dir(), 'OcAcceptanceTests_');
265+
$tempExtractFolder = $tempFile;
223266
\unlink($tempFile); // we only need the name
224267
$tempFile = $tempFile . '.' . $type; // it needs the extension
225268
\file_put_contents($tempFile, $contents);
226269

227270
// open the archive
228-
$archiveData = new RecursiveIteratorIterator(
229-
new PharData($tempFile),
230-
RecursiveIteratorIterator::SELF_FIRST
231-
);
271+
$tar = $this->getArchiveClass($type);
272+
$tar->open($tempFile);
273+
$archiveData = $tar->contents();
274+
275+
// extract the archive
276+
$tar->open($tempFile);
277+
$tar->extract($tempExtractFolder);
278+
$tar->close();
279+
232280
foreach ($expectedFiles->getHash() as $expectedItem) {
233281
$expectedPath = trim($expectedItem['name'], "/");
234282
$found = false;
235283
foreach ($archiveData as $info) {
236284
// get only the parent folder path for the given item
237-
$actualPath = explode(".$type", $info->getPathname())[1];
238-
$actualPath = trim($actualPath, "/");
285+
$actualPath = $info->getPath();
239286

240287
if ($expectedPath === $actualPath) {
241-
if (!$info->isDir()) {
288+
if (!$info->getIsdir()) {
289+
$fileContent = \file_get_contents("$tempExtractFolder/$actualPath");
242290
Assert::assertEquals(
243291
$expectedItem['content'],
244-
$info->getContent(),
292+
$fileContent,
245293
__METHOD__ .
246294
" content of '" . $expectedPath . "' not as expected"
247295
);
@@ -255,5 +303,6 @@ public function theDownloadedArchiveShouldContainTheseFiles(string $type, TableN
255303
}
256304
}
257305
\unlink($tempFile);
306+
$this->removeDir($tempExtractFolder);
258307
}
259308
}

vendor-bin/behat/composer.json

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
{
2-
"config" : {
3-
"platform": {
4-
"php": "8.2"
5-
},
6-
"allow-plugins": {
7-
"composer/package-versions-deprecated": true
8-
}
2+
"config": {
3+
"platform": {
4+
"php": "8.2"
95
},
10-
"require": {
11-
"behat/behat": "^3.13",
12-
"behat/gherkin": "^4.9",
13-
"behat/mink": "1.7.1",
14-
"friends-of-behat/mink-extension": "^2.7",
15-
"ciaranmcnulty/behat-stepthroughextension" : "dev-master",
16-
"rdx/behat-variables": "^1.2",
17-
"sensiolabs/behat-page-object-extension": "^2.3",
18-
"symfony/translation": "^5.4",
19-
"sabre/xml": "^2.2",
20-
"guzzlehttp/guzzle": "^7.7",
21-
"phpunit/phpunit": "^9.6",
22-
"laminas/laminas-ldap": "^2.15",
23-
"ankitpokhrel/tus-php": "^2.3",
24-
"wapmorgan/unified-archive": "^1.1.10",
25-
"swaggest/json-schema": "^0.12.42"
6+
"allow-plugins": {
7+
"composer/package-versions-deprecated": true
268
}
9+
},
10+
"require": {
11+
"behat/behat": "^3.13",
12+
"behat/gherkin": "^4.9",
13+
"behat/mink": "1.7.1",
14+
"friends-of-behat/mink-extension": "^2.7",
15+
"ciaranmcnulty/behat-stepthroughextension": "dev-master",
16+
"rdx/behat-variables": "^1.2",
17+
"sensiolabs/behat-page-object-extension": "^2.3",
18+
"symfony/translation": "^5.4",
19+
"sabre/xml": "^2.2",
20+
"guzzlehttp/guzzle": "^7.7",
21+
"phpunit/phpunit": "^9.6",
22+
"laminas/laminas-ldap": "^2.15",
23+
"ankitpokhrel/tus-php": "^2.3",
24+
"swaggest/json-schema": "^0.12.42",
25+
"splitbrain/php-archive": "^1.3"
2726
}
27+
}

0 commit comments

Comments
 (0)