Skip to content

Commit 7e3d3de

Browse files
committed
Display error when package is not installed
1 parent b769164 commit 7e3d3de

4 files changed

Lines changed: 64 additions & 1 deletion

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace ProcessMaker\Exception;
4+
5+
use Exception;
6+
7+
class PackageNotInstalledException extends Exception
8+
{
9+
}

‎ProcessMaker/ImportExport/Manifest.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace ProcessMaker\ImportExport;
44

55
use Illuminate\Support\Arr;
6+
use ProcessMaker\Exception\PackageNotInstalledException;
67
use ProcessMaker\ImportExport\Exporters\ExporterInterface;
78
use ReflectionClass;
89

@@ -83,6 +84,7 @@ public static function fromArray(array $array, Options $options)
8384
$manifest = new self();
8485
foreach ($array as $uuid => $assetInfo) {
8586
$exporterClass = $assetInfo['exporter'];
87+
self::checkClass($exporterClass);
8688
$modeOption = $options->get('mode', $uuid);
8789
list($mode, $model, $matchedBy) = self::getModel($uuid, $assetInfo, $modeOption, $exporterClass);
8890
$exporter = new $exporterClass($model, $manifest, $options, false);
@@ -99,6 +101,17 @@ public static function fromArray(array $array, Options $options)
99101
return $manifest;
100102
}
101103

104+
public static function checkClass($exporterClass)
105+
{
106+
if (!class_exists($exporterClass)) {
107+
if (preg_match('/.*\\\\(.+)\\\\ImportExport/', $exporterClass, $matches)) {
108+
$package = trim(preg_replace('/(?!^)[A-Z]{2,}(?=[A-Z][a-z])|[A-Z][a-z]/', ' $0', $matches[1]));
109+
throw new PackageNotInstalledException("Can not import because $package is not installed.");
110+
}
111+
throw new PackageNotInstalledException("$exporterClass not found");
112+
}
113+
}
114+
102115
public function push(string $uuid, ExporterInterface $exporter)
103116
{
104117
$this->manifest[$uuid] = $exporter;

‎resources/js/processes/import/components/ImportManagerView.vue‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ export default {
293293
} else if (error.response?.data?.error === 'incorrect password') {
294294
this.passwordError = "Incorrect Password";
295295
} else {
296-
const message = error.response?.data?.error || error.message;
296+
const message = error.response?.data?.error || error.response?.data?.message || error.message;
297297
ProcessMaker.alert(message, 'danger');
298298
}
299299
});

‎tests/Feature/ImportExport/ManifestTest.php‎

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

33
namespace Tests\Feature\ImportExport;
44

5+
use ProcessMaker\Exception\PackageNotInstalledException;
56
use ProcessMaker\ImportExport\Dependent;
67
use ProcessMaker\ImportExport\Exporter;
78
use ProcessMaker\ImportExport\Exporters\ScreenExporter;
@@ -130,4 +131,44 @@ public function testGetProcessManager()
130131
$processManager = $payload['export'][$process->uuid]['process_manager'];
131132
$this->assertEquals('John Doe', $processManager);
132133
}
134+
135+
/**
136+
* @dataProvider exporterClassProvider
137+
*/
138+
public function testExceptionThrownIfPackageNotInstalled(string $exporterClass)
139+
{
140+
$this->expectException(PackageNotInstalledException::class);
141+
$this->expectExceptionMessage('Can not import because My Foo Package is not installed.');
142+
143+
$payload = [
144+
'123abc' => [
145+
'exporter' => $exporterClass,
146+
'dependents' => [],
147+
],
148+
];
149+
Manifest::fromArray($payload, new Options([]));
150+
}
151+
152+
public function exporterClassProvider(): array
153+
{
154+
return [
155+
['ProcessMaker\Packages\Connectors\MyFooPackage\ImportExport\SomeExporter'],
156+
['ProcessMaker\Package\MyFooPackage\ImportExport\SomeExporter'],
157+
['ProcessMaker\Plugins\MyFooPackage\ImportExport\SomeExporter'],
158+
];
159+
}
160+
161+
public function testExceptionThrownIfUnknownPackageMatch()
162+
{
163+
$this->expectException(PackageNotInstalledException::class);
164+
$this->expectExceptionMessage('Some\Other\Class not found');
165+
166+
$payload = [
167+
'123abc' => [
168+
'exporter' => 'Some\Other\Class',
169+
'dependents' => [],
170+
],
171+
];
172+
Manifest::fromArray($payload, new Options([]));
173+
}
133174
}

0 commit comments

Comments
 (0)