Fix sync command, minor cleanup (#4645)
Fixes #4642, adds some unit testing for the AutoDatasource. Credit to @bennothommo
This commit is contained in:
parent
96e335aebc
commit
07d74ebb1b
|
|
@ -4,7 +4,6 @@ use App;
|
||||||
use Event;
|
use Event;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Cms\Classes\Theme;
|
use Cms\Classes\Theme;
|
||||||
use Cms\Classes\ThemeManager;
|
|
||||||
|
|
||||||
use Illuminate\Console\Command;
|
use Illuminate\Console\Command;
|
||||||
use Symfony\Component\Console\Input\InputOption;
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
|
|
@ -54,11 +53,6 @@ class ThemeSync extends Command
|
||||||
*/
|
*/
|
||||||
protected $source;
|
protected $source;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array Models
|
|
||||||
*/
|
|
||||||
protected $halyconModels = [];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute the console command.
|
* Execute the console command.
|
||||||
* @return void
|
* @return void
|
||||||
|
|
@ -76,7 +70,6 @@ class ThemeSync extends Command
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check to see if the provided theme exists
|
// Check to see if the provided theme exists
|
||||||
$themeManager = ThemeManager::instance();
|
|
||||||
$themeName = $this->argument('name') ?: Theme::getActiveThemeCode();
|
$themeName = $this->argument('name') ?: Theme::getActiveThemeCode();
|
||||||
$themeExists = Theme::exists($themeName);
|
$themeExists = Theme::exists($themeName);
|
||||||
if (!$themeExists) {
|
if (!$themeExists) {
|
||||||
|
|
@ -91,13 +84,12 @@ class ThemeSync extends Command
|
||||||
// Get the target and source datasources
|
// Get the target and source datasources
|
||||||
$availableSources = ['filesystem', 'database'];
|
$availableSources = ['filesystem', 'database'];
|
||||||
$target = $this->option('target') ?: 'filesystem';
|
$target = $this->option('target') ?: 'filesystem';
|
||||||
$source = 'filesystem';
|
$source = ($target === 'filesystem') ? 'database' : 'filesystem';
|
||||||
if ($target === 'filesystem') {
|
|
||||||
$source = 'database';
|
|
||||||
}
|
|
||||||
if (!in_array($target, $availableSources)) {
|
if (!in_array($target, $availableSources)) {
|
||||||
return $this->error(sprintf("Provided --target of %s is invalid. Allowed: filesystem, database", $target));
|
return $this->error(sprintf("Provided --target of %s is invalid. Allowed: filesystem, database", $target));
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->source = $source;
|
$this->source = $source;
|
||||||
$this->target = $target;
|
$this->target = $target;
|
||||||
|
|
||||||
|
|
@ -107,8 +99,7 @@ class ThemeSync extends Command
|
||||||
|
|
||||||
if (!isset($userPaths)) {
|
if (!isset($userPaths)) {
|
||||||
$paths = $themePaths;
|
$paths = $themePaths;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
$paths = [];
|
$paths = [];
|
||||||
$userPaths = array_map('trim', explode(',', $userPaths));
|
$userPaths = array_map('trim', explode(',', $userPaths));
|
||||||
|
|
||||||
|
|
@ -166,7 +157,6 @@ class ThemeSync extends Command
|
||||||
if (
|
if (
|
||||||
starts_with($path, $model->getObjectTypeDirName() . '/')
|
starts_with($path, $model->getObjectTypeDirName() . '/')
|
||||||
&& in_array(pathinfo($path, PATHINFO_EXTENSION), $model->getAllowedExtensions())
|
&& in_array(pathinfo($path, PATHINFO_EXTENSION), $model->getAllowedExtensions())
|
||||||
&& file_exists($theme->getPath($theme->getDirName()) . '/' . $path)
|
|
||||||
) {
|
) {
|
||||||
$validPaths[$path] = get_class($model);
|
$validPaths[$path] = get_class($model);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue