Laravel Lang, redirects in Pest tests not working right.
greggh opened this issue · comments
Environment
- Laravel-Lang Version: "mcamara/laravel-localization": "^1.8"
- Laravel Version: 10
- PHP Version: 8.2
Issue description
I am using Pest 2 for my testing framework. I have converted all of the default tests that come along with a Laravel 10 + Jetstream install. So everything is Pest now.
My site uses localization in the URL to handle the languages. This is part of my routes file:
Route::group(['prefix' => LaravelLocalization::setLocale(), 'middleware' => 'localeSessionRedirect'], function () { Route::get('/', function () { return view('welcome'); })->name('home');
There is a lot more to the routes file, but this shows that the localization is in place.
This works in normal usage. In the web browser if I go to /forgot-password, I end up at /en/forgot-password
In testing, I just end up at /en. It does not take me to /en/forgot-password.
Steps to reproduce
- Install Laravel 10 with Jetstream and Livewire
- Configure database and migrate.
- Install Laravel Lang
- Install Pest
- Convert tests to Pest
- Add Laravel Lang to routing.
- Edit PasswordResetTest.php changing assertStatus to assertRedirect calls.
- Run tests
The specific test that I am currently using to attempt to solve this is the "reset password link screen can be rendered" test.
This is in the PasswordResetTest.php.
`test('reset password link screen can be rendered', function () {
if (! Features::enabled(Features::resetPasswords())) {
$this->markTestSkipped('Password updates are not enabled.');
return;
}
$response = $this->get('/forgot-password');
$response->assertRedirect('/en/forgot-password');`
It fails like this:
FAILED Tests\Feature\PasswordResetTest > reset password link screen can be rendered
Failed asserting that two strings are equal.
-'http://domain.test/en/forgot-password'
+'http://domain.test/en'
I am expecting it to do what happens in the web browser, which is redirect to /en/forgot-password.
Did you only install the laravel-lang/lang
package or something else?
Laravel now does not contain translation files by default and if they are not explicitly published using the php artisan lang:publish
built-in command or using our installer, then I can assume that the mcamara/laravel-localization
package cannot detect it.
In addition, I will say that the Laravel Lang
project does not provide routing capabilities. We only translate messages. The programmatic solution for changing the URL is mcamara/laravel-localization
, which we have nothing to do with.
Thanks or the quick response!
No, I followed the instructions here: https://laravel-lang.com/installation/
So I have these installed:
laravel-lang/common
mcamara/laravel-localization
In my Kernel.php I have the LocaleSessionRedirect:
'localeSessionRedirect' => \Mcamara\LaravelLocalization\Middleware\LocaleSessionRedirect::class,
And when I use my code in a web browser, everything is working great.
But now it seems like I should be talking to mcamara/laravel-localization!
If the Mcamara package really does not find localization files (if it does exactly that, which I cannot say), then try publishing them first by calling the console command php artisan lang:update
I call the lang:update in the update script section in composer.json. So that is happening. I think it's something about how that package handles routing when in tests, like everything doesnt get called. I am going to go look at it from that side now.
So, everything turned out to be even simpler: the mcamara/laravel-localization
package determines the list of available localizations from its own configuration file
.
The "Laravel Lang: Publisher" project has a list of available localizations
, but it does not meet the requirements of mcamara/laravel-localization
. We have an enum
class, and they have an associative array
.
Thus, you need to manually activate the desired localizations through the settings file.
Thank you for your message!
If you have any questions do not hesitate to contact us.