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\Facades\Theme;
12 use BookStack\Theming\ThemeEvents;
13 use BookStack\Users\Models\User;
14 use Illuminate\Console\Command;
15 use Illuminate\Http\Request;
16 use Illuminate\Http\Response;
17 use Illuminate\Support\Facades\Artisan;
18 use Illuminate\Support\Facades\File;
19 use League\CommonMark\Environment\Environment;
21 class ThemeTest extends TestCase
23 protected string $themeFolderName;
24 protected string $themeFolderPath;
26 public function test_translation_text_can_be_overridden_via_theme()
28 $this->usingThemeFolder(function () {
29 $translationPath = theme_path('/lang/en');
30 File::makeDirectory($translationPath, 0777, true);
32 $customTranslations = '<?php
33 return [\'books\' => \'Sandwiches\'];
35 file_put_contents($translationPath . '/entities.php', $customTranslations);
37 $homeRequest = $this->actingAs($this->users->viewer())->get('/');
38 $this->withHtml($homeRequest)->assertElementContains('header nav', 'Sandwiches');
42 public function test_theme_functions_file_used_and_app_boot_event_runs()
44 $this->usingThemeFolder(function ($themeFolder) {
45 $functionsFile = theme_path('functions.php');
46 app()->alias('cat', 'dog');
47 file_put_contents($functionsFile, "<?php\nTheme::listen(\BookStack\Theming\ThemeEvents::APP_BOOT, function(\$app) { \$app->alias('cat', 'dog');});");
48 $this->runWithEnv('APP_THEME', $themeFolder, function () {
49 $this->assertEquals('cat', $this->app->getAlias('dog'));
54 public function test_event_commonmark_environment_configure()
56 $callbackCalled = false;
57 $callback = function ($environment) use (&$callbackCalled) {
58 $this->assertInstanceOf(Environment::class, $environment);
59 $callbackCalled = true;
63 Theme::listen(ThemeEvents::COMMONMARK_ENVIRONMENT_CONFIGURE, $callback);
65 $page = $this->entities->page();
66 $content = new PageContent($page);
67 $content->setNewMarkdown('# test');
69 $this->assertTrue($callbackCalled);
72 public function test_event_web_middleware_before()
74 $callbackCalled = false;
76 $callback = function ($request) use (&$callbackCalled, &$requestParam) {
77 $requestParam = $request;
78 $callbackCalled = true;
81 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_BEFORE, $callback);
82 $this->get('/login', ['Donkey' => 'cat']);
84 $this->assertTrue($callbackCalled);
85 $this->assertInstanceOf(Request::class, $requestParam);
86 $this->assertEquals('cat', $requestParam->header('donkey'));
89 public function test_event_web_middleware_before_return_val_used_as_response()
91 $callback = function (Request $request) {
92 return response('cat', 412);
95 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_BEFORE, $callback);
96 $resp = $this->get('/login', ['Donkey' => 'cat']);
97 $resp->assertSee('cat');
98 $resp->assertStatus(412);
101 public function test_event_web_middleware_after()
103 $callbackCalled = false;
104 $requestParam = null;
105 $responseParam = null;
106 $callback = function ($request, Response $response) use (&$callbackCalled, &$requestParam, &$responseParam) {
107 $requestParam = $request;
108 $responseParam = $response;
109 $callbackCalled = true;
110 $response->header('donkey', 'cat123');
113 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_AFTER, $callback);
115 $resp = $this->get('/login', ['Donkey' => 'cat']);
116 $this->assertTrue($callbackCalled);
117 $this->assertInstanceOf(Request::class, $requestParam);
118 $this->assertInstanceOf(Response::class, $responseParam);
119 $resp->assertHeader('donkey', 'cat123');
122 public function test_event_web_middleware_after_return_val_used_as_response()
124 $callback = function () {
125 return response('cat456', 443);
128 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_AFTER, $callback);
130 $resp = $this->get('/login', ['Donkey' => 'cat']);
131 $resp->assertSee('cat456');
132 $resp->assertStatus(443);
135 public function test_event_auth_login_standard()
138 $callback = function (...$eventArgs) use (&$args) {
142 Theme::listen(ThemeEvents::AUTH_LOGIN, $callback);
145 $this->assertCount(2, $args);
146 $this->assertEquals('standard', $args[0]);
147 $this->assertInstanceOf(User::class, $args[1]);
150 public function test_event_auth_register_standard()
153 $callback = function (...$eventArgs) use (&$args) {
156 Theme::listen(ThemeEvents::AUTH_REGISTER, $callback);
157 $this->setSettings(['registration-enabled' => 'true']);
159 $user = User::factory()->make();
160 $this->post('/register', ['email' => $user->email, 'name' => $user->name, 'password' => 'password']);
162 $this->assertCount(2, $args);
163 $this->assertEquals('standard', $args[0]);
164 $this->assertInstanceOf(User::class, $args[1]);
167 public function test_event_webhook_call_before()
170 $callback = function (...$eventArgs) use (&$args) {
173 return ['test' => 'hello!'];
175 Theme::listen(ThemeEvents::WEBHOOK_CALL_BEFORE, $callback);
177 $responses = $this->mockHttpClient([new \GuzzleHttp\Psr7\Response(200, [], '')]);
179 $webhook = new Webhook(['name' => 'Test webhook', 'endpoint' => 'https://example.com']);
181 $event = ActivityType::PAGE_UPDATE;
182 $detail = Page::query()->first();
184 dispatch((new DispatchWebhookJob($webhook, $event, $detail)));
186 $this->assertCount(5, $args);
187 $this->assertEquals($event, $args[0]);
188 $this->assertEquals($webhook->id, $args[1]->id);
189 $this->assertEquals($detail->id, $args[2]->id);
191 $this->assertEquals(1, $responses->requestCount());
192 $request = $responses->latestRequest();
193 $reqData = json_decode($request->getBody(), true);
194 $this->assertEquals('hello!', $reqData['test']);
197 public function test_event_activity_logged()
199 $book = $this->entities->book();
201 $callback = function (...$eventArgs) use (&$args) {
205 Theme::listen(ThemeEvents::ACTIVITY_LOGGED, $callback);
206 $this->asEditor()->put($book->getUrl(), ['name' => 'My cool update book!']);
208 $this->assertCount(2, $args);
209 $this->assertEquals(ActivityType::BOOK_UPDATE, $args[0]);
210 $this->assertTrue($args[1] instanceof Book);
211 $this->assertEquals($book->id, $args[1]->id);
214 public function test_event_page_include_parse()
216 /** @var Page $page */
217 /** @var Page $otherPage */
218 $page = $this->entities->page();
219 $otherPage = Page::query()->where('id', '!=', $page->id)->first();
220 $otherPage->html = '<p id="bkmrk-cool">This is a really cool section</p>';
221 $page->html = "<p>{{@{$otherPage->id}#bkmrk-cool}}</p>";
226 $callback = function (...$eventArgs) use (&$args) {
229 return '<strong>Big & content replace surprise!</strong>';
232 Theme::listen(ThemeEvents::PAGE_INCLUDE_PARSE, $callback);
233 $resp = $this->asEditor()->get($page->getUrl());
234 $this->withHtml($resp)->assertElementContains('.page-content strong', 'Big & content replace surprise!');
236 $this->assertCount(4, $args);
237 $this->assertEquals($otherPage->id . '#bkmrk-cool', $args[0]);
238 $this->assertEquals('This is a really cool section', $args[1]);
239 $this->assertTrue($args[2] instanceof Page);
240 $this->assertTrue($args[3] instanceof Page);
241 $this->assertEquals($page->id, $args[2]->id);
242 $this->assertEquals($otherPage->id, $args[3]->id);
245 public function test_add_social_driver()
247 Theme::addSocialDriver('catnet', [
248 'client_id' => 'abc123',
249 'client_secret' => 'def456',
250 ], 'SocialiteProviders\Discord\DiscordExtendSocialite@handleTesting');
252 $this->assertEquals('catnet', config('services.catnet.name'));
253 $this->assertEquals('abc123', config('services.catnet.client_id'));
254 $this->assertEquals(url('/login/service/catnet/callback'), config('services.catnet.redirect'));
256 $loginResp = $this->get('/login');
257 $loginResp->assertSee('login/service/catnet');
260 public function test_add_social_driver_uses_name_in_config_if_given()
262 Theme::addSocialDriver('catnet', [
263 'client_id' => 'abc123',
264 'client_secret' => 'def456',
265 'name' => 'Super Cat Name',
266 ], 'SocialiteProviders\Discord\DiscordExtendSocialite@handleTesting');
268 $this->assertEquals('Super Cat Name', config('services.catnet.name'));
269 $loginResp = $this->get('/login');
270 $loginResp->assertSee('Super Cat Name');
273 public function test_add_social_driver_allows_a_configure_for_redirect_callback_to_be_passed()
275 Theme::addSocialDriver(
278 'client_id' => 'abc123',
279 'client_secret' => 'def456',
280 'name' => 'Super Cat Name',
282 'SocialiteProviders\Discord\DiscordExtendSocialite@handle',
284 $driver->with(['donkey' => 'donut']);
288 $loginResp = $this->get('/login/service/discord');
289 $redirect = $loginResp->headers->get('location');
290 $this->assertStringContainsString('donkey=donut', $redirect);
293 public function test_register_command_allows_provided_command_to_be_usable_via_artisan()
295 Theme::registerCommand(new MyCustomCommand());
297 Artisan::call('bookstack:test-custom-command', []);
298 $output = Artisan::output();
300 $this->assertStringContainsString('Command ran!', $output);
303 public function test_base_body_start_and_end_template_files_can_be_used()
305 $bodyStartStr = 'barry-fought-against-the-panther';
306 $bodyEndStr = 'barry-lost-his-fight-with-grace';
308 $this->usingThemeFolder(function (string $folder) use ($bodyStartStr, $bodyEndStr) {
309 $viewDir = theme_path('layouts/parts');
310 mkdir($viewDir, 0777, true);
311 file_put_contents($viewDir . '/base-body-start.blade.php', $bodyStartStr);
312 file_put_contents($viewDir . '/base-body-end.blade.php', $bodyEndStr);
314 $resp = $this->asEditor()->get('/');
315 $resp->assertSee($bodyStartStr);
316 $resp->assertSee($bodyEndStr);
320 public function test_export_body_start_and_end_template_files_can_be_used()
322 $bodyStartStr = 'garry-fought-against-the-panther';
323 $bodyEndStr = 'garry-lost-his-fight-with-grace';
324 $page = $this->entities->page();
326 $this->usingThemeFolder(function (string $folder) use ($bodyStartStr, $bodyEndStr, $page) {
327 $viewDir = theme_path('layouts/parts');
328 mkdir($viewDir, 0777, true);
329 file_put_contents($viewDir . '/export-body-start.blade.php', $bodyStartStr);
330 file_put_contents($viewDir . '/export-body-end.blade.php', $bodyEndStr);
332 $resp = $this->asEditor()->get($page->getUrl('/export/html'));
333 $resp->assertSee($bodyStartStr);
334 $resp->assertSee($bodyEndStr);
338 public function test_login_and_register_message_template_files_can_be_used()
340 $loginMessage = 'Welcome to this instance, login below you scallywag';
341 $registerMessage = 'You want to register? Enter the deets below you numpty';
343 $this->usingThemeFolder(function (string $folder) use ($loginMessage, $registerMessage) {
344 $viewDir = theme_path('auth/parts');
345 mkdir($viewDir, 0777, true);
346 file_put_contents($viewDir . '/login-message.blade.php', $loginMessage);
347 file_put_contents($viewDir . '/register-message.blade.php', $registerMessage);
348 $this->setSettings(['registration-enabled' => 'true']);
350 $this->get('/login')->assertSee($loginMessage);
351 $this->get('/register')->assertSee($registerMessage);
355 protected function usingThemeFolder(callable $callback)
357 // Create a folder and configure a theme
358 $themeFolderName = 'testing_theme_' . str_shuffle(rtrim(base64_encode(time()), '='));
359 config()->set('view.theme', $themeFolderName);
360 $themeFolderPath = theme_path('');
362 // Create theme folder and clean it up on application tear-down
363 File::makeDirectory($themeFolderPath);
364 $this->beforeApplicationDestroyed(fn() => File::deleteDirectory($themeFolderPath));
366 // Run provided callback with theme env option set
367 $this->runWithEnv('APP_THEME', $themeFolderName, function () use ($callback, $themeFolderName) {
368 call_user_func($callback, $themeFolderName);
373 class MyCustomCommand extends Command
375 protected $signature = 'bookstack:test-custom-command';
377 public function handle()
379 $this->line('Command ran!');