diff --git a/app/Config/Database.php b/app/Config/Database.php index f3fe66c7048c..b31541a83bcd 100644 --- a/app/Config/Database.php +++ b/app/Config/Database.php @@ -62,9 +62,9 @@ class Database extends \CodeIgniter\Database\Config 'hostname' => '127.0.0.1', 'username' => '', 'password' => '', - 'database' => '', - 'DBDriver' => '', - 'DBPrefix' => 'db_', // Needed to ensure we're working correctly with prefixes live. DO NOT REMOVE. + 'database' => ':memory:', + 'DBDriver' => 'SQLite3', + 'DBPrefix' => 'db_', // Needed to ensure we're working correctly with prefixes live. DO NOT REMOVE FOR CI DEVS 'pConnect' => false, 'DBDebug' => (ENVIRONMENT !== 'production'), 'cacheOn' => false, diff --git a/system/HTTP/RedirectResponse.php b/system/HTTP/RedirectResponse.php index abcf262e0c24..70cf8dc67214 100644 --- a/system/HTTP/RedirectResponse.php +++ b/system/HTTP/RedirectResponse.php @@ -92,7 +92,7 @@ public function route(string $route, array $params = [], int $code = 302, string throw HTTPException::forInvalidRedirectRoute($route); } - return $this->redirect(base_url($route), $method, $code); + return $this->redirect(site_url($route), $method, $code); } /** diff --git a/system/HTTP/Response.php b/system/HTTP/Response.php index f4f0230c5d92..866106ae6bb7 100644 --- a/system/HTTP/Response.php +++ b/system/HTTP/Response.php @@ -537,9 +537,9 @@ public function getXML() * @throws \InvalidArgumentException If the body property is not string or array. */ protected function formatBody($body, string $format) - { + { $this->bodyFormat = ($format === 'json-unencoded' ? 'json' : $format); - $mime = "application/{$this->bodyFormat}"; + $mime = "application/{$this->bodyFormat}"; $this->setContentType($mime); // Nothing much to do for a string... diff --git a/tests/system/HTTP/RedirectResponseTest.php b/tests/system/HTTP/RedirectResponseTest.php index 499ff0b02f64..fb1599fe88ea 100644 --- a/tests/system/HTTP/RedirectResponseTest.php +++ b/tests/system/HTTP/RedirectResponseTest.php @@ -3,6 +3,7 @@ namespace CodeIgniter\HTTP; use Config\App; +use CodeIgniter\Config\Config; use CodeIgniter\Config\Services; use CodeIgniter\Validation\Validation; use CodeIgniter\Router\RouteCollection; @@ -57,14 +58,14 @@ public function testRedirectRoute() $response->route('exampleRoute'); $this->assertTrue($response->hasHeader('Location')); - $this->assertEquals('http://example.com/exampleRoute', $response->getHeaderLine('Location')); + $this->assertEquals('http://example.com/index.php/exampleRoute', $response->getHeaderLine('Location')); $this->routes->add('exampleRoute', 'Home::index', ['as' => 'home']); $response->route('home'); $this->assertTrue($response->hasHeader('Location')); - $this->assertEquals('http://example.com/exampleRoute', $response->getHeaderLine('Location')); + $this->assertEquals('http://example.com/index.php/exampleRoute', $response->getHeaderLine('Location')); } public function testRedirectRouteBad() @@ -186,4 +187,27 @@ public function testRedirectBackMissing() $this->assertSame($response, $returned); } + /** + * @see https://github.com/codeigniter4/CodeIgniter4/issues/2119 + */ + public function testRedirectRouteBaseUrl() + { + $config = new App(); + $config->baseURL = 'http://example.com/test/'; + Config::injectMock('App', $config); + + $request = new MockIncomingRequest($config, new URI('http://example.com/test/'), null, new UserAgent()); + Services::injectMock('request', $request); + + $response = new RedirectResponse(new App()); + + $this->routes->add('exampleRoute', 'Home::index'); + + $response->route('exampleRoute'); + + $this->assertTrue($response->hasHeader('Location')); + $this->assertEquals('http://example.com/test/index.php/exampleRoute', $response->getHeaderLine('Location')); + + Config::reset(); + } } diff --git a/tests/system/HTTP/ResponseTest.php b/tests/system/HTTP/ResponseTest.php index 9eb225755db4..f46ddf7296ed 100644 --- a/tests/system/HTTP/ResponseTest.php +++ b/tests/system/HTTP/ResponseTest.php @@ -6,6 +6,7 @@ use Config\Format; use DateTime; use DateTimeZone; +use CodeIgniter\Config\Config; use Tests\Support\HTTP\MockResponse; class ResponseTest extends \CIUnitTestCase @@ -20,6 +21,7 @@ protected function setUp(): void public function tearDown(): void { $_SERVER = $this->server; + Config::reset(); } public function testCanSetStatusCode() @@ -158,28 +160,33 @@ public function testSetDateRemembersDateInUTC() public function testSetLink() { - $response = new Response(new App()); + // Ensure our URL is not getting overridden + $config = new App(); + $config->baseURL = 'http://example.com/test'; + Config::injectMock('App', $config); + + $response = new Response($config); $pager = \Config\Services::pager(); $pager->store('default', 3, 10, 200); $response->setLink($pager); $this->assertEquals( - '<http://example.com?page=1>; rel="first",<http://example.com?page=2>; rel="prev",<http://example.com?page=4>; rel="next",<http://example.com?page=20>; rel="last"', $response->getHeader('Link')->getValue() + '<http://example.com/test/?page=1>; rel="first",<http://example.com/test/?page=2>; rel="prev",<http://example.com/test/?page=4>; rel="next",<http://example.com/test/?page=20>; rel="last"', $response->getHeader('Link')->getValue() ); $pager->store('default', 1, 10, 200); $response->setLink($pager); $this->assertEquals( - '<http://example.com?page=2>; rel="next",<http://example.com?page=20>; rel="last"', $response->getHeader('Link')->getValue() + '<http://example.com/test/?page=2>; rel="next",<http://example.com/test/?page=20>; rel="last"', $response->getHeader('Link')->getValue() ); $pager->store('default', 20, 10, 200); $response->setLink($pager); $this->assertEquals( - '<http://example.com?page=1>; rel="first",<http://example.com?page=19>; rel="prev"', $response->getHeader('Link')->getValue() + '<http://example.com/test/?page=1>; rel="first",<http://example.com/test/?page=19>; rel="prev"', $response->getHeader('Link')->getValue() ); } diff --git a/tests/system/Helpers/FormHelperTest.php b/tests/system/Helpers/FormHelperTest.php index e4f4fd05722f..acb04c14b84a 100644 --- a/tests/system/Helpers/FormHelperTest.php +++ b/tests/system/Helpers/FormHelperTest.php @@ -173,41 +173,6 @@ public function testFormOpenWithHidden() $this->assertEquals($expected, form_open('foo/bar', $attributes, $hidden)); } - // ------------------------------------------------------------------------ - //FIXME This needs dynamic filters to complete - // public function testFormOpenWithCSRF() - // { - // $config = new App(); - // $config->baseURL = ''; - // $config->indexPage = 'index.php'; - // $request = Services::request($config); - // $request->uri = new URI('http://example.com/'); - // - // Services::injectMock('request', $request); - // - // $filters = Services::filters(); - // $filters->globals['before'][] = 'csrf'; // force CSRF - // $before = $filters->globals['before']; - // - // $Value = csrf_hash(); - // $Name = csrf_token(); - // $expected = <<<EOH - //<form action="http://example.com/index.php/foo/bar" name="form" id="form" method="POST" accept-charset="utf-8"> - //<input type="hidden" name="foo" value="bar" style="display: none;" /> - //<input type="hidden" name="$Name" value="$Value" style="display: none;" /> - // - //EOH; - // - // $attributes = [ - // 'name' => 'form', - // 'id' => 'form', - // 'method' => 'POST' - // ]; - // $hidden = [ - // 'foo' => 'bar' - // ]; - // $this->assertEquals($expected, form_open('foo/bar', $attributes, $hidden)); - // } // ------------------------------------------------------------------------ public function testFormOpenMultipart() { diff --git a/tests/system/Helpers/URLHelperTest.php b/tests/system/Helpers/URLHelperTest.php index 4f1015d68e0f..c966562b2509 100644 --- a/tests/system/Helpers/URLHelperTest.php +++ b/tests/system/Helpers/URLHelperTest.php @@ -19,6 +19,13 @@ protected function setUp(): void Services::reset(); } + public function tearDown() + { + parent::tearDown(); + + $_SERVER = []; + } + //-------------------------------------------------------------------- // Test site_url