From a18a5118afbd13d12d5885392fdc65fcd3b16c0a Mon Sep 17 00:00:00 2001 From: nghiem-mb Date: Tue, 3 Oct 2023 17:44:12 +0900 Subject: [PATCH 1/7] BcBaserHelperTest --- plugins/baser-core/src/Utility/BcFile.php | 11 +++++++++++ .../tests/TestCase/Utility/BcFileTest.php | 14 ++++++++++++++ .../TestCase/View/Helper/BcBaserHelperTest.php | 8 ++++---- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/plugins/baser-core/src/Utility/BcFile.php b/plugins/baser-core/src/Utility/BcFile.php index 9804ef1647..d437c9b4a5 100644 --- a/plugins/baser-core/src/Utility/BcFile.php +++ b/plugins/baser-core/src/Utility/BcFile.php @@ -101,4 +101,15 @@ public function write($data) return (bool) file_put_contents($this->path, $data); } + /** + * ファイルを削除 + * @return bool + */ + public function delete() + { + if(!is_file($this->path)) { + return false; + } + return unlink($this->path); + } } diff --git a/plugins/baser-core/tests/TestCase/Utility/BcFileTest.php b/plugins/baser-core/tests/TestCase/Utility/BcFileTest.php index caf8486187..6b05f0ae06 100644 --- a/plugins/baser-core/tests/TestCase/Utility/BcFileTest.php +++ b/plugins/baser-core/tests/TestCase/Utility/BcFileTest.php @@ -68,4 +68,18 @@ public function testWriteAndRead() $this->assertEquals('test', $file->read()); (new BcFolder(dirname($path)))->delete(); } + + /** + * test Delete + * @return void + */ + public function testDelete() + { + $path = TMP_TESTS . 'test' . DS . 'test.txt'; + $file = new BcFile($path); + $file->create(); + $this->assertTrue($file->delete()); + (new BcFolder(dirname($path)))->delete(); + $this->assertFalse($file->delete()); + } } diff --git a/plugins/baser-core/tests/TestCase/View/Helper/BcBaserHelperTest.php b/plugins/baser-core/tests/TestCase/View/Helper/BcBaserHelperTest.php index 289f73193f..84b2b62ac5 100644 --- a/plugins/baser-core/tests/TestCase/View/Helper/BcBaserHelperTest.php +++ b/plugins/baser-core/tests/TestCase/View/Helper/BcBaserHelperTest.php @@ -11,11 +11,11 @@ namespace BaserCore\Test\TestCase\View\Helper; +use BaserCore\Utility\BcFile; use ReflectionClass; use Cake\Event\Event; use Cake\Core\Configure; use Cake\Routing\Router; -use Cake\Filesystem\File; use Cake\Event\EventManager; use Cake\View\Helper\UrlHelper; use Cake\View\Helper\HtmlHelper; @@ -222,13 +222,13 @@ public function testGetElement() // 管理画面用のテンプレートがなくフロントのテンプレートがある場合 $templateDir = ROOT . DS . 'plugins' . DS . 'bc-admin-third' . DS . 'templates'. DS; - $fileFront = new File($templateDir . 'element' . DS . 'test.php'); + $fileFront = new BcFile($templateDir . 'element' . DS . 'test.php'); $fileFront->create(); $fileFront->write('front'); $this->assertTextContains('front', $this->BcBaser->getElement('test')); // 管理画面用のテンプレートとフロントのテンプレートの両方がある場合 - $fileAdmin = new File($templateDir . 'Admin' . DS . 'element' . DS . 'test.php'); + $fileAdmin = new BcFile($templateDir . 'Admin' . DS . 'element' . DS . 'test.php'); $fileAdmin->create(); $fileAdmin->write('admin'); $this->assertTextContains('admin', $this->BcBaser->getElement('test')); @@ -1813,7 +1813,7 @@ public function testIncludeCore($selectPlugin, $name, $data, $options, $expected $path2 = ROOT . '/lib/Baser/Plugin/Test/View'; mkdir($path2); $path3 = ROOT . '/lib/Baser/Plugin/Test/View/test.php'; - $plugin = new File($path3); + $plugin = new BcFile($path3); $plugin->write('test'); $plugin->close(); } From 347ded8b812d0e85fde609d71d7cca5e232de4f6 Mon Sep 17 00:00:00 2001 From: nghiem-mb Date: Wed, 4 Oct 2023 16:16:52 +0900 Subject: [PATCH 2/7] BcDatabaseServiceTest --- .../Service/BcDatabaseServiceTest.php | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/plugins/baser-core/tests/TestCase/Service/BcDatabaseServiceTest.php b/plugins/baser-core/tests/TestCase/Service/BcDatabaseServiceTest.php index 5bfd0765ce..4c1fd2ac76 100644 --- a/plugins/baser-core/tests/TestCase/Service/BcDatabaseServiceTest.php +++ b/plugins/baser-core/tests/TestCase/Service/BcDatabaseServiceTest.php @@ -25,16 +25,16 @@ use BaserCore\Test\Factory\UsersUserGroupFactory; use BaserCore\TestSuite\BcTestCase; use BaserCore\Utility\BcContainerTrait; +use BaserCore\Utility\BcFile; +use BaserCore\Utility\BcFolder; use BaserCore\Utility\BcUtil; use Cake\Cache\Cache; use Cake\Core\Configure; use Cake\Database\Driver\Mysql; use Cake\Database\Driver\Postgres; use Cake\Database\Driver\Sqlite; -use Cake\Filesystem\Folder; use Cake\ORM\TableRegistry; use Cake\TestSuite\IntegrationTestTrait; -use Cake\Filesystem\File; use Cake\Utility\Inflector; use Migrations\Migrations; @@ -261,7 +261,7 @@ public function test_loadCsv() // csvフォルダーを作成する $csvFolder = TMP . 'csv' . DS; if (!is_dir($csvFolder)) { - new Folder($csvFolder, true, 0777); + new BcFolder($csvFolder); } // csvファイルを作成する $table = 'pages'; @@ -334,7 +334,7 @@ public function test_loadCsvToArray() $this->assertEquals('メインサイト', $rs[0]['title']); $this->assertTrue(mb_check_encoding($rs[0]['title'], 'UTF-8')); - $file = new File($path); + $file = new BcFile($path); $file->delete(); } @@ -422,9 +422,9 @@ public function test_loadDefaultDataPattern() $this->execPrivateMethod($this->BcDatabaseService, '_loadDefaultDataPattern', [$pattern, $theme]); $path = BcUtil::getDefaultDataPath($theme, $pattern); $this->assertNotNull($path); - $Folder = new Folder($path . DS . $plugin); - $files = $Folder->read(true, true, true); - $csvList = $files[1]; + $Folder = new BcFolder($path . DS . $plugin); + $files = $Folder->getFiles(['sort' => true]); + $csvList = $files; foreach ($csvList as $path) { $table = basename($path, '.csv'); if (!in_array($table, $tableList)) continue; @@ -537,7 +537,7 @@ public function test_writeCsv() $this->assertEquals('', $rs[0]['modified']); $this->assertEquals('', $rs[0]['created']); - $file = new File($path); + $file = new BcFile($path); $file->delete(); } /** @@ -599,7 +599,7 @@ public function test_loadSchema() { $path = TMP . 'schema' . DS; $fileName = 'UserActionsSchema.php'; - $schemaFile = new File($path . $fileName, true); + $schemaFile = new BcFile($path . $fileName, true); $table = 'user_actions'; // スキーマファイルを生成 $schemaFile->write("assertFileExists($expectedFile); - $file = new File($expectedFile); + $file = new BcFile($expectedFile); $file->delete(); } From e87e168f17baecd913f1f38fa388d5c5101e9446 Mon Sep 17 00:00:00 2001 From: nghiem-mb Date: Wed, 4 Oct 2023 16:59:57 +0900 Subject: [PATCH 3/7] BcDatabaseServiceTest --- .../src/Service/BcDatabaseService.php | 37 ++++++++++--------- .../Service/BcDatabaseServiceTest.php | 6 ++- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/plugins/baser-core/src/Service/BcDatabaseService.php b/plugins/baser-core/src/Service/BcDatabaseService.php index bf7c706c6b..2493325ea3 100644 --- a/plugins/baser-core/src/Service/BcDatabaseService.php +++ b/plugins/baser-core/src/Service/BcDatabaseService.php @@ -15,6 +15,8 @@ use BaserCore\Error\BcException; use BaserCore\Model\Table\AppTable; use BaserCore\Utility\BcContainerTrait; +use BaserCore\Utility\BcFile; +use BaserCore\Utility\BcFolder; use BaserCore\Utility\BcUtil; use Cake\Cache\Cache; use Cake\Core\Configure; @@ -27,8 +29,6 @@ use Cake\Datasource\ConnectionManager; use Cake\Datasource\Exception\MissingDatasourceConfigException; use Cake\Event\EventManager; -use Cake\Filesystem\File; -use Cake\Filesystem\Folder; use Cake\Log\LogTrait; use Cake\ORM\Entity; use Cake\ORM\Table; @@ -369,9 +369,10 @@ protected function _loadDefaultDataPattern($pattern, $theme, $plugin = 'BaserCor $path = BcUtil::getDefaultDataPath($theme, $pattern); if (!$path) return true; - $Folder = new Folder($path . DS . $plugin); - $files = $Folder->read(true, true, true); - $targetTables = $files[1]; + $Folder = new BcFolder($path . DS . $plugin); + $Folder->create(); + $files = $Folder->getFiles(); + $targetTables = $files; $tableList = $this->getAppTableList($plugin); $result = true; foreach($targetTables as $targetTable) { @@ -379,7 +380,7 @@ protected function _loadDefaultDataPattern($pattern, $theme, $plugin = 'BaserCor if (in_array($targetTable, $excludes)) continue; if (!in_array($targetTable, $tableList)) continue; // 初期データ投入 - foreach($files[1] as $file) { + foreach($files as $file) { if (!preg_match('/\.csv$/', $file)) continue; $table = basename($file, '.csv'); if ($table !== $targetTable) continue; @@ -929,10 +930,11 @@ public function getAppTableList($plugin = ''): array if (!$pluginPath) continue; $path = $pluginPath . 'config' . DS . 'Migrations'; if (!is_dir($path)) continue; - $folder = new Folder($path); - $files = $folder->read(true, true); - if (empty($files[1])) continue; - foreach($files[1] as $file) { + $folder = new BcFolder($path); + $folder->create(); + $files = $folder->getFiles(); + if (empty($files)) continue; + foreach($files as $file) { if (!preg_match('/Create([a-zA-Z]+)\./', $file, $matches)) continue; $tableName = Inflector::tableize($matches[1]); $checkName = $prefix . $tableName; @@ -983,8 +985,8 @@ public function writeSchema($table, $options) $dir = dirname($options['path']); if (!is_dir($dir)) { - $folder = new Folder(); - $folder->create($dir); + $folder = new BcFolder($dir); + $folder->create(); } $describe = TableRegistry::getTableLocator() ->get('BaserCore.App') @@ -1010,13 +1012,12 @@ public function writeSchema($table, $options) BcUtil::onEvent($eventManager, 'View.afterRender', $afterRenderListeners); if (!is_dir($options['path'])) { - $folder = new Folder(); - $folder->create($options['path']); + $folder = new BcFolder($options['path']); + $folder->create(); } - $file = new File($options['path'] . DS . Inflector::camelize($table) . 'Schema.php'); + $file = new BcFile($options['path'] . DS . Inflector::camelize($table) . 'Schema.php'); $file->write($content); - $file->close(); return true; } @@ -1193,8 +1194,8 @@ public function connectDb(array $config, $name = 'default') ]); if($config['datasource'] === 'sqlite') { if(!is_dir(ROOT . DS . 'db' . DS . 'sqlite')) { - $folder = new Folder(ROOT . DS . 'db' . DS . 'sqlite'); - $folder->create(ROOT . DS . 'db' . DS . 'sqlite', 0777); + $folder = new BcFolder(ROOT . DS . 'db' . DS . 'sqlite'); + $folder->create(); } } $db = ConnectionManager::get($name); diff --git a/plugins/baser-core/tests/TestCase/Service/BcDatabaseServiceTest.php b/plugins/baser-core/tests/TestCase/Service/BcDatabaseServiceTest.php index 4c1fd2ac76..2e7c706cd1 100644 --- a/plugins/baser-core/tests/TestCase/Service/BcDatabaseServiceTest.php +++ b/plugins/baser-core/tests/TestCase/Service/BcDatabaseServiceTest.php @@ -261,7 +261,8 @@ public function test_loadCsv() // csvフォルダーを作成する $csvFolder = TMP . 'csv' . DS; if (!is_dir($csvFolder)) { - new BcFolder($csvFolder); + $csv = new BcFolder($csvFolder); + $csv->create(); } // csvファイルを作成する $table = 'pages'; @@ -423,7 +424,8 @@ public function test_loadDefaultDataPattern() $path = BcUtil::getDefaultDataPath($theme, $pattern); $this->assertNotNull($path); $Folder = new BcFolder($path . DS . $plugin); - $files = $Folder->getFiles(['sort' => true]); + $Folder->create(); + $files = $Folder->getFiles(); $csvList = $files; foreach ($csvList as $path) { $table = basename($path, '.csv'); From 0d16b2681c18262270a0ea8e3a82a67443e5fdd2 Mon Sep 17 00:00:00 2001 From: nghiem-mb Date: Thu, 5 Oct 2023 10:30:43 +0900 Subject: [PATCH 4/7] fix bug --- plugins/baser-core/src/Service/BcDatabaseService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/baser-core/src/Service/BcDatabaseService.php b/plugins/baser-core/src/Service/BcDatabaseService.php index 2493325ea3..cafbd1412d 100644 --- a/plugins/baser-core/src/Service/BcDatabaseService.php +++ b/plugins/baser-core/src/Service/BcDatabaseService.php @@ -371,7 +371,7 @@ protected function _loadDefaultDataPattern($pattern, $theme, $plugin = 'BaserCor $Folder = new BcFolder($path . DS . $plugin); $Folder->create(); - $files = $Folder->getFiles(); + $files = $Folder->getFiles(['full'=>true]); $targetTables = $files; $tableList = $this->getAppTableList($plugin); $result = true; From a58b8555f42717fa5b306ba94fdf9800265450dd Mon Sep 17 00:00:00 2001 From: nghiem-mb Date: Thu, 5 Oct 2023 10:54:14 +0900 Subject: [PATCH 5/7] fix bug --- .../baser-core/tests/TestCase/Service/BcDatabaseServiceTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/baser-core/tests/TestCase/Service/BcDatabaseServiceTest.php b/plugins/baser-core/tests/TestCase/Service/BcDatabaseServiceTest.php index 2e7c706cd1..0213498a73 100644 --- a/plugins/baser-core/tests/TestCase/Service/BcDatabaseServiceTest.php +++ b/plugins/baser-core/tests/TestCase/Service/BcDatabaseServiceTest.php @@ -425,7 +425,7 @@ public function test_loadDefaultDataPattern() $this->assertNotNull($path); $Folder = new BcFolder($path . DS . $plugin); $Folder->create(); - $files = $Folder->getFiles(); + $files = $Folder->getFiles(['full'=>true]); $csvList = $files; foreach ($csvList as $path) { $table = basename($path, '.csv'); From b94fe0c86d41e55ff7af71962cf71ba4cda3ada4 Mon Sep 17 00:00:00 2001 From: nghiem-mb Date: Fri, 6 Oct 2023 09:09:28 +0900 Subject: [PATCH 6/7] fix bug --- .../baser-core/tests/TestCase/View/Helper/BcBaserHelperTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/plugins/baser-core/tests/TestCase/View/Helper/BcBaserHelperTest.php b/plugins/baser-core/tests/TestCase/View/Helper/BcBaserHelperTest.php index 84b2b62ac5..ac88e9685c 100644 --- a/plugins/baser-core/tests/TestCase/View/Helper/BcBaserHelperTest.php +++ b/plugins/baser-core/tests/TestCase/View/Helper/BcBaserHelperTest.php @@ -1815,7 +1815,6 @@ public function testIncludeCore($selectPlugin, $name, $data, $options, $expected $path3 = ROOT . '/lib/Baser/Plugin/Test/View/test.php'; $plugin = new BcFile($path3); $plugin->write('test'); - $plugin->close(); } $this->expectOutputRegex('/' . $expected . '/', $message); From ed2f12aa39f45753e6741daffe318524096855d2 Mon Sep 17 00:00:00 2001 From: nghiem-mb Date: Mon, 9 Oct 2023 11:04:00 +0900 Subject: [PATCH 7/7] fix comment --- plugins/baser-core/src/Service/BcDatabaseService.php | 1 - .../baser-core/tests/TestCase/Service/BcDatabaseServiceTest.php | 1 - 2 files changed, 2 deletions(-) diff --git a/plugins/baser-core/src/Service/BcDatabaseService.php b/plugins/baser-core/src/Service/BcDatabaseService.php index cafbd1412d..aeaa751464 100644 --- a/plugins/baser-core/src/Service/BcDatabaseService.php +++ b/plugins/baser-core/src/Service/BcDatabaseService.php @@ -931,7 +931,6 @@ public function getAppTableList($plugin = ''): array $path = $pluginPath . 'config' . DS . 'Migrations'; if (!is_dir($path)) continue; $folder = new BcFolder($path); - $folder->create(); $files = $folder->getFiles(); if (empty($files)) continue; foreach($files as $file) { diff --git a/plugins/baser-core/tests/TestCase/Service/BcDatabaseServiceTest.php b/plugins/baser-core/tests/TestCase/Service/BcDatabaseServiceTest.php index 0213498a73..1cf6cd5430 100644 --- a/plugins/baser-core/tests/TestCase/Service/BcDatabaseServiceTest.php +++ b/plugins/baser-core/tests/TestCase/Service/BcDatabaseServiceTest.php @@ -424,7 +424,6 @@ public function test_loadDefaultDataPattern() $path = BcUtil::getDefaultDataPath($theme, $pattern); $this->assertNotNull($path); $Folder = new BcFolder($path . DS . $plugin); - $Folder->create(); $files = $Folder->getFiles(['full'=>true]); $csvList = $files; foreach ($csvList as $path) {