]> BookStack Code Mirror - bookstack/blob - tests/ThemeTest.php
Added front-end toggle and testing of inline attachments
[bookstack] / tests / ThemeTest.php
1 <?php namespace Tests;
2
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;
8 use File;
9 use Illuminate\Http\Request;
10 use Illuminate\Http\Response;
11 use League\CommonMark\ConfigurableEnvironmentInterface;
12
13 class ThemeTest extends TestCase
14 {
15     protected $themeFolderName;
16     protected $themeFolderPath;
17
18     public function test_translation_text_can_be_overridden_via_theme()
19     {
20         $this->usingThemeFolder(function () {
21             $translationPath = theme_path('/lang/en');
22             File::makeDirectory($translationPath, 0777, true);
23
24             $customTranslations = '<?php
25             return [\'books\' => \'Sandwiches\'];
26         ';
27             file_put_contents($translationPath . '/entities.php', $customTranslations);
28
29             $homeRequest = $this->actingAs($this->getViewer())->get('/');
30             $homeRequest->assertElementContains('header nav', 'Sandwiches');
31         });
32     }
33
34     public function test_theme_functions_file_used_and_app_boot_event_runs()
35     {
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'));
42             });
43         });
44     }
45
46     public function test_event_commonmark_environment_configure()
47     {
48         $callbackCalled = false;
49         $callback = function ($environment) use (&$callbackCalled) {
50             $this->assertInstanceOf(ConfigurableEnvironmentInterface::class, $environment);
51             $callbackCalled = true;
52             return $environment;
53         };
54         Theme::listen(ThemeEvents::COMMONMARK_ENVIRONMENT_CONFIGURE, $callback);
55
56         $page = Page::query()->first();
57         $content = new PageContent($page);
58         $content->setNewMarkdown('# test');
59
60         $this->assertTrue($callbackCalled);
61     }
62
63     public function test_event_web_middleware_before()
64     {
65         $callbackCalled = false;
66         $requestParam = null;
67         $callback = function ($request) use (&$callbackCalled, &$requestParam) {
68             $requestParam = $request;
69             $callbackCalled = true;
70         };
71
72         Theme::listen(ThemeEvents::WEB_MIDDLEWARE_BEFORE, $callback);
73         $this->get('/login', ['Donkey' => 'cat']);
74
75         $this->assertTrue($callbackCalled);
76         $this->assertInstanceOf(Request::class, $requestParam);
77         $this->assertEquals('cat', $requestParam->header('donkey'));
78     }
79
80     public function test_event_web_middleware_before_return_val_used_as_response()
81     {
82         $callback = function (Request $request) {
83             return response('cat', 412);
84         };
85
86         Theme::listen(ThemeEvents::WEB_MIDDLEWARE_BEFORE, $callback);
87         $resp = $this->get('/login', ['Donkey' => 'cat']);
88         $resp->assertSee('cat');
89         $resp->assertStatus(412);
90     }
91
92     public function test_event_web_middleware_after()
93     {
94         $callbackCalled = false;
95         $requestParam = null;
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');
102         };
103
104         Theme::listen(ThemeEvents::WEB_MIDDLEWARE_AFTER, $callback);
105
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');
111     }
112
113     public function test_event_web_middleware_after_return_val_used_as_response()
114     {
115         $callback = function () {
116             return response('cat456', 443);
117         };
118
119         Theme::listen(ThemeEvents::WEB_MIDDLEWARE_AFTER, $callback);
120
121         $resp = $this->get('/login', ['Donkey' => 'cat']);
122         $resp->assertSee('cat456');
123         $resp->assertStatus(443);
124     }
125
126     public function test_event_auth_login_standard()
127     {
128         $args = [];
129         $callback = function (...$eventArgs) use (&$args) {
130             $args = $eventArgs;
131         };
132
133         Theme::listen(ThemeEvents::AUTH_LOGIN, $callback);
134         $this->post('/login', ['email' => '[email protected]', 'password' => 'password']);
135
136         $this->assertCount(2, $args);
137         $this->assertEquals('standard', $args[0]);
138         $this->assertInstanceOf(User::class, $args[1]);
139     }
140
141     public function test_event_auth_register_standard()
142     {
143         $args = [];
144         $callback = function (...$eventArgs) use (&$args) {
145             $args = $eventArgs;
146         };
147         Theme::listen(ThemeEvents::AUTH_REGISTER, $callback);
148         $this->setSettings(['registration-enabled' => 'true']);
149
150         $user = factory(User::class)->make();
151         $this->post('/register', ['email' => $user->email, 'name' => $user->name, 'password' => 'password']);
152
153         $this->assertCount(2, $args);
154         $this->assertEquals('standard', $args[0]);
155         $this->assertInstanceOf(User::class, $args[1]);
156     }
157
158     public function test_add_social_driver()
159     {
160         Theme::addSocialDriver('catnet', [
161             'client_id' => 'abc123',
162             'client_secret' => 'def456'
163         ], 'SocialiteProviders\Discord\DiscordExtendSocialite@handleTesting');
164
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'));
168
169         $loginResp = $this->get('/login');
170         $loginResp->assertSee('login/service/catnet');
171     }
172
173     public function test_add_social_driver_uses_name_in_config_if_given()
174     {
175         Theme::addSocialDriver('catnet', [
176             'client_id' => 'abc123',
177             'client_secret' => 'def456',
178             'name' => 'Super Cat Name',
179         ], 'SocialiteProviders\Discord\DiscordExtendSocialite@handleTesting');
180
181         $this->assertEquals('Super Cat Name', config('services.catnet.name'));
182         $loginResp = $this->get('/login');
183         $loginResp->assertSee('Super Cat Name');
184     }
185
186
187     public function test_add_social_driver_allows_a_configure_for_redirect_callback_to_be_passed()
188     {
189         Theme::addSocialDriver(
190             'discord',
191             [
192                 'client_id' => 'abc123',
193                 'client_secret' => 'def456',
194                 'name' => 'Super Cat Name',
195             ],
196             'SocialiteProviders\Discord\DiscordExtendSocialite@handle',
197             function ($driver) {
198                 $driver->with(['donkey' => 'donut']);
199             }
200         );
201
202         $loginResp = $this->get('/login/service/discord');
203         $redirect = $loginResp->headers->get('location');
204         $this->assertStringContainsString('donkey=donut', $redirect);
205     }
206
207
208     protected function usingThemeFolder(callable $callback)
209     {
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);
215
216         call_user_func($callback, $themeFolderName);
217
218         // Cleanup the custom theme folder we created
219         File::deleteDirectory($themeFolderPath);
220     }
221
222 }