5 use BookStack\Activity\ActivityType;
6 use BookStack\Activity\DispatchWebhookJob;
7 use BookStack\Activity\Models\Webhook;
8 use BookStack\Entities\Models\Book;
9 use BookStack\Entities\Models\Page;
10 use BookStack\Entities\Tools\PageContent;
11 use BookStack\Exceptions\ThemeException;
12 use BookStack\Facades\Theme;
13 use BookStack\Theming\ThemeEvents;
14 use BookStack\Users\Models\User;
15 use Illuminate\Console\Command;
16 use Illuminate\Http\Request;
17 use Illuminate\Http\Response;
18 use Illuminate\Support\Facades\Artisan;
19 use Illuminate\Support\Facades\File;
20 use League\CommonMark\Environment\Environment;
22 class ThemeTest extends TestCase
24 protected string $themeFolderName;
25 protected string $themeFolderPath;
27 public function test_translation_text_can_be_overridden_via_theme()
29 $this->usingThemeFolder(function () {
30 $translationPath = theme_path('/lang/en');
31 File::makeDirectory($translationPath, 0777, true);
33 $customTranslations = '<?php
34 return [\'books\' => \'Sandwiches\'];
36 file_put_contents($translationPath . '/entities.php', $customTranslations);
38 $homeRequest = $this->actingAs($this->users->viewer())->get('/');
39 $this->withHtml($homeRequest)->assertElementContains('header nav', 'Sandwiches');
43 public function test_theme_functions_file_used_and_app_boot_event_runs()
45 $this->usingThemeFolder(function ($themeFolder) {
46 $functionsFile = theme_path('functions.php');
47 app()->alias('cat', 'dog');
48 file_put_contents($functionsFile, "<?php\nTheme::listen(\BookStack\Theming\ThemeEvents::APP_BOOT, function(\$app) { \$app->alias('cat', 'dog');});");
49 $this->runWithEnv('APP_THEME', $themeFolder, function () {
50 $this->assertEquals('cat', $this->app->getAlias('dog'));
55 public function test_theme_functions_loads_errors_are_caught_and_logged()
57 $this->usingThemeFolder(function ($themeFolder) {
58 $functionsFile = theme_path('functions.php');
59 file_put_contents($functionsFile, "<?php\n\\BookStack\\Biscuits::eat();");
61 $this->expectException(ThemeException::class);
62 $this->expectExceptionMessageMatches('/Failed loading theme functions file at ".*?" with error: Class "BookStack\\\\Biscuits" not found/');
64 $this->runWithEnv('APP_THEME', $themeFolder, fn() => null);
68 public function test_event_commonmark_environment_configure()
70 $callbackCalled = false;
71 $callback = function ($environment) use (&$callbackCalled) {
72 $this->assertInstanceOf(Environment::class, $environment);
73 $callbackCalled = true;
77 Theme::listen(ThemeEvents::COMMONMARK_ENVIRONMENT_CONFIGURE, $callback);
79 $page = $this->entities->page();
80 $content = new PageContent($page);
81 $content->setNewMarkdown('# test', $this->users->editor());
83 $this->assertTrue($callbackCalled);
86 public function test_event_web_middleware_before()
88 $callbackCalled = false;
90 $callback = function ($request) use (&$callbackCalled, &$requestParam) {
91 $requestParam = $request;
92 $callbackCalled = true;
95 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_BEFORE, $callback);
96 $this->get('/login', ['Donkey' => 'cat']);
98 $this->assertTrue($callbackCalled);
99 $this->assertInstanceOf(Request::class, $requestParam);
100 $this->assertEquals('cat', $requestParam->header('donkey'));
103 public function test_event_web_middleware_before_return_val_used_as_response()
105 $callback = function (Request $request) {
106 return response('cat', 412);
109 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_BEFORE, $callback);
110 $resp = $this->get('/login', ['Donkey' => 'cat']);
111 $resp->assertSee('cat');
112 $resp->assertStatus(412);
115 public function test_event_web_middleware_after()
117 $callbackCalled = false;
118 $requestParam = null;
119 $responseParam = null;
120 $callback = function ($request, Response $response) use (&$callbackCalled, &$requestParam, &$responseParam) {
121 $requestParam = $request;
122 $responseParam = $response;
123 $callbackCalled = true;
124 $response->header('donkey', 'cat123');
127 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_AFTER, $callback);
129 $resp = $this->get('/login', ['Donkey' => 'cat']);
130 $this->assertTrue($callbackCalled);
131 $this->assertInstanceOf(Request::class, $requestParam);
132 $this->assertInstanceOf(Response::class, $responseParam);
133 $resp->assertHeader('donkey', 'cat123');
136 public function test_event_web_middleware_after_return_val_used_as_response()
138 $callback = function () {
139 return response('cat456', 443);
142 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_AFTER, $callback);
144 $resp = $this->get('/login', ['Donkey' => 'cat']);
145 $resp->assertSee('cat456');
146 $resp->assertStatus(443);
149 public function test_event_auth_login_standard()
152 $callback = function (...$eventArgs) use (&$args) {
156 Theme::listen(ThemeEvents::AUTH_LOGIN, $callback);
159 $this->assertCount(2, $args);
160 $this->assertEquals('standard', $args[0]);
161 $this->assertInstanceOf(User::class, $args[1]);
164 public function test_event_auth_register_standard()
167 $callback = function (...$eventArgs) use (&$args) {
170 Theme::listen(ThemeEvents::AUTH_REGISTER, $callback);
171 $this->setSettings(['registration-enabled' => 'true']);
173 $user = User::factory()->make();
174 $this->post('/register', ['email' => $user->email, 'name' => $user->name, 'password' => 'password']);
176 $this->assertCount(2, $args);
177 $this->assertEquals('standard', $args[0]);
178 $this->assertInstanceOf(User::class, $args[1]);
181 public function test_event_webhook_call_before()
184 $callback = function (...$eventArgs) use (&$args) {
187 return ['test' => 'hello!'];
189 Theme::listen(ThemeEvents::WEBHOOK_CALL_BEFORE, $callback);
191 $responses = $this->mockHttpClient([new \GuzzleHttp\Psr7\Response(200, [], '')]);
193 $webhook = new Webhook(['name' => 'Test webhook', 'endpoint' => 'https://example.com']);
195 $event = ActivityType::PAGE_UPDATE;
196 $detail = Page::query()->first();
198 dispatch((new DispatchWebhookJob($webhook, $event, $detail)));
200 $this->assertCount(5, $args);
201 $this->assertEquals($event, $args[0]);
202 $this->assertEquals($webhook->id, $args[1]->id);
203 $this->assertEquals($detail->id, $args[2]->id);
205 $this->assertEquals(1, $responses->requestCount());
206 $request = $responses->latestRequest();
207 $reqData = json_decode($request->getBody(), true);
208 $this->assertEquals('hello!', $reqData['test']);
211 public function test_event_activity_logged()
213 $book = $this->entities->book();
215 $callback = function (...$eventArgs) use (&$args) {
219 Theme::listen(ThemeEvents::ACTIVITY_LOGGED, $callback);
220 $this->asEditor()->put($book->getUrl(), ['name' => 'My cool update book!']);
222 $this->assertCount(2, $args);
223 $this->assertEquals(ActivityType::BOOK_UPDATE, $args[0]);
224 $this->assertTrue($args[1] instanceof Book);
225 $this->assertEquals($book->id, $args[1]->id);
228 public function test_event_page_include_parse()
230 /** @var Page $page */
231 /** @var Page $otherPage */
232 $page = $this->entities->page();
233 $otherPage = Page::query()->where('id', '!=', $page->id)->first();
234 $otherPage->html = '<p id="bkmrk-cool">This is a really cool section</p>';
235 $page->html = "<p>{{@{$otherPage->id}#bkmrk-cool}}</p>";
240 $callback = function (...$eventArgs) use (&$args) {
243 return '<strong>Big & content replace surprise!</strong>';
246 Theme::listen(ThemeEvents::PAGE_INCLUDE_PARSE, $callback);
247 $resp = $this->asEditor()->get($page->getUrl());
248 $this->withHtml($resp)->assertElementContains('.page-content strong', 'Big & content replace surprise!');
250 $this->assertCount(4, $args);
251 $this->assertEquals($otherPage->id . '#bkmrk-cool', $args[0]);
252 $this->assertEquals('This is a really cool section', $args[1]);
253 $this->assertTrue($args[2] instanceof Page);
254 $this->assertTrue($args[3] instanceof Page);
255 $this->assertEquals($page->id, $args[2]->id);
256 $this->assertEquals($otherPage->id, $args[3]->id);
259 public function test_event_routes_register_web_and_web_auth()
261 $functionsContent = <<<'END'
263 use BookStack\Theming\ThemeEvents;
264 use BookStack\Facades\Theme;
265 use Illuminate\Routing\Router;
266 Theme::listen(ThemeEvents::ROUTES_REGISTER_WEB, function (Router $router) {
267 $router->get('/cat', fn () => 'cat')->name('say.cat');
269 Theme::listen(ThemeEvents::ROUTES_REGISTER_WEB_AUTH, function (Router $router) {
270 $router->get('/dog', fn () => 'dog')->name('say.dog');
274 $this->usingThemeFolder(function () use ($functionsContent) {
276 $functionsFile = theme_path('functions.php');
277 file_put_contents($functionsFile, $functionsContent);
279 $app = $this->createApplication();
280 /** @var \Illuminate\Routing\Router $router */
281 $router = $app->get('router');
283 /** @var \Illuminate\Routing\Route $catRoute */
284 $catRoute = $router->getRoutes()->getRoutesByName()['say.cat'];
285 $this->assertEquals(['web'], $catRoute->middleware());
287 /** @var \Illuminate\Routing\Route $dogRoute */
288 $dogRoute = $router->getRoutes()->getRoutesByName()['say.dog'];
289 $this->assertEquals(['web', 'auth'], $dogRoute->middleware());
293 public function test_add_social_driver()
295 Theme::addSocialDriver('catnet', [
296 'client_id' => 'abc123',
297 'client_secret' => 'def456',
298 ], 'SocialiteProviders\Discord\DiscordExtendSocialite@handleTesting');
300 $this->assertEquals('catnet', config('services.catnet.name'));
301 $this->assertEquals('abc123', config('services.catnet.client_id'));
302 $this->assertEquals(url('/login/service/catnet/callback'), config('services.catnet.redirect'));
304 $loginResp = $this->get('/login');
305 $loginResp->assertSee('login/service/catnet');
308 public function test_add_social_driver_uses_name_in_config_if_given()
310 Theme::addSocialDriver('catnet', [
311 'client_id' => 'abc123',
312 'client_secret' => 'def456',
313 'name' => 'Super Cat Name',
314 ], 'SocialiteProviders\Discord\DiscordExtendSocialite@handleTesting');
316 $this->assertEquals('Super Cat Name', config('services.catnet.name'));
317 $loginResp = $this->get('/login');
318 $loginResp->assertSee('Super Cat Name');
321 public function test_add_social_driver_allows_a_configure_for_redirect_callback_to_be_passed()
323 Theme::addSocialDriver(
326 'client_id' => 'abc123',
327 'client_secret' => 'def456',
328 'name' => 'Super Cat Name',
330 'SocialiteProviders\Discord\DiscordExtendSocialite@handle',
332 $driver->with(['donkey' => 'donut']);
336 $loginResp = $this->get('/login/service/discord');
337 $redirect = $loginResp->headers->get('location');
338 $this->assertStringContainsString('donkey=donut', $redirect);
341 public function test_register_command_allows_provided_command_to_be_usable_via_artisan()
343 Theme::registerCommand(new MyCustomCommand());
345 Artisan::call('bookstack:test-custom-command', []);
346 $output = Artisan::output();
348 $this->assertStringContainsString('Command ran!', $output);
351 public function test_base_body_start_and_end_template_files_can_be_used()
353 $bodyStartStr = 'barry-fought-against-the-panther';
354 $bodyEndStr = 'barry-lost-his-fight-with-grace';
356 $this->usingThemeFolder(function (string $folder) use ($bodyStartStr, $bodyEndStr) {
357 $viewDir = theme_path('layouts/parts');
358 mkdir($viewDir, 0777, true);
359 file_put_contents($viewDir . '/base-body-start.blade.php', $bodyStartStr);
360 file_put_contents($viewDir . '/base-body-end.blade.php', $bodyEndStr);
362 $resp = $this->asEditor()->get('/');
363 $resp->assertSee($bodyStartStr);
364 $resp->assertSee($bodyEndStr);
368 public function test_export_body_start_and_end_template_files_can_be_used()
370 $bodyStartStr = 'garry-fought-against-the-panther';
371 $bodyEndStr = 'garry-lost-his-fight-with-grace';
372 $page = $this->entities->page();
374 $this->usingThemeFolder(function (string $folder) use ($bodyStartStr, $bodyEndStr, $page) {
375 $viewDir = theme_path('layouts/parts');
376 mkdir($viewDir, 0777, true);
377 file_put_contents($viewDir . '/export-body-start.blade.php', $bodyStartStr);
378 file_put_contents($viewDir . '/export-body-end.blade.php', $bodyEndStr);
380 $resp = $this->asEditor()->get($page->getUrl('/export/html'));
381 $resp->assertSee($bodyStartStr);
382 $resp->assertSee($bodyEndStr);
386 public function test_login_and_register_message_template_files_can_be_used()
388 $loginMessage = 'Welcome to this instance, login below you scallywag';
389 $registerMessage = 'You want to register? Enter the deets below you numpty';
391 $this->usingThemeFolder(function (string $folder) use ($loginMessage, $registerMessage) {
392 $viewDir = theme_path('auth/parts');
393 mkdir($viewDir, 0777, true);
394 file_put_contents($viewDir . '/login-message.blade.php', $loginMessage);
395 file_put_contents($viewDir . '/register-message.blade.php', $registerMessage);
396 $this->setSettings(['registration-enabled' => 'true']);
398 $this->get('/login')->assertSee($loginMessage);
399 $this->get('/register')->assertSee($registerMessage);
403 public function test_header_links_start_template_file_can_be_used()
405 $content = 'This is added text in the header bar';
407 $this->usingThemeFolder(function (string $folder) use ($content) {
408 $viewDir = theme_path('layouts/parts');
409 mkdir($viewDir, 0777, true);
410 file_put_contents($viewDir . '/header-links-start.blade.php', $content);
411 $this->setSettings(['registration-enabled' => 'true']);
413 $this->get('/login')->assertSee($content);
417 protected function usingThemeFolder(callable $callback)
419 // Create a folder and configure a theme
420 $themeFolderName = 'testing_theme_' . str_shuffle(rtrim(base64_encode(time()), '='));
421 config()->set('view.theme', $themeFolderName);
422 $themeFolderPath = theme_path('');
424 // Create theme folder and clean it up on application tear-down
425 File::makeDirectory($themeFolderPath);
426 $this->beforeApplicationDestroyed(fn() => File::deleteDirectory($themeFolderPath));
428 // Run provided callback with theme env option set
429 $this->runWithEnv('APP_THEME', $themeFolderName, function () use ($callback, $themeFolderName) {
430 call_user_func($callback, $themeFolderName);
435 class MyCustomCommand extends Command
437 protected $signature = 'bookstack:test-custom-command';
439 public function handle()
441 $this->line('Command ran!');