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