Skip to content

Commit 9d3d0a4

Browse files
committed
Theme Modules: Added testing coverage for install command
1 parent 5038d12 commit 9d3d0a4

File tree

3 files changed

+315
-14
lines changed

3 files changed

+315
-14
lines changed

app/Console/Commands/InstallModuleCommand.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,15 @@ public function handle(): int
8686
return 1;
8787
}
8888

89-
$this->info("Module \"{$newModule->name}\" ({$newModule->version}) successfully installed!");
89+
$this->info("Module \"{$newModule->name}\" ({$newModule->getVersion()}) successfully installed!");
9090
$this->info("Install location: {$moduleFolder}/{$newModule->folderName}");
9191
$this->cleanup();
9292
return 0;
9393
}
9494

95+
/**
96+
* @param ThemeModule[] $existingModules
97+
*/
9598
protected function handleExistingModulesWithSameName(array $existingModules, ThemeModuleManager $manager): bool
9699
{
97100
if (count($existingModules) === 0) {
@@ -100,7 +103,7 @@ protected function handleExistingModulesWithSameName(array $existingModules, The
100103

101104
$this->warn("The following modules already exist with the same name:");
102105
foreach ($existingModules as $folder => $module) {
103-
$this->line("{$module->name} ({$folder}:{$module->version}) - {$module->description}");
106+
$this->line("{$module->name} ({$folder}:{$module->getVersion()}) - {$module->description}");
104107
}
105108
$this->line('');
106109

@@ -145,7 +148,7 @@ protected function getModuleFolder(string $themeFolder): string|null
145148
protected function getThemeFolder(): string|null
146149
{
147150
$path = theme_path('');
148-
if (!$path) {
151+
if (!$path || !is_dir($path)) {
149152
$shouldCreate = $this->confirm('No active theme folder found, would you like to create one?');
150153
if (!$shouldCreate) {
151154
return null;
@@ -178,7 +181,7 @@ protected function validateAndGetModuleInfoFromZip(ThemeModuleZip $zip): ThemeMo
178181
}
179182

180183
if ($zip->getContentsSize() > (50 * 1024 * 1024)) {
181-
$this->error("ERROR: Module ZIP file is too large. Maximum size is 50MB");
184+
$this->error("ERROR: Module ZIP file contents are too large. Maximum size is 50MB");
182185
return null;
183186
}
184187

@@ -196,7 +199,7 @@ protected function downloadModuleFile(string $location): string|null
196199
{
197200
$httpRequests = app()->make(HttpRequestService::class);
198201
$client = $httpRequests->buildClient(30, ['stream' => true]);
199-
$originalHost = parse_url($location, PHP_URL_HOST);
202+
$originalUrl = parse_url($location);
200203
$currentLocation = $location;
201204
$maxRedirects = 3;
202205
$redirectCount = 0;
@@ -209,8 +212,12 @@ protected function downloadModuleFile(string $location): string|null
209212
if ($statusCode >= 300 && $statusCode < 400 && $redirectCount < $maxRedirects) {
210213
$redirectLocation = $resp->getHeaderLine('Location');
211214
if ($redirectLocation) {
212-
$redirectHost = parse_url($redirectLocation, PHP_URL_HOST);
213-
if ($redirectHost === $originalHost) {
215+
$redirectUrl = parse_url($redirectLocation);
216+
if (
217+
($originalUrl['host'] ?? '') === ($redirectUrl['host'] ?? '')
218+
&& ($originalUrl['scheme'] ?? '') === ($redirectUrl['scheme'] ?? '')
219+
&& ($originalUrl['port'] ?? '') === ($redirectUrl['port'] ?? '')
220+
) {
214221
$currentLocation = $redirectLocation;
215222
$redirectCount++;
216223
continue;

app/Theming/ThemeModuleZip.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace BookStack\Theming;
44

5+
use ZipArchive;
6+
57
readonly class ThemeModuleZip
68
{
79
public function __construct(
@@ -11,7 +13,7 @@ public function __construct(
1113

1214
public function extractTo(string $destinationPath): void
1315
{
14-
$zip = new \ZipArchive();
16+
$zip = new ZipArchive();
1517
$zip->open($this->path);
1618
$zip->extractTo($destinationPath);
1719
$zip->close();
@@ -23,7 +25,7 @@ public function extractTo(string $destinationPath): void
2325
*/
2426
public function getModuleInstance(): ThemeModule
2527
{
26-
$zip = new \ZipArchive();
28+
$zip = new ZipArchive();
2729
$open = $zip->open($this->path);
2830
if ($open !== true) {
2931
throw new ThemeModuleException("Unable to open zip file at {$this->path}");
@@ -61,18 +63,21 @@ public function exists(): bool
6163
return false;
6264
}
6365

64-
$zip = new \ZipArchive();
65-
$open = $zip->open($this->path);
66-
$zip->close();
67-
return $open === true;
66+
$zip = new ZipArchive();
67+
$open = $zip->open($this->path, ZipArchive::RDONLY);
68+
if ($open === true) {
69+
$zip->close();
70+
return true;
71+
}
72+
return false;
6873
}
6974

7075
/**
7176
* Get the total size of the zip file contents when uncompressed.
7277
*/
7378
public function getContentsSize(): int
7479
{
75-
$zip = new \ZipArchive();
80+
$zip = new ZipArchive();
7681

7782
if ($zip->open($this->path) !== true) {
7883
return 0;

0 commit comments

Comments
 (0)