Fix sync command, minor cleanup (#4645)

Fixes #4642, adds some unit testing for the AutoDatasource. Credit to @bennothommo
This commit is contained in:
Ben Thomson 2019-10-09 22:54:13 +08:00 committed by Luke Towers
parent 96e335aebc
commit 07d74ebb1b
2 changed files with 119 additions and 14 deletions

View File

@ -4,7 +4,6 @@ use App;
use Event;
use Exception;
use Cms\Classes\Theme;
use Cms\Classes\ThemeManager;
use Illuminate\Console\Command;
use Symfony\Component\Console\Input\InputOption;
@ -54,11 +53,6 @@ class ThemeSync extends Command
*/
protected $source;
/**
* @var array Models
*/
protected $halyconModels = [];
/**
* Execute the console command.
* @return void
@ -76,7 +70,6 @@ class ThemeSync extends Command
}
// Check to see if the provided theme exists
$themeManager = ThemeManager::instance();
$themeName = $this->argument('name') ?: Theme::getActiveThemeCode();
$themeExists = Theme::exists($themeName);
if (!$themeExists) {
@ -91,13 +84,12 @@ class ThemeSync extends Command
// Get the target and source datasources
$availableSources = ['filesystem', 'database'];
$target = $this->option('target') ?: 'filesystem';
$source = 'filesystem';
if ($target === 'filesystem') {
$source = 'database';
}
$source = ($target === 'filesystem') ? 'database' : 'filesystem';
if (!in_array($target, $availableSources)) {
return $this->error(sprintf("Provided --target of %s is invalid. Allowed: filesystem, database", $target));
}
$this->source = $source;
$this->target = $target;
@ -107,8 +99,7 @@ class ThemeSync extends Command
if (!isset($userPaths)) {
$paths = $themePaths;
}
else {
} else {
$paths = [];
$userPaths = array_map('trim', explode(',', $userPaths));
@ -166,7 +157,6 @@ class ThemeSync extends Command
if (
starts_with($path, $model->getObjectTypeDirName() . '/')
&& in_array(pathinfo($path, PATHINFO_EXTENSION), $model->getAllowedExtensions())
&& file_exists($theme->getPath($theme->getDirName()) . '/' . $path)
) {
$validPaths[$path] = get_class($model);

View File

@ -0,0 +1,115 @@
<?php
use Cms\Classes\AutoDatasource;
use October\Rain\Database\Model;
use October\Rain\Halcyon\Datasource\DbDatasource;
use October\Rain\Halcyon\Datasource\FileDatasource;
class CmsThemeTemplateFixture extends Model
{
protected $fillable = ['*'];
public $timestamps = false;
public $table = 'cms_theme_templates';
}
class AutoDatasourceTest extends PluginTestCase
{
/**
* Array of model fixtures.
*
* @var array
*/
public $fixtures = [];
/**
* AutoDatasource object.
*
* @var Cms\Classes\AutoDatasource;
*/
public $datasource;
public function setUp()
{
parent::setUp();
$this->fixtures = [];
// Create fixtures of template data
$this->fixtures[] = CmsThemeTemplateFixture::create([
'source' => 'test',
'path' => 'partials/page-partial.htm',
'content' => 'AutoDatasource partials/page-partial.htm',
'file_size' => 40
]);
$this->fixtures[] = CmsThemeTemplateFixture::create([
'source' => 'test',
'path' => 'partials/testpost/default.htm',
'content' => 'AutoDatasource partials/testpost/default.htm',
'file_size' => 44
]);
$this->fixtures[] = CmsThemeTemplateFixture::create([
'source' => 'test',
'path' => 'partials/subdir/test.htm',
'content' => 'AutoDatasource partials/subdir/test.htm',
'file_size' => 39
]);
$this->fixtures[] = CmsThemeTemplateFixture::create([
'source' => 'test',
'path' => 'partials/nesting/level2.htm',
'content' => 'AutoDatasource partials/nesting/level2.htm',
'file_size' => 42,
'deleted_at' => '2019-01-01 00:00:00'
]);
// Create AutoDatasource
$this->datasource = new AutoDatasource([
'database' => new DbDatasource('test', 'cms_theme_templates'),
'filesystem' => new FileDatasource(base_path('tests/fixtures/themes/test'), App::make('files')),
]);
}
public function tearDown()
{
foreach ($this->fixtures as $fixture) {
$fixture->delete();
}
parent::tearDown();
}
public function testSelect()
{
$results = collect($this->datasource->select('partials'))
->keyBy('fileName')
->toArray();
// Should be 14 partials in filesystem (tests/fixtures/themes/test), and 1 created directly in database.
// 1 of the filesystem partials should be marked deleted in database.
$this->assertCount(14, $results);
// Database-only partial should be available
$this->assertArrayHasKey('subdir/test.htm', $results);
$this->assertEquals(
'AutoDatasource partials/subdir/test.htm',
$results['subdir/test.htm']['content']
);
// Two filesystem partials should be overriden by database
$this->assertEquals(
'AutoDatasource partials/page-partial.htm',
$results['page-partial.htm']['content']
);
$this->assertEquals(
'AutoDatasource partials/testpost/default.htm',
$results['testpost/default.htm']['content']
);
// One filesystem partial should be marked deleted in database
$this->assertArrayNotHasKey('nesting/level2.htm', $results);
}
}