From 08c49094f814c6761c4c8af211ce49cf5da1a29e Mon Sep 17 00:00:00 2001 From: thangnnmd <150879641+thangnnmd@users.noreply.github.com> Date: Thu, 17 Oct 2024 05:38:57 +0700 Subject: [PATCH] InstallationsService::addDefaultUser (#3901) Co-authored-by: thangnn --- .../src/Service/InstallationsService.php | 1 + .../Service/InstallationsServiceTest.php | 36 ++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/plugins/bc-installer/src/Service/InstallationsService.php b/plugins/bc-installer/src/Service/InstallationsService.php index b027df2a81..d7e076edc2 100644 --- a/plugins/bc-installer/src/Service/InstallationsService.php +++ b/plugins/bc-installer/src/Service/InstallationsService.php @@ -291,6 +291,7 @@ public function setAdminEmailAndVersion(string $email) * @throws PersistenceFailedException * @checked * @noTodo + * @unitTest */ public function addDefaultUser(array $user) { diff --git a/plugins/bc-installer/tests/TestCase/Service/InstallationsServiceTest.php b/plugins/bc-installer/tests/TestCase/Service/InstallationsServiceTest.php index 2e4069072e..6ea99e31fd 100644 --- a/plugins/bc-installer/tests/TestCase/Service/InstallationsServiceTest.php +++ b/plugins/bc-installer/tests/TestCase/Service/InstallationsServiceTest.php @@ -14,6 +14,7 @@ use BaserCore\Test\Factory\ContentFactory; use BaserCore\Test\Factory\ContentFolderFactory; use BaserCore\Test\Factory\SiteFactory; +use BaserCore\Test\Scenario\InitAppScenario; use BaserCore\TestSuite\BcTestCase; use BaserCore\Utility\BcContainerTrait; use BaserCore\Utility\BcFile; @@ -22,6 +23,7 @@ use BcInstaller\Service\InstallationsServiceInterface; use Cake\Core\Configure; use Cake\ORM\Exception\PersistenceFailedException; +use CakephpFixtureFactories\Scenario\ScenarioAwareTrait; /** * InstallationsServiceTest @@ -34,6 +36,7 @@ class InstallationsServiceTest extends BcTestCase * Trait */ use BcContainerTrait; + use ScenarioAwareTrait; /** * setup @@ -162,7 +165,38 @@ public function testSetAdminEmailAndVersion() */ public function testAddDefaultUser() { - $this->markTestIncomplete('このテストは、まだ実装されていません。'); + $this->loadFixtureScenario(InitAppScenario::class); + $this->loginAdmin($this->getRequest('/')); + + //data + $userData = [ + 'name' => 'testuser', + 'email' => 'testuser@example.com', + 'password_1' => 'Password1234', + 'password_2' => 'Password1234' + ]; + + $result = $this->Installations->addDefaultUser($userData); + $this->assertEquals('testuser', $result['name']); + $this->assertEquals('testuser@example.com', $result['email']); + $this->assertEquals('testuser', $result['real_name_1']); + $this->assertCount(1, $result['user_groups']); + } + + public function testAddDefaultUserThrowsException() + { + $this->loadFixtureScenario(InitAppScenario::class); + $this->loginAdmin($this->getRequest('/')); + + $userData = [ + 'email' => 'testuser@example.com', + 'password_1' => 'password123', + 'password_2' => 'differentpassword' + ]; + + $this->expectException(PersistenceFailedException::class); + $this->expectExceptionMessage('Entity save failure. Found the following errors (password.minLength: "パスワードは12文字以上で入力してください。", password.passwordConfirm: "パスワードが同じものではありません。", password.passwordRequiredCharacterType: "パスワード'); + $this->Installations->addDefaultUser($userData); } /**