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