trypost/tests/Feature/Middleware/SetLocaleTest.php

67 lines
2.2 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
use App\Http\Middleware\App\SetLocale;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
use Symfony\Component\HttpFoundation\Response;
/**
* Run the middleware with the given `locale` cookie (null = no cookie) and
* return the response, whose shared `htmlDir` and queued cookies can be asserted.
*/
function setLocaleResponse(?string $locale): Response
{
$request = Request::create('/', 'GET');
if ($locale !== null) {
$request->cookies->set('locale', $locale);
}
return (new SetLocale)->handle($request, fn () => response('ok'));
}
/** The shared `htmlDir` the Blade root view renders into `<html dir>`. */
function runSetLocale(?string $locale): string
{
setLocaleResponse($locale);
return View::shared('htmlDir');
}
test('shares rtl direction and sets the locale for Arabic', function () {
expect(runSetLocale('ar'))->toBe('rtl');
expect(app()->getLocale())->toBe('ar');
});
test('shares ltr direction for a left-to-right locale', function (string $locale) {
expect(runSetLocale($locale))->toBe('ltr');
expect(app()->getLocale())->toBe($locale);
Add Ukrainian as a supported platform language (#219) * Default AI content language to Ukrainian * Keep English as the default content language * refactor: update content language handling in workspace creation - Changed the default content language in CreateWorkspace to inherit the app's locale instead of defaulting to English. - Updated related tests to reflect this change, ensuring that the content language aligns with the application's current locale settings. - Cleaned up unnecessary code in the BrandTab component for better readability. * Add Ukrainian as a full platform UI locale. Wire uk into languages config with complete lang/uk translations and restore ContentLanguage↔UI parity checks. Co-authored-by: Cursor <cursoragent@cursor.com> * Wire Ukrainian into dayjs and locale coverage tests. Import the uk dayjs locale with Monday week-start and cover uk/uk-UA in brand autofill and SetLocale assertions. Co-authored-by: Cursor <cursoragent@cursor.com> * Harden Ukrainian UI and AI content-language coverage. Cover generator/reviewer/humanizer/image prompts, workspace pickers, persona labels, UI locale switch, and README for uk as a first-class platform language. Co-authored-by: Cursor <cursoragent@cursor.com> * Add Ukrainian Pinterest form strings after main merge. Restore lang/uk posts.php key parity for the new pin title and destination link fields. Co-authored-by: Cursor <cursoragent@cursor.com> * List every supported UI language in the README. Replace the abbreviated multi-language blurb with the full locale set from config. Co-authored-by: Cursor <cursoragent@cursor.com> --------- Co-authored-by: Paulo Castellano <paulo@castellanos.llc> Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-05 22:45:30 +00:00
})->with(['en', 'uk', 'ja', 'pt-BR', 'de']);
test('falls back to the default locale and ltr for an unknown cookie', function () {
expect(runSetLocale('sv'))->toBe('ltr');
expect(app()->getLocale())->toBe(config('languages.default'));
});
test('falls back to the default locale and ltr when no cookie is set', function () {
expect(runSetLocale(null))->toBe('ltr');
expect(app()->getLocale())->toBe(config('languages.default'));
});
test('persists the default locale cookie when the incoming cookie is invalid or absent', function (?string $locale) {
$cookie = collect(setLocaleResponse($locale)->headers->getCookies())
->first(fn ($cookie) => $cookie->getName() === 'locale');
expect($cookie)->not->toBeNull();
expect($cookie->getValue())->toBe(config('languages.default'));
})->with(['sv', null]);
test('does not reset the cookie when a valid locale is present', function () {
$cookie = collect(setLocaleResponse('ar')->headers->getCookies())
->first(fn ($cookie) => $cookie->getName() === 'locale');
expect($cookie)->toBeNull();
});