]> BookStack Code Mirror - bookstack/blob - tests/ThemeTest.php
Added testing for the back-end theme system done so far
[bookstack] / tests / ThemeTest.php
1 <?php namespace Tests;
2
3 use BookStack\Entities\Models\Page;
4 use BookStack\Entities\Tools\PageContent;
5 use BookStack\Facades\Theme;
6 use BookStack\Theming\ThemeEvents;
7 use File;
8 use League\CommonMark\ConfigurableEnvironmentInterface;
9
10 class ThemeTest extends TestCase
11 {
12     protected $themeFolderName;
13     protected $themeFolderPath;
14
15     public function test_translation_text_can_be_overridden_via_theme()
16     {
17         $this->usingThemeFolder(function() {
18             $translationPath = theme_path('/lang/en');
19             File::makeDirectory($translationPath, 0777, true);
20
21             $customTranslations = '<?php
22             return [\'books\' => \'Sandwiches\'];
23         ';
24             file_put_contents($translationPath . '/entities.php', $customTranslations);
25
26             $homeRequest = $this->actingAs($this->getViewer())->get('/');
27             $homeRequest->assertElementContains('header nav', 'Sandwiches');
28         });
29     }
30
31     public function test_theme_functions_file_used_and_app_boot_event_runs()
32     {
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'));
39             });
40         });
41     }
42
43     public function test_event_commonmark_environment_configure()
44     {
45         $callbackCalled = false;
46         $callback = function($environment) use (&$callbackCalled) {
47             $this->assertInstanceOf(ConfigurableEnvironmentInterface::class, $environment);
48             $callbackCalled = true;
49             return $environment;
50         };
51         Theme::listen(ThemeEvents::COMMONMARK_ENVIRONMENT_CONFIGURE, $callback);
52
53         $page = Page::query()->first();
54         $content = new PageContent($page);
55         $content->setNewMarkdown('# test');
56
57         $this->assertTrue($callbackCalled);
58     }
59
60     protected function usingThemeFolder(callable $callback)
61     {
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);
67
68         call_user_func($callback, $themeFolderName);
69
70         // Cleanup the custom theme folder we created
71         File::deleteDirectory($themeFolderPath);
72     }
73
74 }