Skip to content

Commit 581b43a

Browse files
authored
fix #1801 テーマフォルダAPI フォルダ新規追加 (#1814)
1 parent e50554e commit 581b43a

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

plugins/bc-theme-file/src/Controller/Api/ThemeFoldersController.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,36 @@ public function index(ThemeFoldersServiceInterface $service)
112112
*
113113
* @param ThemeFoldersServiceInterface $service
114114
* @return void
115+
*
116+
* @checked
117+
* @noTodo
118+
* @unitTest
115119
*/
116120
public function add(ThemeFoldersServiceInterface $service)
117121
{
118-
//todo テーマフォルダAPI テーマフォルダ新規追加
122+
$this->request->allowMethod(['post', 'put']);
123+
124+
try {
125+
$data = $this->getRequest()->getData();
126+
$data['fullpath'] = $service->getFullpath($data['theme'], $data['type'], $data['path']);
127+
$form = $service->create($data);
128+
$entity = $service->get($form->getData('fullpath'));
129+
$message = __d('baser', 'フォルダ「{0}」を作成しました。', $entity->name);
130+
} catch (BcFormFailedException $e) {
131+
$this->setResponse($this->response->withStatus(400));
132+
$errors = $e->getForm()->getErrors();
133+
$message = __d('baser', '入力エラーです。内容を修正してください。' . $e->getMessage());
134+
} catch (\Throwable $e) {
135+
$this->setResponse($this->response->withStatus(400));
136+
$message = __d('baser', '処理中にエラーが発生しました。');
137+
}
138+
139+
$this->set([
140+
'message' => $message,
141+
'entity' => $entity ?? null,
142+
'errors' => $errors ?? null
143+
]);
144+
$this->viewBuilder()->setOption('serialize', ['message', 'entity', 'errors']);
119145
}
120146

121147
/**

plugins/bc-theme-file/tests/TestCase/Controller/Api/ThemeFoldersControllerTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,24 @@ public function test_index()
8989
*/
9090
public function test_add()
9191
{
92-
$this->markTestIncomplete('このテストは未実装です。');
92+
$fullpath = BASER_PLUGINS . 'BcThemeSample' . '/templates/layout/';
93+
$data = [
94+
'theme' => 'BcThemeSample',
95+
'type' => 'layout',
96+
'path' => '',
97+
'name' => 'new_folder',
98+
];
99+
//APIをコール
100+
$this->post('/baser/api/bc-theme-file/theme_folders/add.json?token=' . $this->accessToken, $data);
101+
//レスポンスコードを確認
102+
$this->assertResponseSuccess();
103+
//戻る値を確認
104+
$result = json_decode((string)$this->_response->getBody());
105+
$this->assertEquals('フォルダ「new_folder」を作成しました。', $result->message);
106+
//実際にフォルダが作成されいてるか確認すること
107+
$this->assertTrue(is_dir($fullpath . 'new_folder'));
108+
//作成されたフォルダを削除
109+
rmdir($fullpath . 'new_folder');
93110
}
94111

95112
/**

0 commit comments

Comments
 (0)