Skip to content

Commit

Permalink
fix baserproject#1959 API カスタムテーブル 編集 (baserproject#1962)
Browse files Browse the repository at this point in the history
Co-authored-by: Đỗ Văn Hùng <HungDV2022>
  • Loading branch information
HungDV2022 committed Mar 12, 2023
1 parent e47751f commit f1f8e64
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,34 @@ public function add(CustomTablesServiceInterface $service)
* 編集API
*
* @param CustomTablesServiceInterface $service
* @param int $id
*
* @checked
* @noTodo
* @unitTest
*/
public function edit(CustomTablesServiceInterface $service)
public function edit(CustomTablesServiceInterface $service, int $id)
{
//todo 編集API
$this->request->allowMethod(['post', 'put']);
$customTable = $errors = null;
try {
$customTable = $service->update($service->get($id), $this->request->getData());
$message = __d('baser_core', 'テーブル「{0}」を更新しました。', $customTable->title);
} catch (PersistenceFailedException $e) {
$errors = $e->getEntity()->getErrors();
$message = __d('baser_core', "入力エラーです。内容を修正してください。");
$this->setResponse($this->response->withStatus(400));
} catch (\Throwable $e) {
$message = __d('baser_core', 'データベース処理中にエラーが発生しました。' . $e->getMessage());
$this->setResponse($this->response->withStatus(500));
}

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
use BaserCore\Service\BcDatabaseServiceInterface;
use BaserCore\Test\Scenario\InitAppScenario;
use BaserCore\TestSuite\BcTestCase;
use BaserCore\Utility\BcContainerTrait;
use BcCustomContent\Service\CustomTablesServiceInterface;
use CakephpFixtureFactories\Scenario\ScenarioAwareTrait;
use Cake\TestSuite\IntegrationTestTrait;

/**
* Class CustomTablesControllerTest
Expand All @@ -27,7 +28,7 @@ class CustomTablesControllerTest extends BcTestCase
* ScenarioAwareTrait
*/
use ScenarioAwareTrait;
use IntegrationTestTrait;
use BcContainerTrait;

/**
* Fixtures
Expand Down Expand Up @@ -144,7 +145,47 @@ public function test_add()
*/
public function test_edit()
{
$this->markTestIncomplete('このテストは、まだ実装されていません。');
//サービスをコル
$dataBaseService = $this->getService(BcDatabaseServiceInterface::class);
$customTable = $this->getService(CustomTablesServiceInterface::class);

//テストデータを生成
$data = [
'type' => 'contact',
'name' => 'contact',
'title' => 'お問い合わせタイトル',
'display_field' => 'お問い合わせ'
];
$customTable->create($data);

//APIを呼ぶ
$this->post('/baser/api/bc-custom-content/custom_tables/edit/1.json?token=' . $this->accessToken, ['name' => 'contact_edit']);
//ステータスを確認
$this->assertResponseOk();
//戻る値を確認
$result = json_decode((string)$this->_response->getBody());
$this->assertEquals('テーブル「お問い合わせタイトル」を更新しました。', $result->message);
$this->assertEquals('contact_edit', $result->customTable->name);

//テーブル名も変更されたの確認
$this->assertTrue($dataBaseService->tableExists('custom_entry_1_contact_edit'));
//変更した前テーブル名が存在しないの確認
$this->assertFalse($dataBaseService->tableExists('custom_entry_1_contact'));

//エラーする時をテスト
//APIを呼ぶ
$this->post('/baser/api/bc-custom-content/custom_tables/edit/1.json?token=' . $this->accessToken, ['name' => 'あああああ']);
//ステータスを確認
$this->assertResponseCode(400);
//戻る値を確認
$result = json_decode((string)$this->_response->getBody());
$this->assertEquals('入力エラーです。内容を修正してください。', $result->message);
$this->assertEquals(
'識別名は半角英数字とアンダースコアのみで入力してください。',
$result->errors->name->regex);

//不要なテーブルを削除
$dataBaseService->dropTable('custom_entry_1_contact_edit');
}

/**
Expand Down

0 comments on commit f1f8e64

Please sign in to comment.