Skip to content

Commit

Permalink
Add more test cases when asserting HTML5 Doctype declaration and upda…
Browse files Browse the repository at this point in the history
…ted README
  • Loading branch information
marcortola committed Aug 19, 2019
1 parent b015e74 commit 8185792
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 46 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Then /^(\d+) random sitemap URLs? should be alive$/
##### HTMLContext
```gherkin
Then the page HTML markup should be valid
Then /^the page HTML5 doctype declaration should (not |)be valid$
```
##### PerformanceContext
```gherkin
Expand Down
50 changes: 16 additions & 34 deletions src/Context/HTMLContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,30 @@ public function thePageHtmlMarkupShouldBeValid(): void
}

/**
* @Then the page HTML markup should not be valid
* @Then the page HTML5 doctype declaration should be valid
*/
public function thePageHtmlMarkupShouldNotBeValid(): void
public function thePageHtml5DoctypeDeclarationShouldBeValid(): void
{
$this->assertInverse(
[$this, 'thePageHtmlMarkupShouldBeValid'],
'HTML markup should not be valid.'
$pageContent = preg_replace(
'/<!--(.|\s)*?-->/',
'',
$this->getSession()->getPage()->getContent()
);

Assert::startsWith(
strtolower(trim($pageContent ?? '')),
'<!doctype html>'
);
}

/**
* @Then the page HTML5 doctype declaration should be valid
* @Then the page HTML markup should not be valid
*/
public function thePageHtml5DoctypeDeclarationShouldBeValid(): void
public function thePageHtmlMarkupShouldNotBeValid(): void
{
$html5DoctypeMarkup = '<!doctype html>';

Assert::eq(
$html5DoctypeMarkup,
$this->pageDoctypeMarkup($html5DoctypeMarkup)
$this->assertInverse(
[$this, 'thePageHtmlMarkupShouldBeValid'],
'HTML markup should not be valid.'
);
}

Expand All @@ -88,26 +92,4 @@ public function thePageHtml5DoctypeDeclarationShouldNotBeValid(): void
'The page HTML5 doctype declaration should not be valid.'
);
}

private function pageDoctypeMarkup(string $htmlDoctypeMarkup): string
{
$pageContent = $this->removeCodeBeforePageDoctypeMarkup(
$this->getSession()->getPage()->getContent(),
$htmlDoctypeMarkup
);

$pageContentLines = explode(PHP_EOL, $pageContent);
return trim(strtolower($pageContentLines[0]));
}

private function removeCodeBeforePageDoctypeMarkup(string $pageContent, string $htmlDoctypeMarkup): string
{
$htmlDoctypeMarkupPositionInPageContent = stripos($pageContent, $htmlDoctypeMarkup);

if ($htmlDoctypeMarkupPositionInPageContent != false) {
$pageContent = substr($pageContent, $htmlDoctypeMarkupPositionInPageContent);
}

return $pageContent;
}
}
5 changes: 4 additions & 1 deletion tests/features/html.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ Feature: HTML feature

When I am on "/html/not-valid-html.html"
Then the page HTML markup should not be valid

When I am on "/html/valid-html5-doctype-declaration.html"
Then the page HTML5 doctype declaration should be valid

When I am on "/html/valid-html4-doctype-declaration.html"
Then the page HTML5 doctype declaration should not be valid

When I am on "/html/not-valid-html5-doctype-declaration.html"
Then the page HTML5 doctype declaration should not be valid
22 changes: 12 additions & 10 deletions tests/fixtures/web/html/not-valid-html5-doctype-declaration.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta charset="utf-8">
<title>Invalid HTML5 Doctype declaration</title>
</head>
<body>
<p>Invalid HTML5 Doctype declaration</p>
</body>
</html>
<title>Wrong title</title>

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Valid HTML5 Doctype declaration</title>
</head>
<body>
<p>Valid HTML5 Doctype declaration</p>
</body>
</html>
10 changes: 10 additions & 0 deletions tests/fixtures/web/html/valid-html4-doctype-declaration.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta charset="utf-8">
<title>Invalid HTML5 Doctype declaration</title>
</head>
<body>
<p>Invalid HTML5 Doctype declaration</p>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
<body>
<p>Valid HTML5 Doctype declaration</p>
</body>
</html>
</html>

0 comments on commit 8185792

Please sign in to comment.