5 use BookStack\Actions\ActivityType;
6 use BookStack\Actions\DispatchWebhookJob;
7 use BookStack\Actions\Webhook;
8 use BookStack\Auth\User;
9 use BookStack\Entities\Models\Book;
10 use BookStack\Entities\Models\Page;
11 use BookStack\Entities\Tools\PageContent;
12 use BookStack\Facades\Theme;
13 use BookStack\Theming\ThemeEvents;
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;
23 class ThemeTest extends TestCase
25 protected $themeFolderName;
26 protected $themeFolderPath;
28 public function test_translation_text_can_be_overridden_via_theme()
30 $this->usingThemeFolder(function () {
31 $translationPath = theme_path('/lang/en');
32 File::makeDirectory($translationPath, 0777, true);
34 $customTranslations = '<?php
35 return [\'books\' => \'Sandwiches\'];
37 file_put_contents($translationPath . '/entities.php', $customTranslations);
39 $homeRequest = $this->actingAs($this->getViewer())->get('/');
40 $this->withHtml($homeRequest)->assertElementContains('header nav', 'Sandwiches');
44 public function test_theme_functions_file_used_and_app_boot_event_runs()
46 $this->usingThemeFolder(function ($themeFolder) {
47 $functionsFile = theme_path('functions.php');
48 app()->alias('cat', 'dog');
49 file_put_contents($functionsFile, "<?php\nTheme::listen(\BookStack\Theming\ThemeEvents::APP_BOOT, function(\$app) { \$app->alias('cat', 'dog');});");
50 $this->runWithEnv('APP_THEME', $themeFolder, function () {
51 $this->assertEquals('cat', $this->app->getAlias('dog'));
56 public function test_event_commonmark_environment_configure()
58 $callbackCalled = false;
59 $callback = function ($environment) use (&$callbackCalled) {
60 $this->assertInstanceOf(ConfigurableEnvironmentInterface::class, $environment);
61 $callbackCalled = true;
65 Theme::listen(ThemeEvents::COMMONMARK_ENVIRONMENT_CONFIGURE, $callback);
67 $page = Page::query()->first();
68 $content = new PageContent($page);
69 $content->setNewMarkdown('# test');
71 $this->assertTrue($callbackCalled);
74 public function test_event_web_middleware_before()
76 $callbackCalled = false;
78 $callback = function ($request) use (&$callbackCalled, &$requestParam) {
79 $requestParam = $request;
80 $callbackCalled = true;
83 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_BEFORE, $callback);
84 $this->get('/login', ['Donkey' => 'cat']);
86 $this->assertTrue($callbackCalled);
87 $this->assertInstanceOf(Request::class, $requestParam);
88 $this->assertEquals('cat', $requestParam->header('donkey'));
91 public function test_event_web_middleware_before_return_val_used_as_response()
93 $callback = function (Request $request) {
94 return response('cat', 412);
97 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_BEFORE, $callback);
98 $resp = $this->get('/login', ['Donkey' => 'cat']);
99 $resp->assertSee('cat');
100 $resp->assertStatus(412);
103 public function test_event_web_middleware_after()
105 $callbackCalled = false;
106 $requestParam = null;
107 $responseParam = null;
108 $callback = function ($request, Response $response) use (&$callbackCalled, &$requestParam, &$responseParam) {
109 $requestParam = $request;
110 $responseParam = $response;
111 $callbackCalled = true;
112 $response->header('donkey', 'cat123');
115 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_AFTER, $callback);
117 $resp = $this->get('/login', ['Donkey' => 'cat']);
118 $this->assertTrue($callbackCalled);
119 $this->assertInstanceOf(Request::class, $requestParam);
120 $this->assertInstanceOf(Response::class, $responseParam);
121 $resp->assertHeader('donkey', 'cat123');
124 public function test_event_web_middleware_after_return_val_used_as_response()
126 $callback = function () {
127 return response('cat456', 443);
130 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_AFTER, $callback);
132 $resp = $this->get('/login', ['Donkey' => 'cat']);
133 $resp->assertSee('cat456');
134 $resp->assertStatus(443);
137 public function test_event_auth_login_standard()
140 $callback = function (...$eventArgs) use (&$args) {
144 Theme::listen(ThemeEvents::AUTH_LOGIN, $callback);
147 $this->assertCount(2, $args);
148 $this->assertEquals('standard', $args[0]);
149 $this->assertInstanceOf(User::class, $args[1]);
152 public function test_event_auth_register_standard()
155 $callback = function (...$eventArgs) use (&$args) {
158 Theme::listen(ThemeEvents::AUTH_REGISTER, $callback);
159 $this->setSettings(['registration-enabled' => 'true']);
161 $user = User::factory()->make();
162 $this->post('/register', ['email' => $user->email, 'name' => $user->name, 'password' => 'password']);
164 $this->assertCount(2, $args);
165 $this->assertEquals('standard', $args[0]);
166 $this->assertInstanceOf(User::class, $args[1]);
169 public function test_event_webhook_call_before()
172 $callback = function (...$eventArgs) use (&$args) {
175 return ['test' => 'hello!'];
177 Theme::listen(ThemeEvents::WEBHOOK_CALL_BEFORE, $callback);
180 '*' => Http::response('', 200),
183 $webhook = new Webhook(['name' => 'Test webhook', 'endpoint' => 'https://example.com']);
185 $event = ActivityType::PAGE_UPDATE;
186 $detail = Page::query()->first();
188 dispatch((new DispatchWebhookJob($webhook, $event, $detail)));
190 $this->assertCount(5, $args);
191 $this->assertEquals($event, $args[0]);
192 $this->assertEquals($webhook->id, $args[1]->id);
193 $this->assertEquals($detail->id, $args[2]->id);
195 Http::assertSent(function (HttpClientRequest $request) {
196 return $request->isJson() && $request->data()['test'] === 'hello!';
200 public function test_event_activity_logged()
202 $book = Book::query()->first();
204 $callback = function (...$eventArgs) use (&$args) {
208 Theme::listen(ThemeEvents::ACTIVITY_LOGGED, $callback);
209 $this->asEditor()->put($book->getUrl(), ['name' => 'My cool update book!']);
211 $this->assertCount(2, $args);
212 $this->assertEquals(ActivityType::BOOK_UPDATE, $args[0]);
213 $this->assertTrue($args[1] instanceof Book);
214 $this->assertEquals($book->id, $args[1]->id);
217 public function test_event_page_include_parse()
219 /** @var Page $page */
220 /** @var Page $otherPage */
221 $page = Page::query()->first();
222 $otherPage = Page::query()->where('id', '!=', $page->id)->first();
223 $otherPage->html = '<p id="bkmrk-cool">This is a really cool section</p>';
224 $page->html = "<p>{{@{$otherPage->id}#bkmrk-cool}}</p>";
229 $callback = function (...$eventArgs) use (&$args) {
232 return '<strong>Big & content replace surprise!</strong>';
235 Theme::listen(ThemeEvents::PAGE_INCLUDE_PARSE, $callback);
236 $resp = $this->asEditor()->get($page->getUrl());
237 $this->withHtml($resp)->assertElementContains('.page-content strong', 'Big & content replace surprise!');
239 $this->assertCount(4, $args);
240 $this->assertEquals($otherPage->id . '#bkmrk-cool', $args[0]);
241 $this->assertEquals('This is a really cool section', $args[1]);
242 $this->assertTrue($args[2] instanceof Page);
243 $this->assertTrue($args[3] instanceof Page);
244 $this->assertEquals($page->id, $args[2]->id);
245 $this->assertEquals($otherPage->id, $args[3]->id);
248 public function test_add_social_driver()
250 Theme::addSocialDriver('catnet', [
251 'client_id' => 'abc123',
252 'client_secret' => 'def456',
253 ], 'SocialiteProviders\Discord\DiscordExtendSocialite@handleTesting');
255 $this->assertEquals('catnet', config('services.catnet.name'));
256 $this->assertEquals('abc123', config('services.catnet.client_id'));
257 $this->assertEquals(url('/login/service/catnet/callback'), config('services.catnet.redirect'));
259 $loginResp = $this->get('/login');
260 $loginResp->assertSee('login/service/catnet');
263 public function test_add_social_driver_uses_name_in_config_if_given()
265 Theme::addSocialDriver('catnet', [
266 'client_id' => 'abc123',
267 'client_secret' => 'def456',
268 'name' => 'Super Cat Name',
269 ], 'SocialiteProviders\Discord\DiscordExtendSocialite@handleTesting');
271 $this->assertEquals('Super Cat Name', config('services.catnet.name'));
272 $loginResp = $this->get('/login');
273 $loginResp->assertSee('Super Cat Name');
276 public function test_add_social_driver_allows_a_configure_for_redirect_callback_to_be_passed()
278 Theme::addSocialDriver(
281 'client_id' => 'abc123',
282 'client_secret' => 'def456',
283 'name' => 'Super Cat Name',
285 'SocialiteProviders\Discord\DiscordExtendSocialite@handle',
287 $driver->with(['donkey' => 'donut']);
291 $loginResp = $this->get('/login/service/discord');
292 $redirect = $loginResp->headers->get('location');
293 $this->assertStringContainsString('donkey=donut', $redirect);
296 public function test_register_command_allows_provided_command_to_be_usable_via_artisan()
298 Theme::registerCommand(new MyCustomCommand());
300 Artisan::call('bookstack:test-custom-command', []);
301 $output = Artisan::output();
303 $this->assertStringContainsString('Command ran!', $output);
306 public function test_base_body_start_and_end_template_files_can_be_used()
308 $bodyStartStr = 'barry-fought-against-the-panther';
309 $bodyEndStr = 'barry-lost-his-fight-with-grace';
311 $this->usingThemeFolder(function (string $folder) use ($bodyStartStr, $bodyEndStr) {
312 $viewDir = theme_path('layouts/parts');
313 mkdir($viewDir, 0777, true);
314 file_put_contents($viewDir . '/base-body-start.blade.php', $bodyStartStr);
315 file_put_contents($viewDir . '/base-body-end.blade.php', $bodyEndStr);
317 $resp = $this->asEditor()->get('/');
318 $resp->assertSee($bodyStartStr);
319 $resp->assertSee($bodyEndStr);
323 public function test_export_body_start_and_end_template_files_can_be_used()
325 $bodyStartStr = 'garry-fought-against-the-panther';
326 $bodyEndStr = 'garry-lost-his-fight-with-grace';
327 /** @var Page $page */
328 $page = Page::query()->first();
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 protected function usingThemeFolder(callable $callback)
344 // Create a folder and configure a theme
345 $themeFolderName = 'testing_theme_' . str_shuffle(rtrim(base64_encode(time()), '='));
346 config()->set('view.theme', $themeFolderName);
347 $themeFolderPath = theme_path('');
349 // Create theme folder and clean it up on application tear-down
350 File::makeDirectory($themeFolderPath);
351 $this->beforeApplicationDestroyed(fn() => File::deleteDirectory($themeFolderPath));
353 // Run provided callback with theme env option set
354 $this->runWithEnv('APP_THEME', $themeFolderName, function () use ($callback, $themeFolderName) {
355 call_user_func($callback, $themeFolderName);
360 class MyCustomCommand extends Command
362 protected $signature = 'bookstack:test-custom-command';
364 public function handle()
366 $this->line('Command ran!');