5 use BookStack\Auth\User;
6 use BookStack\Entities\Models\Page;
7 use BookStack\Entities\Tools\PageContent;
8 use BookStack\Facades\Theme;
9 use BookStack\Theming\ThemeEvents;
11 use Illuminate\Http\Request;
12 use Illuminate\Http\Response;
13 use League\CommonMark\ConfigurableEnvironmentInterface;
15 class ThemeTest extends TestCase
17 protected $themeFolderName;
18 protected $themeFolderPath;
20 public function test_translation_text_can_be_overridden_via_theme()
22 $this->usingThemeFolder(function () {
23 $translationPath = theme_path('/lang/en');
24 File::makeDirectory($translationPath, 0777, true);
26 $customTranslations = '<?php
27 return [\'books\' => \'Sandwiches\'];
29 file_put_contents($translationPath . '/entities.php', $customTranslations);
31 $homeRequest = $this->actingAs($this->getViewer())->get('/');
32 $homeRequest->assertElementContains('header nav', 'Sandwiches');
36 public function test_theme_functions_file_used_and_app_boot_event_runs()
38 $this->usingThemeFolder(function ($themeFolder) {
39 $functionsFile = theme_path('functions.php');
40 app()->alias('cat', 'dog');
41 file_put_contents($functionsFile, "<?php\nTheme::listen(\BookStack\Theming\ThemeEvents::APP_BOOT, function(\$app) { \$app->alias('cat', 'dog');});");
42 $this->runWithEnv('APP_THEME', $themeFolder, function () {
43 $this->assertEquals('cat', $this->app->getAlias('dog'));
48 public function test_event_commonmark_environment_configure()
50 $callbackCalled = false;
51 $callback = function ($environment) use (&$callbackCalled) {
52 $this->assertInstanceOf(ConfigurableEnvironmentInterface::class, $environment);
53 $callbackCalled = true;
57 Theme::listen(ThemeEvents::COMMONMARK_ENVIRONMENT_CONFIGURE, $callback);
59 $page = Page::query()->first();
60 $content = new PageContent($page);
61 $content->setNewMarkdown('# test');
63 $this->assertTrue($callbackCalled);
66 public function test_event_web_middleware_before()
68 $callbackCalled = false;
70 $callback = function ($request) use (&$callbackCalled, &$requestParam) {
71 $requestParam = $request;
72 $callbackCalled = true;
75 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_BEFORE, $callback);
76 $this->get('/login', ['Donkey' => 'cat']);
78 $this->assertTrue($callbackCalled);
79 $this->assertInstanceOf(Request::class, $requestParam);
80 $this->assertEquals('cat', $requestParam->header('donkey'));
83 public function test_event_web_middleware_before_return_val_used_as_response()
85 $callback = function (Request $request) {
86 return response('cat', 412);
89 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_BEFORE, $callback);
90 $resp = $this->get('/login', ['Donkey' => 'cat']);
91 $resp->assertSee('cat');
92 $resp->assertStatus(412);
95 public function test_event_web_middleware_after()
97 $callbackCalled = false;
99 $responseParam = null;
100 $callback = function ($request, Response $response) use (&$callbackCalled, &$requestParam, &$responseParam) {
101 $requestParam = $request;
102 $responseParam = $response;
103 $callbackCalled = true;
104 $response->header('donkey', 'cat123');
107 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_AFTER, $callback);
109 $resp = $this->get('/login', ['Donkey' => 'cat']);
110 $this->assertTrue($callbackCalled);
111 $this->assertInstanceOf(Request::class, $requestParam);
112 $this->assertInstanceOf(Response::class, $responseParam);
113 $resp->assertHeader('donkey', 'cat123');
116 public function test_event_web_middleware_after_return_val_used_as_response()
118 $callback = function () {
119 return response('cat456', 443);
122 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_AFTER, $callback);
124 $resp = $this->get('/login', ['Donkey' => 'cat']);
125 $resp->assertSee('cat456');
126 $resp->assertStatus(443);
129 public function test_event_auth_login_standard()
132 $callback = function (...$eventArgs) use (&$args) {
136 Theme::listen(ThemeEvents::AUTH_LOGIN, $callback);
139 $this->assertCount(2, $args);
140 $this->assertEquals('standard', $args[0]);
141 $this->assertInstanceOf(User::class, $args[1]);
144 public function test_event_auth_register_standard()
147 $callback = function (...$eventArgs) use (&$args) {
150 Theme::listen(ThemeEvents::AUTH_REGISTER, $callback);
151 $this->setSettings(['registration-enabled' => 'true']);
153 $user = factory(User::class)->make();
154 $this->post('/register', ['email' => $user->email, 'name' => $user->name, 'password' => 'password']);
156 $this->assertCount(2, $args);
157 $this->assertEquals('standard', $args[0]);
158 $this->assertInstanceOf(User::class, $args[1]);
161 public function test_add_social_driver()
163 Theme::addSocialDriver('catnet', [
164 'client_id' => 'abc123',
165 'client_secret' => 'def456',
166 ], 'SocialiteProviders\Discord\DiscordExtendSocialite@handleTesting');
168 $this->assertEquals('catnet', config('services.catnet.name'));
169 $this->assertEquals('abc123', config('services.catnet.client_id'));
170 $this->assertEquals(url('/login/service/catnet/callback'), config('services.catnet.redirect'));
172 $loginResp = $this->get('/login');
173 $loginResp->assertSee('login/service/catnet');
176 public function test_add_social_driver_uses_name_in_config_if_given()
178 Theme::addSocialDriver('catnet', [
179 'client_id' => 'abc123',
180 'client_secret' => 'def456',
181 'name' => 'Super Cat Name',
182 ], 'SocialiteProviders\Discord\DiscordExtendSocialite@handleTesting');
184 $this->assertEquals('Super Cat Name', config('services.catnet.name'));
185 $loginResp = $this->get('/login');
186 $loginResp->assertSee('Super Cat Name');
189 public function test_add_social_driver_allows_a_configure_for_redirect_callback_to_be_passed()
191 Theme::addSocialDriver(
194 'client_id' => 'abc123',
195 'client_secret' => 'def456',
196 'name' => 'Super Cat Name',
198 'SocialiteProviders\Discord\DiscordExtendSocialite@handle',
200 $driver->with(['donkey' => 'donut']);
204 $loginResp = $this->get('/login/service/discord');
205 $redirect = $loginResp->headers->get('location');
206 $this->assertStringContainsString('donkey=donut', $redirect);
209 protected function usingThemeFolder(callable $callback)
211 // Create a folder and configure a theme
212 $themeFolderName = 'testing_theme_' . rtrim(base64_encode(time()), '=');
213 config()->set('view.theme', $themeFolderName);
214 $themeFolderPath = theme_path('');
215 File::makeDirectory($themeFolderPath);
217 call_user_func($callback, $themeFolderName);
219 // Cleanup the custom theme folder we created
220 File::deleteDirectory($themeFolderPath);