Skip to content

Commit 1edad4c

Browse files
authored
fix #1770 テーマファイルAPI ファイル編集 (#1782)
1 parent 6e7ee39 commit 1edad4c

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ protected function parseArgs($args)
589589
$args[0] instanceof ThemeFoldersAdminServiceInterface ||
590590
$args[0] instanceof ThemeFilesServiceInterface ||
591591
$args[0] instanceof ThemeFoldersServiceInterface
592-
) {
592+
) {
593593
unset($args[0]);
594594
$args = array_merge($args);
595595
}

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use BaserCore\Annotation\UnitTest;
1717
use BaserCore\Controller\Api\BcApiController;
1818
use BaserCore\Error\BcFormFailedException;
19+
use BcThemeFile\Controller\ThemeFileAppController;
1920
use BcThemeFile\Service\ThemeFilesServiceInterface;
2021

2122
/**
@@ -58,10 +59,35 @@ public function add(ThemeFilesServiceInterface $service)
5859
* [API] テーマファイル ファイル編集
5960
*
6061
* @param ThemeFilesServiceInterface $service
62+
*
63+
* @noTodo
64+
* @checked
65+
* @unitTest
6166
*/
6267
public function edit(ThemeFilesServiceInterface $service)
6368
{
64-
//todo テーマファイルAPI ファイル編集 #1770
69+
$this->request->allowMethod(['post', 'put']);
70+
try {
71+
$data = $this->getRequest()->getData();
72+
$data['fullpath'] = $service->getFullpath($data['theme'], $data['type'], $data['path']);
73+
$themeFileForm = $service->update($data);
74+
$entity = $service->get($themeFileForm->getData('fullpath'));
75+
$message = __d('baser', 'ファイル「{0}」を更新しました。', $entity->name);
76+
} catch (BcFormFailedException $e) {
77+
$this->setResponse($this->response->withStatus(400));
78+
$errors = $e->getForm()->getErrors();
79+
$message = __d('baser', '入力エラーです。内容を修正してください。' . $e->getMessage());
80+
} catch (\Throwable $e) {
81+
$this->setResponse($this->response->withStatus(400));
82+
$message = __d('baser', '処理中にエラーが発生しました。');
83+
}
84+
85+
$this->set([
86+
'message' => $message,
87+
'entity' => $entity ?? null,
88+
'errors' => $errors ?? null
89+
]);
90+
$this->viewBuilder()->setOption('serialize', ['message', 'entity', 'errors']);
6591
}
6692

6793
/**
@@ -123,4 +149,5 @@ public function img_thumb(ThemeFilesServiceInterface $service)
123149
{
124150
//todo テーマファイルAPI 画像のサムネイルを表示 #1777
125151
}
152+
126153
}

plugins/bc-theme-file/src/Service/ThemeFilesService.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,9 @@ public function copyToTheme(array $params)
229229
}
230230
}
231231

232+
public function getFullpath(string $theme, string $type, string $path)
233+
{
234+
return Plugin::templatePath($theme) . $type . DS . $path;
235+
}
236+
232237
}

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use BaserCore\Test\Scenario\InitAppScenario;
1515
use BaserCore\TestSuite\BcTestCase;
16+
use Cake\Filesystem\File;
1617
use CakephpFixtureFactories\Scenario\ScenarioAwareTrait;
1718

1819
class ThemeFilesControllerTest extends BcTestCase
@@ -93,7 +94,59 @@ public function test_add()
9394
*/
9495
public function test_edit()
9596
{
96-
$this->markTestIncomplete('このテストは未実装です。');
97+
//POSTデータを生成
98+
$fullpath = BASER_PLUGINS . 'BcThemeSample' . '/templates/layout/';
99+
new File($fullpath . 'base_name_1.php', true);
100+
new File($fullpath . 'Admin/default.php', true);
101+
$data = [
102+
'theme' => 'BcThemeSample',
103+
'type' => 'layout',
104+
'path' => 'base_name_1.php',
105+
'base_name' => 'base_name_2',
106+
'contents' => 'this is a content changed!',
107+
'ext' => 'php',
108+
];
109+
//APIをコール
110+
$this->post('/baser/api/bc-theme-file/theme_files/edit.json?token=' . $this->accessToken, $data);
111+
//レスポンスコードを確認
112+
$this->assertResponseSuccess();
113+
//戻る値を確認
114+
$result = json_decode((string)$this->_response->getBody());
115+
$this->assertEquals('ファイル「base_name_2.php」を更新しました。', $result->message);
116+
$this->assertEquals($fullpath . 'base_name_2.php', $result->entity->fullpath);
117+
//実際にファイルが変更されいてるか確認すること
118+
$this->assertTrue(file_exists($fullpath . 'base_name_2.php'));
119+
//ファイルの中身を確認
120+
$this->assertEquals('this is a content changed!' , file_get_contents($fullpath . 'base_name_2.php'));
121+
//変更した前にファイル名が存在しないか確認すること
122+
$this->assertFalse(file_exists($fullpath . 'base_name_1.php'));
123+
124+
//path が、Admin/default.php と階層化されている場合、
125+
$data = [
126+
'theme' => 'BcThemeSample',
127+
'type' => 'layout',
128+
'path' => 'Admin/default.php',
129+
'base_name' => 'default_changed',
130+
'contents' => 'this is a content changed!',
131+
'ext' => 'php',
132+
];
133+
//APIをコール
134+
$this->post('/baser/api/bc-theme-file/theme_files/edit.json?token=' . $this->accessToken, $data);
135+
//レスポンスコードを確認
136+
$this->assertResponseSuccess();
137+
$result = json_decode((string)$this->_response->getBody());
138+
$this->assertEquals('ファイル「default_changed.php」を更新しました。', $result->message);
139+
$this->assertEquals($fullpath . 'Admin/default_changed.php', $result->entity->fullpath);
140+
//実際にファイルが変更されいてるか確認すること
141+
$this->assertTrue(file_exists($fullpath . 'Admin/default_changed.php'));
142+
//ファイルの中身を確認
143+
$this->assertEquals('this is a content changed!' , file_get_contents($fullpath . 'Admin/default_changed.php'));
144+
//変更した前にファイル名が存在しないか確認すること
145+
$this->assertFalse(file_exists($fullpath . 'Admin/default.php'));
146+
147+
//作成されたファイルを削除
148+
unlink($fullpath . 'base_name_2.php');
149+
unlink($fullpath . 'Admin/default_changed.php');
97150
}
98151

99152
/**

0 commit comments

Comments
 (0)