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 test_locales_all_have_language_dropdown_entry()
32 $this->markTestSkipped('Only used when checking language inclusion');
34 $dropdownLocales = array_keys(trans('settings.language_select', [], 'en'));
35 sort($dropdownLocales);
37 $diffs = array_diff($this->langs, $dropdownLocales);
38 if (count($diffs) > 0) {
39 $diffText = implode(',', $diffs);
40 $warning = "Languages: {$diffText} found in files but not in language select dropdown.";
41 $this->fail($warning);
43 $this->assertTrue(true);
46 public function test_correct_language_if_not_logged_in()
48 $loginReq = $this->get('/login');
49 $loginReq->assertSee('Log In');
51 $loginPageFrenchReq = $this->get('/login', ['Accept-Language' => 'fr']);
52 $loginPageFrenchReq->assertSee('Se Connecter');
55 public function test_public_lang_autodetect_can_be_disabled()
57 config()->set('app.auto_detect_locale', false);
58 $loginReq = $this->get('/login');
59 $loginReq->assertSee('Log In');
61 $loginPageFrenchReq = $this->get('/login', ['Accept-Language' => 'fr']);
62 $loginPageFrenchReq->assertDontSee('Se Connecter');
65 public function test_all_lang_files_loadable()
67 $files = array_diff(scandir(lang_path('en')), ['..', '.']);
68 foreach ($this->langs as $lang) {
69 foreach ($files as $file) {
73 $translations = trans(str_replace('.php', '', $file), [], $lang);
74 } catch (\Exception $e) {
77 $this->assertFalse($loadError, "Translation file {$lang}/{$file} failed to load");
82 public function test_views_use_rtl_if_rtl_language_is_set()
84 $this->asEditor()->withHtml($this->get('/'))->assertElementExists('html[dir="ltr"]');
86 setting()->putUser($this->users->editor(), 'language', 'ar');
88 $this->withHtml($this->get('/'))->assertElementExists('html[dir="rtl"]');
91 public function test_unknown_lang_does_not_break_app()
93 config()->set('app.locale', 'zz');
95 $loginReq = $this->get('/login', ['Accept-Language' => 'zz']);
96 $loginReq->assertOk();
97 $loginReq->assertSee('Log In');
100 public function test_all_activity_types_have_activity_text()
102 foreach (ActivityType::all() as $activityType) {
103 $langKey = 'activities.' . $activityType;
104 $this->assertNotEquals($langKey, trans($langKey, [], 'en'));