SetLanguageMiddlewareTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace Tests\Feature\Http\Middlewares;
  3. use App\Http\Middleware\SetLanguage;
  4. use App\Models\User;
  5. use Illuminate\Support\Facades\App;
  6. use Illuminate\Support\Facades\Config;
  7. use PHPUnit\Framework\Attributes\CoversClass;
  8. use PHPUnit\Framework\Attributes\Test;
  9. use Tests\TestCase;
  10. #[CoversClass(SetLanguage::class)]
  11. class SetLanguageMiddlewareTest extends TestCase
  12. {
  13. const IS_FR = 'fr';
  14. const IS_FR_WITH_VARIANT = 'fr-CA';
  15. const IS_DE = 'de';
  16. const UNSUPPORTED_LANGUAGE = 'yy';
  17. const UNSUPPORTED_LANGUAGES = 'yy, ww';
  18. const ACCEPTED_LANGUAGES_DE_FR = 'de, fr';
  19. const ACCEPTED_LANGUAGES_UNSUPPORTED_DE = 'yy, de';
  20. const ACCEPTED_LANGUAGES_WEIGHTED_DE_FR = 'fr;q=0.4, de;q=0.7';
  21. const ACCEPTED_LANGUAGES_WEIGHTED_FR_VARIANTS = 'fr;q=0.4, fr-CA;q=0.7, de;q=0.5';
  22. const ACCEPTED_LANGUAGES_WEIGHTED_ALL_DE_FR = 'fr;q=0.4, de;q=0.7, *;q=0.9';
  23. /**
  24. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  25. */
  26. protected $user;
  27. #[Test]
  28. public function test_it_applies_fallback_locale()
  29. {
  30. Config::set('app.fallback_locale', self::IS_FR);
  31. $this->json('GET', '/', [], ['Accept-Language' => null]);
  32. $this->assertEquals(self::IS_FR, App::getLocale());
  33. }
  34. #[Test]
  35. public function test_it_applies_fallback_locale_if_header_ask_for_unsupported()
  36. {
  37. Config::set('app.fallback_locale', self::IS_FR);
  38. $this->json('GET', '/', [], ['Accept-Language' => self::UNSUPPORTED_LANGUAGE]);
  39. $this->assertEquals(self::IS_FR, App::getLocale());
  40. }
  41. #[Test]
  42. public function test_it_applies_fallback_locale_if_header_ask_for_several_unsupported()
  43. {
  44. Config::set('app.fallback_locale', self::IS_FR);
  45. $this->json('GET', '/', [], ['Accept-Language' => self::UNSUPPORTED_LANGUAGES]);
  46. $this->assertEquals(self::IS_FR, App::getLocale());
  47. }
  48. #[Test]
  49. public function test_it_applies_fallback_locale_if_header_ask_for_wildcard()
  50. {
  51. Config::set('app.fallback_locale', self::IS_FR);
  52. $this->json('GET', '/', [], ['Accept-Language' => '*']);
  53. $this->assertEquals(self::IS_FR, App::getLocale());
  54. }
  55. #[Test]
  56. public function test_it_applies_accepted_language_from_header()
  57. {
  58. $this->json('GET', '/', [], ['Accept-Language' => self::IS_FR]);
  59. $this->assertEquals(self::IS_FR, App::getLocale());
  60. }
  61. #[Test]
  62. public function test_it_applies_first_accepted_language_from_header()
  63. {
  64. $this->json('GET', '/', [], ['Accept-Language' => self::ACCEPTED_LANGUAGES_DE_FR]);
  65. $this->assertEquals('de', App::getLocale());
  66. }
  67. #[Test]
  68. public function test_it_applies_heaviest_language_from_header()
  69. {
  70. $this->json('GET', '/', [], ['Accept-Language' => self::ACCEPTED_LANGUAGES_WEIGHTED_DE_FR]);
  71. $this->assertEquals('de', App::getLocale());
  72. }
  73. #[Test]
  74. public function test_it_applies_heaviest_language_with_variant_from_header()
  75. {
  76. $this->json('GET', '/', [], ['Accept-Language' => self::ACCEPTED_LANGUAGES_WEIGHTED_FR_VARIANTS]);
  77. $this->assertEquals('fr', App::getLocale());
  78. }
  79. #[Test]
  80. public function test_it_ignores_unsupported_language_from_header()
  81. {
  82. $this->json('GET', '/', [], ['Accept-Language' => self::ACCEPTED_LANGUAGES_UNSUPPORTED_DE]);
  83. $this->assertEquals('de', App::getLocale());
  84. }
  85. #[Test]
  86. public function test_user_preference_overrides_header()
  87. {
  88. $this->user = new User;
  89. $this->user['preferences->lang'] = self::IS_FR;
  90. $this->actingAs($this->user)->json('GET', '/', [], ['Accept-Language' => self::IS_DE]);
  91. $this->assertEquals(self::IS_FR, App::getLocale());
  92. }
  93. #[Test]
  94. public function test_user_preference_applies_header()
  95. {
  96. $this->user = new User;
  97. $this->user['preferences->lang'] = 'browser';
  98. $this->actingAs($this->user)->json('GET', '/', [], ['Accept-Language' => self::IS_DE]);
  99. $this->assertEquals(self::IS_DE, App::getLocale());
  100. }
  101. #[Test]
  102. public function test_user_preference_overrides_fallback()
  103. {
  104. Config::set('app.fallback_locale', self::IS_DE);
  105. $this->user = new User;
  106. $this->user['preferences->lang'] = self::IS_FR;
  107. $this->actingAs($this->user)->json('GET', '/', [], ['Accept-Language' => null]);
  108. $this->assertEquals(self::IS_FR, App::getLocale());
  109. }
  110. }