From 06623c9fbaa88e129eacf4f368b768cffbfb8717 Mon Sep 17 00:00:00 2001 From: Nicolas Roggli Date: Wed, 18 Sep 2024 13:09:41 +0200 Subject: [PATCH 1/4] Solves issue #607 --- src/Model/Behavior/UploadBehavior.php | 17 ++++++++++++++++- .../Model/Behavior/UploadBehaviorTest.php | 18 +++++++++--------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/Model/Behavior/UploadBehavior.php b/src/Model/Behavior/UploadBehavior.php index 583f12f..f283cb3 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -8,6 +8,7 @@ use Cake\Datasource\EntityInterface; use Cake\Event\EventInterface; use Cake\ORM\Behavior; +use Cake\ORM\Query\SelectQuery; use Cake\Utility\Hash; use Josegonzalez\Upload\File\Path\DefaultProcessor; use Josegonzalez\Upload\File\Path\ProcessorInterface; @@ -55,7 +56,7 @@ 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'); + $schema->addColumn($field, ['type' => 'upload.file']); } $this->_table->setSchema($schema); } @@ -92,6 +93,20 @@ public function beforeMarshal(EventInterface $event, ArrayObject $data, ArrayObj } } + /** + * Prevents virtual fields from being added to the query + * + * @param EventInterface $event + * @param SelectQuery $query + * @param ArrayObject $options + * @param bool $primary + * @return void + */ + public function beforeFind(EventInterface $event, SelectQuery $query, ArrayObject $options, bool $primary) + { + $query->selectAllExcept($this->_table, array_keys($this->getConfig())); + } + /** * Modifies the entity before it is saved so that uploaded file data is persisted * in the database too. diff --git a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php index 465e23a..0bb7e90 100644 --- a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php @@ -68,12 +68,12 @@ public function testInitialize() { $table = $this->getMockBuilder('Cake\ORM\Table')->getMock(); $schema = $this->getMockBuilder('Cake\Database\Schema\TableSchema') - ->onlyMethods(['setColumnType']) + ->onlyMethods(['addColumn']) ->disableOriginalConstructor() ->getMock(); $schema->expects($this->once()) - ->method('setColumnType') - ->with('field', 'upload.file'); + ->method('addColumn') + ->with('field', ['type' => 'upload.file']); $table->expects($this->any()) ->method('getSchema') ->will($this->returnValue($schema)); @@ -110,12 +110,12 @@ public function testInitializeIndexedConfig() $settings = ['field']; $table = $this->getMockBuilder('Cake\ORM\Table')->getMock(); $schema = $this->getMockBuilder('Cake\Database\Schema\TableSchema') - ->onlyMethods(['setColumnType']) + ->onlyMethods(['addColumn']) ->disableOriginalConstructor() ->getMock(); $schema->expects($this->once()) - ->method('setColumnType') - ->with('field', 'upload.file'); + ->method('addColumn') + ->with('field', ['type' => 'upload.file']); $table->expects($this->any()) ->method('getSchema') ->will($this->returnValue($schema)); @@ -142,12 +142,12 @@ public function testInitializeAddBehaviorOptionsInterfaceConfig() ]; $table = $this->getMockBuilder('Cake\ORM\Table')->getMock(); $schema = $this->getMockBuilder('Cake\Database\Schema\TableSchema') - ->onlyMethods(['setColumnType']) + ->onlyMethods(['addColumn']) ->disableOriginalConstructor() ->getMock(); $schema->expects($this->once()) - ->method('setColumnType') - ->with('field', 'upload.file'); + ->method('addColumn') + ->with('field', ['type' => 'upload.file']); $table->expects($this->any()) ->method('getSchema') ->will($this->returnValue($schema)); From bd75476ad3e115437b1251b22666031de78fd4a2 Mon Sep 17 00:00:00 2001 From: Nicolas Roggli Date: Wed, 18 Sep 2024 14:17:45 +0200 Subject: [PATCH 2/4] Fixes cs and stan errors --- src/Model/Behavior/UploadBehavior.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Model/Behavior/UploadBehavior.php b/src/Model/Behavior/UploadBehavior.php index f283cb3..08a1df9 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -96,15 +96,15 @@ public function beforeMarshal(EventInterface $event, ArrayObject $data, ArrayObj /** * Prevents virtual fields from being added to the query * - * @param EventInterface $event - * @param SelectQuery $query - * @param ArrayObject $options + * @param \Cake\Event\EventInterface $event + * @param \Cake\ORM\Query\SelectQuery $query + * @param \ArrayObject $options * @param bool $primary * @return void */ - public function beforeFind(EventInterface $event, SelectQuery $query, ArrayObject $options, bool $primary) + public function beforeFind(EventInterface $event, SelectQuery $query, ArrayObject $options, bool $primary): void { - $query->selectAllExcept($this->_table, array_keys($this->getConfig())); + $query->selectAllExcept($this->_table, array_map(fn ($f) => (string)$f, array_keys($this->getConfig()))); } /** From 49f237d6fb53b640ffad4d6f4d078197bc4589f1 Mon Sep 17 00:00:00 2001 From: Nicolas Roggli Date: Wed, 18 Sep 2024 15:40:16 +0200 Subject: [PATCH 3/4] Simplified as spotted --- src/Model/Behavior/UploadBehavior.php | 19 +++------------ .../Model/Behavior/UploadBehaviorTest.php | 24 +++++++++---------- 2 files changed, 15 insertions(+), 28 deletions(-) diff --git a/src/Model/Behavior/UploadBehavior.php b/src/Model/Behavior/UploadBehavior.php index 08a1df9..0a11c06 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -8,7 +8,6 @@ use Cake\Datasource\EntityInterface; use Cake\Event\EventInterface; use Cake\ORM\Behavior; -use Cake\ORM\Query\SelectQuery; use Cake\Utility\Hash; use Josegonzalez\Upload\File\Path\DefaultProcessor; use Josegonzalez\Upload\File\Path\ProcessorInterface; @@ -56,7 +55,9 @@ public function initialize(array $config): void $schema = $this->_table->getSchema(); /** @var string $field */ foreach (array_keys($this->getConfig()) as $field) { - $schema->addColumn($field, ['type' => 'upload.file']); + if ($schema->hasColumn($field)) { + $schema->setColumnType($field, 'upload.file'); + } } $this->_table->setSchema($schema); } @@ -93,20 +94,6 @@ public function beforeMarshal(EventInterface $event, ArrayObject $data, ArrayObj } } - /** - * Prevents virtual fields from being added to the query - * - * @param \Cake\Event\EventInterface $event - * @param \Cake\ORM\Query\SelectQuery $query - * @param \ArrayObject $options - * @param bool $primary - * @return void - */ - public function beforeFind(EventInterface $event, SelectQuery $query, ArrayObject $options, bool $primary): void - { - $query->selectAllExcept($this->_table, array_map(fn ($f) => (string)$f, array_keys($this->getConfig()))); - } - /** * Modifies the entity before it is saved so that uploaded file data is persisted * in the database too. diff --git a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php index 0bb7e90..b8a4969 100644 --- a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php @@ -68,12 +68,12 @@ public function testInitialize() { $table = $this->getMockBuilder('Cake\ORM\Table')->getMock(); $schema = $this->getMockBuilder('Cake\Database\Schema\TableSchema') - ->onlyMethods(['addColumn']) + ->onlyMethods(['setColumnType']) ->disableOriginalConstructor() ->getMock(); - $schema->expects($this->once()) - ->method('addColumn') - ->with('field', ['type' => 'upload.file']); + $schema->expects($this->any()) + ->method('setColumnType') + ->with('field', 'upload.file'); $table->expects($this->any()) ->method('getSchema') ->will($this->returnValue($schema)); @@ -110,12 +110,12 @@ public function testInitializeIndexedConfig() $settings = ['field']; $table = $this->getMockBuilder('Cake\ORM\Table')->getMock(); $schema = $this->getMockBuilder('Cake\Database\Schema\TableSchema') - ->onlyMethods(['addColumn']) + ->onlyMethods(['setColumnType']) ->disableOriginalConstructor() ->getMock(); - $schema->expects($this->once()) - ->method('addColumn') - ->with('field', ['type' => 'upload.file']); + $schema->expects($this->any()) + ->method('setColumnType') + ->with('field', 'upload.file'); $table->expects($this->any()) ->method('getSchema') ->will($this->returnValue($schema)); @@ -142,12 +142,12 @@ public function testInitializeAddBehaviorOptionsInterfaceConfig() ]; $table = $this->getMockBuilder('Cake\ORM\Table')->getMock(); $schema = $this->getMockBuilder('Cake\Database\Schema\TableSchema') - ->onlyMethods(['addColumn']) + ->onlyMethods(['setColumnType']) ->disableOriginalConstructor() ->getMock(); - $schema->expects($this->once()) - ->method('addColumn') - ->with('field', ['type' => 'upload.file']); + $schema->expects($this->any()) + ->method('setColumnType') + ->with('field', 'upload.file'); $table->expects($this->any()) ->method('getSchema') ->will($this->returnValue($schema)); From d2d6367d9a632dcf6eb13d5b4a710f5d3e912e74 Mon Sep 17 00:00:00 2001 From: Nicolas Roggli Date: Thu, 19 Sep 2024 14:40:56 +0200 Subject: [PATCH 4/4] Added field column, mocked hasColumn --- .../Model/Behavior/UploadBehaviorTest.php | 24 ++++++++++++++----- tests/schema.php | 1 + 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php index b8a4969..14b4845 100644 --- a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php @@ -68,10 +68,14 @@ 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->any()) + $schema->expects($this->once()) + ->method('hasColumn') + ->with('field') + ->willReturn(true); + $schema->expects($this->once()) ->method('setColumnType') ->with('field', 'upload.file'); $table->expects($this->any()) @@ -110,10 +114,14 @@ 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->any()) + $schema->expects($this->once()) + ->method('hasColumn') + ->with('field') + ->willReturn(true); + $schema->expects($this->once()) ->method('setColumnType') ->with('field', 'upload.file'); $table->expects($this->any()) @@ -142,10 +150,14 @@ 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->any()) + $schema->expects($this->once()) + ->method('hasColumn') + ->with('field') + ->willReturn(true); + $schema->expects($this->once()) ->method('setColumnType') ->with('field', 'upload.file'); $table->expects($this->any()) 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' => [