Skip to content

Commit

Permalink
PluginsService and PluginsServiceTest (#2845)
Browse files Browse the repository at this point in the history
  • Loading branch information
nghiem-mb authored Nov 15, 2023
1 parent ddb173d commit e8ec3fe
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 31 deletions.
16 changes: 8 additions & 8 deletions plugins/baser-core/src/Service/PluginsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 [];
Expand Down
9 changes: 4 additions & 5 deletions plugins/baser-core/src/Utility/BcFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public function delete()
rmdir($this->path);
return true;
}

/**
* ディレクトリをコピーする
* @checked
Expand All @@ -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);
}
Expand All @@ -202,7 +201,7 @@ public function copy($source, $dest): bool
}
return true;
}

/**
* ディレクトリを移動する
* @checked
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
29 changes: 16 additions & 13 deletions plugins/baser-core/tests/TestCase/Service/PluginsServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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';
Expand All @@ -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);
Expand All @@ -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 を超えた場合、サーバーに設定されているサイズ制限を超えた場合、
Expand Down Expand Up @@ -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);
// キャッシュを削除
Expand Down Expand Up @@ -551,9 +555,8 @@ public function createReleaseRss(array $versions)
</channel>
</rss>
EOF;
$file = new File($url);
$file = new BcFile($url);
$file->write($rss);
$file->close();
}

}
9 changes: 5 additions & 4 deletions plugins/baser-core/tests/TestCase/Utility/BcFolderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down

0 comments on commit e8ec3fe

Please sign in to comment.