]> BookStack Code Mirror - bookstack/blob - tests/Unit/ConfigTest.php
Merge branch 'master' of https://github.com/jasonhoule/BookStack into jasonhoule...
[bookstack] / tests / Unit / ConfigTest.php
1 <?php namespace Tests\Unit;
2
3 use Illuminate\Support\Facades\Log;
4 use Tests\TestCase;
5
6 /**
7  * Class ConfigTest
8  * Many of the tests here are to check on tweaks made
9  * to maintain backwards compatibility.
10  *
11  * @package Tests
12  */
13 class ConfigTest extends TestCase
14 {
15
16     public function test_filesystem_images_falls_back_to_storage_type_var()
17     {
18         $this->runWithEnv('STORAGE_TYPE', 'local_secure', function() {
19             $this->checkEnvConfigResult('STORAGE_IMAGE_TYPE', 's3', 'filesystems.images', 's3');
20             $this->checkEnvConfigResult('STORAGE_IMAGE_TYPE', null, 'filesystems.images', 'local_secure');
21         });
22     }
23
24     public function test_filesystem_attachments_falls_back_to_storage_type_var()
25     {
26         $this->runWithEnv('STORAGE_TYPE', 'local_secure', function() {
27             $this->checkEnvConfigResult('STORAGE_ATTACHMENT_TYPE', 's3', 'filesystems.attachments', 's3');
28             $this->checkEnvConfigResult('STORAGE_ATTACHMENT_TYPE', null, 'filesystems.attachments', 'local_secure');
29         });
30     }
31
32     public function test_app_url_blank_if_old_default_value()
33     {
34         $initUrl = 'https://example.com/docs';
35         $oldDefault = 'http://bookstack.dev';
36         $this->checkEnvConfigResult('APP_URL', $initUrl, 'app.url', $initUrl);
37         $this->checkEnvConfigResult('APP_URL', $oldDefault, 'app.url', '');
38     }
39
40     public function test_errorlog_plain_webserver_channel()
41     {
42         // We can't full test this due to it being targeted for the SAPI logging handler
43         // so we just overwrite that component so we can capture the error log output.
44         config()->set([
45             'logging.channels.errorlog_plain_webserver.handler_with' => [0],
46         ]);
47
48         $temp = tempnam(sys_get_temp_dir(), 'bs-test');
49         $original = ini_set( 'error_log', $temp);
50
51         Log::channel('errorlog_plain_webserver')->info('Aww, look, a cute puppy');
52
53         ini_set( 'error_log', $original);
54
55         $output = file_get_contents($temp);
56         $this->assertStringContainsString('Aww, look, a cute puppy', $output);
57         $this->assertStringNotContainsString('INFO', $output);
58         $this->assertStringNotContainsString('info', $output);
59         $this->assertStringNotContainsString('testing', $output);
60     }
61
62     public function test_session_cookie_uses_sub_path_from_app_url()
63     {
64         $this->checkEnvConfigResult('APP_URL', 'https://example.com', 'session.path', '/');
65         $this->checkEnvConfigResult('APP_URL', 'https://a.com/b', 'session.path', '/b');
66         $this->checkEnvConfigResult('APP_URL', 'https://a.com/b/d/e', 'session.path', '/b/d/e');
67         $this->checkEnvConfigResult('APP_URL', '', 'session.path', '/');
68     }
69
70     public function test_saml2_idp_authn_context_string_parsed_as_space_separated_array()
71     {
72         $this->checkEnvConfigResult(
73             'SAML2_IDP_AUTHNCONTEXT',
74             'urn:federation:authentication:windows urn:federation:authentication:linux',
75             'saml2.onelogin.security.requestedAuthnContext',
76             ['urn:federation:authentication:windows', 'urn:federation:authentication:linux']
77         );
78     }
79
80     /**
81      * Set an environment variable of the given name and value
82      * then check the given config key to see if it matches the given result.
83      * Providing a null $envVal clears the variable.
84      * @param mixed $expectedResult
85      */
86     protected function checkEnvConfigResult(string $envName, ?string $envVal, string $configKey, $expectedResult)
87     {
88         $this->runWithEnv($envName, $envVal, function() use ($configKey, $expectedResult) {
89             $this->assertEquals($expectedResult, config($configKey));
90         });
91     }
92
93 }