Skip to content

Commit

Permalink
fix #1802 テーマフォルダAPI フォルダ編集 (#1815)
Browse files Browse the repository at this point in the history
  • Loading branch information
HungDV2022 committed Feb 10, 2023
1 parent 581b43a commit b9dae41
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ public function add(ThemeFoldersServiceInterface $service)
$data = $this->getRequest()->getData();
$data['fullpath'] = $service->getFullpath($data['theme'], $data['type'], $data['path']);
$form = $service->create($data);
$entity = $service->get($form->getData('fullpath'));
$message = __d('baser', 'フォルダ「{0}」を作成しました。', $entity->name);
$themeFolder = $service->get($form->getData('fullpath'));
$message = __d('baser', 'フォルダ「{0}」を作成しました。', $themeFolder->name);
} catch (BcFormFailedException $e) {
$this->setResponse($this->response->withStatus(400));
$errors = $e->getForm()->getErrors();
Expand All @@ -138,21 +138,47 @@ public function add(ThemeFoldersServiceInterface $service)

$this->set([
'message' => $message,
'entity' => $entity ?? null,
'themeFolder' => $themeFolder ?? null,
'errors' => $errors ?? null
]);
$this->viewBuilder()->setOption('serialize', ['message', 'entity', 'errors']);
$this->viewBuilder()->setOption('serialize', ['message', 'themeFolder', 'errors']);
}

/**
* テーマフォルダAPI テーマフォルダ編集
*
* @param ThemeFoldersServiceInterface $service
* @return void
*
* @checked
* @noTodo
* @unitTest
*/
public function edit(ThemeFoldersServiceInterface $service)
{
//todo テーマフォルダAPI テーマフォルダ編集
$this->request->allowMethod(['post', 'put']);

try {
$data = $this->getRequest()->getData();
$data['fullpath'] = $service->getFullpath($data['theme'], $data['type'], $data['path']);
$form = $service->update($data);
$themeFolder = $service->get($form->getData('fullpath'));
$message = __d('baser', 'フォルダ名を「{0}」に変更しました。', $themeFolder->name);
} catch (BcFormFailedException $e) {
$this->setResponse($this->response->withStatus(400));
$errors = $e->getForm()->getErrors();
$message = __d('baser', '入力エラーです。内容を修正してください。' . $e->getMessage());
} catch (\Throwable $e) {
$this->setResponse($this->response->withStatus(400));
$message = __d('baser', '処理中にエラーが発生しました。');
}

$this->set([
'message' => $message,
'themeFolder' => $themeFolder ?? null,
'errors' => $errors ?? null
]);
$this->viewBuilder()->setOption('serialize', ['message', 'themeFolder', 'errors']);
}

/**
Expand Down
49 changes: 49 additions & 0 deletions plugins/bc-theme-file/src/Service/BcThemeFileService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* baserCMS : Based Website Development Project <https://basercms.net>
* Copyright (c) NPO baser foundation <https://baserfoundation.org/>
*
* @copyright Copyright (c) NPO baser foundation
* @link https://basercms.net baserCMS Project
* @since 5.0.0
* @license https://basercms.net/license/index.html MIT License
*/

namespace BcThemeFile\Service;

use BaserCore\Annotation\NoTodo;
use BaserCore\Annotation\Checked;
use BaserCore\Annotation\UnitTest;
use Cake\Core\Plugin;

/**
* BcThemeFileService
*
*/
class BcThemeFileService implements BcThemeFileServiceInterface
{
/**
* fullpathを作成
* @param string $theme
* @param string $type
* @param string $path
* @return string
*
* @checked
* @noTodo
*/
public function getFullpath(string $theme, string $type, string $path)
{
$assets = [
'css',
'js',
'img'
];
if (in_array($type, $assets)) {
$viewPath = Plugin::path($theme) . 'webroot' . DS . $type . DS . $path;
} else {
$viewPath = Plugin::templatePath($theme) . $type . DS . $path;
}
return $viewPath;
}
}
35 changes: 35 additions & 0 deletions plugins/bc-theme-file/src/Service/BcThemeFileServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* baserCMS : Based Website Development Project <https://basercms.net>
* Copyright (c) NPO baser foundation <https://baserfoundation.org/>
*
* @copyright Copyright (c) NPO baser foundation
* @link https://basercms.net baserCMS Project
* @since 5.0.0
* @license https://basercms.net/license/index.html MIT License
*/

namespace BcThemeFile\Service;

use BaserCore\Annotation\NoTodo;
use BaserCore\Annotation\Checked;
use BaserCore\Annotation\UnitTest;

/**
* BcThemeFileServiceInterface
*/
interface BcThemeFileServiceInterface
{
/**
* fullpathを作成
* @param string $theme
* @param string $type
* @param string $path
* @return string
*
* @checked
* @noTodo
* @unitTest
*/
public function getFullpath(string $theme, string $type, string $path);
}
8 changes: 1 addition & 7 deletions plugins/bc-theme-file/src/Service/ThemeFilesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*
* @property ThemeFileForm $ThemeFileForm
*/
class ThemeFilesService implements ThemeFilesServiceInterface
class ThemeFilesService extends BcThemeFileService implements ThemeFilesServiceInterface
{

/**
Expand Down Expand Up @@ -228,10 +228,4 @@ public function copyToTheme(array $params)
return false;
}
}

public function getFullpath(string $theme, string $type, string $path)
{
return Plugin::templatePath($theme) . $type . DS . $path;
}

}
27 changes: 1 addition & 26 deletions plugins/bc-theme-file/src/Service/ThemeFoldersService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
/**
* ThemeFoldersService
*/
class ThemeFoldersService implements ThemeFoldersServiceInterface
class ThemeFoldersService extends BcThemeFileService implements ThemeFoldersServiceInterface
{

/**
Expand Down Expand Up @@ -313,29 +313,4 @@ public function getForm(array $data)
{
return (new ThemeFolderForm())->setData($data);
}

/**
* fullpathを作成
* @param string $theme
* @param string $type
* @param string $path
* @return string
*
* @checked
* @noTodo
*/
public function getFullpath(string $theme, string $type, string $path)
{
$assets = [
'css',
'js',
'img'
];
if (in_array($type, $assets)) {
$viewPath = Plugin::path($theme) . 'webroot' . DS . $type . DS . $path;
} else {
$viewPath = Plugin::templatePath($theme) . $type . DS . $path;
}
return $viewPath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ public function test_add()
$this->assertResponseSuccess();
//戻る値を確認
$result = json_decode((string)$this->_response->getBody());
//themeFolderを確認
$this->assertEquals($fullpath . 'new_folder', $result->themeFolder->fullpath);
//メッセージを確認
$this->assertEquals('フォルダ「new_folder」を作成しました。', $result->message);
//実際にフォルダが作成されいてるか確認すること
$this->assertTrue(is_dir($fullpath . 'new_folder'));
Expand All @@ -114,7 +117,32 @@ public function test_add()
*/
public function test_edit()
{
$this->markTestIncomplete('このテストは未実装です。');
//テストテーマフォルダを作成
$fullpath = BASER_PLUGINS . 'BcThemeSample' . '/templates/layout/';
mkdir($fullpath . 'new_folder');
//Postデータを生成
$data = [
'theme' => 'BcThemeSample',
'type' => 'layout',
'path' => 'new_folder',
'name' => 'edit_folder',
];
//APIをコール
$this->post('/baser/api/bc-theme-file/theme_folders/edit.json?token=' . $this->accessToken, $data);
//レスポンスコードを確認
$this->assertResponseSuccess();
//戻る値を確認
$result = json_decode((string)$this->_response->getBody());
//themeFolderを確認
$this->assertEquals($fullpath . 'edit_folder', $result->themeFolder->fullpath);
//メッセージを確認
$this->assertEquals('フォルダ名を「edit_folder」に変更しました。', $result->message);
//実際にフォルダが変更されいてるか確認すること
$this->assertTrue(is_dir($fullpath . 'edit_folder'));
//変更前のフォルダが存在しないか確認すること
$this->assertFalse(is_dir($fullpath . 'new_folder'));
//変更されたフォルダを削除
rmdir($fullpath . 'edit_folder');
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* baserCMS : Based Website Development Project <https://basercms.net>
* Copyright (c) NPO baser foundation <https://baserfoundation.org/>
*
* @copyright Copyright (c) NPO baser foundation
* @link https://basercms.net baserCMS Project
* @since 5.0.0
* @license https://basercms.net/license/index.html MIT License
*/

namespace BcThemeFile\Test\TestCase\Service;

use BaserCore\TestSuite\BcTestCase;
use BcThemeFile\Service\BcThemeFileService;

/**
* BcThemeFileServiceTest
*/
class BcThemeFileServiceTest extends BcTestCase
{

public $BcThemeFileService = null;

/**
* set up
*/
public function setUp(): void
{
$this->setFixtureTruncate();
parent::setUp();
$this->BcThemeFileService = new BcThemeFileService();
}

/**
* tear down
*/
public function tearDown(): void
{
parent::tearDown();
}

/**
* test getFullpath
*/
public function test_getFullpath()
{
//typeが$assetsではない場合、
$themePath = $this->BcThemeFileService->getFullpath('BcThemeSample', 'layout', 'default.php');
$this->assertEquals('/var/www/html/plugins/BcThemeSample/templates/layout/default.php', $themePath);

//typeがimgの場合、
$themePath = $this->BcThemeFileService->getFullpath('BcFront', 'img', 'logo.png');
$this->assertEquals('/var/www/html/plugins/bc-front/webroot/img/logo.png', $themePath);
}
}

0 comments on commit b9dae41

Please sign in to comment.