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