Skip to content

Commit 7eca58e

Browse files
Validating the field value in afterDelete
1 parent ffd0f86 commit 7eca58e

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

src/Model/Behavior/UploadBehavior.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public function afterDelete(EventInterface $event, EntityInterface $entity, Arra
163163
$result = true;
164164

165165
foreach ($this->getConfig(null, []) as $field => $settings) {
166-
if (in_array($field, $this->protectedFieldNames) || Hash::get($settings, 'keepFilesOnDelete', true)) {
166+
if (in_array($field, $this->protectedFieldNames) || Hash::get($settings, 'keepFilesOnDelete', true) || $entity->get($field) === null) {
167167
continue;
168168
}
169169

tests/TestCase/Model/Behavior/UploadBehaviorTest.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ public function testAfterDeleteUsesPathProcessorToDetectPathToTheFile()
524524
->with('dir')
525525
->will($this->returnValue(false));
526526

527-
$this->entity->expects($this->exactly(2))
527+
$this->entity->expects($this->exactly(3))
528528
->method('get')
529529
->with('field')
530530
->will($this->returnValue($field));
@@ -601,7 +601,7 @@ public function testAfterDeleteNoDeleteCallback()
601601
$this->configOk['field']['deleteCallback'] = null;
602602

603603
$behavior->setConfig($this->configOk);
604-
$this->entity->expects($this->exactly(2))
604+
$this->entity->expects($this->exactly(3))
605605
->method('get')
606606
->with('field')
607607
->will($this->returnValue($field));
@@ -642,7 +642,7 @@ public function testAfterDeleteUsesDeleteCallback()
642642
};
643643

644644
$behavior->setConfig($this->configOk);
645-
$this->entity->expects($this->exactly(4))
645+
$this->entity->expects($this->exactly(5))
646646
->method('get')
647647
->with('field')
648648
->will($this->returnValue($field));
@@ -687,6 +687,26 @@ public function testAfterDeleteWithProtectedFieldName()
687687
$this->assertTrue($behavior->afterDelete(new Event('fake.event'), $this->entity, new ArrayObject()));
688688
}
689689

690+
public function testAfterDeleteWithNullableFileField()
691+
{
692+
$methods = array_diff($this->behaviorMethods, ['afterDelete', 'config', 'setConfig', 'getConfig']);
693+
$behavior = $this->getMockBuilder('Josegonzalez\Upload\Model\Behavior\UploadBehavior')
694+
->onlyMethods($methods)
695+
->setConstructorArgs([$this->table, $this->settings])
696+
->getMock();
697+
$behavior->setConfig($this->configOk);
698+
699+
$this->entity->expects($this->once())
700+
->method('get')
701+
->with('field')
702+
->will($this->returnValue(null));
703+
704+
$behavior->expects($this->never())
705+
->method('getPathProcessor');
706+
707+
$behavior->afterDelete(new Event('fake.event'), $this->entity, new ArrayObject());
708+
}
709+
690710
public function testGetWriter()
691711
{
692712
$processor = $this->behavior->getWriter($this->entity, new UploadedFile(fopen('php://temp', 'rw+'), 1, UPLOAD_ERR_OK, 'file.txt'), 'field', []);
@@ -783,12 +803,6 @@ public function testGetPathProcessor()
783803
$this->assertInstanceOf('Josegonzalez\Upload\File\Path\ProcessorInterface', $processor);
784804
}
785805

786-
public function testGetPathProcessorWithNoFile()
787-
{
788-
$processor = $this->behavior->getPathProcessor($this->entity, null, 'field', []);
789-
$this->assertInstanceOf('Josegonzalez\Upload\File\Path\ProcessorInterface', $processor);
790-
}
791-
792806
public function testNameCallback()
793807
{
794808
$table = $this->getTableLocator()->get('Files');

0 commit comments

Comments
 (0)