From e8ec3feac151e2caf91e241dd0302c0c3ea1b43d Mon Sep 17 00:00:00 2001 From: nghiem-mb <127474186+nghiem-mb@users.noreply.github.com> Date: Wed, 15 Nov 2023 16:32:24 +0700 Subject: [PATCH] PluginsService and PluginsServiceTest (#2845) --- .../baser-core/src/Service/PluginsService.php | 16 +++++----- plugins/baser-core/src/Utility/BcFolder.php | 9 +++--- .../Api/Admin/PluginsControllerTest.php | 1 - .../TestCase/Service/PluginsServiceTest.php | 29 ++++++++++--------- .../tests/TestCase/Utility/BcFolderTest.php | 9 +++--- 5 files changed, 33 insertions(+), 31 deletions(-) diff --git a/plugins/baser-core/src/Service/PluginsService.php b/plugins/baser-core/src/Service/PluginsService.php index 76486700eb..97c826a365 100644 --- a/plugins/baser-core/src/Service/PluginsService.php +++ b/plugins/baser-core/src/Service/PluginsService.php @@ -15,12 +15,13 @@ use BaserCore\Model\Entity\Plugin; use BaserCore\Model\Table\PluginsTable; use BaserCore\Utility\BcContainerTrait; +use BaserCore\Utility\BcFile; +use BaserCore\Utility\BcFolder; use BaserCore\Utility\BcSiteConfig; use BaserCore\Utility\BcUpdateLog; use BaserCore\Utility\BcZip; use Cake\Cache\Cache; use Cake\Datasource\Exception\RecordNotFoundException; -use Cake\Filesystem\File; use Cake\Http\Client; use Cake\Http\Client\Exception\NetworkException; use Cake\ORM\TableRegistry; @@ -29,7 +30,6 @@ use Cake\Core\Configure; use BaserCore\Utility\BcUtil; use Cake\Core\App; -use Cake\Filesystem\Folder; use Cake\Core\Plugin as CakePlugin; use Cake\Datasource\EntityInterface; use Cake\Utility\Xml; @@ -109,9 +109,9 @@ public function getIndex(string $sortMode): array } $paths = App::path('plugins'); foreach($paths as $path) { - $Folder = new Folder($path); - $files = $Folder->read(true, true, true); - foreach($files[0] as $file) { + $Folder = new BcFolder($path); + $files = $Folder->getFolders(['full'=>true]); + foreach($files as $file) { $name = Inflector::camelize(Inflector::underscore(basename($file))); if (in_array(Inflector::camelize(basename($file), '-'), Configure::read('BcApp.core'))) continue; if (in_array($name, $registeredName)) { @@ -657,8 +657,8 @@ public function add(array $postData) $num++; $dstName = Inflector::camelize($baseName) . $num; } - $folder = new Folder(TMP . $srcName); - $folder->move(BASER_PLUGINS . $dstName, ['mode' => 0777]); + $folder = new BcFolder(TMP . $srcName); + $folder->move(TMP . $srcName, BASER_PLUGINS. $dstName); unlink(TMP . $name); BcUtil::changePluginNameSpace($dstName); return $dstName; @@ -694,7 +694,7 @@ public function getAvailableCoreVersionInfo() $body = $response->getStringBody(); } catch (InvalidArgumentException $e) { // ユニットテストの場合にhttpでアクセスできないので、ファイルから直接読み込む - $file = new File($releaseUrl); + $file = new BcFile($releaseUrl); $body = $file->read(); } catch (NetworkException $e) { return []; diff --git a/plugins/baser-core/src/Utility/BcFolder.php b/plugins/baser-core/src/Utility/BcFolder.php index f4be9937e8..ba45147dc0 100644 --- a/plugins/baser-core/src/Utility/BcFolder.php +++ b/plugins/baser-core/src/Utility/BcFolder.php @@ -173,7 +173,7 @@ public function delete() rmdir($this->path); return true; } - + /** * ディレクトリをコピーする * @checked @@ -185,12 +185,11 @@ public function copy($source, $dest): bool if (!is_dir($source)) return false; if(is_dir($source)) { $dir_handle=opendir($source); - $sourceFolder = basename($source); - mkdir($dest."/".$sourceFolder); + mkdir($dest); while($file=readdir($dir_handle)){ if($file!="." && $file!=".."){ if(is_dir($source."/".$file)){ - self::copy($source."/".$file, $dest."/".$sourceFolder); + self::copy($source .DS. $file, $dest .DS. $file); } else { copy($source."/".$file, $dest."/".$file); } @@ -202,7 +201,7 @@ public function copy($source, $dest): bool } return true; } - + /** * ディレクトリを移動する * @checked diff --git a/plugins/baser-core/tests/TestCase/Controller/Api/Admin/PluginsControllerTest.php b/plugins/baser-core/tests/TestCase/Controller/Api/Admin/PluginsControllerTest.php index d3b7885f4e..85cbf2d7d0 100644 --- a/plugins/baser-core/tests/TestCase/Controller/Api/Admin/PluginsControllerTest.php +++ b/plugins/baser-core/tests/TestCase/Controller/Api/Admin/PluginsControllerTest.php @@ -16,7 +16,6 @@ use Cake\Core\App; use Cake\Core\Configure; use Cake\Core\Configure\Engine\PhpConfig; -use Cake\Filesystem\Folder; use Cake\TestSuite\IntegrationTestTrait; use Composer\Package\Archiver\ZipArchiver; diff --git a/plugins/baser-core/tests/TestCase/Service/PluginsServiceTest.php b/plugins/baser-core/tests/TestCase/Service/PluginsServiceTest.php index a5c2114ea3..50efdd9e77 100644 --- a/plugins/baser-core/tests/TestCase/Service/PluginsServiceTest.php +++ b/plugins/baser-core/tests/TestCase/Service/PluginsServiceTest.php @@ -15,6 +15,8 @@ use BaserCore\Test\Factory\PluginFactory; use BaserCore\Test\Factory\SiteConfigFactory; use BaserCore\TestSuite\BcTestCase; +use BaserCore\Utility\BcFile; +use BaserCore\Utility\BcFolder; use BaserCore\Utility\BcUtil; use Cake\Cache\Cache; use Cake\Core\Configure; @@ -388,9 +390,9 @@ public function test_add() { $path = BASER_PLUGINS . 'BcThemeSample'; $zipSrcPath = TMP . 'zip' . DS; - $folder = new Folder(); - $folder->create($zipSrcPath, 0777); - $folder->copy($zipSrcPath . 'BcThemeSample2', ['from' => $path, 'mode' => 0777]); + $folder = new BcFolder($zipSrcPath); + $folder->create(); + $folder->copy($path, $zipSrcPath. 'BcThemeSample2'); $plugin = 'BcThemeSample2'; $zip = new ZipArchiver(); $testFile = $zipSrcPath . $plugin . '.zip'; @@ -415,8 +417,9 @@ public function test_add() $this->assertTrue(is_dir(ROOT . DS . 'plugins' . DS . $plugin)); // 既に /plugins/ 内に同名のプラグインが存在する場合には、数字付きのディレクトリ名(PluginName2)にリネームする。 - $folder->create($zipSrcPath, 0777); - $folder->copy($zipSrcPath . 'BcThemeSample2', ['from' => $path, 'mode' => 0777]); + $folder = new BcFolder($zipSrcPath); + $folder->create(); + $folder->copy($path, $zipSrcPath . 'BcThemeSample2'); $zip = new ZipArchiver(); $zip->archive($zipSrcPath, $testFile, true); $this->setUploadFileToRequest('file', $testFile); @@ -432,10 +435,12 @@ public function test_add() $this->assertEquals('BcThemeSample3', $rs); //テスト実行後不要ファイルを削除 - $folder = new Folder(); - $folder->delete(ROOT . DS . 'plugins' . DS . $plugin); - $folder->delete(ROOT . DS . 'plugins' . DS . 'BcThemeSample22'); - $folder->delete($zipSrcPath); + $folder = new BcFolder(ROOT . DS . 'plugins' . DS . $plugin); + $folder->delete(); + $folder = new BcFolder(ROOT . DS . 'plugins' . DS . 'BcThemeSample22'); + $folder->delete(); + $folder = new BcFolder($zipSrcPath); + $folder->delete(); // TODO ローカルでは成功するが、GitHubActions上でうまくいかないためコメントアウト(原因不明) // post_max_size を超えた場合、サーバーに設定されているサイズ制限を超えた場合、 @@ -479,9 +484,8 @@ public function test_getAvailableCoreVersionInfo( // BcApp.coreReleaseUrl を書き換える Configure::write('BcApp.coreReleaseUrl', $rssPath); // バージョンを書き換える - $file = new File($versionPath); + $file = new BcFile($versionPath); $file->write($currentVersion); - $file->close(); // RSSを生成 $this->createReleaseRss($releaseVersions); // キャッシュを削除 @@ -551,9 +555,8 @@ public function createReleaseRss(array $versions) EOF; - $file = new File($url); + $file = new BcFile($url); $file->write($rss); - $file->close(); } } diff --git a/plugins/baser-core/tests/TestCase/Utility/BcFolderTest.php b/plugins/baser-core/tests/TestCase/Utility/BcFolderTest.php index 6853bc4fd2..be252e0b8c 100644 --- a/plugins/baser-core/tests/TestCase/Utility/BcFolderTest.php +++ b/plugins/baser-core/tests/TestCase/Utility/BcFolderTest.php @@ -134,16 +134,17 @@ public function test_copy() $path = TMP_TESTS . 'test'; $folder1 = new BcFolder($path); $folder1->create(); + (new BcFolder($path .DS. 'test1'))->create(); + (new BcFile($path .DS. 'test1' .DS. 'test1.txt'))->create(); $file = new BcFile($path. DS. 'test.txt'); $file->create(); $des = TMP_TESTS . 'test_des'; - $folder2 = new BcFolder($des); - $folder2->create(); - $result = $folder2->copy($path, $des); + $result = $folder1->copy($path, $des); $this->assertTrue($result); $this->assertFileExists($des. DS. 'test.txt'); + $this->assertFileExists($des. DS. 'test1' .DS. 'test1.txt'); $folder1->delete(); - $folder2->delete(); + (new BcFolder($des))->delete(); } /**