]> BookStack Code Mirror - bookstack/blob - tests/ThemeTest.php
Played around with a new app structure
[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\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;
23
24 class ThemeTest extends TestCase
25 {
26     protected string $themeFolderName;
27     protected string $themeFolderPath;
28
29     public function test_translation_text_can_be_overridden_via_theme()
30     {
31         $this->usingThemeFolder(function () {
32             $translationPath = theme_path('/lang/en');
33             File::makeDirectory($translationPath, 0777, true);
34
35             $customTranslations = '<?php
36             return [\'books\' => \'Sandwiches\'];
37         ';
38             file_put_contents($translationPath . '/entities.php', $customTranslations);
39
40             $homeRequest = $this->actingAs($this->users->viewer())->get('/');
41             $this->withHtml($homeRequest)->assertElementContains('header nav', 'Sandwiches');
42         });
43     }
44
45     public function test_theme_functions_file_used_and_app_boot_event_runs()
46     {
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'));
53             });
54         });
55     }
56
57     public function test_event_commonmark_environment_configure()
58     {
59         $callbackCalled = false;
60         $callback = function ($environment) use (&$callbackCalled) {
61             $this->assertInstanceOf(Environment::class, $environment);
62             $callbackCalled = true;
63
64             return $environment;
65         };
66         Theme::listen(ThemeEvents::COMMONMARK_ENVIRONMENT_CONFIGURE, $callback);
67
68         $page = $this->entities->page();
69         $content = new PageContent($page);
70         $content->setNewMarkdown('# test');
71
72         $this->assertTrue($callbackCalled);
73     }
74
75     public function test_event_web_middleware_before()
76     {
77         $callbackCalled = false;
78         $requestParam = null;
79         $callback = function ($request) use (&$callbackCalled, &$requestParam) {
80             $requestParam = $request;
81             $callbackCalled = true;
82         };
83
84         Theme::listen(ThemeEvents::WEB_MIDDLEWARE_BEFORE, $callback);
85         $this->get('/login', ['Donkey' => 'cat']);
86
87         $this->assertTrue($callbackCalled);
88         $this->assertInstanceOf(Request::class, $requestParam);
89         $this->assertEquals('cat', $requestParam->header('donkey'));
90     }
91
92     public function test_event_web_middleware_before_return_val_used_as_response()
93     {
94         $callback = function (Request $request) {
95             return response('cat', 412);
96         };
97
98         Theme::listen(ThemeEvents::WEB_MIDDLEWARE_BEFORE, $callback);
99         $resp = $this->get('/login', ['Donkey' => 'cat']);
100         $resp->assertSee('cat');
101         $resp->assertStatus(412);
102     }
103
104     public function test_event_web_middleware_after()
105     {
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');
114         };
115
116         Theme::listen(ThemeEvents::WEB_MIDDLEWARE_AFTER, $callback);
117
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');
123     }
124
125     public function test_event_web_middleware_after_return_val_used_as_response()
126     {
127         $callback = function () {
128             return response('cat456', 443);
129         };
130
131         Theme::listen(ThemeEvents::WEB_MIDDLEWARE_AFTER, $callback);
132
133         $resp = $this->get('/login', ['Donkey' => 'cat']);
134         $resp->assertSee('cat456');
135         $resp->assertStatus(443);
136     }
137
138     public function test_event_auth_login_standard()
139     {
140         $args = [];
141         $callback = function (...$eventArgs) use (&$args) {
142             $args = $eventArgs;
143         };
144
145         Theme::listen(ThemeEvents::AUTH_LOGIN, $callback);
146         $this->post('/login', ['email' => '[email protected]', 'password' => 'password']);
147
148         $this->assertCount(2, $args);
149         $this->assertEquals('standard', $args[0]);
150         $this->assertInstanceOf(User::class, $args[1]);
151     }
152
153     public function test_event_auth_register_standard()
154     {
155         $args = [];
156         $callback = function (...$eventArgs) use (&$args) {
157             $args = $eventArgs;
158         };
159         Theme::listen(ThemeEvents::AUTH_REGISTER, $callback);
160         $this->setSettings(['registration-enabled' => 'true']);
161
162         $user = User::factory()->make();
163         $this->post('/register', ['email' => $user->email, 'name' => $user->name, 'password' => 'password']);
164
165         $this->assertCount(2, $args);
166         $this->assertEquals('standard', $args[0]);
167         $this->assertInstanceOf(User::class, $args[1]);
168     }
169
170     public function test_event_webhook_call_before()
171     {
172         $args = [];
173         $callback = function (...$eventArgs) use (&$args) {
174             $args = $eventArgs;
175
176             return ['test' => 'hello!'];
177         };
178         Theme::listen(ThemeEvents::WEBHOOK_CALL_BEFORE, $callback);
179
180         Http::fake([
181             '*' => Http::response('', 200),
182         ]);
183
184         $webhook = new Webhook(['name' => 'Test webhook', 'endpoint' => 'https://example.com']);
185         $webhook->save();
186         $event = ActivityType::PAGE_UPDATE;
187         $detail = Page::query()->first();
188
189         dispatch((new DispatchWebhookJob($webhook, $event, $detail)));
190
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);
195
196         Http::assertSent(function (HttpClientRequest $request) {
197             return $request->isJson() && $request->data()['test'] === 'hello!';
198         });
199     }
200
201     public function test_event_activity_logged()
202     {
203         $book = $this->entities->book();
204         $args = [];
205         $callback = function (...$eventArgs) use (&$args) {
206             $args = $eventArgs;
207         };
208
209         Theme::listen(ThemeEvents::ACTIVITY_LOGGED, $callback);
210         $this->asEditor()->put($book->getUrl(), ['name' => 'My cool update book!']);
211
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);
216     }
217
218     public function test_event_page_include_parse()
219     {
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>";
226         $page->save();
227         $otherPage->save();
228
229         $args = [];
230         $callback = function (...$eventArgs) use (&$args) {
231             $args = $eventArgs;
232
233             return '<strong>Big &amp; content replace surprise!</strong>';
234         };
235
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!');
239
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);
247     }
248
249     public function test_add_social_driver()
250     {
251         Theme::addSocialDriver('catnet', [
252             'client_id'     => 'abc123',
253             'client_secret' => 'def456',
254         ], 'SocialiteProviders\Discord\DiscordExtendSocialite@handleTesting');
255
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'));
259
260         $loginResp = $this->get('/login');
261         $loginResp->assertSee('login/service/catnet');
262     }
263
264     public function test_add_social_driver_uses_name_in_config_if_given()
265     {
266         Theme::addSocialDriver('catnet', [
267             'client_id'     => 'abc123',
268             'client_secret' => 'def456',
269             'name'          => 'Super Cat Name',
270         ], 'SocialiteProviders\Discord\DiscordExtendSocialite@handleTesting');
271
272         $this->assertEquals('Super Cat Name', config('services.catnet.name'));
273         $loginResp = $this->get('/login');
274         $loginResp->assertSee('Super Cat Name');
275     }
276
277     public function test_add_social_driver_allows_a_configure_for_redirect_callback_to_be_passed()
278     {
279         Theme::addSocialDriver(
280             'discord',
281             [
282                 'client_id'     => 'abc123',
283                 'client_secret' => 'def456',
284                 'name'          => 'Super Cat Name',
285             ],
286             'SocialiteProviders\Discord\DiscordExtendSocialite@handle',
287             function ($driver) {
288                 $driver->with(['donkey' => 'donut']);
289             }
290         );
291
292         $loginResp = $this->get('/login/service/discord');
293         $redirect = $loginResp->headers->get('location');
294         $this->assertStringContainsString('donkey=donut', $redirect);
295     }
296
297     public function test_register_command_allows_provided_command_to_be_usable_via_artisan()
298     {
299         Theme::registerCommand(new MyCustomCommand());
300
301         Artisan::call('bookstack:test-custom-command', []);
302         $output = Artisan::output();
303
304         $this->assertStringContainsString('Command ran!', $output);
305     }
306
307     public function test_base_body_start_and_end_template_files_can_be_used()
308     {
309         $bodyStartStr = 'barry-fought-against-the-panther';
310         $bodyEndStr = 'barry-lost-his-fight-with-grace';
311
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);
317
318             $resp = $this->asEditor()->get('/');
319             $resp->assertSee($bodyStartStr);
320             $resp->assertSee($bodyEndStr);
321         });
322     }
323
324     public function test_export_body_start_and_end_template_files_can_be_used()
325     {
326         $bodyStartStr = 'garry-fought-against-the-panther';
327         $bodyEndStr = 'garry-lost-his-fight-with-grace';
328         $page = $this->entities->page();
329
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);
335
336             $resp = $this->asEditor()->get($page->getUrl('/export/html'));
337             $resp->assertSee($bodyStartStr);
338             $resp->assertSee($bodyEndStr);
339         });
340     }
341
342     public function test_login_and_register_message_template_files_can_be_used()
343     {
344         $loginMessage = 'Welcome to this instance, login below you scallywag';
345         $registerMessage = 'You want to register? Enter the deets below you numpty';
346
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']);
353
354             $this->get('/login')->assertSee($loginMessage);
355             $this->get('/register')->assertSee($registerMessage);
356         });
357     }
358
359     protected function usingThemeFolder(callable $callback)
360     {
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('');
365
366         // Create theme folder and clean it up on application tear-down
367         File::makeDirectory($themeFolderPath);
368         $this->beforeApplicationDestroyed(fn() => File::deleteDirectory($themeFolderPath));
369
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);
373         });
374     }
375 }
376
377 class MyCustomCommand extends Command
378 {
379     protected $signature = 'bookstack:test-custom-command';
380
381     public function handle()
382     {
383         $this->line('Command ran!');
384     }
385 }