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\Page;
10 use BookStack\Entities\Tools\PageContent;
11 use BookStack\Facades\Theme;
12 use BookStack\Theming\ThemeEvents;
13 use Illuminate\Console\Command;
14 use Illuminate\Http\Client\Request as HttpClientRequest;
15 use Illuminate\Http\Request;
16 use Illuminate\Http\Response;
17 use Illuminate\Support\Facades\Artisan;
18 use Illuminate\Support\Facades\File;
19 use Illuminate\Support\Facades\Http;
20 use League\CommonMark\ConfigurableEnvironmentInterface;
22 class ThemeTest extends TestCase
24 protected $themeFolderName;
25 protected $themeFolderPath;
27 public function test_translation_text_can_be_overridden_via_theme()
29 $this->usingThemeFolder(function () {
30 $translationPath = theme_path('/lang/en');
31 File::makeDirectory($translationPath, 0777, true);
33 $customTranslations = '<?php
34 return [\'books\' => \'Sandwiches\'];
36 file_put_contents($translationPath . '/entities.php', $customTranslations);
38 $homeRequest = $this->actingAs($this->getViewer())->get('/');
39 $homeRequest->assertElementContains('header nav', 'Sandwiches');
43 public function test_theme_functions_file_used_and_app_boot_event_runs()
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'));
55 public function test_event_commonmark_environment_configure()
57 $callbackCalled = false;
58 $callback = function ($environment) use (&$callbackCalled) {
59 $this->assertInstanceOf(ConfigurableEnvironmentInterface::class, $environment);
60 $callbackCalled = true;
64 Theme::listen(ThemeEvents::COMMONMARK_ENVIRONMENT_CONFIGURE, $callback);
66 $page = Page::query()->first();
67 $content = new PageContent($page);
68 $content->setNewMarkdown('# test');
70 $this->assertTrue($callbackCalled);
73 public function test_event_web_middleware_before()
75 $callbackCalled = false;
77 $callback = function ($request) use (&$callbackCalled, &$requestParam) {
78 $requestParam = $request;
79 $callbackCalled = true;
82 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_BEFORE, $callback);
83 $this->get('/login', ['Donkey' => 'cat']);
85 $this->assertTrue($callbackCalled);
86 $this->assertInstanceOf(Request::class, $requestParam);
87 $this->assertEquals('cat', $requestParam->header('donkey'));
90 public function test_event_web_middleware_before_return_val_used_as_response()
92 $callback = function (Request $request) {
93 return response('cat', 412);
96 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_BEFORE, $callback);
97 $resp = $this->get('/login', ['Donkey' => 'cat']);
98 $resp->assertSee('cat');
99 $resp->assertStatus(412);
102 public function test_event_web_middleware_after()
104 $callbackCalled = false;
105 $requestParam = null;
106 $responseParam = null;
107 $callback = function ($request, Response $response) use (&$callbackCalled, &$requestParam, &$responseParam) {
108 $requestParam = $request;
109 $responseParam = $response;
110 $callbackCalled = true;
111 $response->header('donkey', 'cat123');
114 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_AFTER, $callback);
116 $resp = $this->get('/login', ['Donkey' => 'cat']);
117 $this->assertTrue($callbackCalled);
118 $this->assertInstanceOf(Request::class, $requestParam);
119 $this->assertInstanceOf(Response::class, $responseParam);
120 $resp->assertHeader('donkey', 'cat123');
123 public function test_event_web_middleware_after_return_val_used_as_response()
125 $callback = function () {
126 return response('cat456', 443);
129 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_AFTER, $callback);
131 $resp = $this->get('/login', ['Donkey' => 'cat']);
132 $resp->assertSee('cat456');
133 $resp->assertStatus(443);
136 public function test_event_auth_login_standard()
139 $callback = function (...$eventArgs) use (&$args) {
143 Theme::listen(ThemeEvents::AUTH_LOGIN, $callback);
144 $this->post('/login', ['email' => 'admin@admin.com', 'password' => 'password']);
146 $this->assertCount(2, $args);
147 $this->assertEquals('standard', $args[0]);
148 $this->assertInstanceOf(User::class, $args[1]);
151 public function test_event_auth_register_standard()
154 $callback = function (...$eventArgs) use (&$args) {
157 Theme::listen(ThemeEvents::AUTH_REGISTER, $callback);
158 $this->setSettings(['registration-enabled' => 'true']);
160 $user = User::factory()->make();
161 $this->post('/register', ['email' => $user->email, 'name' => $user->name, 'password' => 'password']);
163 $this->assertCount(2, $args);
164 $this->assertEquals('standard', $args[0]);
165 $this->assertInstanceOf(User::class, $args[1]);
168 public function test_event_webhook_call_before()
171 $callback = function (...$eventArgs) use (&$args) {
174 return ['test' => 'hello!'];
176 Theme::listen(ThemeEvents::WEBHOOK_CALL_BEFORE, $callback);
179 '*' => Http::response('', 200),
182 $webhook = new Webhook(['name' => 'Test webhook', 'endpoint' => 'https://example.com']);
184 $event = ActivityType::PAGE_UPDATE;
185 $detail = Page::query()->first();
187 dispatch((new DispatchWebhookJob($webhook, $event, $detail)));
189 $this->assertCount(5, $args);
190 $this->assertEquals($event, $args[0]);
191 $this->assertEquals($webhook->id, $args[1]->id);
192 $this->assertEquals($detail->id, $args[2]->id);
194 Http::assertSent(function (HttpClientRequest $request) {
195 return $request->isJson() && $request->data()['test'] === 'hello!';
199 public function test_add_social_driver()
201 Theme::addSocialDriver('catnet', [
202 'client_id' => 'abc123',
203 'client_secret' => 'def456',
204 ], 'SocialiteProviders\Discord\DiscordExtendSocialite@handleTesting');
206 $this->assertEquals('catnet', config('services.catnet.name'));
207 $this->assertEquals('abc123', config('services.catnet.client_id'));
208 $this->assertEquals(url('/login/service/catnet/callback'), config('services.catnet.redirect'));
210 $loginResp = $this->get('/login');
211 $loginResp->assertSee('login/service/catnet');
214 public function test_add_social_driver_uses_name_in_config_if_given()
216 Theme::addSocialDriver('catnet', [
217 'client_id' => 'abc123',
218 'client_secret' => 'def456',
219 'name' => 'Super Cat Name',
220 ], 'SocialiteProviders\Discord\DiscordExtendSocialite@handleTesting');
222 $this->assertEquals('Super Cat Name', config('services.catnet.name'));
223 $loginResp = $this->get('/login');
224 $loginResp->assertSee('Super Cat Name');
227 public function test_add_social_driver_allows_a_configure_for_redirect_callback_to_be_passed()
229 Theme::addSocialDriver(
232 'client_id' => 'abc123',
233 'client_secret' => 'def456',
234 'name' => 'Super Cat Name',
236 'SocialiteProviders\Discord\DiscordExtendSocialite@handle',
238 $driver->with(['donkey' => 'donut']);
242 $loginResp = $this->get('/login/service/discord');
243 $redirect = $loginResp->headers->get('location');
244 $this->assertStringContainsString('donkey=donut', $redirect);
247 public function test_register_command_allows_provided_command_to_be_usable_via_artisan()
249 Theme::registerCommand(new MyCustomCommand());
251 Artisan::call('bookstack:test-custom-command', []);
252 $output = Artisan::output();
254 $this->assertStringContainsString('Command ran!', $output);
257 protected function usingThemeFolder(callable $callback)
259 // Create a folder and configure a theme
260 $themeFolderName = 'testing_theme_' . rtrim(base64_encode(time()), '=');
261 config()->set('view.theme', $themeFolderName);
262 $themeFolderPath = theme_path('');
263 File::makeDirectory($themeFolderPath);
265 call_user_func($callback, $themeFolderName);
267 // Cleanup the custom theme folder we created
268 File::deleteDirectory($themeFolderPath);
272 class MyCustomCommand extends Command
274 protected $signature = 'bookstack:test-custom-command';
276 public function handle()
278 $this->line('Command ran!');