From c9105427a2a7e540cc0f1c51f00b87c22a2d9cea Mon Sep 17 00:00:00 2001 From: AlexAlexandru Date: Mon, 6 Aug 2018 21:27:59 +0300 Subject: [PATCH 01/12] add base64 upload --- src/File/Path/Base64Processor.php | 39 ++++++++++++++++ src/File/Transformer/Base64Transformer.php | 36 +++++++++++++++ src/Model/Behavior/UploadBehavior.php | 41 +++++++++++++++-- src/UploadValidator/Base64UploadValidator.php | 19 ++++++++ .../DefaultUploadValidator.php | 46 +++++++++++++++++++ .../UploadValidatorInterface.php | 23 ++++++++++ 6 files changed, 199 insertions(+), 5 deletions(-) create mode 100644 src/File/Path/Base64Processor.php create mode 100644 src/File/Transformer/Base64Transformer.php create mode 100644 src/UploadValidator/Base64UploadValidator.php create mode 100644 src/UploadValidator/DefaultUploadValidator.php create mode 100644 src/UploadValidator/UploadValidatorInterface.php diff --git a/src/File/Path/Base64Processor.php b/src/File/Path/Base64Processor.php new file mode 100644 index 00000000..312953a4 --- /dev/null +++ b/src/File/Path/Base64Processor.php @@ -0,0 +1,39 @@ +settings, 'nameCallback', null); + $extension = Hash::get($this->settings, 'base64_extension', '.png'); + if (is_callable($processor)) { + $numberOfParameters = (new \ReflectionFunction($processor))->getNumberOfParameters(); + if ($numberOfParameters == 2) { + return $processor($this->data, $this->settings); + } + + return $processor($this->table, $this->entity, $this->data, $this->field, $this->settings); + } + + return Text::uuid() . "$extension"; + } +} \ No newline at end of file diff --git a/src/File/Transformer/Base64Transformer.php b/src/File/Transformer/Base64Transformer.php new file mode 100644 index 00000000..1b828306 --- /dev/null +++ b/src/File/Transformer/Base64Transformer.php @@ -0,0 +1,36 @@ + 'file.pdf', + * '/tmp/path/to/file/on/disk-2' => 'file-preview.png', + * ] + * + * @return array key/value pairs of temp files mapping to their names + */ + public function transform() + { + $decoded = base64_decode($this->data['data']); + $tmp = tempnam(sys_get_temp_dir(), 'upload'); + file_put_contents($tmp,$decoded); + return [ + $tmp => $this->data['name'], + ]; + } +} \ No newline at end of file diff --git a/src/Model/Behavior/UploadBehavior.php b/src/Model/Behavior/UploadBehavior.php index 876bc5ff..4785df48 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -87,7 +87,8 @@ public function beforeSave(Event $event, Entity $entity, ArrayObject $options) continue; } - if (Hash::get((array)$entity->get($field), 'error') !== UPLOAD_ERR_OK) { + $uploadValidator = $this->getUploadValidator($entity, $settings, $field); + if ($uploadValidator->hasUploadFailed()) { if (Hash::get($settings, 'restoreValueOnFailure', true)) { $entity->set($field, $entity->getOriginal($field)); $entity->setDirty($field, false); @@ -99,7 +100,14 @@ public function beforeSave(Event $event, Entity $entity, ArrayObject $options) $path = $this->getPathProcessor($entity, $data, $field, $settings); $basepath = $path->basepath(); $filename = $path->filename(); - $data['name'] = $filename; + if (is_string($data)) { + $temp = []; + $temp['name'] = $filename; + $temp['data'] = $data; + $data = $temp; + } else { + $data['name'] = $filename; + } $files = $this->constructFiles($entity, $data, $field, $settings, $basepath); $writer = $this->getWriter($entity, $data, $field, $settings); @@ -111,8 +119,10 @@ public function beforeSave(Event $event, Entity $entity, ArrayObject $options) $entity->set($field, $filename); $entity->set(Hash::get($settings, 'fields.dir', 'dir'), $basepath); - $entity->set(Hash::get($settings, 'fields.size', 'size'), $data['size']); - $entity->set(Hash::get($settings, 'fields.type', 'type'), $data['type']); + if (!isset($temp)) { + $entity->set(Hash::get($settings, 'fields.size', 'size'), $data['size']); + $entity->set(Hash::get($settings, 'fields.type', 'type'), $data['type']); + } } } @@ -205,6 +215,27 @@ public function getWriter(Entity $entity, $data, $field, $settings) )); } + /** + * Retrieves an instance of a validator that validates that the current upload has succeded + * + * @param \Cake\ORM\Entity $entity an entity + * @param array $data the data being submitted for a save + * @return \Josegonzalez\Upload\File\Path\AbstractProcessor + */ + public function getUploadValidator(Entity $entity, $settings , $field) + { + $default = 'Josegonzalez\Upload\UploadValidator\DefaultUploadValidator'; + $uploadValidatorClass = Hash::get($settings, 'uploadValidator', $default); + if (is_a($uploadValidatorClass, 'Josegonzalez\Upload\UploadValidator\UploadValidatorInterface', true)) { + return new $uploadValidatorClass($entity, $field); + } + + throw new UnexpectedValueException(sprintf( + "'uploadValidator' not set to instance of UploadValidatorInterface: %s", + $uploadValidatorClass + )); + } + /** * Creates a set of files from the initial data and returns them as key/value * pairs, where the path on disk maps to name which each file should have. @@ -253,4 +284,4 @@ public function constructFiles(Entity $entity, $data, $field, $settings, $basepa return $results; } -} +} \ No newline at end of file diff --git a/src/UploadValidator/Base64UploadValidator.php b/src/UploadValidator/Base64UploadValidator.php new file mode 100644 index 00000000..adeee261 --- /dev/null +++ b/src/UploadValidator/Base64UploadValidator.php @@ -0,0 +1,19 @@ +entity->get($this->field), true); + } +} diff --git a/src/UploadValidator/DefaultUploadValidator.php b/src/UploadValidator/DefaultUploadValidator.php new file mode 100644 index 00000000..868d15a4 --- /dev/null +++ b/src/UploadValidator/DefaultUploadValidator.php @@ -0,0 +1,46 @@ +entity = $entity; + $this->field = $field; + } + + /** + * Check's data for any upload errors. + * pairs, where the path on disk maps to name which each file should have. + * + * @return boolean `true` if upload failed + */ + public function hasUploadFailed() + { + return Hash::get((array)$this->entity->get($this->field), 'error') !== UPLOAD_ERR_OK; + } +} diff --git a/src/UploadValidator/UploadValidatorInterface.php b/src/UploadValidator/UploadValidatorInterface.php new file mode 100644 index 00000000..292ef553 --- /dev/null +++ b/src/UploadValidator/UploadValidatorInterface.php @@ -0,0 +1,23 @@ + Date: Mon, 6 Aug 2018 21:32:03 +0300 Subject: [PATCH 02/12] newline --- src/Model/Behavior/UploadBehavior.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/Behavior/UploadBehavior.php b/src/Model/Behavior/UploadBehavior.php index 4785df48..f45118d8 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -284,4 +284,4 @@ public function constructFiles(Entity $entity, $data, $field, $settings, $basepa return $results; } -} \ No newline at end of file +} From 6c83c5ccfbaa07162a62f9d817cbc95636c908d3 Mon Sep 17 00:00:00 2001 From: AlexAlexandru Date: Mon, 6 Aug 2018 21:41:45 +0300 Subject: [PATCH 03/12] newline --- src/File/Transformer/Base64Transformer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/File/Transformer/Base64Transformer.php b/src/File/Transformer/Base64Transformer.php index 1b828306..e0c0ec6e 100644 --- a/src/File/Transformer/Base64Transformer.php +++ b/src/File/Transformer/Base64Transformer.php @@ -33,4 +33,4 @@ public function transform() $tmp => $this->data['name'], ]; } -} \ No newline at end of file +} From a0573c0ee6d6a85aace4d1c736bab1fc2a14d71f Mon Sep 17 00:00:00 2001 From: AlexAlexandru Date: Tue, 7 Aug 2018 22:31:35 +0300 Subject: [PATCH 04/12] added unit tests --- src/File/Path/Base64Processor.php | 7 --- src/File/Transformer/Base64Transformer.php | 25 ++++---- src/Model/Behavior/UploadBehavior.php | 2 +- .../File/Path/Base64ProcessorTest.php | 45 ++++++++++++++ .../Transformer/Base64TransformerTest.php | 48 +++++++++++++++ .../Model/Behavior/UploadBehaviorTest.php | 34 +++++++++++ .../Base64UploadValidatorTest.php | 39 +++++++++++++ .../DefaultUploadValidatorTest.php | 58 +++++++++++++++++++ 8 files changed, 240 insertions(+), 18 deletions(-) create mode 100644 tests/TestCase/File/Path/Base64ProcessorTest.php create mode 100644 tests/TestCase/File/Transformer/Base64TransformerTest.php create mode 100644 tests/TestCase/UploadValidator/Base64UploadValidatorTest.php create mode 100644 tests/TestCase/UploadValidator/DefaultUploadValidatorTest.php diff --git a/src/File/Path/Base64Processor.php b/src/File/Path/Base64Processor.php index 312953a4..6c09ef66 100644 --- a/src/File/Path/Base64Processor.php +++ b/src/File/Path/Base64Processor.php @@ -1,11 +1,4 @@ data['data']); - $tmp = tempnam(sys_get_temp_dir(), 'upload'); - file_put_contents($tmp,$decoded); + file_put_contents($this->getPath(),$decoded); return [ - $tmp => $this->data['name'], + $this->getPath() => $this->data['name'], ]; } + + public function setPath($path = '') { + if (empty($path)) { + $this->path = tempnam(sys_get_temp_dir(), 'upload'); + } + $this->path = $path; + } + + public function getPath() { + return $this->path; + } } diff --git a/src/Model/Behavior/UploadBehavior.php b/src/Model/Behavior/UploadBehavior.php index f45118d8..4a3799e0 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -220,7 +220,7 @@ public function getWriter(Entity $entity, $data, $field, $settings) * * @param \Cake\ORM\Entity $entity an entity * @param array $data the data being submitted for a save - * @return \Josegonzalez\Upload\File\Path\AbstractProcessor + * @return \Josegonzalez\Upload\UploadValidator\UploadValidatorInterface */ public function getUploadValidator(Entity $entity, $settings , $field) { diff --git a/tests/TestCase/File/Path/Base64ProcessorTest.php b/tests/TestCase/File/Path/Base64ProcessorTest.php new file mode 100644 index 00000000..5b5fbdc8 --- /dev/null +++ b/tests/TestCase/File/Path/Base64ProcessorTest.php @@ -0,0 +1,45 @@ +getMockBuilder('Cake\ORM\Entity')->getMock(); + $table = $this->getMockBuilder('Cake\ORM\Table')->getMock(); + $data = ['name' => 'filename']; + $field = 'field'; + $settings = []; + $processor = new Base64Processor($table, $entity, $data, $field, $settings); + $this->assertInstanceOf('Josegonzalez\Upload\File\Path\ProcessorInterface', $processor); + } + + public function testRandomFileNameDefaultExtension() + { + $entity = $this->getMockBuilder('Cake\ORM\Entity')->getMock(); + $table = $this->getMockBuilder('Cake\ORM\Table')->getMock(); + $data = ['name' => 'filename']; + $field = 'field'; + $settings = []; + $processor = new Base64Processor($table, $entity, $data, $field, $settings); + $fileName = $processor->filename(); + $found = strpos($fileName, '.png'); + $this->assertNotFalse($found); + } + + public function testRandomFileNameCustomExtension() + { + $entity = $this->getMockBuilder('Cake\ORM\Entity')->getMock(); + $table = $this->getMockBuilder('Cake\ORM\Table')->getMock(); + $data = ['name' => 'filename']; + $field = 'field'; + $settings = ['base64_extension' => '.cake']; + $processor = new Base64Processor($table, $entity, $data, $field, $settings); + $fileName = $processor->filename(); + $found = strpos($fileName, '.cake'); + $this->assertNotFalse($found); + } +} diff --git a/tests/TestCase/File/Transformer/Base64TransformerTest.php b/tests/TestCase/File/Transformer/Base64TransformerTest.php new file mode 100644 index 00000000..4f572f39 --- /dev/null +++ b/tests/TestCase/File/Transformer/Base64TransformerTest.php @@ -0,0 +1,48 @@ +entity = $this->getMockBuilder('Cake\ORM\Entity')->getMock(); + $this->table = $this->getMockBuilder('Cake\ORM\Table')->getMock(); + $this->data = ['data' => 'Y2FrZXBocA==', 'name' => '5a2e69ff-c2c0-44c1-94a7-d791202f0067.txt']; + $this->field = 'field'; + $this->settings = []; + $this->transformer = new Base64Transformer( + $this->table, + $this->entity, + $this->data, + $this->field, + $this->settings + ); + + $this->vfs = new Vfs; + mkdir($this->vfs->path('/tmp')); + file_put_contents($this->vfs->path('/tmp/tempfile'), $this->data['data']); + } + + public function teardown() + { + unset($this->transformer); + } + + public function testTransform() + { + $this->transformer->setPath($this->vfs->path('/tmp/tempfile')); + $expected = [$this->vfs->path('/tmp/tempfile') => '5a2e69ff-c2c0-44c1-94a7-d791202f0067.txt']; + $this->assertEquals($expected, $this->transformer->transform()); + } + + public function testIsTransformerInterface() + { + $this->assertInstanceOf('Josegonzalez\Upload\File\Transformer\TransformerInterface', $this->transformer); + } +} \ No newline at end of file diff --git a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php index e3aaa9aa..91a85446 100644 --- a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php @@ -53,6 +53,10 @@ public function setup() ->setMethods([]) ->setConstructorArgs([$this->table, $this->entity, $this->dataOk, $this->field, $this->settings]) ->getMock(); + $this->uploadValidator = $this->getMockBuilder('Josegonzalez\Upload\UploadValidator\DefaultUploadValidator') + ->setMethods([]) + ->setConstructorArgs([$this->entity, $this->field]) + ->getMock(); $this->behaviorMethods = get_class_methods('Josegonzalez\Upload\Model\Behavior\UploadBehavior'); } @@ -265,6 +269,12 @@ public function testBeforeSaveUploadError() ->method('get') ->with('field') ->will($this->returnValue($this->dataError['field'])); + $behavior->expects($this->any()) + ->method('getUploadValidator') + ->will($this->returnValue($this->uploadValidator)); + $this->uploadValidator->expects($this->any()) + ->method('hasUploadFailed') + ->will($this->returnValue(true)); $this->entity->expects($this->any()) ->method('getOriginal') ->with('field') @@ -293,6 +303,12 @@ public function testBeforeSaveWriteFail() $behavior->expects($this->any()) ->method('getPathProcessor') ->will($this->returnValue($this->processor)); + $behavior->expects($this->any()) + ->method('getUploadValidator') + ->will($this->returnValue($this->uploadValidator)); + $this->uploadValidator->expects($this->any()) + ->method('hasUploadFailed') + ->will($this->returnValue(false)); $behavior->expects($this->any()) ->method('getWriter') ->will($this->returnValue($this->writer)); @@ -318,6 +334,12 @@ public function testBeforeSaveOk() ->method('get') ->with('field') ->will($this->returnValue($this->dataOk['field'])); + $behavior->expects($this->any()) + ->method('getUploadValidator') + ->will($this->returnValue($this->uploadValidator)); + $this->uploadValidator->expects($this->any()) + ->method('hasUploadFailed') + ->will($this->returnValue(false)); $behavior->expects($this->any()) ->method('getPathProcessor') ->will($this->returnValue($this->processor)); @@ -345,6 +367,12 @@ public function testBeforeSaveDoesNotRestoreOriginalValue() ->setConstructorArgs([$this->table, $this->settings]) ->getMock(); $behavior->setConfig($settings); + $behavior->expects($this->any()) + ->method('getUploadValidator') + ->will($this->returnValue($this->uploadValidator)); + $this->uploadValidator->expects($this->any()) + ->method('hasUploadFailed') + ->will($this->returnValue(true)); $this->entity->expects($this->never())->method('getOriginal'); $this->entity->expects($this->never())->method('set'); @@ -362,6 +390,12 @@ public function testBeforeSaveWithProtectedFieldName() ->setConstructorArgs([$this->table, $this->settings]) ->getMock(); $behavior->setConfig($settings); + $behavior->expects($this->any()) + ->method('getUploadValidator') + ->will($this->returnValue($this->uploadValidator)); + $this->uploadValidator->expects($this->any()) + ->method('hasUploadFailed') + ->will($this->returnValue(true)); $this->assertNull($behavior->beforeSave(new Event('fake.event'), $this->entity, new ArrayObject)); } diff --git a/tests/TestCase/UploadValidator/Base64UploadValidatorTest.php b/tests/TestCase/UploadValidator/Base64UploadValidatorTest.php new file mode 100644 index 00000000..0067410a --- /dev/null +++ b/tests/TestCase/UploadValidator/Base64UploadValidatorTest.php @@ -0,0 +1,39 @@ +entity = $this->getMockBuilder('Cake\ORM\Entity')->getMock(); + $this->dataOk = "Y2FrZXBocA=="; + $this->dataError = ' '; + $this->field = 'field'; + $this->base64UploadValidator = new Base64UploadValidator($this->entity, $this->field); + } + + public function testOK() { + $this->entity->expects($this->any()) + ->method('get') + ->with('field') + ->will($this->returnValue($this->dataOk)); + $this->assertFalse($this->$this->base64UploadValidator->hasUploadFailed()); + } + + public function testFail() { + $this->entity->expects($this->any()) + ->method('get') + ->with('field') + ->will($this->returnValue($this->dataError)); + $this->assertTrue($this->$this->base64UploadValidator->hasUploadFailed()); + } + + public function testIsUploadValidatorInterface() + { + $interface = 'Josegonzalez\Upload\UploadValidator\UploadValidatorInterface'; + $this->assertInstanceOf($interface, $this->base64UploadValidator); + } +} \ No newline at end of file diff --git a/tests/TestCase/UploadValidator/DefaultUploadValidatorTest.php b/tests/TestCase/UploadValidator/DefaultUploadValidatorTest.php new file mode 100644 index 00000000..44f81bb6 --- /dev/null +++ b/tests/TestCase/UploadValidator/DefaultUploadValidatorTest.php @@ -0,0 +1,58 @@ +entity = $this->getMockBuilder('Cake\ORM\Entity')->getMock(); + $this->dataOk = [ + 'field' => [ + 'tmp_name' => 'path/to/file', + 'name' => 'derp', + 'error' => UPLOAD_ERR_OK, + 'size' => 1, + 'type' => 'text', + 'keepFilesOnDelete' => false, + 'deleteCallback' => null + ] + ]; + $this->dataError = [ + 'field' => [ + 'tmp_name' => 'path/to/file', + 'name' => 'derp', + 'error' => UPLOAD_ERR_NO_FILE, + 'size' => 0, + 'type' => '', + ] + ]; + $this->field = 'field'; + + $this->defaultUploadValidator = new DefaultUploadValidator($this->entity, $this->field); + } + + public function testOK() { + $this->entity->expects($this->any()) + ->method('get') + ->with('field') + ->will($this->returnValue($this->dataOk['field'])); + $this->assertFalse($this->defaultUploadValidator->hasUploadFailed()); + } + + public function testFail() { + $this->entity->expects($this->any()) + ->method('get') + ->with('field') + ->will($this->returnValue($this->dataError['field'])); + $this->assertTrue($this->defaultUploadValidator->hasUploadFailed()); + } + + public function testIsUploadValidatorInterface() + { + $interface = 'Josegonzalez\Upload\UploadValidator\UploadValidatorInterface'; + $this->assertInstanceOf($interface, $this->defaultUploadValidator); + } +} \ No newline at end of file From af00b227f187d7c0e39f4646e72c33b159592211 Mon Sep 17 00:00:00 2001 From: AlexAlexandru Date: Tue, 7 Aug 2018 22:38:42 +0300 Subject: [PATCH 05/12] fix type from rename --- tests/TestCase/UploadValidator/Base64UploadValidatorTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/TestCase/UploadValidator/Base64UploadValidatorTest.php b/tests/TestCase/UploadValidator/Base64UploadValidatorTest.php index 0067410a..0ce17c9a 100644 --- a/tests/TestCase/UploadValidator/Base64UploadValidatorTest.php +++ b/tests/TestCase/UploadValidator/Base64UploadValidatorTest.php @@ -20,7 +20,7 @@ public function testOK() { ->method('get') ->with('field') ->will($this->returnValue($this->dataOk)); - $this->assertFalse($this->$this->base64UploadValidator->hasUploadFailed()); + $this->assertFalse($this->base64UploadValidator->hasUploadFailed()); } public function testFail() { @@ -28,7 +28,7 @@ public function testFail() { ->method('get') ->with('field') ->will($this->returnValue($this->dataError)); - $this->assertTrue($this->$this->base64UploadValidator->hasUploadFailed()); + $this->assertTrue($this->base64UploadValidator->hasUploadFailed()); } public function testIsUploadValidatorInterface() From 7777cb531e475b82e0efe1fe35aa13a99cbac5ec Mon Sep 17 00:00:00 2001 From: AlexAlexandru Date: Wed, 8 Aug 2018 21:00:37 +0300 Subject: [PATCH 06/12] fixes code sniffer errors --- src/File/Path/Base64Processor.php | 3 +-- src/File/Transformer/Base64Transformer.php | 27 +++++++++++++++---- src/Model/Behavior/UploadBehavior.php | 2 +- src/UploadValidator/Base64UploadValidator.php | 2 +- .../DefaultUploadValidator.php | 2 +- .../UploadValidatorInterface.php | 2 +- .../Transformer/Base64TransformerTest.php | 2 +- .../Base64UploadValidatorTest.php | 8 +++--- .../DefaultUploadValidatorTest.php | 8 +++--- 9 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/File/Path/Base64Processor.php b/src/File/Path/Base64Processor.php index 6c09ef66..33993fa7 100644 --- a/src/File/Path/Base64Processor.php +++ b/src/File/Path/Base64Processor.php @@ -1,7 +1,6 @@ data['data']); - file_put_contents($this->getPath(),$decoded); + file_put_contents($this->getPath(), $decoded); + return [ $this->getPath() => $this->data['name'], ]; } - public function setPath($path = '') { + /** + * Sets the path for the file to be written + * + * @param string $path Path to write the file + * @return void + */ + public function setPath($path = '') + { if (empty($path)) { $this->path = tempnam(sys_get_temp_dir(), 'upload'); } $this->path = $path; } - public function getPath() { + /** + * Returns the path where the file will be written + * + * @return string|empty + */ + public function getPath() + { return $this->path; } } diff --git a/src/Model/Behavior/UploadBehavior.php b/src/Model/Behavior/UploadBehavior.php index 4a3799e0..3d1051a5 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -222,7 +222,7 @@ public function getWriter(Entity $entity, $data, $field, $settings) * @param array $data the data being submitted for a save * @return \Josegonzalez\Upload\UploadValidator\UploadValidatorInterface */ - public function getUploadValidator(Entity $entity, $settings , $field) + public function getUploadValidator(Entity $entity, $settings, $field) { $default = 'Josegonzalez\Upload\UploadValidator\DefaultUploadValidator'; $uploadValidatorClass = Hash::get($settings, 'uploadValidator', $default); diff --git a/src/UploadValidator/Base64UploadValidator.php b/src/UploadValidator/Base64UploadValidator.php index adeee261..2e286537 100644 --- a/src/UploadValidator/Base64UploadValidator.php +++ b/src/UploadValidator/Base64UploadValidator.php @@ -10,7 +10,7 @@ class Base64UploadValidator extends DefaultUploadValidator * Check's data for any upload errors. * pairs, where the path on disk maps to name which each file should have. * - * @return boolean `true` if upload failed + * @return bool `true` if upload failed */ public function hasUploadFailed() { diff --git a/src/UploadValidator/DefaultUploadValidator.php b/src/UploadValidator/DefaultUploadValidator.php index 868d15a4..da01911d 100644 --- a/src/UploadValidator/DefaultUploadValidator.php +++ b/src/UploadValidator/DefaultUploadValidator.php @@ -37,7 +37,7 @@ public function __construct(Entity $entity, $field) * Check's data for any upload errors. * pairs, where the path on disk maps to name which each file should have. * - * @return boolean `true` if upload failed + * @return bool `true` if upload failed */ public function hasUploadFailed() { diff --git a/src/UploadValidator/UploadValidatorInterface.php b/src/UploadValidator/UploadValidatorInterface.php index 292ef553..d6e5ef82 100644 --- a/src/UploadValidator/UploadValidatorInterface.php +++ b/src/UploadValidator/UploadValidatorInterface.php @@ -17,7 +17,7 @@ public function __construct(Entity $entity, $field); * Check's data for any upload errors. * pairs, where the path on disk maps to name which each file should have. * - * @return boolean `true` if upload failed + * @return bool `true` if upload failed */ public function hasUploadFailed(); } diff --git a/tests/TestCase/File/Transformer/Base64TransformerTest.php b/tests/TestCase/File/Transformer/Base64TransformerTest.php index 4f572f39..1fffcb76 100644 --- a/tests/TestCase/File/Transformer/Base64TransformerTest.php +++ b/tests/TestCase/File/Transformer/Base64TransformerTest.php @@ -45,4 +45,4 @@ public function testIsTransformerInterface() { $this->assertInstanceOf('Josegonzalez\Upload\File\Transformer\TransformerInterface', $this->transformer); } -} \ No newline at end of file +} diff --git a/tests/TestCase/UploadValidator/Base64UploadValidatorTest.php b/tests/TestCase/UploadValidator/Base64UploadValidatorTest.php index 0ce17c9a..33ba0452 100644 --- a/tests/TestCase/UploadValidator/Base64UploadValidatorTest.php +++ b/tests/TestCase/UploadValidator/Base64UploadValidatorTest.php @@ -15,7 +15,8 @@ public function setup() $this->base64UploadValidator = new Base64UploadValidator($this->entity, $this->field); } - public function testOK() { + public function testOK() + { $this->entity->expects($this->any()) ->method('get') ->with('field') @@ -23,7 +24,8 @@ public function testOK() { $this->assertFalse($this->base64UploadValidator->hasUploadFailed()); } - public function testFail() { + public function testFail() + { $this->entity->expects($this->any()) ->method('get') ->with('field') @@ -36,4 +38,4 @@ public function testIsUploadValidatorInterface() $interface = 'Josegonzalez\Upload\UploadValidator\UploadValidatorInterface'; $this->assertInstanceOf($interface, $this->base64UploadValidator); } -} \ No newline at end of file +} diff --git a/tests/TestCase/UploadValidator/DefaultUploadValidatorTest.php b/tests/TestCase/UploadValidator/DefaultUploadValidatorTest.php index 44f81bb6..5817d706 100644 --- a/tests/TestCase/UploadValidator/DefaultUploadValidatorTest.php +++ b/tests/TestCase/UploadValidator/DefaultUploadValidatorTest.php @@ -34,7 +34,8 @@ public function setup() $this->defaultUploadValidator = new DefaultUploadValidator($this->entity, $this->field); } - public function testOK() { + public function testOK() + { $this->entity->expects($this->any()) ->method('get') ->with('field') @@ -42,7 +43,8 @@ public function testOK() { $this->assertFalse($this->defaultUploadValidator->hasUploadFailed()); } - public function testFail() { + public function testFail() + { $this->entity->expects($this->any()) ->method('get') ->with('field') @@ -55,4 +57,4 @@ public function testIsUploadValidatorInterface() $interface = 'Josegonzalez\Upload\UploadValidator\UploadValidatorInterface'; $this->assertInstanceOf($interface, $this->defaultUploadValidator); } -} \ No newline at end of file +} From 0919441d47eb37bde40e4ef0945c69cd7babc23c Mon Sep 17 00:00:00 2001 From: AlexAlexandru Date: Thu, 9 Aug 2018 20:03:32 +0300 Subject: [PATCH 07/12] UploadBehaviour for base64 is missing, I should add it --- src/File/Transformer/Base64Transformer.php | 8 ++++---- src/Model/Behavior/UploadBehavior.php | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/File/Transformer/Base64Transformer.php b/src/File/Transformer/Base64Transformer.php index cde151ee..25ee5465 100644 --- a/src/File/Transformer/Base64Transformer.php +++ b/src/File/Transformer/Base64Transformer.php @@ -38,11 +38,8 @@ public function transform() * @param string $path Path to write the file * @return void */ - public function setPath($path = '') + public function setPath($path) { - if (empty($path)) { - $this->path = tempnam(sys_get_temp_dir(), 'upload'); - } $this->path = $path; } @@ -53,6 +50,9 @@ public function setPath($path = '') */ public function getPath() { + if (empty($this->path)) { + return $this->path = tempnam(sys_get_temp_dir(), 'upload'); + } return $this->path; } } diff --git a/src/Model/Behavior/UploadBehavior.php b/src/Model/Behavior/UploadBehavior.php index 3d1051a5..4876b8ae 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -226,7 +226,7 @@ public function getUploadValidator(Entity $entity, $settings, $field) { $default = 'Josegonzalez\Upload\UploadValidator\DefaultUploadValidator'; $uploadValidatorClass = Hash::get($settings, 'uploadValidator', $default); - if (is_a($uploadValidatorClass, 'Josegonzalez\Upload\UploadValidator\UploadValidatorInterface', true)) { + if (is_subclass_of($uploadValidatorClass, 'Josegonzalez\Upload\UploadValidator\UploadValidatorInterface')) { return new $uploadValidatorClass($entity, $field); } From 4fafa7a08c26378ce882ccada73054628aba5b3b Mon Sep 17 00:00:00 2001 From: AlexAlexandru Date: Thu, 9 Aug 2018 20:16:07 +0300 Subject: [PATCH 08/12] I was talking about the UploadBehaviorTest for base64 that is missing From 8a6e4396746c1559a47f62e34359776cab4fb61a Mon Sep 17 00:00:00 2001 From: AlexAlexandru Date: Thu, 9 Aug 2018 20:17:57 +0300 Subject: [PATCH 09/12] code style --- src/File/Transformer/Base64Transformer.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/File/Transformer/Base64Transformer.php b/src/File/Transformer/Base64Transformer.php index 25ee5465..a82fd573 100644 --- a/src/File/Transformer/Base64Transformer.php +++ b/src/File/Transformer/Base64Transformer.php @@ -51,8 +51,9 @@ public function setPath($path) public function getPath() { if (empty($this->path)) { - return $this->path = tempnam(sys_get_temp_dir(), 'upload'); + return $this->path = tempnam(sys_get_temp_dir(), 'upload'); } + return $this->path; } } From 889215f26b373939155745c9d12df5b80895c67d Mon Sep 17 00:00:00 2001 From: AlexAlexandru Date: Thu, 9 Aug 2018 20:50:30 +0300 Subject: [PATCH 10/12] FIX STYL --- src/File/Transformer/Base64Transformer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/File/Transformer/Base64Transformer.php b/src/File/Transformer/Base64Transformer.php index a82fd573..a5373a22 100644 --- a/src/File/Transformer/Base64Transformer.php +++ b/src/File/Transformer/Base64Transformer.php @@ -53,7 +53,7 @@ public function getPath() if (empty($this->path)) { return $this->path = tempnam(sys_get_temp_dir(), 'upload'); } - + return $this->path; } } From 5468717b86867f1b8db9cc000911de39daae6ec8 Mon Sep 17 00:00:00 2001 From: AlexAlexandru Date: Wed, 22 Aug 2018 21:01:01 +0300 Subject: [PATCH 11/12] added base64 validation --- src/Validation/Base64Validation.php | 10 +++++++ src/Validation/DefaultValidation.php | 2 ++ .../Traits/Base64ValidationTrait.php | 20 ++++++++++++++ .../Validation/Base64ValidationTest.php | 26 +++++++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 src/Validation/Base64Validation.php create mode 100644 src/Validation/Traits/Base64ValidationTrait.php create mode 100644 tests/TestCase/Validation/Base64ValidationTest.php diff --git a/src/Validation/Base64Validation.php b/src/Validation/Base64Validation.php new file mode 100644 index 00000000..22656cae --- /dev/null +++ b/src/Validation/Base64Validation.php @@ -0,0 +1,10 @@ +assertTrue(Base64Validation::isMimeType($png, 'image/png')); + } + + public function testIsMimeTypeInvalid() { + $phpCode = 'PD9waHAgZWNobyAnQ2FrZVBocCc7ID8+'; + $this->assertFalse(Base64Validation::isMimeType($phpCode, 'image/png')); + } +} From 8ceb3737b8016c6756ce5029b0fe4108add4e59c Mon Sep 17 00:00:00 2001 From: AlexAlexandru Date: Wed, 22 Aug 2018 21:16:43 +0300 Subject: [PATCH 12/12] code style --- src/Validation/DefaultValidation.php | 2 +- src/Validation/Traits/Base64ValidationTrait.php | 4 +++- tests/TestCase/Validation/Base64ValidationTest.php | 7 ++++--- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Validation/DefaultValidation.php b/src/Validation/DefaultValidation.php index dbdbf5fd..7eec376b 100644 --- a/src/Validation/DefaultValidation.php +++ b/src/Validation/DefaultValidation.php @@ -8,7 +8,7 @@ class DefaultValidation { + use Base64ValidationTrait; use ImageValidationTrait; use UploadValidationTrait; - use Base64ValidationTrait; } diff --git a/src/Validation/Traits/Base64ValidationTrait.php b/src/Validation/Traits/Base64ValidationTrait.php index 7e8d9ad4..bd7a9a00 100644 --- a/src/Validation/Traits/Base64ValidationTrait.php +++ b/src/Validation/Traits/Base64ValidationTrait.php @@ -2,7 +2,8 @@ namespace Josegonzalez\Upload\Validation\Traits; -trait Base64ValidationTrait { +trait Base64ValidationTrait +{ /** * Allows only the specified mime type to be upload when using base64 uploader @@ -15,6 +16,7 @@ public static function isMimeType($check, $allowedMimeType) { $f = finfo_open(); $mimeType = finfo_buffer($f, base64_decode($check), FILEINFO_MIME_TYPE); + return $mimeType === $allowedMimeType; } } diff --git a/tests/TestCase/Validation/Base64ValidationTest.php b/tests/TestCase/Validation/Base64ValidationTest.php index 6ab02cab..e8f520da 100644 --- a/tests/TestCase/Validation/Base64ValidationTest.php +++ b/tests/TestCase/Validation/Base64ValidationTest.php @@ -2,7 +2,6 @@ namespace Josegonzalez\Upload\Test\TestCase\Validation; - use Cake\TestSuite\TestCase; use Josegonzalez\Upload\Validation\Base64Validation; @@ -14,12 +13,14 @@ public function teardown() parent::tearDown(); } - public function testIsMimeTypeOK() { + public function testIsMimeTypeOK() + { $png = 'iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAB3RJTUUH4AMUECwX5I9GIwAAACFJREFUOMtj/P//PwM1ARMDlcGogaMGjho4auCogUPFQABpCwMlgqgSYAAAAABJRU5ErkJggg=='; $this->assertTrue(Base64Validation::isMimeType($png, 'image/png')); } - public function testIsMimeTypeInvalid() { + public function testIsMimeTypeInvalid() + { $phpCode = 'PD9waHAgZWNobyAnQ2FrZVBocCc7ID8+'; $this->assertFalse(Base64Validation::isMimeType($phpCode, 'image/png')); }