-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ZIP Exports: Added ID checks and testing to validator
- Loading branch information
1 parent
c2c64e2
commit e2f6e50
Showing
10 changed files
with
130 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
namespace BookStack\Exports\ZipExports; | ||
|
||
use Closure; | ||
use Illuminate\Contracts\Validation\ValidationRule; | ||
|
||
class ZipUniqueIdRule implements ValidationRule | ||
{ | ||
public function __construct( | ||
protected ZipValidationHelper $context, | ||
protected string $modelType, | ||
) { | ||
} | ||
|
||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function validate(string $attribute, mixed $value, Closure $fail): void | ||
{ | ||
if ($this->context->hasIdBeenUsed($this->modelType, $value)) { | ||
$fail('validation.zip_unique')->translate(['attribute' => $attribute]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
namespace Tests\Exports; | ||
|
||
use BookStack\Entities\Models\Book; | ||
use BookStack\Entities\Models\Chapter; | ||
use BookStack\Entities\Models\Page; | ||
use BookStack\Exports\ZipExports\ZipExportReader; | ||
use BookStack\Exports\ZipExports\ZipExportValidator; | ||
use BookStack\Exports\ZipExports\ZipImportRunner; | ||
use BookStack\Uploads\Image; | ||
use Tests\TestCase; | ||
|
||
class ZipExportValidatorTests extends TestCase | ||
{ | ||
protected array $filesToRemove = []; | ||
|
||
protected function tearDown(): void | ||
{ | ||
foreach ($this->filesToRemove as $file) { | ||
unlink($file); | ||
} | ||
|
||
parent::tearDown(); | ||
} | ||
|
||
protected function getValidatorForData(array $zipData, array $files = []): ZipExportValidator | ||
{ | ||
$upload = ZipTestHelper::zipUploadFromData($zipData, $files); | ||
$path = $upload->getRealPath(); | ||
$this->filesToRemove[] = $path; | ||
$reader = new ZipExportReader($path); | ||
return new ZipExportValidator($reader); | ||
} | ||
|
||
public function test_ids_have_to_be_unique() | ||
{ | ||
$validator = $this->getValidatorForData([ | ||
'book' => [ | ||
'id' => 4, | ||
'name' => 'My book', | ||
'pages' => [ | ||
[ | ||
'id' => 4, | ||
'name' => 'My page', | ||
'markdown' => 'hello', | ||
'attachments' => [ | ||
['id' => 4, 'name' => 'Attachment A', 'link' => 'https://example.com'], | ||
['id' => 4, 'name' => 'Attachment B', 'link' => 'https://example.com'] | ||
], | ||
'images' => [ | ||
['id' => 4, 'name' => 'Image A', 'type' => 'gallery', 'file' => 'cat'], | ||
['id' => 4, 'name' => 'Image b', 'type' => 'gallery', 'file' => 'cat'], | ||
], | ||
], | ||
['id' => 4, 'name' => 'My page', 'markdown' => 'hello'], | ||
], | ||
'chapters' => [ | ||
['id' => 4, 'name' => 'Chapter 1'], | ||
['id' => 4, 'name' => 'Chapter 2'] | ||
] | ||
] | ||
], ['cat' => $this->files->testFilePath('test-image.png')]); | ||
|
||
$results = $validator->validate(); | ||
$this->assertCount(4, $results); | ||
|
||
$expectedMessage = 'The id must be unique for the object type within the ZIP.'; | ||
$this->assertEquals($expectedMessage, $results['book.pages.0.attachments.1.id']); | ||
$this->assertEquals($expectedMessage, $results['book.pages.0.images.1.id']); | ||
$this->assertEquals($expectedMessage, $results['book.pages.1.id']); | ||
$this->assertEquals($expectedMessage, $results['book.chapters.1.id']); | ||
} | ||
} |