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

Dev #2773 bc plugin #2797

Merged
merged 9 commits into from
Oct 19, 2023
Merged
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
22 changes: 11 additions & 11 deletions plugins/baser-core/src/BcPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
use BaserCore\Service\PermissionGroupsService;
use BaserCore\Service\PermissionGroupsServiceInterface;
use BaserCore\Utility\BcContainerTrait;
use BaserCore\Utility\BcFolder;
use BaserCore\Utility\BcUpdateLog;
use BaserCore\Utility\BcUtil;
use Cake\Core\BasePlugin;
use Cake\Core\Configure;
use Cake\Core\Configure\Engine\PhpConfig;
use Cake\Core\PluginApplicationInterface;
use Cake\Datasource\ConnectionManager;
use Cake\Filesystem\Folder;
use Cake\Http\ServerRequestFactory;
use Cake\I18n\FrozenTime;
use Cake\Log\LogTrait;
Expand Down Expand Up @@ -241,12 +241,12 @@ public function getUpdaters($name = '')

// 有効化されていない可能性があるため CakePlugin::path() は利用しない
$path = BcUtil::getPluginPath($name) . 'config' . DS . 'update';
$folder = new Folder($path);
$files = $folder->read(true, true);
$folder = new BcFolder($path);
$files = $folder->getFolders();
$updaters = [];
$updateVerPoints = [];
if (!empty($files[0])) {
foreach($files[0] as $folder) {
if (!empty($files)) {
foreach($files as $folder) {
$updateVersion = $folder;
$updateVerPoints[$updateVersion] = BcUtil::verpoint($updateVersion);
}
Expand Down Expand Up @@ -289,12 +289,12 @@ public function getUpdateScriptMessages($name = '')

// 有効化されていない可能性があるため CakePlugin::path() は利用しない
$path = BcUtil::getPluginPath($name) . 'config' . DS . 'update';
$folder = new Folder($path);
$files = $folder->read(true, true);
$folder = new BcFolder($path);
$files = $folder->getFolders();
$messages = [];
$updateVerPoints = [];
if (!empty($files[0])) {
foreach($files[0] as $folder) {
if (!empty($files)) {
foreach($files as $folder) {
$updateVersion = $folder;
$updateVerPoints[$updateVersion] = BcUtil::verpoint($updateVersion);
}
Expand Down Expand Up @@ -336,8 +336,8 @@ public function uninstall($options = []): bool

$pluginPath = BcUtil::getPluginPath($pluginName);
if ($pluginPath) {
$Folder = new Folder();
$Folder->delete($pluginPath);
$Folder = new BcFolder($pluginPath);
$Folder->delete();
}
/** @var PermissionGroupsService $permissionGroupsService */
$permissionGroupsService = $this->getService(PermissionGroupsServiceInterface::class);
Expand Down
41 changes: 41 additions & 0 deletions plugins/baser-core/src/Utility/BcFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,47 @@ public function delete()
rmdir($this->path);
return true;
}

/**
* ディレクトリをコピーする
* @checked
* @noTodo
* @unitTest
*/
public function copy($source, $dest): bool
ryuring marked this conversation as resolved.
Show resolved Hide resolved
{
if (!is_dir($source)) return false;
if(is_dir($source)) {
$dir_handle=opendir($source);
$sourceFolder = basename($source);
mkdir($dest."/".$sourceFolder);
while($file=readdir($dir_handle)){
if($file!="." && $file!=".."){
if(is_dir($source."/".$file)){
self::copy($source."/".$file, $dest."/".$sourceFolder);
} else {
copy($source."/".$file, $dest."/".$file);
}
}
}
closedir($dir_handle);
} else {
copy($source, $dest);
}
return true;
}

/**
* ディレクトリを移動する
* @checked
* @noTodo
* @unitTest
*/
public function move($source, $dest): bool
ryuring marked this conversation as resolved.
Show resolved Hide resolved
{
if (!is_dir($source)) return false;
return $this->copy($source, $dest) && $this->delete();
}

/**
* ディレクトリ構造のモードを再帰的に変更します。これにはファイルのモードも変更することが含まれます。
Expand Down
127 changes: 68 additions & 59 deletions plugins/baser-core/tests/TestCase/BcPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
use BaserCore\Test\Factory\PluginFactory;
use BaserCore\Test\Factory\UserFactory;
use BaserCore\TestSuite\BcTestCase;
use BaserCore\Utility\BcFile;
use BaserCore\Utility\BcFolder;
use BaserCore\Utility\BcUtil;
use Cake\Core\Plugin;
use Cake\Datasource\ConnectionManager;
use Cake\Filesystem\File;
use Cake\Filesystem\Folder;
use Cake\ORM\TableRegistry;
use Cake\Routing\Router;
use Cake\TestSuite\IntegrationTestTrait;
Expand Down Expand Up @@ -103,22 +103,15 @@ public function testInstallAndUninstall()
// アンインストール
$from = BcUtil::getPluginPath('BcBlog');
$pluginDir = dirname($from);
$folder = new Folder();
$folder = new BcFolder($from);
$to = $pluginDir . DS . 'BcBlogBak';
$folder->copy($to, [
'from' => $from,
'mode' => 0777
]);
$folder->create($from, 0777);
$folder->copy($from, $to);
$folder->create();
$this->BcPlugin->uninstall(['connection' => 'test']);
$this->assertFalse(is_dir($from));
$plugins = $this->getTableLocator()->get('BaserCore.Plugins')->find()->where(['name' => 'BcBlog'])->first();
$this->assertNull($plugins);
$folder->move($from, [
'from' => $to,
'mode' => 0777,
'schema' => Folder::OVERWRITE
]);
$folder->move($from, $to);
$this->BcPlugin->install(['connection' => 'test']);
}

Expand Down Expand Up @@ -207,39 +200,41 @@ public function test_getUpdateScriptMessagesAndGetUpdaters()
$pluginPath = ROOT . DS . 'plugins' . DS . $name . DS;
$updatePath = $pluginPath . 'config' . DS . 'update' . DS;
PluginFactory::make(['name' => $name, 'title' => 'サンプル', 'version' => '1.0.0'])->persist();
$folder = new Folder();
$folder = new BcFolder($pluginPath);

// 新バージョン
$folder->create($pluginPath);
$file = new File($pluginPath . 'VERSION.txt');
$folder->create();
$file = new BcFile($pluginPath . 'VERSION.txt');
$file->create();
$file->write('1.0.3');
$file->close();
// アップデートスクリプト 0.0.1
$folder->create($updatePath . '0.0.1');
$file = new File($updatePath . '0.0.1' . DS . 'config.php');
$folder = new BcFolder($updatePath . '0.0.1');
$folder->create();
$file = new BcFile($updatePath . '0.0.1' . DS . 'config.php');
$file->create();
$file->write('<?php return [\'updateMessage\' => \'test0\'];');
$file->close();
// アップデートスクリプト 1.0.1
$folder->create($updatePath . '1.0.1');
$file = new File($updatePath . '1.0.1' . DS . 'config.php');
$folder = new BcFolder($updatePath . '1.0.1');
$folder->create();
$file = new BcFile($updatePath . '1.0.1' . DS . 'config.php');
$file->create();
$file->write('<?php return [\'updateMessage\' => \'test1\'];');
$file->close();
$file = new File($updatePath . '1.0.1' . DS . 'updater.php');
$file = new BcFile($updatePath . '1.0.1' . DS . 'updater.php');
$file->create();
$file->close();
// アップデートスクリプト 1.0.2
$folder->create($updatePath . '1.0.2');
$file = new File($updatePath . '1.0.2' . DS . 'config.php');
$folder = new BcFolder($updatePath . '1.0.2');
$folder->create();
$file = new BcFile($updatePath . '1.0.2' . DS . 'config.php');
$file->create();
$file->write('<?php return [\'updateMessage\' => \'test2\'];');
$file->close();
$file = new File($updatePath . '1.0.2' . DS . 'updater.php');
$file = new BcFile($updatePath . '1.0.2' . DS . 'updater.php');
$file->create();
$file->close();
// アップデートスクリプト 1.0.4
$folder->create($updatePath . '1.0.4');
$file = new File($updatePath . '1.0.4' . DS . 'config.php');
$folder = new BcFolder($updatePath . '1.0.4');
$folder->create();
$file = new BcFile($updatePath . '1.0.4' . DS . 'config.php');
$file->create();
$file->write('<?php return [\'updateMessage\' => \'test3\'];');
$file->close();

$this->assertEquals(
['Sample-1.0.1' => 'test1', 'Sample-1.0.2' => 'test2'],
Expand All @@ -249,7 +244,8 @@ public function test_getUpdateScriptMessagesAndGetUpdaters()
['Sample-1.0.1' => 1000001000, 'Sample-1.0.2' => 1000002000],
$this->BcPlugin->getUpdaters($name)
);
$folder->delete($pluginPath);
$folder = new BcFolder($pluginPath);
$folder->delete();
}

/**
Expand All @@ -268,27 +264,27 @@ public function test_execScript()
$this->assertTrue($this->BcPlugin->execScript($version));
// 有効スクリプトあり
UserFactory::make(['id' => 1, 'name' => 'test'])->persist();
$folder = new Folder();
$folder->create($versionPath);
$file = new File($versionPath . DS . 'updater.php');
$folder = new BcFolder($versionPath);
$folder->create();
$file = new BcFile($versionPath . DS . 'updater.php');
$file->create();
$file->write('<?php
use Cake\ORM\TableRegistry;
$users = TableRegistry::getTableLocator()->get(\'BaserCore.Users\');
$user = $users->find()->where([\'id\' => 1])->first();
$user->name = \'hoge\';
$users->save($user);');
$file->close();
$this->BcPlugin->execScript($version);
$users = $this->getTableLocator()->get('BaserCore.Users');
$user = $users->find()->where(['id' => 1])->first();
$this->assertEquals('hoge', $user->name);
// 無効スクリプトあり
$file = new File($versionPath . DS . 'updater.php');
$file = new BcFile($versionPath . DS . 'updater.php');
$file->create();
$file->write('<?php
$this->log(\'test\');');
$file->close();
$this->BcPlugin->execScript($version);
$file = new File(LOGS . 'cli-error.log');
$file = new BcFile(LOGS . 'cli-error.log');
$log = $file->read();
$this->assertStringContainsString('test', $log);
// 初期化
Expand All @@ -311,17 +307,20 @@ public function test_createAssetsSymlink()
public function test_migrate()
{
$pluginPath = ROOT . DS . 'plugins' . DS . 'BcTest' . DS;
$folder = new Folder();
$folder = new BcFolder($pluginPath);

// プラグインフォルダを初期化
$folder->delete($pluginPath);
$folder->delete();
$configPath = $pluginPath . 'config' . DS;
$migrationPath = $configPath . 'Migrations' . DS;
$seedPath = $configPath . 'Seeds' . DS;
$srcPath = $pluginPath . 'src' . DS;
$folder->create($srcPath);
$folder->create($migrationPath);
$folder->create($seedPath);
$folder = new BcFolder($srcPath);
$folder->create();
$folder = new BcFolder($migrationPath);
$folder->create();
$folder = new BcFolder($seedPath);
$folder->create();

// VERSION.txt
$this->createVersionFile($pluginPath, '0.0.1');
Expand Down Expand Up @@ -364,8 +363,9 @@ public function test_migrate()
*/
public function createPluginFile($srcPath)
{
$file = new File($srcPath . 'Plugin.php');
$file->write('<?php
$file = new BcFile($srcPath . 'Plugin.php');
$file->create();
$file->write('<?php
namespace BcTest;
use BaserCore\BcPlugin;
class Plugin extends BcPlugin {}');
Expand All @@ -378,7 +378,8 @@ class Plugin extends BcPlugin {}');
*/
public function createAlterMigrationFile($migrationPath)
{
$file = new File($migrationPath . '20220627000000_AlterBcTest.php', 'w');
$file = new BcFile($migrationPath . '20220627000000_AlterBcTest.php', 'w');
$file->create();
$file->write('<?php
use Migrations\AbstractMigration;
class AlterBcTest extends AbstractMigration
Expand All @@ -399,7 +400,8 @@ public function change()
*/
public function createInitialMigrationFile($migrationPath)
{
$file = new File($migrationPath . '20220626000000_InitialBcTest.php', 'w');
$file = new BcFile($migrationPath . '20220626000000_InitialBcTest.php', 'w');
$file->create();
$file->write('<?php
use Migrations\AbstractMigration;
class InitialBcTest extends AbstractMigration
Expand Down Expand Up @@ -429,7 +431,8 @@ public function down()
*/
public function createVersionFile($pluginPath, $version)
{
$file = new File($pluginPath . 'VERSION.txt');
$file = new BcFile($pluginPath . 'VERSION.txt');
$file->create();
$file->write($version);
}

Expand All @@ -440,7 +443,8 @@ public function createVersionFile($pluginPath, $version)
*/
public function createUpdater($updaterPath)
{
$file = new File($updaterPath . 'updater.php', 'w');
$file = new BcFile($updaterPath . 'updater.php', 'w');
$file->create();
$file->write('<?php
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
Expand All @@ -454,17 +458,20 @@ public function createUpdater($updaterPath)
public function test_execUpdater()
{
$pluginPath = ROOT . DS . 'plugins' . DS . 'BcTest' . DS;
$folder = new Folder();
$folder = new BcFolder($pluginPath);

// プラグインフォルダを初期化
$folder->delete($pluginPath);
$folder->delete();
$configPath = $pluginPath . 'config' . DS;
$migrationPath = $configPath . 'Migrations' . DS;
$seedPath = $configPath . 'Seeds' . DS;
$srcPath = $pluginPath . 'src' . DS;
$folder->create($srcPath);
$folder->create($migrationPath);
$folder->create($seedPath);
$folder = new BcFolder($srcPath);
$folder->create();
$folder = new BcFolder($migrationPath);
$folder->create();
$folder = new BcFolder($seedPath);
$folder->create();

// VERSION.txt
$this->createVersionFile($pluginPath, '0.0.1');
Expand All @@ -484,7 +491,8 @@ public function test_execUpdater()

// config/update/0.0.2/updater.php
$updaterPath = $configPath . 'update' . DS . '0.0.2' . DS;
$folder->create($updaterPath);
$folder = new BcFolder($updaterPath);
$folder->create();
$this->createUpdater($updaterPath);

// アップデート実行
Expand All @@ -494,7 +502,8 @@ public function test_execUpdater()
$this->assertEquals('2022-06-26', (string) $entity->name);

// 初期化
$folder->delete($pluginPath);
$folder = new BcFolder($pluginPath);
$folder->delete();
$this->dropTable('bc_test');
$this->dropTable('bc_test_phinxlog');
}
Expand Down
Loading