Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix populating file when path is assets folder #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.