Skip to content

Commit

Permalink
Merge branch '5.1.x' of github.com:baserproject/basercms into 5.1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuring committed Sep 18, 2024
2 parents 05bf299 + 5ee8012 commit 3356d02
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
1 change: 1 addition & 0 deletions plugins/baser-core/src/Utility/BcFileUploader.php
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,7 @@ public function resetUploaded()
* @param EntityInterface $entity
* @checked
* @noTodo
* @unitTest
*/
public function rollbackFile(EntityInterface $entity)
{
Expand Down
104 changes: 104 additions & 0 deletions plugins/baser-core/tests/TestCase/Utility/BcFileUploaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1269,4 +1269,108 @@ public function testIsUploadedAndReset()
$this->BcFileUploader->resetUploaded();
$this->assertFalse($this->BcFileUploader->isUploaded());
}

/**
* test rollbackFile with errors
* @param array $initialData
* @param array $originalData
* @param array $errors
* @param array $expected
* @dataProvider rollbackFileDataProvider
*/
public function testRollbackFileWithErrors($initialData, $originalData, $errors, $expected)
{
//create BcFileUploader
$BcFileUploader = new BcFileUploader();
$BcFileUploader->settings['fields'] = [
['name' => 'image'],
['name' => 'document']
];

//create Entity
$entity = new Entity($originalData);
$entity->clean();
//Set new data
$entity->set($initialData);

//set errors
if (isset($errors['image'])) {
$entity->setError('image', $errors['image']);
}
if (isset($errors['document'])) {
$entity->setError('document', $errors['document']);
}

//check has error
$this->assertTrue($entity->hasErrors());

$BcFileUploader->rollbackFile($entity);

//Check data after rollback
$this->assertEquals($expected['image'], $entity->get('image'));
$this->assertEquals($expected['document'], $entity->get('document'));

//check errors after rollback
if (isset($errors['image'])) {
$this->assertEquals($errors['image'], $entity->getError('image'));
}
if (isset($errors['document'])) {
$this->assertEquals($errors['document'], $entity->getError('document'));
}
}

public static function rollbackFileDataProvider()
{
return [
[
['image' => 'new_image.jpg', 'document' => 'new_document.pdf'],
['image' => 'original_image.jpg', 'document' => 'original_document.pdf'],
['image' => ['Error message for image'], 'document' => ['Error message for document']],
['image' => 'original_image.jpg', 'document' => 'original_document.pdf']
]
];
}

/**
* test rollbackFile without errors
* @param array $initialData
* @param array $originalData
* @param array $expected
* @dataProvider rollbackFileNoErrorsDataProvider
*/
public function testRollbackFileWithoutErrors($initialData, $originalData, $expected)
{
//create BcFileUploader
$BcFileUploader = new BcFileUploader();
$BcFileUploader->settings['fields'] = [
['name' => 'image'],
['name' => 'document']
];

//create Entity
$entity = new Entity($originalData);
$entity->clean();
//set new data
$entity->set($initialData);

//check has error
$this->assertFalse($entity->hasErrors());

$BcFileUploader->rollbackFile($entity);

//Check data after rollback
$this->assertEquals($expected['image'], $entity->get('image'));
$this->assertEquals($expected['document'], $entity->get('document'));
}

public static function rollbackFileNoErrorsDataProvider()
{
return [
[
['image' => 'new_image.jpg', 'document' => 'new_document.pdf'],
['image' => 'original_image.jpg', 'document' => 'original_document.pdf'],
['image' => 'new_image.jpg', 'document' => 'new_document.pdf']
],
];
}
}

0 comments on commit 3356d02

Please sign in to comment.