From 90675b2bd33bbce4326a78c17ffe4f74165234da Mon Sep 17 00:00:00 2001 From: MolbioUnige Date: Thu, 19 Sep 2024 15:05:33 +0200 Subject: [PATCH] Solves issue #607 (#608) Fix exception with CakePHP 5.1 Closes #607 --- src/Model/Behavior/UploadBehavior.php | 4 +++- .../Model/Behavior/UploadBehaviorTest.php | 18 +++++++++++++++--- tests/schema.php | 1 + 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Model/Behavior/UploadBehavior.php b/src/Model/Behavior/UploadBehavior.php index 583f12f..0a11c06 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -55,7 +55,9 @@ public function initialize(array $config): void $schema = $this->_table->getSchema(); /** @var string $field */ foreach (array_keys($this->getConfig()) as $field) { - $schema->setColumnType($field, 'upload.file'); + if ($schema->hasColumn($field)) { + $schema->setColumnType($field, 'upload.file'); + } } $this->_table->setSchema($schema); } diff --git a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php index 465e23a..14b4845 100644 --- a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php @@ -68,9 +68,13 @@ public function testInitialize() { $table = $this->getMockBuilder('Cake\ORM\Table')->getMock(); $schema = $this->getMockBuilder('Cake\Database\Schema\TableSchema') - ->onlyMethods(['setColumnType']) + ->onlyMethods(['setColumnType', 'hasColumn']) ->disableOriginalConstructor() ->getMock(); + $schema->expects($this->once()) + ->method('hasColumn') + ->with('field') + ->willReturn(true); $schema->expects($this->once()) ->method('setColumnType') ->with('field', 'upload.file'); @@ -110,9 +114,13 @@ public function testInitializeIndexedConfig() $settings = ['field']; $table = $this->getMockBuilder('Cake\ORM\Table')->getMock(); $schema = $this->getMockBuilder('Cake\Database\Schema\TableSchema') - ->onlyMethods(['setColumnType']) + ->onlyMethods(['setColumnType', 'hasColumn']) ->disableOriginalConstructor() ->getMock(); + $schema->expects($this->once()) + ->method('hasColumn') + ->with('field') + ->willReturn(true); $schema->expects($this->once()) ->method('setColumnType') ->with('field', 'upload.file'); @@ -142,9 +150,13 @@ public function testInitializeAddBehaviorOptionsInterfaceConfig() ]; $table = $this->getMockBuilder('Cake\ORM\Table')->getMock(); $schema = $this->getMockBuilder('Cake\Database\Schema\TableSchema') - ->onlyMethods(['setColumnType']) + ->onlyMethods(['setColumnType', 'hasColumn']) ->disableOriginalConstructor() ->getMock(); + $schema->expects($this->once()) + ->method('hasColumn') + ->with('field') + ->willReturn(true); $schema->expects($this->once()) ->method('setColumnType') ->with('field', 'upload.file'); diff --git a/tests/schema.php b/tests/schema.php index 37da7c8..9eca1f5 100644 --- a/tests/schema.php +++ b/tests/schema.php @@ -7,6 +7,7 @@ 'columns' => [ 'id' => ['type' => 'integer'], 'filename' => ['type' => 'string'], + 'field' => ['type' => 'string'], 'created' => ['type' => 'datetime', 'null' => true], ], 'constraints' => [