3 use BookStack\Entities\Models\Page;
4 use BookStack\Entities\Tools\PageContent;
5 use BookStack\Facades\Theme;
6 use BookStack\Theming\ThemeEvents;
8 use League\CommonMark\ConfigurableEnvironmentInterface;
10 class ThemeTest extends TestCase
12 protected $themeFolderName;
13 protected $themeFolderPath;
15 public function test_translation_text_can_be_overridden_via_theme()
17 $this->usingThemeFolder(function() {
18 $translationPath = theme_path('/lang/en');
19 File::makeDirectory($translationPath, 0777, true);
21 $customTranslations = '<?php
22 return [\'books\' => \'Sandwiches\'];
24 file_put_contents($translationPath . '/entities.php', $customTranslations);
26 $homeRequest = $this->actingAs($this->getViewer())->get('/');
27 $homeRequest->assertElementContains('header nav', 'Sandwiches');
31 public function test_theme_functions_file_used_and_app_boot_event_runs()
33 $this->usingThemeFolder(function($themeFolder) {
34 $functionsFile = theme_path('functions.php');
35 app()->alias('cat', 'dog');
36 file_put_contents($functionsFile, "<?php\nTheme::listen(\BookStack\Theming\ThemeEvents::APP_BOOT, function(\$app) { \$app->alias('cat', 'dog');});");
37 $this->runWithEnv('APP_THEME', $themeFolder, function() {
38 $this->assertEquals('cat', $this->app->getAlias('dog'));
43 public function test_event_commonmark_environment_configure()
45 $callbackCalled = false;
46 $callback = function($environment) use (&$callbackCalled) {
47 $this->assertInstanceOf(ConfigurableEnvironmentInterface::class, $environment);
48 $callbackCalled = true;
51 Theme::listen(ThemeEvents::COMMONMARK_ENVIRONMENT_CONFIGURE, $callback);
53 $page = Page::query()->first();
54 $content = new PageContent($page);
55 $content->setNewMarkdown('# test');
57 $this->assertTrue($callbackCalled);
60 protected function usingThemeFolder(callable $callback)
62 // Create a folder and configure a theme
63 $themeFolderName = 'testing_theme_' . rtrim(base64_encode(time()), "=");
64 config()->set('view.theme', $themeFolderName);
65 $themeFolderPath = theme_path('');
66 File::makeDirectory($themeFolderPath);
68 call_user_func($callback, $themeFolderName);
70 // Cleanup the custom theme folder we created
71 File::deleteDirectory($themeFolderPath);