From 177b7407018ed84dbcf8eb6dc25cadd896285fd4 Mon Sep 17 00:00:00 2001 From: PovilasKorop Date: Mon, 1 Nov 2021 07:48:35 +0200 Subject: [PATCH] Profile fields and update tests --- README.md | 65 ++++++++++++++++------ app/Http/Controllers/ProfileController.php | 2 +- tests/Feature/AuthenticationTest.php | 44 +++++++++++++++ 3 files changed, 92 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 261d51b..d7ac311 100644 --- a/README.md +++ b/README.md @@ -1,30 +1,59 @@ -## Laravel Breeze: Tailwind Pages Skeleton +## Test Your Laravel Auth Skills -Laravel boilerplate repository to create simple demo-projects. It allows to quickly add new routes/pages, and has examples of a table page, and a form page. +This repository is a test for you: perform a set of tasks listed below, and fix the PHPUnit tests, which are currently intentionally failing. -It uses the Starter Kit [Laravel Breeze](https://github.com/laravel/breeze) based on Tailwind framework. +To test if all the functions work correctly, there are PHPUnit tests in `tests/Feature/AuthenticationTest.php` file. -![Laravel Breeze Table page](https://laraveldaily.com/wp-content/uploads/2021/09/Screenshot-2021-09-19-at-09.51.38.png) +In the very beginning, if you run `php artisan test`, or `vendor/bin/phpunit`, all tests fail. +Your task is to make those tests pass. -![Laravel Breeze Form page](https://laraveldaily.com/wp-content/uploads/2021/09/Screenshot-2021-09-19-at-09.51.50.png) ------ +## How to Submit Your Solution -### How to use +If you want to submit your solution, you should make a Pull Request to the `main` branch. +It will automatically run the tests via Github Actions and will show you/me if the test pass. -- Clone the project with `git clone` -- Copy `.env.example` file to `.env` and edit database credentials there -- Run `composer install` -- Run `php artisan key:generate` -- Run `php artisan migrate --seed` (it has some seeded data for your testing) -- That's it: launch the main URL +If you don't know how to make a Pull Request, [here's my video with instructions](https://www.youtube.com/watch?v=vEcT6JIFji0). +This task is mostly self-served, so I'm not planning review or merge the Pull Requests. This test is for yourselves to assess your skills, the automated tests will be your answer if you passed the test :) + + +## Questions / Problems? + +If you're struggling with some of the tasks, or you have suggestions how to improve the task, create a Github Issue. + +Good luck! --- -## More from our LaravelDaily Team +## Task 1. Routes Protected by Auth. + +File `routes/web.php`: profile functionality URLs should be available only for logged-in users. + +Test method `test_profile_routes_are_protected_from_public()`. + +--- + +## Task 2. Link Visible to Logged-in Users. + +File `resources/views/layouts/navigation.blade.php`: the "Profile" link should be visible only to logged-in users. + +Test method `test_profile_link_is_invisible_in_public()`. + +--- + +## Task 3. Profile Fields. + +File `resources/views/auth/profile.blade.php`: replace "???" values for name/email with logged-in user's name/email. + +Test method `test_profile_fields_are_visible()`. + +--- + +## Task 4. Profile Update. + +File `app/Http/Controllers/ProfileController.php`: fill in the method `update()` with the code to update the user's name and email. +If the password is filled in, also update that. + +Test methods: `test_profile_name_email_update_successful()` and `test_profile_password_update_successful()`. -- Enroll in our [Laravel Online Courses](https://laraveldaily.teachable.com/) -- Check out our adminpanel generator [QuickAdminPanel](https://quickadminpanel.com) -- Purchase our [Livewire Kit](https://livewirekit.com) -- Subscribe to our [YouTube channel Laravel Daily](https://www.youtube.com/channel/UCTuplgOBi6tJIlesIboymGA) diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index b3272d9..e0093a4 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -16,6 +16,6 @@ class ProfileController extends Controller // Task: fill in the code here to update name and email // Also, update the password if it is set - return redirect()->back()->with('success', 'Profile updated.'); + return redirect()->route('profile.show')->with('success', 'Profile updated.'); } } diff --git a/tests/Feature/AuthenticationTest.php b/tests/Feature/AuthenticationTest.php index a8c6194..2343cbb 100644 --- a/tests/Feature/AuthenticationTest.php +++ b/tests/Feature/AuthenticationTest.php @@ -4,6 +4,7 @@ namespace Tests\Feature; use App\Models\User; use Illuminate\Foundation\Testing\RefreshDatabase; +use Illuminate\Support\Facades\Auth; use Tests\TestCase; class AuthenticationTest extends TestCase @@ -34,4 +35,47 @@ class AuthenticationTest extends TestCase $response = $this->actingAs($user)->get('/'); $this->assertStringContainsString('href="/profile"', $response->getContent()); } + + public function test_profile_fields_are_visible() + { + $user = User::factory()->create(); + $response = $this->actingAs($user)->get('/profile'); + $this->assertStringContainsString('value="'.$user->name.'"', $response->getContent()); + $this->assertStringContainsString('value="'.$user->email.'"', $response->getContent()); + } + + public function test_profile_name_email_update_successful() + { + $user = User::factory()->create(); + $newData = [ + 'name' => 'New name', + 'email' => 'new@email.com' + ]; + $this->actingAs($user)->put('/profile', $newData); + $this->assertDatabaseHas('users', $newData); + + // Check if the user is still able to log in - password unchanged + $this->assertTrue(Auth::attempt([ + 'email' => $user->email, + 'password' => 'password' + ])); + } + + public function test_profile_password_update_successful() + { + $user = User::factory()->create(); + $newData = [ + 'name' => 'New name', + 'email' => 'new@email.com', + 'password' => 'newpassword', + 'password_confirmation' => 'newpassword' + ]; + $this->actingAs($user)->put('/profile', $newData); + + // Check if the user is able to log in with the new password + $this->assertTrue(Auth::attempt([ + 'email' => $user->email, + 'password' => 'newpassword' + ])); + } }