5 use BookStack\Activity\ActivityType;
7 class LanguageTest extends TestCase
9 protected array $langs;
12 * LanguageTest constructor.
14 protected function setUp(): void
17 $this->langs = array_diff(scandir(lang_path('')), ['..', '.']);
20 public function test_locales_config_key_set_properly()
22 $configLocales = config('app.locales');
25 $this->assertEquals(implode(':', $configLocales), implode(':', $this->langs), 'app.locales configuration variable does not match those found in lang files');
28 // Not part of standard phpunit test runs since we sometimes expect non-added langs.
29 public function do_test_locales_all_have_language_dropdown_entry()
31 $dropdownLocales = array_keys(trans('settings.language_select', [], 'en'));
32 sort($dropdownLocales);
34 $diffs = array_diff($this->langs, $dropdownLocales);
35 if (count($diffs) > 0) {
36 $diffText = implode(',', $diffs);
37 $this->addWarning("Languages: {$diffText} found in files but not in language select dropdown.");
39 $this->assertTrue(true);
42 public function test_correct_language_if_not_logged_in()
44 $loginReq = $this->get('/login');
45 $loginReq->assertSee('Log In');
47 $loginPageFrenchReq = $this->get('/login', ['Accept-Language' => 'fr']);
48 $loginPageFrenchReq->assertSee('Se Connecter');
51 public function test_public_lang_autodetect_can_be_disabled()
53 config()->set('app.auto_detect_locale', false);
54 $loginReq = $this->get('/login');
55 $loginReq->assertSee('Log In');
57 $loginPageFrenchReq = $this->get('/login', ['Accept-Language' => 'fr']);
58 $loginPageFrenchReq->assertDontSee('Se Connecter');
61 public function test_all_lang_files_loadable()
63 $files = array_diff(scandir(lang_path('en')), ['..', '.']);
64 foreach ($this->langs as $lang) {
65 foreach ($files as $file) {
69 $translations = trans(str_replace('.php', '', $file), [], $lang);
70 } catch (\Exception $e) {
73 $this->assertFalse($loadError, "Translation file {$lang}/{$file} failed to load");
78 public function test_rtl_config_set_if_lang_is_rtl()
81 $this->assertFalse(config('app.rtl'), 'App RTL config should be false by default');
82 setting()->putUser($this->users->editor(), 'language', 'ar');
84 $this->assertTrue(config('app.rtl'), 'App RTL config should have been set to true by middleware');
87 public function test_unknown_lang_does_not_break_app()
89 config()->set('app.locale', 'zz');
91 $loginReq = $this->get('/login', ['Accept-Language' => 'zz']);
92 $loginReq->assertOk();
93 $loginReq->assertSee('Log In');
96 public function test_all_activity_types_have_activity_text()
98 foreach (ActivityType::all() as $activityType) {
99 $langKey = 'activities.' . $activityType;
100 $this->assertNotEquals($langKey, trans($langKey, [], 'en'));