From 45709418db8067b699fbe93a058b72c2e9c27e22 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 29 Aug 2023 10:01:05 +0900 Subject: [PATCH 1/2] fix: replace `config(DocTypes::class)` with `new DocTypes()` --- system/Common.php | 3 ++- system/Typography/Typography.php | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/system/Common.php b/system/Common.php index ff9ca929c9f9..03f72640e92e 100644 --- a/system/Common.php +++ b/system/Common.php @@ -890,7 +890,8 @@ function redirect(?string $route = null): RedirectResponse */ function _solidus(): string { - if (config(DocTypes::class)->html5 ?? false) { + $docTypes = new DocTypes(); + if ($docTypes->html5 ?? false) { return ''; } diff --git a/system/Typography/Typography.php b/system/Typography/Typography.php index 1ca7a56ff598..fb05b68faefe 100644 --- a/system/Typography/Typography.php +++ b/system/Typography/Typography.php @@ -323,10 +323,11 @@ protected function protectCharacters(array $match): string */ public function nl2brExceptPre(string $str): string { - $newstr = ''; + $newstr = ''; + $docTypes = new DocTypes(); for ($ex = explode('pre>', $str), $ct = count($ex), $i = 0; $i < $ct; $i++) { - $xhtml = ! (config(DocTypes::class)->html5 ?? false); + $xhtml = ! ($docTypes->html5 ?? false); $newstr .= (($i % 2) === 0) ? nl2br($ex[$i], $xhtml) : $ex[$i]; if ($ct - 1 !== $i) { From 3474bc51628915c5c86a3469d8c228073bd01153 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 29 Aug 2023 15:31:25 +0900 Subject: [PATCH 2/2] refactor: add way to chnage DocTypes for testing --- system/Common.php | 13 +++- tests/system/CommonFunctionsTest.php | 21 +++++-- tests/system/Helpers/FormHelperTest.php | 35 ++++++----- tests/system/Helpers/HTMLHelperTest.php | 81 +++++++++++-------------- 4 files changed, 81 insertions(+), 69 deletions(-) diff --git a/system/Common.php b/system/Common.php index 03f72640e92e..c964e10ea899 100644 --- a/system/Common.php +++ b/system/Common.php @@ -886,11 +886,20 @@ function redirect(?string $route = null): RedirectResponse /** * Generates the solidus character (`/`) depending on the HTML5 compatibility flag in `Config\DocTypes` * + * @param DocTypes|null $docTypesConfig New config. For testing purpose only. + * * @internal */ - function _solidus(): string + function _solidus(?DocTypes $docTypesConfig = null): string { - $docTypes = new DocTypes(); + static $docTypes = null; + + if ($docTypesConfig !== null) { + $docTypes = $docTypesConfig; + } + + $docTypes ??= new DocTypes(); + if ($docTypes->html5 ?? false) { return ''; } diff --git a/tests/system/CommonFunctionsTest.php b/tests/system/CommonFunctionsTest.php index c678a0dd1fbb..b6702c5a7376 100644 --- a/tests/system/CommonFunctionsTest.php +++ b/tests/system/CommonFunctionsTest.php @@ -31,6 +31,7 @@ use CodeIgniter\Test\TestLogger; use Config\App; use Config\Cookie; +use Config\DocTypes; use Config\Logger; use Config\Modules; use Config\Routing; @@ -180,14 +181,24 @@ public function testSolidusElement(): void public function testSolidusElementXHTML(): void { - $doctypes = config('DocTypes'); - $default = $doctypes->html5; - $doctypes->html5 = false; + $this->disableHtml5(); $this->assertSame(' /', _solidus()); - // Reset - $doctypes->html5 = $default; + $this->enableHtml5(); + } + + private function disableHtml5() + { + $doctypes = new DocTypes(); + $doctypes->html5 = false; + _solidus($doctypes); + } + + private function enableHtml5() + { + $doctypes = new DocTypes(); + _solidus($doctypes); } public function testView(): void diff --git a/tests/system/Helpers/FormHelperTest.php b/tests/system/Helpers/FormHelperTest.php index 8ed1bdeb7201..3b2482513581 100644 --- a/tests/system/Helpers/FormHelperTest.php +++ b/tests/system/Helpers/FormHelperTest.php @@ -14,6 +14,7 @@ use CodeIgniter\HTTP\SiteURI; use CodeIgniter\Test\CIUnitTestCase; use Config\App; +use Config\DocTypes; use Config\Filters; use Config\Services; @@ -262,9 +263,7 @@ public function testFormInput(): void public function testFormInputXHTML(): void { - $doctypes = config('DocTypes'); - $default = $doctypes->html5; - $doctypes->html5 = false; + $this->disableHtml5(); $expected = <<\n @@ -279,8 +278,20 @@ public function testFormInputXHTML(): void ]; $this->assertSame($expected, form_input($data)); - // Reset - $doctypes->html5 = $default; + $this->enableHtml5(); + } + + private function disableHtml5() + { + $doctypes = new DocTypes(); + $doctypes->html5 = false; + _solidus($doctypes); + } + + private function enableHtml5() + { + $doctypes = new DocTypes(); + _solidus($doctypes); } public function testFormInputWithExtra(): void @@ -317,17 +328,14 @@ public function testFormUpload(): void public function testFormUploadXHTML(): void { - $doctypes = config('DocTypes'); - $default = $doctypes->html5; - $doctypes->html5 = false; + $this->disableHtml5(); $expected = <<\n EOH; $this->assertSame($expected, form_upload('attachment')); - // Reset - $doctypes->html5 = $default; + $this->enableHtml5(); } public function testFormTextarea(): void @@ -656,17 +664,14 @@ public function testFormCheckbox(): void public function testFormCheckboxXHTML(): void { - $doctypes = config('DocTypes'); - $default = $doctypes->html5; - $doctypes->html5 = false; + $this->disableHtml5(); $expected = <<\n EOH; $this->assertSame($expected, form_checkbox('newsletter', 'accept', true)); - // Reset - $doctypes->html5 = $default; + $this->enableHtml5(); } public function testFormCheckboxArrayData(): void diff --git a/tests/system/Helpers/HTMLHelperTest.php b/tests/system/Helpers/HTMLHelperTest.php index 0d9452640730..f591b841024d 100755 --- a/tests/system/Helpers/HTMLHelperTest.php +++ b/tests/system/Helpers/HTMLHelperTest.php @@ -15,6 +15,7 @@ use CodeIgniter\Files\Exceptions\FileNotFoundException; use CodeIgniter\Test\CIUnitTestCase; use Config\App; +use Config\DocTypes; /** * @internal @@ -206,41 +207,48 @@ public function testIMGWithIndexpage(): void public function testIMGXHTML(): void { - $doctypes = config('DocTypes'); - $default = $doctypes->html5; - $doctypes->html5 = false; + $this->disableHtml5(); $target = 'http://site.com/images/picture.jpg'; $expected = ''; $this->assertSame($expected, img($target)); - $doctypes->html5 = $default; + $this->enableHtml5(); } - public function testIMGXHTMLWithoutProtocol(): void + private function disableHtml5() { - $doctypes = config('DocTypes'); - $default = $doctypes->html5; + $doctypes = new DocTypes(); $doctypes->html5 = false; + _solidus($doctypes); + } + + private function enableHtml5() + { + $doctypes = new DocTypes(); + _solidus($doctypes); + } + + public function testIMGXHTMLWithoutProtocol(): void + { + $this->disableHtml5(); $target = 'assets/mugshot.jpg'; $expected = ''; $this->assertSame($expected, img($target)); - $doctypes->html5 = $default; + $this->enableHtml5(); } public function testIMGXHTMLWithIndexpage(): void { - $doctypes = config('DocTypes'); - $default = $doctypes->html5; - $doctypes->html5 = false; + $this->disableHtml5(); $target = 'assets/mugshot.jpg'; $expected = ''; $this->assertSame($expected, img($target, true)); - $doctypes->html5 = $default; + $this->enableHtml5(); } public function testImgData(): void @@ -355,16 +363,13 @@ public function testLinkTag(): void public function testLinkTagXHTML(): void { - $doctypes = config('DocTypes'); - $default = $doctypes->html5; - $doctypes->html5 = false; + $this->disableHtml5(); $target = 'css/mystyles.css'; $expected = ''; $this->assertSame($expected, link_tag($target)); - // Reset - $doctypes->html5 = $default; + $this->enableHtml5(); } public function testLinkTagMedia(): void @@ -509,9 +514,7 @@ public function testVideoWithTracks(): void public function testVideoWithTracksXHTML(): void { - $doctypes = config('DocTypes'); - $default = $doctypes->html5; - $doctypes->html5 = false; + $this->disableHtml5(); $expected = <<<'EOH'