]> BookStack Code Mirror - bookstack/blob - tests/LanguageTest.php
Merge branch 'fix/markdown-export' into development
[bookstack] / tests / LanguageTest.php
1 <?php
2
3 namespace Tests;
4
5 use BookStack\Activity\ActivityType;
6 use BookStack\Translation\LocaleManager;
7
8 class LanguageTest extends TestCase
9 {
10     protected array $langs;
11
12     /**
13      * LanguageTest constructor.
14      */
15     protected function setUp(): void
16     {
17         parent::setUp();
18         $this->langs = array_diff(scandir(lang_path('')), ['..', '.']);
19     }
20
21     public function test_locales_list_set_properly()
22     {
23         $appLocales = $this->app->make(LocaleManager::class)->getAllAppLocales();
24         sort($appLocales);
25         sort($this->langs);
26         $this->assertEquals(implode(':', $this->langs), implode(':', $appLocales), 'app.locales configuration variable does not match those found in lang files');
27     }
28
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()
31     {
32         $dropdownLocales = array_keys(trans('settings.language_select', [], 'en'));
33         sort($dropdownLocales);
34         sort($this->langs);
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);
40         }
41         $this->assertTrue(true);
42     }
43
44     public function test_correct_language_if_not_logged_in()
45     {
46         $loginReq = $this->get('/login');
47         $loginReq->assertSee('Log In');
48
49         $loginPageFrenchReq = $this->get('/login', ['Accept-Language' => 'fr']);
50         $loginPageFrenchReq->assertSee('Se Connecter');
51     }
52
53     public function test_public_lang_autodetect_can_be_disabled()
54     {
55         config()->set('app.auto_detect_locale', false);
56         $loginReq = $this->get('/login');
57         $loginReq->assertSee('Log In');
58
59         $loginPageFrenchReq = $this->get('/login', ['Accept-Language' => 'fr']);
60         $loginPageFrenchReq->assertDontSee('Se Connecter');
61     }
62
63     public function test_all_lang_files_loadable()
64     {
65         $files = array_diff(scandir(lang_path('en')), ['..', '.']);
66         foreach ($this->langs as $lang) {
67             foreach ($files as $file) {
68                 $loadError = false;
69
70                 try {
71                     $translations = trans(str_replace('.php', '', $file), [], $lang);
72                 } catch (\Exception $e) {
73                     $loadError = true;
74                 }
75                 $this->assertFalse($loadError, "Translation file {$lang}/{$file} failed to load");
76             }
77         }
78     }
79
80     public function test_views_use_rtl_if_rtl_language_is_set()
81     {
82         $this->asEditor()->withHtml($this->get('/'))->assertElementExists('html[dir="ltr"]');
83
84         setting()->putUser($this->users->editor(), 'language', 'ar');
85
86         $this->withHtml($this->get('/'))->assertElementExists('html[dir="rtl"]');
87     }
88
89     public function test_unknown_lang_does_not_break_app()
90     {
91         config()->set('app.locale', 'zz');
92
93         $loginReq = $this->get('/login', ['Accept-Language' => 'zz']);
94         $loginReq->assertOk();
95         $loginReq->assertSee('Log In');
96     }
97
98     public function test_all_activity_types_have_activity_text()
99     {
100         foreach (ActivityType::all() as $activityType) {
101             $langKey = 'activities.' . $activityType;
102             $this->assertNotEquals($langKey, trans($langKey, [], 'en'));
103         }
104     }
105 }