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 $this->addWarning("Languages: {$diffText} found in files but not in language select dropdown.");
40 $this->assertTrue(true);
43 public function test_correct_language_if_not_logged_in()
45 $loginReq = $this->get('/login');
46 $loginReq->assertSee('Log In');
48 $loginPageFrenchReq = $this->get('/login', ['Accept-Language' => 'fr']);
49 $loginPageFrenchReq->assertSee('Se Connecter');
52 public function test_public_lang_autodetect_can_be_disabled()
54 config()->set('app.auto_detect_locale', false);
55 $loginReq = $this->get('/login');
56 $loginReq->assertSee('Log In');
58 $loginPageFrenchReq = $this->get('/login', ['Accept-Language' => 'fr']);
59 $loginPageFrenchReq->assertDontSee('Se Connecter');
62 public function test_all_lang_files_loadable()
64 $files = array_diff(scandir(lang_path('en')), ['..', '.']);
65 foreach ($this->langs as $lang) {
66 foreach ($files as $file) {
70 $translations = trans(str_replace('.php', '', $file), [], $lang);
71 } catch (\Exception $e) {
74 $this->assertFalse($loadError, "Translation file {$lang}/{$file} failed to load");
79 public function test_views_use_rtl_if_rtl_language_is_set()
81 $this->asEditor()->withHtml($this->get('/'))->assertElementExists('html[dir="ltr"]');
83 setting()->putUser($this->users->editor(), 'language', 'ar');
85 $this->withHtml($this->get('/'))->assertElementExists('html[dir="rtl"]');
88 public function test_unknown_lang_does_not_break_app()
90 config()->set('app.locale', 'zz');
92 $loginReq = $this->get('/login', ['Accept-Language' => 'zz']);
93 $loginReq->assertOk();
94 $loginReq->assertSee('Log In');
97 public function test_all_activity_types_have_activity_text()
99 foreach (ActivityType::all() as $activityType) {
100 $langKey = 'activities.' . $activityType;
101 $this->assertNotEquals($langKey, trans($langKey, [], 'en'));