From 0a9f083474871880dc8dcaf0555aad0ef99758f2 Mon Sep 17 00:00:00 2001 From: Joris Vaesen Date: Sun, 27 Mar 2016 14:22:51 +0200 Subject: [PATCH] Fix double directory separator in constructFiles when following documentation --- docs/configuration.rst | 2 +- src/Model/Behavior/UploadBehavior.php | 5 +-- .../Model/Behavior/UploadBehaviorTest.php | 36 +++++++++++++++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/docs/configuration.rst b/docs/configuration.rst index dd103c36..6fc28e52 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -19,7 +19,7 @@ passed in under each field in your behavior configuration. - Default: (string) ``Josegonzalez\Upload\File\Transformer\DefaultTransformer`` -- ``path``: A path relative to the ``filesystem.root``. Should end in ``{DS}`` +- ``path``: A path relative to the ``filesystem.root``. - Default: (string) ``'webroot{DS}files{DS}{model}{DS}{field}{DS}'`` diff --git a/src/Model/Behavior/UploadBehavior.php b/src/Model/Behavior/UploadBehavior.php index 0928864e..e393f534 100644 --- a/src/Model/Behavior/UploadBehavior.php +++ b/src/Model/Behavior/UploadBehavior.php @@ -203,6 +203,7 @@ public function getWriter(Entity $entity, $data, $field, $settings) */ public function constructFiles(Entity $entity, $data, $field, $settings, $basepath) { + $basepath = (substr($basepath, -1) == DS ? $basepath : $basepath . DS); $default = 'Josegonzalez\Upload\File\Transformer\DefaultTransformer'; $transformerClass = Hash::get($settings, 'transformer', $default); $results = []; @@ -210,12 +211,12 @@ public function constructFiles(Entity $entity, $data, $field, $settings, $basepa $transformer = new $transformerClass($this->_table, $entity, $data, $field, $settings); $results = $transformer->transform(); foreach ($results as $key => $value) { - $results[$key] = $basepath . '/' . $value; + $results[$key] = $basepath . $value; } } elseif (is_callable($transformerClass)) { $results = $transformerClass($this->_table, $entity, $data, $field, $settings); foreach ($results as $key => $value) { - $results[$key] = $basepath . '/' . $value; + $results[$key] = $basepath . $value; } } else { throw new UnexpectedValueException(sprintf( diff --git a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php index 4bb5641f..44f971f1 100644 --- a/tests/TestCase/Model/Behavior/UploadBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/UploadBehaviorTest.php @@ -309,6 +309,27 @@ public function testConstructFiles() $this->assertEquals(['path/to/file/on/disk' => 'some/path/file.txt'], $files); } + public function testConstructFilesWithBasePathEndingDS() + { + $files = $this->behavior->constructFiles( + $this->entity, + ['tmp_name' => 'path/to/file/on/disk', 'name' => 'file.txt'], + 'field', + [], + 'path/' + ); + $this->assertEquals(['path/to/file/on/disk' => 'path/file.txt'], $files); + + $files = $this->behavior->constructFiles( + $this->entity, + ['tmp_name' => 'path/to/file/on/disk', 'name' => 'file.txt'], + 'field', + [], + 'some/path/' + ); + $this->assertEquals(['path/to/file/on/disk' => 'some/path/file.txt'], $files); + } + public function testConstructFilesWithCallable() { $callable = function () { @@ -324,6 +345,21 @@ public function testConstructFilesWithCallable() $this->assertEquals(['path/to/callable/file/on/disk' => 'some/path/file.text'], $files); } + public function testConstructFilesWithCallableAndBasePathEndingDS() + { + $callable = function () { + return ['path/to/callable/file/on/disk' => 'file.text']; + }; + $files = $this->behavior->constructFiles( + $this->entity, + ['tmp_name' => 'path/to/file/on/disk', 'name' => 'file.txt'], + 'field', + ['transformer' => $callable], + 'some/path/' + ); + $this->assertEquals(['path/to/callable/file/on/disk' => 'some/path/file.text'], $files); + } + public function testConstructFilesException() { $this->setExpectedException('UnexpectedValueException', "'transformer' not set to instance of TransformerInterface: UnexpectedValueException");