]> BookStack Code Mirror - bookstack/blob - tests/LanguageTest.php
OIDC: Added testing coverage for picture fetching
[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 test_locales_all_have_language_dropdown_entry()
31     {
32         $this->markTestSkipped('Only used when checking language inclusion');
33
34         $dropdownLocales = array_keys(trans('settings.language_select', [], 'en'));
35         sort($dropdownLocales);
36         sort($this->langs);
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);
42         }
43         $this->assertTrue(true);
44     }
45
46     public function test_correct_language_if_not_logged_in()
47     {
48         $loginReq = $this->get('/login');
49         $loginReq->assertSee('Log In');
50
51         $loginPageFrenchReq = $this->get('/login', ['Accept-Language' => 'fr']);
52         $loginPageFrenchReq->assertSee('Se Connecter');
53     }
54
55     public function test_public_lang_autodetect_can_be_disabled()
56     {
57         config()->set('app.auto_detect_locale', false);
58         $loginReq = $this->get('/login');
59         $loginReq->assertSee('Log In');
60
61         $loginPageFrenchReq = $this->get('/login', ['Accept-Language' => 'fr']);
62         $loginPageFrenchReq->assertDontSee('Se Connecter');
63     }
64
65     public function test_all_lang_files_loadable()
66     {
67         $files = array_diff(scandir(lang_path('en')), ['..', '.']);
68         foreach ($this->langs as $lang) {
69             foreach ($files as $file) {
70                 $loadError = false;
71
72                 try {
73                     $translations = trans(str_replace('.php', '', $file), [], $lang);
74                 } catch (\Exception $e) {
75                     $loadError = true;
76                 }
77                 $this->assertFalse($loadError, "Translation file {$lang}/{$file} failed to load");
78             }
79         }
80     }
81
82     public function test_views_use_rtl_if_rtl_language_is_set()
83     {
84         $this->asEditor()->withHtml($this->get('/'))->assertElementExists('html[dir="ltr"]');
85
86         setting()->putUser($this->users->editor(), 'language', 'ar');
87
88         $this->withHtml($this->get('/'))->assertElementExists('html[dir="rtl"]');
89     }
90
91     public function test_unknown_lang_does_not_break_app()
92     {
93         config()->set('app.locale', 'zz');
94
95         $loginReq = $this->get('/login', ['Accept-Language' => 'zz']);
96         $loginReq->assertOk();
97         $loginReq->assertSee('Log In');
98     }
99
100     public function test_all_activity_types_have_activity_text()
101     {
102         foreach (ActivityType::all() as $activityType) {
103             $langKey = 'activities.' . $activityType;
104             $this->assertNotEquals($langKey, trans($langKey, [], 'en'));
105         }
106     }
107 }