From 82e48ad8cdb6fb9a647d25199d3e9415d481aa18 Mon Sep 17 00:00:00 2001 From: Nico Hoffmann Date: Fri, 7 Feb 2025 12:54:51 +0100 Subject: [PATCH] Add unit tests --- tests/Panel/Areas/AccountDialogsTest.php | 32 ++++++++++++++++++------ tests/Panel/Areas/AreaTestCase.php | 8 +++--- tests/Panel/Areas/UsersDialogsTest.php | 32 ++++++++++++++++++------ 3 files changed, 55 insertions(+), 17 deletions(-) diff --git a/tests/Panel/Areas/AccountDialogsTest.php b/tests/Panel/Areas/AccountDialogsTest.php index 79a4e041c4..ec8c74edb2 100644 --- a/tests/Panel/Areas/AccountDialogsTest.php +++ b/tests/Panel/Areas/AccountDialogsTest.php @@ -97,6 +97,7 @@ public function testChangePassword(): void $this->assertFormDialog($dialog); + $this->assertSame('Your current password', $props['fields']['currentPassword']['label']); $this->assertSame('New password', $props['fields']['password']['label']); $this->assertSame('Confirm the new password…', $props['fields']['passwordConfirmation']['label']); $this->assertSame('Change', $props['submitButton']); @@ -105,8 +106,9 @@ public function testChangePassword(): void public function testChangePasswordOnSubmit(): void { $this->submit([ - 'password' => '12345678', - 'passwordConfirmation' => '12345678' + 'currentPassword' => '12345678', + 'password' => 'abcdefgh', + 'passwordConfirmation' => 'abcdefgh' ]); $dialog = $this->dialog('account/changePassword'); @@ -114,14 +116,29 @@ public function testChangePasswordOnSubmit(): void $this->assertSame('user.changePassword', $dialog['event']); $this->assertSame(200, $dialog['code']); - $this->assertTrue($this->app->user('test')->validatePassword(12345678)); + $this->assertTrue($this->app->user('test')->validatePassword('abcdefgh')); + } + + public function testChangePasswordOnSubmitWithInvalidCurrentPassword(): void + { + $this->submit([ + 'currentPassword' => '123456', + 'password' => 'abcdefgh', + 'passwordConfirmation' => 'abcdefgh' + ]); + + $dialog = $this->dialog('account/changePassword'); + + $this->assertSame(400, $dialog['code']); + $this->assertSame('Wrong password', $dialog['error']); } public function testChangePasswordOnSubmitWithInvalidPassword(): void { $this->submit([ - 'password' => '1234567', - 'passwordConfirmation' => '1234567' + 'currentPassword' => '12345678', + 'password' => 'abcdef', + 'passwordConfirmation' => 'abcdef' ]); $dialog = $this->dialog('account/changePassword'); @@ -133,8 +150,9 @@ public function testChangePasswordOnSubmitWithInvalidPassword(): void public function testChangePasswordOnSubmitWithInvalidConfirmation(): void { $this->submit([ - 'password' => '12345678', - 'passwordConfirmation' => '1234567' + 'currentPassword' => '12345678', + 'password' => 'abcdefgh', + 'passwordConfirmation' => 'abcdefg' ]); $dialog = $this->dialog('account/changePassword'); diff --git a/tests/Panel/Areas/AreaTestCase.php b/tests/Panel/Areas/AreaTestCase.php index 760e1fb452..8077d6c861 100644 --- a/tests/Panel/Areas/AreaTestCase.php +++ b/tests/Panel/Areas/AreaTestCase.php @@ -4,6 +4,7 @@ use Kirby\Cms\App; use Kirby\Cms\Blueprint; +use Kirby\Cms\User; use Kirby\Filesystem\Dir; use Kirby\Http\Response; use Kirby\Panel\Panel; @@ -82,9 +83,10 @@ public function install(): void $this->app([ 'users' => [ [ - 'id' => 'test', - 'email' => 'test@getkirby.com', - 'role' => 'admin', + 'id' => 'test', + 'email' => 'test@getkirby.com', + 'role' => 'admin', + 'password' => User::hashPassword('12345678') ] ] ]); diff --git a/tests/Panel/Areas/UsersDialogsTest.php b/tests/Panel/Areas/UsersDialogsTest.php index 17995a95ce..035ad51913 100644 --- a/tests/Panel/Areas/UsersDialogsTest.php +++ b/tests/Panel/Areas/UsersDialogsTest.php @@ -160,6 +160,7 @@ public function testChangePassword(): void $this->assertFormDialog($dialog); + $this->assertSame('Your current password', $props['fields']['currentPassword']['label']); $this->assertSame('New password', $props['fields']['password']['label']); $this->assertSame('Confirm the new password…', $props['fields']['passwordConfirmation']['label']); $this->assertSame('Change', $props['submitButton']); @@ -168,8 +169,9 @@ public function testChangePassword(): void public function testChangePasswordOnSubmit(): void { $this->submit([ - 'password' => '12345678', - 'passwordConfirmation' => '12345678' + 'currentPassword' => '12345678', + 'password' => 'abcdefgh', + 'passwordConfirmation' => 'abcdefgh' ]); $dialog = $this->dialog('users/test/changePassword'); @@ -177,14 +179,29 @@ public function testChangePasswordOnSubmit(): void $this->assertSame('user.changePassword', $dialog['event']); $this->assertSame(200, $dialog['code']); - $this->assertTrue($this->app->user('test')->validatePassword(12345678)); + $this->assertTrue($this->app->user('test')->validatePassword('abcdefgh')); + } + + public function testChangePasswordOnSubmitWithInvalidCurrentPassword(): void + { + $this->submit([ + 'currentPassword' => '123456', + 'password' => 'abcdefgh', + 'passwordConfirmation' => 'abcdefgh' + ]); + + $dialog = $this->dialog('users/test/changePassword'); + + $this->assertSame(400, $dialog['code']); + $this->assertSame('Wrong password', $dialog['error']); } public function testChangePasswordOnSubmitWithInvalidPassword(): void { $this->submit([ - 'password' => '1234567', - 'passwordConfirmation' => '1234567' + 'currentPassword' => '12345678', + 'password' => 'abcdef', + 'passwordConfirmation' => 'abcdef' ]); $dialog = $this->dialog('users/test/changePassword'); @@ -196,8 +213,9 @@ public function testChangePasswordOnSubmitWithInvalidPassword(): void public function testChangePasswordOnSubmitWithInvalidConfirmation(): void { $this->submit([ - 'password' => '12345678', - 'passwordConfirmation' => '1234567' + 'currentPassword' => '12345678', + 'password' => 'abcdefgh', + 'passwordConfirmation' => 'abcdefg' ]); $dialog = $this->dialog('users/test/changePassword');