]> BookStack Code Mirror - bookstack/blob - tests/ThemeTest.php
Merge branch 'development' of github.com:BookStackApp/BookStack into development
[bookstack] / tests / ThemeTest.php
1 <?php
2
3 namespace Tests;
4
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;
21
22 class ThemeTest extends TestCase
23 {
24     protected string $themeFolderName;
25     protected string $themeFolderPath;
26
27     public function test_translation_text_can_be_overridden_via_theme()
28     {
29         $this->usingThemeFolder(function () {
30             $translationPath = theme_path('/lang/en');
31             File::makeDirectory($translationPath, 0777, true);
32
33             $customTranslations = '<?php
34             return [\'books\' => \'Sandwiches\'];
35         ';
36             file_put_contents($translationPath . '/entities.php', $customTranslations);
37
38             $homeRequest = $this->actingAs($this->users->viewer())->get('/');
39             $this->withHtml($homeRequest)->assertElementContains('header nav', 'Sandwiches');
40         });
41     }
42
43     public function test_theme_functions_file_used_and_app_boot_event_runs()
44     {
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'));
51             });
52         });
53     }
54
55     public function test_theme_functions_loads_errors_are_caught_and_logged()
56     {
57         $this->usingThemeFolder(function ($themeFolder) {
58             $functionsFile = theme_path('functions.php');
59             file_put_contents($functionsFile, "<?php\n\\BookStack\\Biscuits::eat();");
60
61             $this->expectException(ThemeException::class);
62             $this->expectExceptionMessageMatches('/Failed loading theme functions file at ".*?" with error: Class "BookStack\\\\Biscuits" not found/');
63
64             $this->runWithEnv('APP_THEME', $themeFolder, fn() => null);
65         });
66     }
67
68     public function test_event_commonmark_environment_configure()
69     {
70         $callbackCalled = false;
71         $callback = function ($environment) use (&$callbackCalled) {
72             $this->assertInstanceOf(Environment::class, $environment);
73             $callbackCalled = true;
74
75             return $environment;
76         };
77         Theme::listen(ThemeEvents::COMMONMARK_ENVIRONMENT_CONFIGURE, $callback);
78
79         $page = $this->entities->page();
80         $content = new PageContent($page);
81         $content->setNewMarkdown('# test', $this->users->editor());
82
83         $this->assertTrue($callbackCalled);
84     }
85
86     public function test_event_web_middleware_before()
87     {
88         $callbackCalled = false;
89         $requestParam = null;
90         $callback = function ($request) use (&$callbackCalled, &$requestParam) {
91             $requestParam = $request;
92             $callbackCalled = true;
93         };
94
95         Theme::listen(ThemeEvents::WEB_MIDDLEWARE_BEFORE, $callback);
96         $this->get('/login', ['Donkey' => 'cat']);
97
98         $this->assertTrue($callbackCalled);
99         $this->assertInstanceOf(Request::class, $requestParam);
100         $this->assertEquals('cat', $requestParam->header('donkey'));
101     }
102
103     public function test_event_web_middleware_before_return_val_used_as_response()
104     {
105         $callback = function (Request $request) {
106             return response('cat', 412);
107         };
108
109         Theme::listen(ThemeEvents::WEB_MIDDLEWARE_BEFORE, $callback);
110         $resp = $this->get('/login', ['Donkey' => 'cat']);
111         $resp->assertSee('cat');
112         $resp->assertStatus(412);
113     }
114
115     public function test_event_web_middleware_after()
116     {
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');
125         };
126
127         Theme::listen(ThemeEvents::WEB_MIDDLEWARE_AFTER, $callback);
128
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');
134     }
135
136     public function test_event_web_middleware_after_return_val_used_as_response()
137     {
138         $callback = function () {
139             return response('cat456', 443);
140         };
141
142         Theme::listen(ThemeEvents::WEB_MIDDLEWARE_AFTER, $callback);
143
144         $resp = $this->get('/login', ['Donkey' => 'cat']);
145         $resp->assertSee('cat456');
146         $resp->assertStatus(443);
147     }
148
149     public function test_event_auth_login_standard()
150     {
151         $args = [];
152         $callback = function (...$eventArgs) use (&$args) {
153             $args = $eventArgs;
154         };
155
156         Theme::listen(ThemeEvents::AUTH_LOGIN, $callback);
157         $this->post('/login', ['email' => 'admin@admin.com', 'password' => 'password']);
158
159         $this->assertCount(2, $args);
160         $this->assertEquals('standard', $args[0]);
161         $this->assertInstanceOf(User::class, $args[1]);
162     }
163
164     public function test_event_auth_register_standard()
165     {
166         $args = [];
167         $callback = function (...$eventArgs) use (&$args) {
168             $args = $eventArgs;
169         };
170         Theme::listen(ThemeEvents::AUTH_REGISTER, $callback);
171         $this->setSettings(['registration-enabled' => 'true']);
172
173         $user = User::factory()->make();
174         $this->post('/register', ['email' => $user->email, 'name' => $user->name, 'password' => 'password']);
175
176         $this->assertCount(2, $args);
177         $this->assertEquals('standard', $args[0]);
178         $this->assertInstanceOf(User::class, $args[1]);
179     }
180
181     public function test_event_auth_pre_register()
182     {
183         $args = [];
184         $callback = function (...$eventArgs) use (&$args) {
185             $args = $eventArgs;
186         };
187         Theme::listen(ThemeEvents::AUTH_PRE_REGISTER, $callback);
188         $this->setSettings(['registration-enabled' => 'true']);
189
190         $user = User::factory()->make();
191         $this->post('/register', ['email' => $user->email, 'name' => $user->name, 'password' => 'password']);
192
193         $this->assertCount(2, $args);
194         $this->assertEquals('standard', $args[0]);
195         $this->assertEquals([
196             'email' => $user->email,
197             'name' => $user->name,
198             'password' => 'password',
199         ], $args[1]);
200         $this->assertDatabaseHas('users', ['email' => $user->email]);
201     }
202
203     public function test_event_auth_pre_register_with_false_return_blocks_registration()
204     {
205         $callback = function () {
206             return false;
207         };
208         Theme::listen(ThemeEvents::AUTH_PRE_REGISTER, $callback);
209         $this->setSettings(['registration-enabled' => 'true']);
210
211         $user = User::factory()->make();
212         $resp = $this->post('/register', ['email' => $user->email, 'name' => $user->name, 'password' => 'password']);
213         $resp->assertRedirect('/login');
214         $this->assertSessionError('User account could not be registered for the provided details');
215         $this->assertDatabaseMissing('users', ['email' => $user->email]);
216     }
217
218     public function test_event_webhook_call_before()
219     {
220         $args = [];
221         $callback = function (...$eventArgs) use (&$args) {
222             $args = $eventArgs;
223
224             return ['test' => 'hello!'];
225         };
226         Theme::listen(ThemeEvents::WEBHOOK_CALL_BEFORE, $callback);
227
228         $responses = $this->mockHttpClient([new \GuzzleHttp\Psr7\Response(200, [], '')]);
229
230         $webhook = new Webhook(['name' => 'Test webhook', 'endpoint' => 'https://example.com']);
231         $webhook->save();
232         $event = ActivityType::PAGE_UPDATE;
233         $detail = Page::query()->first();
234
235         dispatch((new DispatchWebhookJob($webhook, $event, $detail)));
236
237         $this->assertCount(5, $args);
238         $this->assertEquals($event, $args[0]);
239         $this->assertEquals($webhook->id, $args[1]->id);
240         $this->assertEquals($detail->id, $args[2]->id);
241
242         $this->assertEquals(1, $responses->requestCount());
243         $request = $responses->latestRequest();
244         $reqData = json_decode($request->getBody(), true);
245         $this->assertEquals('hello!', $reqData['test']);
246     }
247
248     public function test_event_activity_logged()
249     {
250         $book = $this->entities->book();
251         $args = [];
252         $callback = function (...$eventArgs) use (&$args) {
253             $args = $eventArgs;
254         };
255
256         Theme::listen(ThemeEvents::ACTIVITY_LOGGED, $callback);
257         $this->asEditor()->put($book->getUrl(), ['name' => 'My cool update book!']);
258
259         $this->assertCount(2, $args);
260         $this->assertEquals(ActivityType::BOOK_UPDATE, $args[0]);
261         $this->assertTrue($args[1] instanceof Book);
262         $this->assertEquals($book->id, $args[1]->id);
263     }
264
265     public function test_event_page_include_parse()
266     {
267         /** @var Page $page */
268         /** @var Page $otherPage */
269         $page = $this->entities->page();
270         $otherPage = Page::query()->where('id', '!=', $page->id)->first();
271         $otherPage->html = '<p id="bkmrk-cool">This is a really cool section</p>';
272         $page->html = "<p>{{@{$otherPage->id}#bkmrk-cool}}</p>";
273         $page->save();
274         $otherPage->save();
275
276         $args = [];
277         $callback = function (...$eventArgs) use (&$args) {
278             $args = $eventArgs;
279
280             return '<strong>Big &amp; content replace surprise!</strong>';
281         };
282
283         Theme::listen(ThemeEvents::PAGE_INCLUDE_PARSE, $callback);
284         $resp = $this->asEditor()->get($page->getUrl());
285         $this->withHtml($resp)->assertElementContains('.page-content strong', 'Big & content replace surprise!');
286
287         $this->assertCount(4, $args);
288         $this->assertEquals($otherPage->id . '#bkmrk-cool', $args[0]);
289         $this->assertEquals('This is a really cool section', $args[1]);
290         $this->assertTrue($args[2] instanceof Page);
291         $this->assertTrue($args[3] instanceof Page);
292         $this->assertEquals($page->id, $args[2]->id);
293         $this->assertEquals($otherPage->id, $args[3]->id);
294     }
295
296     public function test_event_routes_register_web_and_web_auth()
297     {
298         $functionsContent = <<<'END'
299 <?php
300 use BookStack\Theming\ThemeEvents;
301 use BookStack\Facades\Theme;
302 use Illuminate\Routing\Router;
303 Theme::listen(ThemeEvents::ROUTES_REGISTER_WEB, function (Router $router) {
304     $router->get('/cat', fn () => 'cat')->name('say.cat');
305 });
306 Theme::listen(ThemeEvents::ROUTES_REGISTER_WEB_AUTH, function (Router $router) {
307     $router->get('/dog', fn () => 'dog')->name('say.dog');
308 });
309 END;
310
311         $this->usingThemeFolder(function () use ($functionsContent) {
312
313             $functionsFile = theme_path('functions.php');
314             file_put_contents($functionsFile, $functionsContent);
315
316             $app = $this->createApplication();
317             /** @var \Illuminate\Routing\Router $router */
318             $router = $app->get('router');
319
320             /** @var \Illuminate\Routing\Route $catRoute */
321             $catRoute = $router->getRoutes()->getRoutesByName()['say.cat'];
322             $this->assertEquals(['web'], $catRoute->middleware());
323
324             /** @var \Illuminate\Routing\Route $dogRoute */
325             $dogRoute = $router->getRoutes()->getRoutesByName()['say.dog'];
326             $this->assertEquals(['web', 'auth'], $dogRoute->middleware());
327         });
328     }
329
330     public function test_add_social_driver()
331     {
332         Theme::addSocialDriver('catnet', [
333             'client_id'     => 'abc123',
334             'client_secret' => 'def456',
335         ], 'SocialiteProviders\Discord\DiscordExtendSocialite@handleTesting');
336
337         $this->assertEquals('catnet', config('services.catnet.name'));
338         $this->assertEquals('abc123', config('services.catnet.client_id'));
339         $this->assertEquals(url('/login/service/catnet/callback'), config('services.catnet.redirect'));
340
341         $loginResp = $this->get('/login');
342         $loginResp->assertSee('login/service/catnet');
343     }
344
345     public function test_add_social_driver_uses_name_in_config_if_given()
346     {
347         Theme::addSocialDriver('catnet', [
348             'client_id'     => 'abc123',
349             'client_secret' => 'def456',
350             'name'          => 'Super Cat Name',
351         ], 'SocialiteProviders\Discord\DiscordExtendSocialite@handleTesting');
352
353         $this->assertEquals('Super Cat Name', config('services.catnet.name'));
354         $loginResp = $this->get('/login');
355         $loginResp->assertSee('Super Cat Name');
356     }
357
358     public function test_add_social_driver_allows_a_configure_for_redirect_callback_to_be_passed()
359     {
360         Theme::addSocialDriver(
361             'discord',
362             [
363                 'client_id'     => 'abc123',
364                 'client_secret' => 'def456',
365                 'name'          => 'Super Cat Name',
366             ],
367             'SocialiteProviders\Discord\DiscordExtendSocialite@handle',
368             function ($driver) {
369                 $driver->with(['donkey' => 'donut']);
370             }
371         );
372
373         $loginResp = $this->get('/login/service/discord');
374         $redirect = $loginResp->headers->get('location');
375         $this->assertStringContainsString('donkey=donut', $redirect);
376     }
377
378     public function test_register_command_allows_provided_command_to_be_usable_via_artisan()
379     {
380         Theme::registerCommand(new MyCustomCommand());
381
382         Artisan::call('bookstack:test-custom-command', []);
383         $output = Artisan::output();
384
385         $this->assertStringContainsString('Command ran!', $output);
386     }
387
388     public function test_base_body_start_and_end_template_files_can_be_used()
389     {
390         $bodyStartStr = 'barry-fought-against-the-panther';
391         $bodyEndStr = 'barry-lost-his-fight-with-grace';
392
393         $this->usingThemeFolder(function (string $folder) use ($bodyStartStr, $bodyEndStr) {
394             $viewDir = theme_path('layouts/parts');
395             mkdir($viewDir, 0777, true);
396             file_put_contents($viewDir . '/base-body-start.blade.php', $bodyStartStr);
397             file_put_contents($viewDir . '/base-body-end.blade.php', $bodyEndStr);
398
399             $resp = $this->asEditor()->get('/');
400             $resp->assertSee($bodyStartStr);
401             $resp->assertSee($bodyEndStr);
402         });
403     }
404
405     public function test_export_body_start_and_end_template_files_can_be_used()
406     {
407         $bodyStartStr = 'garry-fought-against-the-panther';
408         $bodyEndStr = 'garry-lost-his-fight-with-grace';
409         $page = $this->entities->page();
410
411         $this->usingThemeFolder(function (string $folder) use ($bodyStartStr, $bodyEndStr, $page) {
412             $viewDir = theme_path('layouts/parts');
413             mkdir($viewDir, 0777, true);
414             file_put_contents($viewDir . '/export-body-start.blade.php', $bodyStartStr);
415             file_put_contents($viewDir . '/export-body-end.blade.php', $bodyEndStr);
416
417             $resp = $this->asEditor()->get($page->getUrl('/export/html'));
418             $resp->assertSee($bodyStartStr);
419             $resp->assertSee($bodyEndStr);
420         });
421     }
422
423     public function test_login_and_register_message_template_files_can_be_used()
424     {
425         $loginMessage = 'Welcome to this instance, login below you scallywag';
426         $registerMessage = 'You want to register? Enter the deets below you numpty';
427
428         $this->usingThemeFolder(function (string $folder) use ($loginMessage, $registerMessage) {
429             $viewDir = theme_path('auth/parts');
430             mkdir($viewDir, 0777, true);
431             file_put_contents($viewDir . '/login-message.blade.php', $loginMessage);
432             file_put_contents($viewDir . '/register-message.blade.php', $registerMessage);
433             $this->setSettings(['registration-enabled' => 'true']);
434
435             $this->get('/login')->assertSee($loginMessage);
436             $this->get('/register')->assertSee($registerMessage);
437         });
438     }
439
440     public function test_header_links_start_template_file_can_be_used()
441     {
442         $content = 'This is added text in the header bar';
443
444         $this->usingThemeFolder(function (string $folder) use ($content) {
445             $viewDir = theme_path('layouts/parts');
446             mkdir($viewDir, 0777, true);
447             file_put_contents($viewDir . '/header-links-start.blade.php', $content);
448             $this->setSettings(['registration-enabled' => 'true']);
449
450             $this->get('/login')->assertSee($content);
451         });
452     }
453
454     public function test_custom_settings_category_page_can_be_added_via_view_file()
455     {
456         $content = 'My SuperCustomSettings';
457
458         $this->usingThemeFolder(function (string $folder) use ($content) {
459             $viewDir = theme_path('settings/categories');
460             mkdir($viewDir, 0777, true);
461             file_put_contents($viewDir . '/beans.blade.php', $content);
462
463             $this->asAdmin()->get('/settings/beans')->assertSee($content);
464         });
465     }
466
467     public function test_public_folder_contents_accessible_via_route()
468     {
469         $this->usingThemeFolder(function (string $themeFolderName) {
470             $publicDir = theme_path('public');
471             mkdir($publicDir, 0777, true);
472
473             $text = 'some-text ' . md5(random_bytes(5));
474             $css = "body { background-color: tomato !important; }";
475             file_put_contents("{$publicDir}/file.txt", $text);
476             file_put_contents("{$publicDir}/file.css", $css);
477             copy($this->files->testFilePath('test-image.png'), "{$publicDir}/image.png");
478
479             $resp = $this->asAdmin()->get("/theme/{$themeFolderName}/file.txt");
480             $resp->assertStreamedContent($text);
481             $resp->assertHeader('Content-Type', 'text/plain; charset=UTF-8');
482             $resp->assertHeader('Cache-Control', 'max-age=86400, private');
483
484             $resp = $this->asAdmin()->get("/theme/{$themeFolderName}/image.png");
485             $resp->assertHeader('Content-Type', 'image/png');
486             $resp->assertHeader('Cache-Control', 'max-age=86400, private');
487
488             $resp = $this->asAdmin()->get("/theme/{$themeFolderName}/file.css");
489             $resp->assertStreamedContent($css);
490             $resp->assertHeader('Content-Type', 'text/css; charset=UTF-8');
491             $resp->assertHeader('Cache-Control', 'max-age=86400, private');
492         });
493     }
494
495     protected function usingThemeFolder(callable $callback)
496     {
497         // Create a folder and configure a theme
498         $themeFolderName = 'testing_theme_' . str_shuffle(rtrim(base64_encode(time()), '='));
499         config()->set('view.theme', $themeFolderName);
500         $themeFolderPath = theme_path('');
501
502         // Create theme folder and clean it up on application tear-down
503         File::makeDirectory($themeFolderPath);
504         $this->beforeApplicationDestroyed(fn() => File::deleteDirectory($themeFolderPath));
505
506         // Run provided callback with theme env option set
507         $this->runWithEnv('APP_THEME', $themeFolderName, function () use ($callback, $themeFolderName) {
508             call_user_func($callback, $themeFolderName);
509         });
510     }
511 }
512
513 class MyCustomCommand extends Command
514 {
515     protected $signature = 'bookstack:test-custom-command';
516
517     public function handle()
518     {
519         $this->line('Command ran!');
520     }
521 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.