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;
13 * Many of the tests here are to check on tweaks made
14 * to maintain backwards compatibility.
16 class ConfigTest extends TestCase
18 public function test_filesystem_images_falls_back_to_storage_type_var()
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');
26 public function test_filesystem_attachments_falls_back_to_storage_type_var()
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');
34 public function test_app_url_blank_if_old_default_value()
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', '');
42 public function test_errorlog_plain_webserver_channel()
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.
47 'logging.channels.errorlog_plain_webserver.handler_with' => [0],
50 $temp = tempnam(sys_get_temp_dir(), 'bs-test');
51 $original = ini_set('error_log', $temp);
53 Log::channel('errorlog_plain_webserver')->info('Aww, look, a cute puppy');
55 ini_set('error_log', $original);
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);
64 public function test_session_cookie_uses_sub_path_from_app_url()
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', '/');
72 public function test_saml2_idp_authn_context_string_parsed_as_space_separated_array()
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']
82 public function test_dompdf_remote_fetching_controlled_by_allow_untrusted_server_fetching_false()
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);
88 public function test_dompdf_paper_size_options_are_limited()
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');
95 public function test_snappy_paper_size_options_are_limited()
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');
102 public function test_sendmail_command_is_configurable()
104 $this->checkEnvConfigResult('MAIL_SENDMAIL_COMMAND', '/var/sendmail -o', 'mail.mailers.sendmail.path', '/var/sendmail -o');
107 public function test_mail_disable_ssl_verification_alters_mailer()
109 $getStreamOptions = function (): array {
110 /** @var EsmtpTransport $transport */
111 $transport = Mail::mailer('smtp')->getSymfonyTransport();
112 return $transport->getStream()->getStreamOptions();
115 $this->assertEmpty($getStreamOptions());
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']);
126 public function test_non_null_mail_encryption_options_enforce_smtp_scheme()
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);
133 public function test_smtp_scheme_and_certain_port_forces_tls_usage()
135 $isMailTlsForcedEnabled = function () {
136 $transport = Mail::mailer('smtp')->getSymfonyTransport();
137 /** @var SocketStream $stream */
138 $stream = $transport->getStream();
140 return $stream->isTLS();
144 'mail.mailers.smtp.scheme' => null,
145 'mail.mailers.smtp.port' => 587,
148 $this->assertFalse($isMailTlsForcedEnabled());
151 'mail.mailers.smtp.scheme' => 'smtps',
152 'mail.mailers.smtp.port' => 587,
155 $this->assertTrue($isMailTlsForcedEnabled());
158 'mail.mailers.smtp.scheme' => '',
159 'mail.mailers.smtp.port' => 465,
162 $this->assertTrue($isMailTlsForcedEnabled());
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.
170 * @param mixed $expectedResult
172 protected function checkEnvConfigResult(string $envName, ?string $envVal, string $configKey, $expectedResult)
174 $this->runWithEnv($envName, $envVal, function () use ($configKey, $expectedResult) {
175 $this->assertEquals($expectedResult, config($configKey));