]> BookStack Code Mirror - bookstack/blob - tests/Unit/ConfigTest.php
API Docs: Add Missing Fields in Example Responses
[bookstack] / tests / Unit / ConfigTest.php
1 <?php
2
3 namespace Tests\Unit;
4
5 use Illuminate\Support\Facades\Log;
6 use Illuminate\Support\Facades\Mail;
7 use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
8 use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
9 use Tests\TestCase;
10
11 /**
12  * Class ConfigTest
13  * Many of the tests here are to check on tweaks made
14  * to maintain backwards compatibility.
15  */
16 class ConfigTest extends TestCase
17 {
18     public function test_filesystem_images_falls_back_to_storage_type_var()
19     {
20         $this->runWithEnv('STORAGE_TYPE', 'local_secure', function () {
21             $this->checkEnvConfigResult('STORAGE_IMAGE_TYPE', 's3', 'filesystems.images', 's3');
22             $this->checkEnvConfigResult('STORAGE_IMAGE_TYPE', null, 'filesystems.images', 'local_secure');
23         });
24     }
25
26     public function test_filesystem_attachments_falls_back_to_storage_type_var()
27     {
28         $this->runWithEnv('STORAGE_TYPE', 'local_secure', function () {
29             $this->checkEnvConfigResult('STORAGE_ATTACHMENT_TYPE', 's3', 'filesystems.attachments', 's3');
30             $this->checkEnvConfigResult('STORAGE_ATTACHMENT_TYPE', null, 'filesystems.attachments', 'local_secure');
31         });
32     }
33
34     public function test_app_url_blank_if_old_default_value()
35     {
36         $initUrl = 'https://example.com/docs';
37         $oldDefault = 'http://bookstack.dev';
38         $this->checkEnvConfigResult('APP_URL', $initUrl, 'app.url', $initUrl);
39         $this->checkEnvConfigResult('APP_URL', $oldDefault, 'app.url', '');
40     }
41
42     public function test_errorlog_plain_webserver_channel()
43     {
44         // We can't full test this due to it being targeted for the SAPI logging handler
45         // so we just overwrite that component so we can capture the error log output.
46         config()->set([
47             'logging.channels.errorlog_plain_webserver.handler_with' => [0],
48         ]);
49
50         $temp = tempnam(sys_get_temp_dir(), 'bs-test');
51         $original = ini_set('error_log', $temp);
52
53         Log::channel('errorlog_plain_webserver')->info('Aww, look, a cute puppy');
54
55         ini_set('error_log', $original);
56
57         $output = file_get_contents($temp);
58         $this->assertStringContainsString('Aww, look, a cute puppy', $output);
59         $this->assertStringNotContainsString('INFO', $output);
60         $this->assertStringNotContainsString('info', $output);
61         $this->assertStringNotContainsString('testing', $output);
62     }
63
64     public function test_session_cookie_uses_sub_path_from_app_url()
65     {
66         $this->checkEnvConfigResult('APP_URL', 'https://example.com', 'session.path', '/');
67         $this->checkEnvConfigResult('APP_URL', 'https://a.com/b', 'session.path', '/b');
68         $this->checkEnvConfigResult('APP_URL', 'https://a.com/b/d/e', 'session.path', '/b/d/e');
69         $this->checkEnvConfigResult('APP_URL', '', 'session.path', '/');
70     }
71
72     public function test_saml2_idp_authn_context_string_parsed_as_space_separated_array()
73     {
74         $this->checkEnvConfigResult(
75             'SAML2_IDP_AUTHNCONTEXT',
76             'urn:federation:authentication:windows urn:federation:authentication:linux',
77             'saml2.onelogin.security.requestedAuthnContext',
78             ['urn:federation:authentication:windows', 'urn:federation:authentication:linux']
79         );
80     }
81
82     public function test_dompdf_remote_fetching_controlled_by_allow_untrusted_server_fetching_false()
83     {
84         $this->checkEnvConfigResult('ALLOW_UNTRUSTED_SERVER_FETCHING', 'false', 'dompdf.options.enable_remote', false);
85         $this->checkEnvConfigResult('ALLOW_UNTRUSTED_SERVER_FETCHING', 'true', 'dompdf.options.enable_remote', true);
86     }
87
88     public function test_dompdf_paper_size_options_are_limited()
89     {
90         $this->checkEnvConfigResult('EXPORT_PAGE_SIZE', 'cat', 'dompdf.options.default_paper_size', 'a4');
91         $this->checkEnvConfigResult('EXPORT_PAGE_SIZE', 'letter', 'dompdf.options.default_paper_size', 'letter');
92         $this->checkEnvConfigResult('EXPORT_PAGE_SIZE', 'a4', 'dompdf.options.default_paper_size', 'a4');
93     }
94
95     public function test_snappy_paper_size_options_are_limited()
96     {
97         $this->checkEnvConfigResult('EXPORT_PAGE_SIZE', 'cat', 'snappy.pdf.options.page-size', 'A4');
98         $this->checkEnvConfigResult('EXPORT_PAGE_SIZE', 'letter', 'snappy.pdf.options.page-size', 'Letter');
99         $this->checkEnvConfigResult('EXPORT_PAGE_SIZE', 'a4', 'snappy.pdf.options.page-size', 'A4');
100     }
101
102     public function test_sendmail_command_is_configurable()
103     {
104         $this->checkEnvConfigResult('MAIL_SENDMAIL_COMMAND', '/var/sendmail -o', 'mail.mailers.sendmail.path', '/var/sendmail -o');
105     }
106
107     public function test_mail_disable_ssl_verification_alters_mailer()
108     {
109         $getStreamOptions = function (): array {
110             /** @var EsmtpTransport $transport */
111             $transport = Mail::mailer('smtp')->getSymfonyTransport();
112             return $transport->getStream()->getStreamOptions();
113         };
114
115         $this->assertEmpty($getStreamOptions());
116
117
118         $this->runWithEnv('MAIL_VERIFY_SSL', 'false', function () use ($getStreamOptions) {
119             $options = $getStreamOptions();
120             $this->assertArrayHasKey('ssl', $options);
121             $this->assertFalse($options['ssl']['verify_peer']);
122             $this->assertFalse($options['ssl']['verify_peer_name']);
123         });
124     }
125
126     public function test_non_null_mail_encryption_options_enforce_smtp_scheme()
127     {
128         $this->checkEnvConfigResult('MAIL_ENCRYPTION', 'tls', 'mail.mailers.smtp.scheme', 'smtps');
129         $this->checkEnvConfigResult('MAIL_ENCRYPTION', 'ssl', 'mail.mailers.smtp.scheme', 'smtps');
130         $this->checkEnvConfigResult('MAIL_ENCRYPTION', 'null', 'mail.mailers.smtp.scheme', null);
131     }
132
133     public function test_smtp_scheme_and_certain_port_forces_tls_usage()
134     {
135         $isMailTlsForcedEnabled = function () {
136             $transport = Mail::mailer('smtp')->getSymfonyTransport();
137             /** @var SocketStream $stream */
138             $stream = $transport->getStream();
139             Mail::purge('smtp');
140             return $stream->isTLS();
141         };
142
143         config()->set([
144             'mail.mailers.smtp.scheme' => null,
145             'mail.mailers.smtp.port' => 587,
146         ]);
147
148         $this->assertFalse($isMailTlsForcedEnabled());
149
150         config()->set([
151             'mail.mailers.smtp.scheme' => 'smtps',
152             'mail.mailers.smtp.port' => 587,
153         ]);
154
155         $this->assertTrue($isMailTlsForcedEnabled());
156
157         config()->set([
158             'mail.mailers.smtp.scheme' => '',
159             'mail.mailers.smtp.port' => 465,
160         ]);
161
162         $this->assertTrue($isMailTlsForcedEnabled());
163     }
164
165     /**
166      * Set an environment variable of the given name and value
167      * then check the given config key to see if it matches the given result.
168      * Providing a null $envVal clears the variable.
169      *
170      * @param mixed $expectedResult
171      */
172     protected function checkEnvConfigResult(string $envName, ?string $envVal, string $configKey, $expectedResult)
173     {
174         $this->runWithEnv($envName, $envVal, function () use ($configKey, $expectedResult) {
175             $this->assertEquals($expectedResult, config($configKey));
176         });
177     }
178 }