Skip to content

Commit 9a6507c

Browse files
committed
Fix populating file when path is assets folder
1 parent 718b2a5 commit 9a6507c

3 files changed

Lines changed: 72 additions & 3 deletions

File tree

code/PopulateFactory.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,18 @@ private function populateFile($data)
254254
$file = Image::create();
255255
}
256256

257-
$folder = Folder::find_or_make(dirname($filenameWithoutAssets));
257+
$fileFolder = dirname($filenameWithoutAssets);
258258
$filename = basename($filenameWithoutAssets);
259+
$folder = null;
260+
261+
// Create a folder if the YML configuration indicates that the file should be created within a folder
262+
if ($fileFolder !== '.') {
263+
$folder = Folder::find_or_make($fileFolder);
264+
$fileFolder = $folder->getFilename();
265+
}
259266

260267
// We could just use $data['Filename'], but we need to allow for filsystem abstraction
261-
$filePath = File::join_paths($folder->getFilename(), $filename);
268+
$filePath = File::join_paths($fileFolder, $filename);
262269

263270
$fileCfg = [
264271
// if there's a filename conflict we've got new content so overwrite it.
@@ -279,7 +286,7 @@ private function populateFile($data)
279286
$file->setFromString(file_get_contents($fixtureFilePath), $filePath, null, null, $fileCfg);
280287
// Setting ParentID needs to come after setFromString() as (at least sometimes) setFromString() resets the
281288
// file Parent back to the "Uploads" folder
282-
$file->ParentID = $folder->ID;
289+
$file->ParentID = $folder instanceof Folder ? $folder->ID : 0;
283290
$file->write();
284291
$file->publishRecursive();
285292
} catch (Exception $e) {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace DNADesign\Populate\Tests;
4+
5+
use DNADesign\Populate\PopulateFactory;
6+
use DNADesign\Populate\Tests\PopulateFactoryTest\PopulateFactoryTestObject;
7+
use DNADesign\Populate\Tests\PopulateFactoryTest\PopulateFactoryTestVersionedObject;
8+
use SilverStripe\Assets\Image;
9+
use SilverStripe\Dev\SapphireTest;
10+
use SilverStripe\Dev\TestOnly;
11+
use SilverStripe\Versioned\Versioned;
12+
13+
/**
14+
* Test populating files into assets folder.
15+
*/
16+
class PopulateAssetFactoryTest extends SapphireTest implements TestOnly
17+
{
18+
/**
19+
* @var PopulateFactory
20+
*/
21+
protected $factory;
22+
23+
protected $usesDatabase = true;
24+
25+
public function setUp()
26+
{
27+
parent::setUp();
28+
$this->factory = new PopulateFactory();
29+
}
30+
31+
/**
32+
* Assert that a file is loaded into the expected path within assets directory (Root path).
33+
*/
34+
public function testLoadingFileToRootAssetsDirectory()
35+
{
36+
// Load a file using populate factory
37+
$obj = $this->factory->createObject(Image::class, 'image1', [
38+
'Filename' => 'image1.png',
39+
'PopulateFileFrom' => 'tests/php/fixture/assets/image1.png',
40+
]);
41+
42+
// Assert that the file is created and exists in expected root directory of assets
43+
$this->assertEquals('./image1.png', $obj->getFilename());
44+
$this->assertFileExists(__DIR__ . '/../../assets/image1.png');
45+
}
46+
47+
/**
48+
* Assert that a file is loaded into the expected path within assets directory (Within folder)
49+
*/
50+
public function testLoadingFileToFolderInAssetsDirectory()
51+
{
52+
// Load a file using populate factory
53+
$obj = $this->factory->createObject(Image::class, 'image1', [
54+
'Filename' => 'fixture/image1.png',
55+
'PopulateFileFrom' => 'tests/php/fixture/assets/image1.png',
56+
]);
57+
58+
// Assert that the file is created and exists in expected root directory of assets
59+
$this->assertEquals('fixture/image1.png', $obj->getFilename());
60+
$this->assertFileExists(__DIR__ . '/../../assets/fixture/image1.png');
61+
}
62+
}
9.12 KB
Loading

0 commit comments

Comments
 (0)