From 07d74ebb1bd4ec2f04048c6369da5a03c2457147 Mon Sep 17 00:00:00 2001 From: Ben Thomson Date: Wed, 9 Oct 2019 22:54:13 +0800 Subject: [PATCH] Fix sync command, minor cleanup (#4645) Fixes #4642, adds some unit testing for the AutoDatasource. Credit to @bennothommo --- modules/system/console/ThemeSync.php | 18 +-- .../system/classes/AutoDatasourceTest.php | 115 ++++++++++++++++++ 2 files changed, 119 insertions(+), 14 deletions(-) create mode 100644 tests/unit/system/classes/AutoDatasourceTest.php diff --git a/modules/system/console/ThemeSync.php b/modules/system/console/ThemeSync.php index b74eac611..3ace3cc15 100644 --- a/modules/system/console/ThemeSync.php +++ b/modules/system/console/ThemeSync.php @@ -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); diff --git a/tests/unit/system/classes/AutoDatasourceTest.php b/tests/unit/system/classes/AutoDatasourceTest.php new file mode 100644 index 000000000..43410cc77 --- /dev/null +++ b/tests/unit/system/classes/AutoDatasourceTest.php @@ -0,0 +1,115 @@ +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); + } +}