Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PluginsService and PluginsServiceTest #2841

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
52 changes: 26 additions & 26 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 @@ -93,11 +95,10 @@ public function testGetIndex($sortMode, $expectedPlugin): void
{
// テスト用のプラグインフォルダ作成
$pluginPath = App::path('plugins')[0] . DS . 'BcTest';
$folder = new Folder($pluginPath);
$folder->create($pluginPath, 0777);
$file = new File($pluginPath . DS . 'config.php');
$folder = new BcFolder($pluginPath);
$folder->create();
$file = new BcFile($pluginPath . DS . 'config.php');
$file->write("<?php return ['type' => 'Plugin'];");
$file->close();

$plugins = $this->Plugins->getIndex($sortMode);
$pluginNames = [];
Expand Down Expand Up @@ -140,14 +141,14 @@ public function testInstall()
}
// フォルダはあるがインストールできない場合
$pluginPath = App::path('plugins')[0] . DS . 'BcTest';
$folder = new Folder($pluginPath);
$folder->create($pluginPath, 0777);
$folder = new BcFolder($pluginPath);
$folder->create();
try {
$this->assertNull($this->Plugins->install('BcTest', true, 'test'));
} catch (\Exception $e) {
$this->assertEquals("プラグインに Plugin クラスが存在しません。src ディレクトリ配下に作成してください。", $e->getMessage());
}
$folder->delete($pluginPath);
$folder->delete();
}

/**
Expand Down Expand Up @@ -205,10 +206,10 @@ public function testGetInstallStatusMessage()
$this->assertEquals('既にインストール済のプラグインです。', $this->Plugins->getInstallStatusMessage('BcBlog'));
$this->assertEquals('インストールしようとしているプラグインのフォルダが存在しません。', $this->Plugins->getInstallStatusMessage('BcTest'));
$pluginPath = App::path('plugins')[0] . DS . 'BcTest';
$folder = new Folder($pluginPath);
$folder->create($pluginPath, 0777);
$folder = new BcFolder($pluginPath);
$folder->create();
$this->assertEquals('', $this->Plugins->getInstallStatusMessage('BcTest'));
$folder->delete($pluginPath);
$folder->delete();
}

/**
Expand All @@ -231,15 +232,15 @@ public function test_update()
$this->Plugins->install('BcPluginSample', true, 'test');
$pluginPath = Plugin::path('BcPluginSample');
rename($pluginPath . 'VERSION.txt', $pluginPath . 'VERSION.bak.txt');
$file = new File($pluginPath . 'VERSION.txt');
$file = new BcFile($pluginPath . 'VERSION.txt');
$file->write('10.0.0');
$this->Plugins->update('BcPluginSample', 'test');
$this->assertEquals('10.0.0', $this->Plugins->getVersion('BcPluginSample'));
rename($pluginPath . 'VERSION.bak.txt', $pluginPath . 'VERSION.txt');

// コア
rename(BASER . 'VERSION.txt', BASER . 'VERSION.bak.txt');
$file = new File(BASER . 'VERSION.txt');
$file = new BcFile(BASER . 'VERSION.txt');
$file->write('10.0.0');
$this->Plugins->update('BaserCore', 'test');
$plugins = array_merge(['BaserCore'], Configure::read('BcApp.corePlugins'));
Expand All @@ -257,7 +258,7 @@ public function test_update()
public function test_updateCoreFails()
{
rename(BASER . 'VERSION.txt', BASER . 'VERSION.bak.txt');
$file = new File(BASER . 'VERSION.txt');
$file = new BcFile(BASER . 'VERSION.txt');
$file->write('10.0.0');

// 失敗用のマイグレーションファイルを作成
Expand Down Expand Up @@ -285,7 +286,7 @@ public function test_updateCoreFails()
protected function createFailMigration()
{
$path = Plugin::path('BaserCore') . 'config' . DS . 'Migrations' . DS . '20230328000000_TestMigration.php';
$file = new File($path);
$file = new BcFile($path);
$data = <<< EOF
<?php
use BaserCore\Database\Migration\BcMigration;
Expand All @@ -295,7 +296,6 @@ public function up(){}
}
EOF;
$file->write($data);
$file->close();
return $path;
}

Expand Down Expand Up @@ -388,9 +388,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 . 'BcThemeSample2');
$folder->create();
$folder->copy($path, $zipSrcPath . 'BcThemeSample2');
$plugin = 'BcThemeSample2';
$zip = new ZipArchiver();
$testFile = $zipSrcPath . $plugin . '.zip';
Expand Down Expand Up @@ -432,10 +432,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 +481,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 +552,8 @@ public function createReleaseRss(array $versions)
</channel>
</rss>
EOF;
$file = new File($url);
$file = new BcFile($url);
$file->write($rss);
$file->close();
}

}
Loading