diff --git a/code/PopulateFactory.php b/code/PopulateFactory.php index 4ee5235..ddcafd5 100644 --- a/code/PopulateFactory.php +++ b/code/PopulateFactory.php @@ -254,11 +254,18 @@ private function populateFile($data) $file = Image::create(); } - $folder = Folder::find_or_make(dirname($filenameWithoutAssets)); + $fileFolder = dirname($filenameWithoutAssets); $filename = basename($filenameWithoutAssets); + $folder = null; + + // Create a folder if the YML configuration indicates that the file should be created within a folder + if ($fileFolder !== '.') { + $folder = Folder::find_or_make($fileFolder); + $fileFolder = $folder->getFilename(); + } // We could just use $data['Filename'], but we need to allow for filsystem abstraction - $filePath = File::join_paths($folder->getFilename(), $filename); + $filePath = File::join_paths($fileFolder, $filename); $fileCfg = [ // if there's a filename conflict we've got new content so overwrite it. @@ -279,7 +286,7 @@ private function populateFile($data) $file->setFromString(file_get_contents($fixtureFilePath), $filePath, null, null, $fileCfg); // Setting ParentID needs to come after setFromString() as (at least sometimes) setFromString() resets the // file Parent back to the "Uploads" folder - $file->ParentID = $folder->ID; + $file->ParentID = $folder instanceof Folder ? $folder->ID : 0; $file->write(); $file->publishRecursive(); } catch (Exception $e) { diff --git a/tests/php/PopulateAssetFactoryTest.php b/tests/php/PopulateAssetFactoryTest.php new file mode 100644 index 0000000..423a9f5 --- /dev/null +++ b/tests/php/PopulateAssetFactoryTest.php @@ -0,0 +1,60 @@ +factory = new PopulateFactory(); + } + + /** + * Assert that a file is loaded into the expected path within assets directory (Root path). + */ + public function testLoadingFileToRootAssetsDirectory() + { + // Load a file using populate factory + $obj = $this->factory->createObject(Image::class, 'image1', [ + 'Filename' => 'image1.png', + 'PopulateFileFrom' => 'tests/php/fixture/assets/image1.png', + ]); + + // Assert that the file is created and exists in expected root directory of assets + $this->assertEquals('./image1.png', $obj->getFilename()); + $this->assertFileExists(__DIR__ . '/../../assets/image1.png'); + } + + /** + * Assert that a file is loaded into the expected path within assets directory (Within folder) + */ + public function testLoadingFileToFolderInAssetsDirectory() + { + // Load a file using populate factory + $obj = $this->factory->createObject(Image::class, 'image1', [ + 'Filename' => 'fixture/image1.png', + 'PopulateFileFrom' => 'tests/php/fixture/assets/image1.png', + ]); + + // Assert that the file is created and exists in expected root directory of assets + $this->assertEquals('fixture/image1.png', $obj->getFilename()); + $this->assertFileExists(__DIR__ . '/../../assets/fixture/image1.png'); + } +} diff --git a/tests/php/fixture/assets/image1.png b/tests/php/fixture/assets/image1.png new file mode 100644 index 0000000..e16ff20 Binary files /dev/null and b/tests/php/fixture/assets/image1.png differ