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