samedi 23 janvier 2021

Missing required parameter for [Route: verification.verify] [URI: {lang}/verify-email/{id}/{hash}] [Missing parameter: lang] Laravel 8

Email verification test can't be validated because of the below issue: Missing required parameter for [Route: verification.verify] [URI: {lang}/verify-email/{id}/{hash}] [Missing parameter: lang].

I have implemented a multi-language approach in my application. So I am passing the locale through the middleware. I have changed all my views to take into consideration the locale app()->getLocale() etc..

I have added Route::Group in my web.php shown below:

<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;

Route::redirect('/', 'en', 301);

Route::group(['prefix' => '{lang}','where' => ['lang' => '[a-zA-Z]{2}'],'middleware' => 'setlanguage'], function() {
  

Route::get('/','App\Http\Controllers\welcomeController@index')->name('welcome');

Auth::routes();
require __DIR__.'/auth.php';

Route::get('/dashboard', function () {
    return view('dashboard');
})->middleware(['auth'])->name('dashboard');


Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

});

As you can see, I am calling auth.php which contains the route for my email verificaiton controllers:

<?php
use App\Http\Controllers\Auth\EmailVerificationNotificationController;
use App\Http\Controllers\Auth\EmailVerificationPromptController;
use App\Http\Controllers\Auth\VerifyEmailController;
use Illuminate\Support\Facades\Route;

Route::get('/verify-email', [EmailVerificationPromptController::class, '__invoke'])
                ->middleware('auth')
                ->name('verification.notice');

Route::get('/verify-email/{id}/{hash}', [VerifyEmailController::class, '__invoke'])
                ->middleware(['auth', 'signed', 'throttle:6,1'])
                ->name('verification.verify');

Route::post('/email/verification-notification', [EmailVerificationNotificationController::class, 'store'])
                ->middleware(['auth', 'throttle:6,1'])
                ->name('verification.send');

and this is my EmailVerificationTest.php where second and third tests failed:

<?php

namespace Tests\Feature;

use App\Models\User;
use App\Providers\RouteServiceProvider;
use Illuminate\Auth\Events\Verified;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\URL;
use Tests\TestCase;

class EmailVerificationTest extends TestCase
{
    use RefreshDatabase;

    public function test_email_verification_screen_can_be_rendered()
    {
        $user = User::factory()->create([
            'email_verified_at' => null,
        ]);

        $response = $this->actingAs($user)->get('/en/verify-email');

        $response->assertStatus(200);
    }

    public function test_email_can_be_verified()
    {
        Event::fake();

        $user = User::factory()->create([
            'email_verified_at' => null,
        ]);

        $verificationUrl = URL::temporarySignedRoute(
            'verification.verify',
            now()->addMinutes(60),
            ['id' => $user->id, 'hash' => sha1($user->email)]
        );

        $response = $this->actingAs($user)->get($verificationUrl);

        Event::assertDispatched(Verified::class);
        $this->assertTrue($user->fresh()->hasVerifiedEmail());
        $response->assertRedirect(RouteServiceProvider::HOME.'?verified=1');
    }

    public function test_email_is_not_verified_with_invalid_hash()
    {
        $user = User::factory()->create([
            'email_verified_at' => null,
        ]);

        $verificationUrl = URL::temporarySignedRoute(
            'verification.verify',
            now()->addMinutes(60),
            ['id' => $user->id, 'hash' => sha1('wrong-email')]
        );

        $this->actingAs($user)->get($verificationUrl);

        $this->assertFalse($user->fresh()->hasVerifiedEmail());
    }
}

if I take out Auth:routes() and require DIR.'/auth.php' from outside the Route::Group, it will work but it will not consider the locale in its routing which I would like to keep.

Any ideas about how can I make the above tests checked while keeping the locale.

Thanks.

Aucun commentaire:

Enregistrer un commentaire