Skip to content

Commit

Permalink
refactor: add way to chnage DocTypes for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Aug 29, 2023
1 parent 4570941 commit 3474bc5
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 69 deletions.
13 changes: 11 additions & 2 deletions system/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
}
Expand Down
21 changes: 16 additions & 5 deletions tests/system/CommonFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
35 changes: 20 additions & 15 deletions tests/system/Helpers/FormHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use CodeIgniter\HTTP\SiteURI;
use CodeIgniter\Test\CIUnitTestCase;
use Config\App;
use Config\DocTypes;
use Config\Filters;
use Config\Services;

Expand Down Expand Up @@ -262,9 +263,7 @@ public function testFormInput(): void

public function testFormInputXHTML(): void
{
$doctypes = config('DocTypes');
$default = $doctypes->html5;
$doctypes->html5 = false;
$this->disableHtml5();

$expected = <<<EOH
<input type="text" name="username" value="johndoe" id="username" maxlength="100" size="50" style="width:50%" />\n
Expand All @@ -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
Expand Down Expand Up @@ -317,17 +328,14 @@ public function testFormUpload(): void

public function testFormUploadXHTML(): void
{
$doctypes = config('DocTypes');
$default = $doctypes->html5;
$doctypes->html5 = false;
$this->disableHtml5();

$expected = <<<EOH
<input type="file" name="attachment" />\n
EOH;
$this->assertSame($expected, form_upload('attachment'));

// Reset
$doctypes->html5 = $default;
$this->enableHtml5();
}

public function testFormTextarea(): void
Expand Down Expand Up @@ -656,17 +664,14 @@ public function testFormCheckbox(): void

public function testFormCheckboxXHTML(): void
{
$doctypes = config('DocTypes');
$default = $doctypes->html5;
$doctypes->html5 = false;
$this->disableHtml5();

$expected = <<<EOH
<input type="checkbox" name="newsletter" value="accept" checked="checked" />\n
EOH;
$this->assertSame($expected, form_checkbox('newsletter', 'accept', true));

// Reset
$doctypes->html5 = $default;
$this->enableHtml5();
}

public function testFormCheckboxArrayData(): void
Expand Down
81 changes: 34 additions & 47 deletions tests/system/Helpers/HTMLHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use CodeIgniter\Files\Exceptions\FileNotFoundException;
use CodeIgniter\Test\CIUnitTestCase;
use Config\App;
use Config\DocTypes;

/**
* @internal
Expand Down Expand Up @@ -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 = '<img src="http://site.com/images/picture.jpg" alt="" />';
$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 = '<img src="http://example.com/assets/mugshot.jpg" alt="" />';
$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 = '<img src="http://example.com/index.php/assets/mugshot.jpg" alt="" />';
$this->assertSame($expected, img($target, true));

$doctypes->html5 = $default;
$this->enableHtml5();
}

public function testImgData(): void
Expand Down Expand Up @@ -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 = '<link href="http://example.com/css/mystyles.css" rel="stylesheet" type="text/css" />';
$this->assertSame($expected, link_tag($target));

// Reset
$doctypes->html5 = $default;
$this->enableHtml5();
}

public function testLinkTagMedia(): void
Expand Down Expand Up @@ -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'
<video src="http://example.com/test.mp4" controls>
Expand All @@ -531,8 +534,7 @@ public function testVideoWithTracksXHTML(): void
$video = video($target, $message, 'controls', $tracks);
$this->assertSame($expected, $video);

// Reset
$doctypes->html5 = $default;
$this->enableHtml5();
}

public function testVideoWithTracksAndIndex(): void
Expand Down Expand Up @@ -581,9 +583,7 @@ public function testVideoMultipleSources(): void

public function testVideoMultipleSourcesXHTML(): void
{
$doctypes = config('DocTypes');
$default = $doctypes->html5;
$doctypes->html5 = false;
$this->disableHtml5();

$expected = <<<'EOH'
<video class="test" controls>
Expand Down Expand Up @@ -613,8 +613,7 @@ public function testVideoMultipleSourcesXHTML(): void

$this->assertSame($expected, $video);

// Reset
$doctypes->html5 = $default;
$this->enableHtml5();
}

public function testAudio(): void
Expand Down Expand Up @@ -642,9 +641,7 @@ public function testAudio(): void

public function testAudioXHTML(): void
{
$doctypes = config('DocTypes');
$default = $doctypes->html5;
$doctypes->html5 = false;
$this->disableHtml5();

$expected = <<<'EOH'
<audio id="test" controls>
Expand All @@ -670,8 +667,7 @@ public function testAudioXHTML(): void

$this->assertSame($expected, $audio);

// Reset
$doctypes->html5 = $default;
$this->enableHtml5();
}

public function testAudioSimple(): void
Expand Down Expand Up @@ -774,15 +770,12 @@ public function testSource(): void

public function testSourceXHTML(): void
{
$doctypes = config('DocTypes');
$default = $doctypes->html5;
$doctypes->html5 = false;
$this->disableHtml5();

$expected = '<source src="http://example.com/index.php/sound.mpeg" type="audio/mpeg" />';
$this->assertSame($expected, source('sound.mpeg', 'audio/mpeg', '', true));

// Reset
$doctypes->html5 = $default;
$this->enableHtml5();
}

public function testEmbed(): void
Expand All @@ -799,9 +792,7 @@ public function testEmbed(): void

public function testEmbedXHTML(): void
{
$doctypes = config('DocTypes');
$default = $doctypes->html5;
$doctypes->html5 = false;
$this->disableHtml5();

$expected = <<<'EOH'
<embed src="http://example.com/movie.mov" type="video/quicktime" class="test" />
Expand All @@ -812,8 +803,7 @@ public function testEmbedXHTML(): void
$embed = embed('movie.mov', $type, 'class="test"');
$this->assertSame($expected, $embed);

// Reset
$doctypes->html5 = $default;
$this->enableHtml5();
}

public function testEmbedIndexed(): void
Expand Down Expand Up @@ -862,9 +852,7 @@ public function testObjectWithParams(): void

public function testObjectWithParamsXHTML(): void
{
$doctypes = config('DocTypes');
$default = $doctypes->html5;
$doctypes->html5 = false;
$this->disableHtml5();

$expected = <<<'EOH'
<object data="http://example.com/movie.swf" class="test">
Expand All @@ -882,8 +870,7 @@ public function testObjectWithParamsXHTML(): void
$object = object('movie.swf', $type, 'class="test"', $parms);
$this->assertSame($expected, $object);

// Reset
$doctypes->html5 = $default;
$this->enableHtml5();
}

public function testObjectIndexed(): void
Expand Down

0 comments on commit 3474bc5

Please sign in to comment.