3 use BookStack\Auth\User;
4 use BookStack\Entities\Models\Page;
5 use BookStack\Entities\Tools\PageContent;
6 use BookStack\Facades\Theme;
7 use BookStack\Theming\ThemeEvents;
9 use Illuminate\Http\Request;
10 use Illuminate\Http\Response;
11 use League\CommonMark\ConfigurableEnvironmentInterface;
13 class ThemeTest extends TestCase
15 protected $themeFolderName;
16 protected $themeFolderPath;
18 public function test_translation_text_can_be_overridden_via_theme()
20 $this->usingThemeFolder(function () {
21 $translationPath = theme_path('/lang/en');
22 File::makeDirectory($translationPath, 0777, true);
24 $customTranslations = '<?php
25 return [\'books\' => \'Sandwiches\'];
27 file_put_contents($translationPath . '/entities.php', $customTranslations);
29 $homeRequest = $this->actingAs($this->getViewer())->get('/');
30 $homeRequest->assertElementContains('header nav', 'Sandwiches');
34 public function test_theme_functions_file_used_and_app_boot_event_runs()
36 $this->usingThemeFolder(function ($themeFolder) {
37 $functionsFile = theme_path('functions.php');
38 app()->alias('cat', 'dog');
39 file_put_contents($functionsFile, "<?php\nTheme::listen(\BookStack\Theming\ThemeEvents::APP_BOOT, function(\$app) { \$app->alias('cat', 'dog');});");
40 $this->runWithEnv('APP_THEME', $themeFolder, function () {
41 $this->assertEquals('cat', $this->app->getAlias('dog'));
46 public function test_event_commonmark_environment_configure()
48 $callbackCalled = false;
49 $callback = function ($environment) use (&$callbackCalled) {
50 $this->assertInstanceOf(ConfigurableEnvironmentInterface::class, $environment);
51 $callbackCalled = true;
54 Theme::listen(ThemeEvents::COMMONMARK_ENVIRONMENT_CONFIGURE, $callback);
56 $page = Page::query()->first();
57 $content = new PageContent($page);
58 $content->setNewMarkdown('# test');
60 $this->assertTrue($callbackCalled);
63 public function test_event_web_middleware_before()
65 $callbackCalled = false;
67 $callback = function ($request) use (&$callbackCalled, &$requestParam) {
68 $requestParam = $request;
69 $callbackCalled = true;
72 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_BEFORE, $callback);
73 $this->get('/login', ['Donkey' => 'cat']);
75 $this->assertTrue($callbackCalled);
76 $this->assertInstanceOf(Request::class, $requestParam);
77 $this->assertEquals('cat', $requestParam->header('donkey'));
80 public function test_event_web_middleware_before_return_val_used_as_response()
82 $callback = function (Request $request) {
83 return response('cat', 412);
86 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_BEFORE, $callback);
87 $resp = $this->get('/login', ['Donkey' => 'cat']);
88 $resp->assertSee('cat');
89 $resp->assertStatus(412);
92 public function test_event_web_middleware_after()
94 $callbackCalled = false;
96 $responseParam = null;
97 $callback = function ($request, Response $response) use (&$callbackCalled, &$requestParam, &$responseParam) {
98 $requestParam = $request;
99 $responseParam = $response;
100 $callbackCalled = true;
101 $response->header('donkey', 'cat123');
104 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_AFTER, $callback);
106 $resp = $this->get('/login', ['Donkey' => 'cat']);
107 $this->assertTrue($callbackCalled);
108 $this->assertInstanceOf(Request::class, $requestParam);
109 $this->assertInstanceOf(Response::class, $responseParam);
110 $resp->assertHeader('donkey', 'cat123');
113 public function test_event_web_middleware_after_return_val_used_as_response()
115 $callback = function () {
116 return response('cat456', 443);
119 Theme::listen(ThemeEvents::WEB_MIDDLEWARE_AFTER, $callback);
121 $resp = $this->get('/login', ['Donkey' => 'cat']);
122 $resp->assertSee('cat456');
123 $resp->assertStatus(443);
126 public function test_event_auth_login_standard()
129 $callback = function (...$eventArgs) use (&$args) {
133 Theme::listen(ThemeEvents::AUTH_LOGIN, $callback);
136 $this->assertCount(2, $args);
137 $this->assertEquals('standard', $args[0]);
138 $this->assertInstanceOf(User::class, $args[1]);
141 public function test_event_auth_register_standard()
144 $callback = function (...$eventArgs) use (&$args) {
147 Theme::listen(ThemeEvents::AUTH_REGISTER, $callback);
148 $this->setSettings(['registration-enabled' => 'true']);
150 $user = factory(User::class)->make();
151 $this->post('/register', ['email' => $user->email, 'name' => $user->name, 'password' => 'password']);
153 $this->assertCount(2, $args);
154 $this->assertEquals('standard', $args[0]);
155 $this->assertInstanceOf(User::class, $args[1]);
158 public function test_add_social_driver()
160 Theme::addSocialDriver('catnet', [
161 'client_id' => 'abc123',
162 'client_secret' => 'def456'
163 ], 'SocialiteProviders\Discord\DiscordExtendSocialite@handleTesting');
165 $this->assertEquals('catnet', config('services.catnet.name'));
166 $this->assertEquals('abc123', config('services.catnet.client_id'));
167 $this->assertEquals(url('/login/service/catnet/callback'), config('services.catnet.redirect'));
169 $loginResp = $this->get('/login');
170 $loginResp->assertSee('login/service/catnet');
173 public function test_add_social_driver_uses_name_in_config_if_given()
175 Theme::addSocialDriver('catnet', [
176 'client_id' => 'abc123',
177 'client_secret' => 'def456',
178 'name' => 'Super Cat Name',
179 ], 'SocialiteProviders\Discord\DiscordExtendSocialite@handleTesting');
181 $this->assertEquals('Super Cat Name', config('services.catnet.name'));
182 $loginResp = $this->get('/login');
183 $loginResp->assertSee('Super Cat Name');
187 public function test_add_social_driver_allows_a_configure_for_redirect_callback_to_be_passed()
189 Theme::addSocialDriver(
192 'client_id' => 'abc123',
193 'client_secret' => 'def456',
194 'name' => 'Super Cat Name',
196 'SocialiteProviders\Discord\DiscordExtendSocialite@handle',
198 $driver->with(['donkey' => 'donut']);
202 $loginResp = $this->get('/login/service/discord');
203 $redirect = $loginResp->headers->get('location');
204 $this->assertStringContainsString('donkey=donut', $redirect);
208 protected function usingThemeFolder(callable $callback)
210 // Create a folder and configure a theme
211 $themeFolderName = 'testing_theme_' . rtrim(base64_encode(time()), "=");
212 config()->set('view.theme', $themeFolderName);
213 $themeFolderPath = theme_path('');
214 File::makeDirectory($themeFolderPath);
216 call_user_func($callback, $themeFolderName);
218 // Cleanup the custom theme folder we created
219 File::deleteDirectory($themeFolderPath);