Skip to content

Commit

Permalink
Fix populating file when path is assets folder
Browse files Browse the repository at this point in the history
  • Loading branch information
satrun77 committed Nov 27, 2021
1 parent 0c40e4e commit 218ad4f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
13 changes: 10 additions & 3 deletions code/PopulateFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand Down
62 changes: 62 additions & 0 deletions tests/php/PopulateAssetFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace DNADesign\Populate\Tests;

use DNADesign\Populate\PopulateFactory;
use DNADesign\Populate\Tests\PopulateFactoryTest\PopulateFactoryTestObject;
use DNADesign\Populate\Tests\PopulateFactoryTest\PopulateFactoryTestVersionedObject;
use SilverStripe\Assets\Image;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Dev\TestOnly;
use SilverStripe\Versioned\Versioned;

/**
* Test populating files into assets folder.
*/
class PopulateAssetFactoryTest extends SapphireTest implements TestOnly
{
/**
* @var PopulateFactory
*/
protected $factory;

protected $usesDatabase = true;

public function setUp()
{
parent::setUp();
$this->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');
}
}
Binary file added tests/php/fixture/assets/image1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 218ad4f

Please sign in to comment.