]> BookStack Code Mirror - bookstack/blob - tests/ThemeTest.php
Test comment creator name truncation
[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\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;
20
21 class ThemeTest extends TestCase
22 {
23     protected string $themeFolderName;
24     protected string $themeFolderPath;
25
26     public function test_translation_text_can_be_overridden_via_theme()
27     {
28         $this->usingThemeFolder(function () {
29             $translationPath = theme_path('/lang/en');
30             File::makeDirectory($translationPath, 0777, true);
31
32             $customTranslations = '<?php
33             return [\'books\' => \'Sandwiches\'];
34         ';
35             file_put_contents($translationPath . '/entities.php', $customTranslations);
36
37             $homeRequest = $this->actingAs($this->users->viewer())->get('/');
38             $this->withHtml($homeRequest)->assertElementContains('header nav', 'Sandwiches');
39         });
40     }
41
42     public function test_theme_functions_file_used_and_app_boot_event_runs()
43     {
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'));
50             });
51         });
52     }
53
54     public function test_event_commonmark_environment_configure()
55     {
56         $callbackCalled = false;
57         $callback = function ($environment) use (&$callbackCalled) {
58             $this->assertInstanceOf(Environment::class, $environment);
59             $callbackCalled = true;
60
61             return $environment;
62         };
63         Theme::listen(ThemeEvents::COMMONMARK_ENVIRONMENT_CONFIGURE, $callback);
64
65         $page = $this->entities->page();
66         $content = new PageContent($page);
67         $content->setNewMarkdown('# test');
68
69         $this->assertTrue($callbackCalled);
70     }
71
72     public function test_event_web_middleware_before()
73     {
74         $callbackCalled = false;
75         $requestParam = null;
76         $callback = function ($request) use (&$callbackCalled, &$requestParam) {
77             $requestParam = $request;
78             $callbackCalled = true;
79         };
80
81         Theme::listen(ThemeEvents::WEB_MIDDLEWARE_BEFORE, $callback);
82         $this->get('/login', ['Donkey' => 'cat']);
83
84         $this->assertTrue($callbackCalled);
85         $this->assertInstanceOf(Request::class, $requestParam);
86         $this->assertEquals('cat', $requestParam->header('donkey'));
87     }
88
89     public function test_event_web_middleware_before_return_val_used_as_response()
90     {
91         $callback = function (Request $request) {
92             return response('cat', 412);
93         };
94
95         Theme::listen(ThemeEvents::WEB_MIDDLEWARE_BEFORE, $callback);
96         $resp = $this->get('/login', ['Donkey' => 'cat']);
97         $resp->assertSee('cat');
98         $resp->assertStatus(412);
99     }
100
101     public function test_event_web_middleware_after()
102     {
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');
111         };
112
113         Theme::listen(ThemeEvents::WEB_MIDDLEWARE_AFTER, $callback);
114
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');
120     }
121
122     public function test_event_web_middleware_after_return_val_used_as_response()
123     {
124         $callback = function () {
125             return response('cat456', 443);
126         };
127
128         Theme::listen(ThemeEvents::WEB_MIDDLEWARE_AFTER, $callback);
129
130         $resp = $this->get('/login', ['Donkey' => 'cat']);
131         $resp->assertSee('cat456');
132         $resp->assertStatus(443);
133     }
134
135     public function test_event_auth_login_standard()
136     {
137         $args = [];
138         $callback = function (...$eventArgs) use (&$args) {
139             $args = $eventArgs;
140         };
141
142         Theme::listen(ThemeEvents::AUTH_LOGIN, $callback);
143         $this->post('/login', ['email' => '[email protected]', 'password' => 'password']);
144
145         $this->assertCount(2, $args);
146         $this->assertEquals('standard', $args[0]);
147         $this->assertInstanceOf(User::class, $args[1]);
148     }
149
150     public function test_event_auth_register_standard()
151     {
152         $args = [];
153         $callback = function (...$eventArgs) use (&$args) {
154             $args = $eventArgs;
155         };
156         Theme::listen(ThemeEvents::AUTH_REGISTER, $callback);
157         $this->setSettings(['registration-enabled' => 'true']);
158
159         $user = User::factory()->make();
160         $this->post('/register', ['email' => $user->email, 'name' => $user->name, 'password' => 'password']);
161
162         $this->assertCount(2, $args);
163         $this->assertEquals('standard', $args[0]);
164         $this->assertInstanceOf(User::class, $args[1]);
165     }
166
167     public function test_event_webhook_call_before()
168     {
169         $args = [];
170         $callback = function (...$eventArgs) use (&$args) {
171             $args = $eventArgs;
172
173             return ['test' => 'hello!'];
174         };
175         Theme::listen(ThemeEvents::WEBHOOK_CALL_BEFORE, $callback);
176
177         $responses = $this->mockHttpClient([new \GuzzleHttp\Psr7\Response(200, [], '')]);
178
179         $webhook = new Webhook(['name' => 'Test webhook', 'endpoint' => 'https://example.com']);
180         $webhook->save();
181         $event = ActivityType::PAGE_UPDATE;
182         $detail = Page::query()->first();
183
184         dispatch((new DispatchWebhookJob($webhook, $event, $detail)));
185
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);
190
191         $this->assertEquals(1, $responses->requestCount());
192         $request = $responses->latestRequest();
193         $reqData = json_decode($request->getBody(), true);
194         $this->assertEquals('hello!', $reqData['test']);
195     }
196
197     public function test_event_activity_logged()
198     {
199         $book = $this->entities->book();
200         $args = [];
201         $callback = function (...$eventArgs) use (&$args) {
202             $args = $eventArgs;
203         };
204
205         Theme::listen(ThemeEvents::ACTIVITY_LOGGED, $callback);
206         $this->asEditor()->put($book->getUrl(), ['name' => 'My cool update book!']);
207
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);
212     }
213
214     public function test_event_page_include_parse()
215     {
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>";
222         $page->save();
223         $otherPage->save();
224
225         $args = [];
226         $callback = function (...$eventArgs) use (&$args) {
227             $args = $eventArgs;
228
229             return '<strong>Big &amp; content replace surprise!</strong>';
230         };
231
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!');
235
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);
243     }
244
245     public function test_add_social_driver()
246     {
247         Theme::addSocialDriver('catnet', [
248             'client_id'     => 'abc123',
249             'client_secret' => 'def456',
250         ], 'SocialiteProviders\Discord\DiscordExtendSocialite@handleTesting');
251
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'));
255
256         $loginResp = $this->get('/login');
257         $loginResp->assertSee('login/service/catnet');
258     }
259
260     public function test_add_social_driver_uses_name_in_config_if_given()
261     {
262         Theme::addSocialDriver('catnet', [
263             'client_id'     => 'abc123',
264             'client_secret' => 'def456',
265             'name'          => 'Super Cat Name',
266         ], 'SocialiteProviders\Discord\DiscordExtendSocialite@handleTesting');
267
268         $this->assertEquals('Super Cat Name', config('services.catnet.name'));
269         $loginResp = $this->get('/login');
270         $loginResp->assertSee('Super Cat Name');
271     }
272
273     public function test_add_social_driver_allows_a_configure_for_redirect_callback_to_be_passed()
274     {
275         Theme::addSocialDriver(
276             'discord',
277             [
278                 'client_id'     => 'abc123',
279                 'client_secret' => 'def456',
280                 'name'          => 'Super Cat Name',
281             ],
282             'SocialiteProviders\Discord\DiscordExtendSocialite@handle',
283             function ($driver) {
284                 $driver->with(['donkey' => 'donut']);
285             }
286         );
287
288         $loginResp = $this->get('/login/service/discord');
289         $redirect = $loginResp->headers->get('location');
290         $this->assertStringContainsString('donkey=donut', $redirect);
291     }
292
293     public function test_register_command_allows_provided_command_to_be_usable_via_artisan()
294     {
295         Theme::registerCommand(new MyCustomCommand());
296
297         Artisan::call('bookstack:test-custom-command', []);
298         $output = Artisan::output();
299
300         $this->assertStringContainsString('Command ran!', $output);
301     }
302
303     public function test_base_body_start_and_end_template_files_can_be_used()
304     {
305         $bodyStartStr = 'barry-fought-against-the-panther';
306         $bodyEndStr = 'barry-lost-his-fight-with-grace';
307
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);
313
314             $resp = $this->asEditor()->get('/');
315             $resp->assertSee($bodyStartStr);
316             $resp->assertSee($bodyEndStr);
317         });
318     }
319
320     public function test_export_body_start_and_end_template_files_can_be_used()
321     {
322         $bodyStartStr = 'garry-fought-against-the-panther';
323         $bodyEndStr = 'garry-lost-his-fight-with-grace';
324         $page = $this->entities->page();
325
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);
331
332             $resp = $this->asEditor()->get($page->getUrl('/export/html'));
333             $resp->assertSee($bodyStartStr);
334             $resp->assertSee($bodyEndStr);
335         });
336     }
337
338     public function test_login_and_register_message_template_files_can_be_used()
339     {
340         $loginMessage = 'Welcome to this instance, login below you scallywag';
341         $registerMessage = 'You want to register? Enter the deets below you numpty';
342
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']);
349
350             $this->get('/login')->assertSee($loginMessage);
351             $this->get('/register')->assertSee($registerMessage);
352         });
353     }
354
355     protected function usingThemeFolder(callable $callback)
356     {
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('');
361
362         // Create theme folder and clean it up on application tear-down
363         File::makeDirectory($themeFolderPath);
364         $this->beforeApplicationDestroyed(fn() => File::deleteDirectory($themeFolderPath));
365
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);
369         });
370     }
371 }
372
373 class MyCustomCommand extends Command
374 {
375     protected $signature = 'bookstack:test-custom-command';
376
377     public function handle()
378     {
379         $this->line('Command ran!');
380     }
381 }