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\Client\Request as HttpClientRequest;
16 use Illuminate\Http\Request;
17 use Illuminate\Http\Response;
18 use Illuminate\Support\Facades\Artisan;
19 use Illuminate\Support\Facades\File;
20 use Illuminate\Support\Facades\Http;
21 use League\CommonMark\ConfigurableEnvironmentInterface;
22 use League\CommonMark\Environment\Environment;
24 class ThemeTest extends TestCase
26 protected string $themeFolderName;
27 protected string $themeFolderPath;
29 public function test_translation_text_can_be_overridden_via_theme()
31 $this->usingThemeFolder(function () {
32 $translationPath = theme_path('/lang/en');
33 File::makeDirectory($translationPath, 0777, true);
35 $customTranslations = '<?php
36 return [\'books\' => \'Sandwiches\'];
38 file_put_contents($translationPath . '/entities.php', $customTranslations);
40 $homeRequest = $this->actingAs($this->users->viewer())->get('/');
41 $this->withHtml($homeRequest)->assertElementContains('header nav', 'Sandwiches');
45 public function test_theme_functions_file_used_and_app_boot_event_runs()
47 $this->usingThemeFolder(function ($themeFolder) {
48 $functionsFile = theme_path('functions.php');
49 app()->alias('cat', 'dog');
50 file_put_contents($functionsFile, "<?php\nTheme::listen(\BookStack\Theming\ThemeEvents::APP_BOOT, function(\$app) { \$app->alias('cat', 'dog');});");
51 $this->runWithEnv('APP_THEME', $themeFolder, function () {
52 $this->assertEquals('cat', $this->app->getAlias('dog'));
57 public function test_event_commonmark_environment_configure()
59 $callbackCalled = false;
60 $callback = function ($environment) use (&$callbackCalled) {
61 $this->assertInstanceOf(Environment::class, $environment);
62 $callbackCalled = true;
66 Theme::listen(ThemeEvents::COMMONMARK_ENVIRONMENT_CONFIGURE, $callback);
68 $page = $this->entities->page();
69 $content = new PageContent($page);
70 $content->setNewMarkdown('# test');
72 $this->assertTrue($callbackCalled);
75 public function test_event_web_middleware_before()
77 $callbackCalled = false;
79 $callback = function ($request) use (&$callbackCalled, &$requestParam) {
80 $requestParam = $request;
81 $callbackCalled = true;
84 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_BEFORE, $callback);
85 $this->get('/login', ['Donkey' => 'cat']);
87 $this->assertTrue($callbackCalled);
88 $this->assertInstanceOf(Request::class, $requestParam);
89 $this->assertEquals('cat', $requestParam->header('donkey'));
92 public function test_event_web_middleware_before_return_val_used_as_response()
94 $callback = function (Request $request) {
95 return response('cat', 412);
98 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_BEFORE, $callback);
99 $resp = $this->get('/login', ['Donkey' => 'cat']);
100 $resp->assertSee('cat');
101 $resp->assertStatus(412);
104 public function test_event_web_middleware_after()
106 $callbackCalled = false;
107 $requestParam = null;
108 $responseParam = null;
109 $callback = function ($request, Response $response) use (&$callbackCalled, &$requestParam, &$responseParam) {
110 $requestParam = $request;
111 $responseParam = $response;
112 $callbackCalled = true;
113 $response->header('donkey', 'cat123');
116 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_AFTER, $callback);
118 $resp = $this->get('/login', ['Donkey' => 'cat']);
119 $this->assertTrue($callbackCalled);
120 $this->assertInstanceOf(Request::class, $requestParam);
121 $this->assertInstanceOf(Response::class, $responseParam);
122 $resp->assertHeader('donkey', 'cat123');
125 public function test_event_web_middleware_after_return_val_used_as_response()
127 $callback = function () {
128 return response('cat456', 443);
131 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_AFTER, $callback);
133 $resp = $this->get('/login', ['Donkey' => 'cat']);
134 $resp->assertSee('cat456');
135 $resp->assertStatus(443);
138 public function test_event_auth_login_standard()
141 $callback = function (...$eventArgs) use (&$args) {
145 Theme::listen(ThemeEvents::AUTH_LOGIN, $callback);
148 $this->assertCount(2, $args);
149 $this->assertEquals('standard', $args[0]);
150 $this->assertInstanceOf(User::class, $args[1]);
153 public function test_event_auth_register_standard()
156 $callback = function (...$eventArgs) use (&$args) {
159 Theme::listen(ThemeEvents::AUTH_REGISTER, $callback);
160 $this->setSettings(['registration-enabled' => 'true']);
162 $user = User::factory()->make();
163 $this->post('/register', ['email' => $user->email, 'name' => $user->name, 'password' => 'password']);
165 $this->assertCount(2, $args);
166 $this->assertEquals('standard', $args[0]);
167 $this->assertInstanceOf(User::class, $args[1]);
170 public function test_event_webhook_call_before()
173 $callback = function (...$eventArgs) use (&$args) {
176 return ['test' => 'hello!'];
178 Theme::listen(ThemeEvents::WEBHOOK_CALL_BEFORE, $callback);
181 '*' => Http::response('', 200),
184 $webhook = new Webhook(['name' => 'Test webhook', 'endpoint' => 'https://example.com']);
186 $event = ActivityType::PAGE_UPDATE;
187 $detail = Page::query()->first();
189 dispatch((new DispatchWebhookJob($webhook, $event, $detail)));
191 $this->assertCount(5, $args);
192 $this->assertEquals($event, $args[0]);
193 $this->assertEquals($webhook->id, $args[1]->id);
194 $this->assertEquals($detail->id, $args[2]->id);
196 Http::assertSent(function (HttpClientRequest $request) {
197 return $request->isJson() && $request->data()['test'] === 'hello!';
201 public function test_event_activity_logged()
203 $book = $this->entities->book();
205 $callback = function (...$eventArgs) use (&$args) {
209 Theme::listen(ThemeEvents::ACTIVITY_LOGGED, $callback);
210 $this->asEditor()->put($book->getUrl(), ['name' => 'My cool update book!']);
212 $this->assertCount(2, $args);
213 $this->assertEquals(ActivityType::BOOK_UPDATE, $args[0]);
214 $this->assertTrue($args[1] instanceof Book);
215 $this->assertEquals($book->id, $args[1]->id);
218 public function test_event_page_include_parse()
220 /** @var Page $page */
221 /** @var Page $otherPage */
222 $page = $this->entities->page();
223 $otherPage = Page::query()->where('id', '!=', $page->id)->first();
224 $otherPage->html = '<p id="bkmrk-cool">This is a really cool section</p>';
225 $page->html = "<p>{{@{$otherPage->id}#bkmrk-cool}}</p>";
230 $callback = function (...$eventArgs) use (&$args) {
233 return '<strong>Big & content replace surprise!</strong>';
236 Theme::listen(ThemeEvents::PAGE_INCLUDE_PARSE, $callback);
237 $resp = $this->asEditor()->get($page->getUrl());
238 $this->withHtml($resp)->assertElementContains('.page-content strong', 'Big & content replace surprise!');
240 $this->assertCount(4, $args);
241 $this->assertEquals($otherPage->id . '#bkmrk-cool', $args[0]);
242 $this->assertEquals('This is a really cool section', $args[1]);
243 $this->assertTrue($args[2] instanceof Page);
244 $this->assertTrue($args[3] instanceof Page);
245 $this->assertEquals($page->id, $args[2]->id);
246 $this->assertEquals($otherPage->id, $args[3]->id);
249 public function test_add_social_driver()
251 Theme::addSocialDriver('catnet', [
252 'client_id' => 'abc123',
253 'client_secret' => 'def456',
254 ], 'SocialiteProviders\Discord\DiscordExtendSocialite@handleTesting');
256 $this->assertEquals('catnet', config('services.catnet.name'));
257 $this->assertEquals('abc123', config('services.catnet.client_id'));
258 $this->assertEquals(url('/login/service/catnet/callback'), config('services.catnet.redirect'));
260 $loginResp = $this->get('/login');
261 $loginResp->assertSee('login/service/catnet');
264 public function test_add_social_driver_uses_name_in_config_if_given()
266 Theme::addSocialDriver('catnet', [
267 'client_id' => 'abc123',
268 'client_secret' => 'def456',
269 'name' => 'Super Cat Name',
270 ], 'SocialiteProviders\Discord\DiscordExtendSocialite@handleTesting');
272 $this->assertEquals('Super Cat Name', config('services.catnet.name'));
273 $loginResp = $this->get('/login');
274 $loginResp->assertSee('Super Cat Name');
277 public function test_add_social_driver_allows_a_configure_for_redirect_callback_to_be_passed()
279 Theme::addSocialDriver(
282 'client_id' => 'abc123',
283 'client_secret' => 'def456',
284 'name' => 'Super Cat Name',
286 'SocialiteProviders\Discord\DiscordExtendSocialite@handle',
288 $driver->with(['donkey' => 'donut']);
292 $loginResp = $this->get('/login/service/discord');
293 $redirect = $loginResp->headers->get('location');
294 $this->assertStringContainsString('donkey=donut', $redirect);
297 public function test_register_command_allows_provided_command_to_be_usable_via_artisan()
299 Theme::registerCommand(new MyCustomCommand());
301 Artisan::call('bookstack:test-custom-command', []);
302 $output = Artisan::output();
304 $this->assertStringContainsString('Command ran!', $output);
307 public function test_base_body_start_and_end_template_files_can_be_used()
309 $bodyStartStr = 'barry-fought-against-the-panther';
310 $bodyEndStr = 'barry-lost-his-fight-with-grace';
312 $this->usingThemeFolder(function (string $folder) use ($bodyStartStr, $bodyEndStr) {
313 $viewDir = theme_path('layouts/parts');
314 mkdir($viewDir, 0777, true);
315 file_put_contents($viewDir . '/base-body-start.blade.php', $bodyStartStr);
316 file_put_contents($viewDir . '/base-body-end.blade.php', $bodyEndStr);
318 $resp = $this->asEditor()->get('/');
319 $resp->assertSee($bodyStartStr);
320 $resp->assertSee($bodyEndStr);
324 public function test_export_body_start_and_end_template_files_can_be_used()
326 $bodyStartStr = 'garry-fought-against-the-panther';
327 $bodyEndStr = 'garry-lost-his-fight-with-grace';
328 $page = $this->entities->page();
330 $this->usingThemeFolder(function (string $folder) use ($bodyStartStr, $bodyEndStr, $page) {
331 $viewDir = theme_path('layouts/parts');
332 mkdir($viewDir, 0777, true);
333 file_put_contents($viewDir . '/export-body-start.blade.php', $bodyStartStr);
334 file_put_contents($viewDir . '/export-body-end.blade.php', $bodyEndStr);
336 $resp = $this->asEditor()->get($page->getUrl('/export/html'));
337 $resp->assertSee($bodyStartStr);
338 $resp->assertSee($bodyEndStr);
342 public function test_login_and_register_message_template_files_can_be_used()
344 $loginMessage = 'Welcome to this instance, login below you scallywag';
345 $registerMessage = 'You want to register? Enter the deets below you numpty';
347 $this->usingThemeFolder(function (string $folder) use ($loginMessage, $registerMessage) {
348 $viewDir = theme_path('auth/parts');
349 mkdir($viewDir, 0777, true);
350 file_put_contents($viewDir . '/login-message.blade.php', $loginMessage);
351 file_put_contents($viewDir . '/register-message.blade.php', $registerMessage);
352 $this->setSettings(['registration-enabled' => 'true']);
354 $this->get('/login')->assertSee($loginMessage);
355 $this->get('/register')->assertSee($registerMessage);
359 protected function usingThemeFolder(callable $callback)
361 // Create a folder and configure a theme
362 $themeFolderName = 'testing_theme_' . str_shuffle(rtrim(base64_encode(time()), '='));
363 config()->set('view.theme', $themeFolderName);
364 $themeFolderPath = theme_path('');
366 // Create theme folder and clean it up on application tear-down
367 File::makeDirectory($themeFolderPath);
368 $this->beforeApplicationDestroyed(fn() => File::deleteDirectory($themeFolderPath));
370 // Run provided callback with theme env option set
371 $this->runWithEnv('APP_THEME', $themeFolderName, function () use ($callback, $themeFolderName) {
372 call_user_func($callback, $themeFolderName);
377 class MyCustomCommand extends Command
379 protected $signature = 'bookstack:test-custom-command';
381 public function handle()
383 $this->line('Command ran!');