mcamara / laravel-localization

Easy localization for Laravel
MIT License
3.32k stars 507 forks source link

Running a Dusk test bypasses language negotiation #868

Closed dizco closed 1 year ago

dizco commented 1 year ago

Describe the bug This line: https://github.com/mcamara/laravel-localization/blob/24af7ee06e48f45c884018f8ab33332bbb210c03/src/Mcamara/LaravelLocalization/LaravelLocalization.php#L575

When running a Dusk test, the value of PHP_SAPI is cli, therefore app->runningInConsole() is true. However, in reality we still want content negotiation to happen and not only use the default language.

To Reproduce Steps to reproduce the behavior:

  1. Create a default Laravel app with Dusk

  2. Add laravel-localization with a default route /

    // config > laravellocalization.php
    'supportedLocales' => [
        'en' => ['name' => 'English',       'script' => 'Latn', 'native' => 'English', 'regional' => 'en_CA'],
        'fr' => ['name' => 'French',        'script' => 'Latn', 'native' => 'français', 'regional' => 'fr_CA']
    ],
    'hideDefaultLocaleInURL' => false,
    'useAcceptLanguageHeader' => true,
    'localesMapping' => [],
    // web.php
    Route::group(
        [
            'prefix' => LaravelLocalization::setLocale(),
            'middleware' => ['localize', 'localeCookieRedirect'],
        ],
        function () {
            Route::get(LaravelLocalization::transRoute('routes.index'), [WebController::class, 'getIndex']);
        }
    );
  3. Override the Dusk driver

    protected function driver(): RemoteWebDriver
    {
        $options = (new ChromeOptions)->addArguments(collect([
            '--window-size=1920,1080',
            '--accept-lang=fr',
        ])->unless($this->hasHeadlessDisabled(), function ($items) {
            return $items->merge([
                '--disable-gpu',
                '--headless',
            ]);
        })->all());
    
        return RemoteWebDriver::create(
            $_ENV['DUSK_DRIVER_URL'] ?? 'http://localhost:9515',
            DesiredCapabilities::chrome()->setCapability(
                ChromeOptions::CAPABILITY, $options
            )
        );
    }
  4. Run a dusk test

    public function testHomePageRedirectUsesBrowserAcceptLanguage()
    {
        $this->browse(function (Browser $browser) {
            $browser->visit('/');
    
            $browser->assertPathIs('/fr');
        });
    }

Expected behavior When running a dusk test, it should not change the content negotiation.

More info:

Additional context Add any other context about the problem here.

dizco commented 1 year ago

Closing this issue sorry, I believe I misunderstood the issue I am having.