Skip to content

Commit a42b84f

Browse files
authored
Merge pull request #7901 from kenjis/fix-cli-base_url
fix: base_url()/site_url() does not work on CLI
2 parents 0333436 + e0df5f2 commit a42b84f

File tree

2 files changed

+336
-0
lines changed

2 files changed

+336
-0
lines changed

system/HTTP/CLIRequest.php

+3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public function __construct(App $config)
7373
ignore_user_abort(true);
7474

7575
$this->parseCommand();
76+
77+
// Set SiteURI for this request
78+
$this->uri = new SiteURI($config, $this->getPath());
7679
}
7780

7881
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,333 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Helpers\URLHelper;
13+
14+
use CodeIgniter\Config\Factories;
15+
use CodeIgniter\Config\Services;
16+
use CodeIgniter\HTTP\CLIRequest;
17+
use CodeIgniter\Test\CIUnitTestCase;
18+
use Config\App;
19+
20+
/**
21+
* Since base_url() only slightly modifies
22+
* site_url() these functions are tested
23+
* simultaneously.
24+
*
25+
* @backupGlobals enabled
26+
*
27+
* @internal
28+
*
29+
* @group Others
30+
*/
31+
final class SiteUrlCliTest extends CIUnitTestCase
32+
{
33+
private App $config;
34+
35+
protected function setUp(): void
36+
{
37+
parent::setUp();
38+
39+
Services::reset(true);
40+
41+
$this->config = new App();
42+
}
43+
44+
protected function tearDown(): void
45+
{
46+
parent::tearDown();
47+
48+
$_SERVER = [];
49+
}
50+
51+
private function createRequest(?App $config = null): void
52+
{
53+
$config ??= new App();
54+
55+
$request = new CLIRequest($config);
56+
Services::injectMock('request', $request);
57+
58+
Factories::injectMock('config', 'App', $config);
59+
}
60+
61+
/**
62+
* Takes a multitude of various config input and verifies
63+
* that base_url() and site_url() return the expected result.
64+
*
65+
* @param string $baseURL
66+
* @param string $indexPage
67+
* @param string|null $scheme
68+
* @param bool $secure
69+
* @param string $path
70+
* @param string $expectedSiteUrl
71+
* @param string $expectedBaseUrl
72+
*
73+
* @dataProvider provideUrls
74+
*/
75+
public function testUrls(
76+
$baseURL,
77+
$indexPage,
78+
$scheme,
79+
$secure,
80+
$path,
81+
$expectedSiteUrl,
82+
$expectedBaseUrl
83+
): void {
84+
// Set the config
85+
$this->config->baseURL = $baseURL;
86+
$this->config->indexPage = $indexPage;
87+
$this->config->forceGlobalSecureRequests = $secure;
88+
89+
$this->createRequest($this->config);
90+
91+
$this->assertSame($expectedSiteUrl, site_url($path, $scheme, $this->config));
92+
$this->assertSame($expectedBaseUrl, base_url($path, $scheme));
93+
}
94+
95+
public static function provideUrls(): iterable
96+
{
97+
// baseURL, indexPage, scheme, secure, path, expectedSiteUrl, expectedBaseUrl
98+
return [
99+
'forceGlobalSecure' => [
100+
'http://example.com/',
101+
'index.php',
102+
null,
103+
true,
104+
'',
105+
'https://example.com/index.php',
106+
'https://example.com/',
107+
],
108+
[
109+
'http://example.com/',
110+
'index.php',
111+
null,
112+
false,
113+
'',
114+
'http://example.com/index.php',
115+
'http://example.com/',
116+
],
117+
'baseURL missing /' => [
118+
'http://example.com',
119+
'index.php',
120+
null,
121+
false,
122+
'',
123+
'http://example.com/index.php',
124+
'http://example.com/',
125+
],
126+
[
127+
'http://example.com/',
128+
'',
129+
null,
130+
false,
131+
'',
132+
'http://example.com/',
133+
'http://example.com/',
134+
],
135+
[
136+
'http://example.com/',
137+
'banana.php',
138+
null,
139+
false,
140+
'',
141+
'http://example.com/banana.php',
142+
'http://example.com/',
143+
],
144+
[
145+
'http://example.com/',
146+
'',
147+
null,
148+
false,
149+
'abc',
150+
'http://example.com/abc',
151+
'http://example.com/abc',
152+
],
153+
[
154+
'http://example.com/',
155+
'',
156+
null,
157+
false,
158+
'/abc',
159+
'http://example.com/abc',
160+
'http://example.com/abc',
161+
],
162+
[
163+
'http://example.com/',
164+
'',
165+
null,
166+
false,
167+
'/abc/',
168+
'http://example.com/abc/',
169+
'http://example.com/abc/',
170+
],
171+
[
172+
'http://example.com/',
173+
'',
174+
null,
175+
false,
176+
'/abc/def',
177+
'http://example.com/abc/def',
178+
'http://example.com/abc/def',
179+
],
180+
'URL decode' => [
181+
'http://example.com/',
182+
'',
183+
null,
184+
false,
185+
'template/meet-%26-greet',
186+
'http://example.com/template/meet-&-greet',
187+
'http://example.com/template/meet-&-greet',
188+
],
189+
'URL encode' => [
190+
'http://example.com/',
191+
'',
192+
null,
193+
false,
194+
'<s>alert</s>',
195+
'http://example.com/%3Cs%3Ealert%3C/s%3E',
196+
'http://example.com/%3Cs%3Ealert%3C/s%3E',
197+
],
198+
[
199+
'http://example.com/public/',
200+
'index.php',
201+
null,
202+
false,
203+
'',
204+
'http://example.com/public/index.php',
205+
'http://example.com/public/',
206+
],
207+
[
208+
'http://example.com/public/',
209+
'',
210+
null,
211+
false,
212+
'',
213+
'http://example.com/public/',
214+
'http://example.com/public/',
215+
],
216+
[
217+
'http://example.com/public',
218+
'',
219+
null,
220+
false,
221+
'',
222+
'http://example.com/public/',
223+
'http://example.com/public/',
224+
],
225+
[
226+
'http://example.com/public',
227+
'index.php',
228+
null,
229+
false,
230+
'/',
231+
'http://example.com/public/index.php/',
232+
'http://example.com/public/',
233+
],
234+
[
235+
'http://example.com/public/',
236+
'index.php',
237+
null,
238+
false,
239+
'/',
240+
'http://example.com/public/index.php/',
241+
'http://example.com/public/',
242+
],
243+
[
244+
'http://example.com/',
245+
'index.php',
246+
null,
247+
false,
248+
'foo',
249+
'http://example.com/index.php/foo',
250+
'http://example.com/foo',
251+
],
252+
[
253+
'http://example.com/',
254+
'index.php',
255+
null,
256+
false,
257+
'0',
258+
'http://example.com/index.php/0',
259+
'http://example.com/0',
260+
],
261+
[
262+
'http://example.com/public',
263+
'index.php',
264+
null,
265+
false,
266+
'foo',
267+
'http://example.com/public/index.php/foo',
268+
'http://example.com/public/foo',
269+
],
270+
[
271+
'http://example.com/',
272+
'index.php',
273+
null,
274+
false,
275+
'foo?bar=bam',
276+
'http://example.com/index.php/foo?bar=bam',
277+
'http://example.com/foo?bar=bam',
278+
],
279+
[
280+
'http://example.com/',
281+
'index.php',
282+
null,
283+
false,
284+
'test#banana',
285+
'http://example.com/index.php/test#banana',
286+
'http://example.com/test#banana',
287+
],
288+
[
289+
'http://example.com/',
290+
'index.php',
291+
'ftp',
292+
false,
293+
'foo',
294+
'ftp://example.com/index.php/foo',
295+
'ftp://example.com/foo',
296+
],
297+
[
298+
'http://example.com/',
299+
'index.php',
300+
null,
301+
false,
302+
'news/local/123',
303+
'http://example.com/index.php/news/local/123',
304+
'http://example.com/news/local/123',
305+
],
306+
[
307+
'http://example.com/',
308+
'index.php',
309+
null,
310+
false,
311+
['news', 'local', '123'],
312+
'http://example.com/index.php/news/local/123',
313+
'http://example.com/news/local/123',
314+
],
315+
];
316+
}
317+
318+
public function testSiteURLWithEmptyStringScheme(): void
319+
{
320+
$this->config->baseURL = 'http://example.com/';
321+
$this->config->indexPage = 'index.php';
322+
$this->config->forceGlobalSecureRequests = false;
323+
324+
$this->assertSame(
325+
'//example.com/index.php/test',
326+
site_url('test', '', $this->config)
327+
);
328+
$this->assertSame(
329+
'//example.com/img/test.jpg',
330+
base_url('img/test.jpg', '')
331+
);
332+
}
333+
}

0 commit comments

Comments
 (0)