]> BookStack Code Mirror - bookstack/blob - tests/ThemeTest.php
Merge branch 'v23-10' 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' => '[email protected]', '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_webhook_call_before()
182     {
183         $args = [];
184         $callback = function (...$eventArgs) use (&$args) {
185             $args = $eventArgs;
186
187             return ['test' => 'hello!'];
188         };
189         Theme::listen(ThemeEvents::WEBHOOK_CALL_BEFORE, $callback);
190
191         $responses = $this->mockHttpClient([new \GuzzleHttp\Psr7\Response(200, [], '')]);
192
193         $webhook = new Webhook(['name' => 'Test webhook', 'endpoint' => 'https://example.com']);
194         $webhook->save();
195         $event = ActivityType::PAGE_UPDATE;
196         $detail = Page::query()->first();
197
198         dispatch((new DispatchWebhookJob($webhook, $event, $detail)));
199
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);
204
205         $this->assertEquals(1, $responses->requestCount());
206         $request = $responses->latestRequest();
207         $reqData = json_decode($request->getBody(), true);
208         $this->assertEquals('hello!', $reqData['test']);
209     }
210
211     public function test_event_activity_logged()
212     {
213         $book = $this->entities->book();
214         $args = [];
215         $callback = function (...$eventArgs) use (&$args) {
216             $args = $eventArgs;
217         };
218
219         Theme::listen(ThemeEvents::ACTIVITY_LOGGED, $callback);
220         $this->asEditor()->put($book->getUrl(), ['name' => 'My cool update book!']);
221
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);
226     }
227
228     public function test_event_page_include_parse()
229     {
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>";
236         $page->save();
237         $otherPage->save();
238
239         $args = [];
240         $callback = function (...$eventArgs) use (&$args) {
241             $args = $eventArgs;
242
243             return '<strong>Big &amp; content replace surprise!</strong>';
244         };
245
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!');
249
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);
257     }
258
259     public function test_event_routes_register_web_and_web_auth()
260     {
261         $functionsContent = <<<'END'
262 <?php
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');
268 });
269 Theme::listen(ThemeEvents::ROUTES_REGISTER_WEB_AUTH, function (Router $router) {
270     $router->get('/dog', fn () => 'dog')->name('say.dog');
271 });
272 END;
273
274         $this->usingThemeFolder(function () use ($functionsContent) {
275
276             $functionsFile = theme_path('functions.php');
277             file_put_contents($functionsFile, $functionsContent);
278
279             $app = $this->createApplication();
280             /** @var \Illuminate\Routing\Router $router */
281             $router = $app->get('router');
282
283             /** @var \Illuminate\Routing\Route $catRoute */
284             $catRoute = $router->getRoutes()->getRoutesByName()['say.cat'];
285             $this->assertEquals(['web'], $catRoute->middleware());
286
287             /** @var \Illuminate\Routing\Route $dogRoute */
288             $dogRoute = $router->getRoutes()->getRoutesByName()['say.dog'];
289             $this->assertEquals(['web', 'auth'], $dogRoute->middleware());
290         });
291     }
292
293     public function test_add_social_driver()
294     {
295         Theme::addSocialDriver('catnet', [
296             'client_id'     => 'abc123',
297             'client_secret' => 'def456',
298         ], 'SocialiteProviders\Discord\DiscordExtendSocialite@handleTesting');
299
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'));
303
304         $loginResp = $this->get('/login');
305         $loginResp->assertSee('login/service/catnet');
306     }
307
308     public function test_add_social_driver_uses_name_in_config_if_given()
309     {
310         Theme::addSocialDriver('catnet', [
311             'client_id'     => 'abc123',
312             'client_secret' => 'def456',
313             'name'          => 'Super Cat Name',
314         ], 'SocialiteProviders\Discord\DiscordExtendSocialite@handleTesting');
315
316         $this->assertEquals('Super Cat Name', config('services.catnet.name'));
317         $loginResp = $this->get('/login');
318         $loginResp->assertSee('Super Cat Name');
319     }
320
321     public function test_add_social_driver_allows_a_configure_for_redirect_callback_to_be_passed()
322     {
323         Theme::addSocialDriver(
324             'discord',
325             [
326                 'client_id'     => 'abc123',
327                 'client_secret' => 'def456',
328                 'name'          => 'Super Cat Name',
329             ],
330             'SocialiteProviders\Discord\DiscordExtendSocialite@handle',
331             function ($driver) {
332                 $driver->with(['donkey' => 'donut']);
333             }
334         );
335
336         $loginResp = $this->get('/login/service/discord');
337         $redirect = $loginResp->headers->get('location');
338         $this->assertStringContainsString('donkey=donut', $redirect);
339     }
340
341     public function test_register_command_allows_provided_command_to_be_usable_via_artisan()
342     {
343         Theme::registerCommand(new MyCustomCommand());
344
345         Artisan::call('bookstack:test-custom-command', []);
346         $output = Artisan::output();
347
348         $this->assertStringContainsString('Command ran!', $output);
349     }
350
351     public function test_base_body_start_and_end_template_files_can_be_used()
352     {
353         $bodyStartStr = 'barry-fought-against-the-panther';
354         $bodyEndStr = 'barry-lost-his-fight-with-grace';
355
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);
361
362             $resp = $this->asEditor()->get('/');
363             $resp->assertSee($bodyStartStr);
364             $resp->assertSee($bodyEndStr);
365         });
366     }
367
368     public function test_export_body_start_and_end_template_files_can_be_used()
369     {
370         $bodyStartStr = 'garry-fought-against-the-panther';
371         $bodyEndStr = 'garry-lost-his-fight-with-grace';
372         $page = $this->entities->page();
373
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);
379
380             $resp = $this->asEditor()->get($page->getUrl('/export/html'));
381             $resp->assertSee($bodyStartStr);
382             $resp->assertSee($bodyEndStr);
383         });
384     }
385
386     public function test_login_and_register_message_template_files_can_be_used()
387     {
388         $loginMessage = 'Welcome to this instance, login below you scallywag';
389         $registerMessage = 'You want to register? Enter the deets below you numpty';
390
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']);
397
398             $this->get('/login')->assertSee($loginMessage);
399             $this->get('/register')->assertSee($registerMessage);
400         });
401     }
402
403     public function test_header_links_start_template_file_can_be_used()
404     {
405         $content = 'This is added text in the header bar';
406
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']);
412
413             $this->get('/login')->assertSee($content);
414         });
415     }
416
417     protected function usingThemeFolder(callable $callback)
418     {
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('');
423
424         // Create theme folder and clean it up on application tear-down
425         File::makeDirectory($themeFolderPath);
426         $this->beforeApplicationDestroyed(fn() => File::deleteDirectory($themeFolderPath));
427
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);
431         });
432     }
433 }
434
435 class MyCustomCommand extends Command
436 {
437     protected $signature = 'bookstack:test-custom-command';
438
439     public function handle()
440     {
441         $this->line('Command ran!');
442     }
443 }