Skip to content

Commit 1d1b647

Browse files
authored
fix #1805 テーマフォルダAPI ファイルアップロード (#1818)
1 parent 68227bc commit 1d1b647

File tree

4 files changed

+74
-19
lines changed

4 files changed

+74
-19
lines changed

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,38 @@ public function img_thumb(ThemeFilesServiceInterface $service)
249249
{
250250
//todo テーマファイルAPI 画像のサムネイルを表示 #1777
251251
}
252+
253+
/**
254+
* テーマフォルダAPI テーマファイルアップロード
255+
*
256+
* @param ThemeFilesServiceInterface $service
257+
* @return void
258+
*
259+
* @noTodo
260+
* @checked
261+
* @unitTest
262+
*/
263+
public function upload(ThemeFilesServiceInterface $service)
264+
{
265+
$this->request->allowMethod(['post', 'put']);
266+
try {
267+
$data = $this->getRequest()->getData();
268+
$data['fullpath'] = $service->getFullpath($data['theme'], $data['type'], $data['path']);
269+
$service->upload($data['fullpath'], $data);
270+
$message = __d('baser', 'アップロードに成功しました。');
271+
} catch (BcFormFailedException $e) {
272+
$this->setResponse($this->response->withStatus(400));
273+
$errors = $e->getForm()->getErrors();
274+
$message = __d('baser', '入力エラーです。内容を修正してください。' . $e->getMessage());
275+
} catch (\Throwable $e) {
276+
$this->setResponse($this->response->withStatus(400));
277+
$message = __d('baser', '処理中にエラーが発生しました。');
278+
}
279+
280+
$this->set([
281+
'message' => $message,
282+
'errors' => $errors ?? null
283+
]);
284+
$this->viewBuilder()->setOption('serialize', ['message', 'errors']);
285+
}
252286
}

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -231,17 +231,6 @@ public function copy(ThemeFoldersServiceInterface $service)
231231
//todo テーマフォルダAPI テーマフォルダコピー
232232
}
233233

234-
/**
235-
* テーマフォルダAPI テーマフォルダアップロード
236-
*
237-
* @param ThemeFoldersServiceInterface $service
238-
* @return void
239-
*/
240-
public function upload(ThemeFoldersServiceInterface $service)
241-
{
242-
//todo テーマフォルダAPI テーマフォルダアップロード
243-
}
244-
245234
/**
246235
* テーマフォルダAPI 現在のテーマにテーマフォルダをコピー
247236
*

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,44 @@ public function test_img_thumb()
271271
{
272272
$this->markTestIncomplete('このテストは未実装です。');
273273
}
274+
275+
/**
276+
* [API] テーマフォルダAPI テーマファイルアップロード
277+
*/
278+
public function test_upload()
279+
{
280+
//テストテーマフォルダを作成
281+
$fullpath = BASER_PLUGINS . 'BcThemeSample' . '/templates/layout/';
282+
mkdir($fullpath . 'new_folder', 0777);
283+
284+
//テストファイルを作成
285+
$filePath = TMP . 'test_upload' . DS;
286+
mkdir( TMP . 'test_upload', 0777);
287+
$testFile = $filePath . 'uploadTestFile.html';
288+
new File($testFile, true);
289+
290+
//Postデータを生成
291+
$data = [
292+
'theme' => 'BcThemeSample',
293+
'type' => 'layout',
294+
'path' => 'new_folder',
295+
];
296+
$this->setUploadFileToRequest('file', $testFile);
297+
$this->setUnlockedFields(['file']);
298+
//APIをコール
299+
$this->post('/baser/api/bc-theme-file/theme_files/upload.json?token=' . $this->accessToken, $data);
300+
//レスポンスコードを確認
301+
$this->assertResponseSuccess();
302+
//戻る値を確認
303+
$result = json_decode((string)$this->_response->getBody());
304+
$this->assertEquals('アップロードに成功しました。', $result->message);
305+
//実際にファイルが存在するか確認すること
306+
$this->assertTrue(file_exists($fullpath . 'new_folder/uploadTestFile.html'));
307+
308+
//テストファイルとフォルダを削除
309+
unlink($testFile);
310+
rmdir($filePath);
311+
unlink($fullpath . 'new_folder/uploadTestFile.html');
312+
rmdir($fullpath . 'new_folder');
313+
}
274314
}

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,6 @@ public function test_copy()
187187
$this->markTestIncomplete('このテストは未実装です。');
188188
}
189189

190-
/**
191-
* [API] テーマフォルダ テーマフォルダアップロード
192-
*/
193-
public function test_upload()
194-
{
195-
$this->markTestIncomplete('このテストは未実装です。');
196-
}
197-
198190
/**
199191
* [API] テーマフォルダ テーマフォルダコピー
200192
*/

0 commit comments

Comments
 (0)