]> BookStack Code Mirror - bookstack/blob - tests/Unit/ConfigTest.php
10376751633c5e225a1451cc361ddb41870ae2e5
[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', 'dompdf.options.enable_remote', false);
84         $this->checkEnvConfigResult('ALLOW_UNTRUSTED_SERVER_FETCHING', 'true', 'dompdf.options.enable_remote', true);
85     }
86
87     public function test_dompdf_paper_size_options_are_limited()
88     {
89         $this->checkEnvConfigResult('EXPORT_PAGE_SIZE', 'cat', 'dompdf.options.default_paper_size', 'a4');
90         $this->checkEnvConfigResult('EXPORT_PAGE_SIZE', 'letter', 'dompdf.options.default_paper_size', 'letter');
91         $this->checkEnvConfigResult('EXPORT_PAGE_SIZE', 'a4', 'dompdf.options.default_paper_size', 'a4');
92     }
93
94     public function test_snappy_paper_size_options_are_limited()
95     {
96         $this->checkEnvConfigResult('EXPORT_PAGE_SIZE', 'cat', 'snappy.pdf.options.page-size', 'A4');
97         $this->checkEnvConfigResult('EXPORT_PAGE_SIZE', 'letter', 'snappy.pdf.options.page-size', 'Letter');
98         $this->checkEnvConfigResult('EXPORT_PAGE_SIZE', 'a4', 'snappy.pdf.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     /**
126      * Set an environment variable of the given name and value
127      * then check the given config key to see if it matches the given result.
128      * Providing a null $envVal clears the variable.
129      *
130      * @param mixed $expectedResult
131      */
132     protected function checkEnvConfigResult(string $envName, ?string $envVal, string $configKey, $expectedResult)
133     {
134         $this->runWithEnv($envName, $envVal, function () use ($configKey, $expectedResult) {
135             $this->assertEquals($expectedResult, config($configKey));
136         });
137     }
138 }