5 use BookStack\Activity\ActivityType;
6 use BookStack\Translation\LocaleManager;
8 class LanguageTest extends TestCase
10 protected array $langs;
13 * LanguageTest constructor.
15 protected function setUp(): void
18 $this->langs = array_diff(scandir(lang_path('')), ['..', '.']);
21 public function test_locales_list_set_properly()
23 $appLocales = $this->app->make(LocaleManager::class)->getAllAppLocales();
26 $this->assertEquals(implode(':', $this->langs), implode(':', $appLocales), 'app.locales configuration variable does not match those found in lang files');
29 // Not part of standard phpunit test runs since we sometimes expect non-added langs.
30 public function do_test_locales_all_have_language_dropdown_entry()
32 $dropdownLocales = array_keys(trans('settings.language_select', [], 'en'));
33 sort($dropdownLocales);
35 $diffs = array_diff($this->langs, $dropdownLocales);
36 if (count($diffs) > 0) {
37 $diffText = implode(',', $diffs);
38 $warning = "Languages: {$diffText} found in files but not in language select dropdown.";
39 $this->fail($warning);
41 $this->assertTrue(true);
44 public function test_correct_language_if_not_logged_in()
46 $loginReq = $this->get('/login');
47 $loginReq->assertSee('Log In');
49 $loginPageFrenchReq = $this->get('/login', ['Accept-Language' => 'fr']);
50 $loginPageFrenchReq->assertSee('Se Connecter');
53 public function test_public_lang_autodetect_can_be_disabled()
55 config()->set('app.auto_detect_locale', false);
56 $loginReq = $this->get('/login');
57 $loginReq->assertSee('Log In');
59 $loginPageFrenchReq = $this->get('/login', ['Accept-Language' => 'fr']);
60 $loginPageFrenchReq->assertDontSee('Se Connecter');
63 public function test_all_lang_files_loadable()
65 $files = array_diff(scandir(lang_path('en')), ['..', '.']);
66 foreach ($this->langs as $lang) {
67 foreach ($files as $file) {
71 $translations = trans(str_replace('.php', '', $file), [], $lang);
72 } catch (\Exception $e) {
75 $this->assertFalse($loadError, "Translation file {$lang}/{$file} failed to load");
80 public function test_views_use_rtl_if_rtl_language_is_set()
82 $this->asEditor()->withHtml($this->get('/'))->assertElementExists('html[dir="ltr"]');
84 setting()->putUser($this->users->editor(), 'language', 'ar');
86 $this->withHtml($this->get('/'))->assertElementExists('html[dir="rtl"]');
89 public function test_unknown_lang_does_not_break_app()
91 config()->set('app.locale', 'zz');
93 $loginReq = $this->get('/login', ['Accept-Language' => 'zz']);
94 $loginReq->assertOk();
95 $loginReq->assertSee('Log In');
98 public function test_all_activity_types_have_activity_text()
100 foreach (ActivityType::all() as $activityType) {
101 $langKey = 'activities.' . $activityType;
102 $this->assertNotEquals($langKey, trans($langKey, [], 'en'));