diff options
| author | Greg Roach <greg@subaqua.co.uk> | 2021-02-19 11:02:04 +0000 |
|---|---|---|
| committer | Greg Roach <greg@subaqua.co.uk> | 2021-02-20 08:52:19 +0000 |
| commit | 4a213054c13c5caf2ed7b812a0d260527e167ef6 (patch) | |
| tree | 9f525f579db4188b79864c3eb27e5b84baeb1752 /tests | |
| parent | ac6993c3f1c434c393e586a8723beb315514b0ec (diff) | |
| download | webtrees-4a213054c13c5caf2ed7b812a0d260527e167ef6.tar.gz webtrees-4a213054c13c5caf2ed7b812a0d260527e167ef6.tar.bz2 webtrees-4a213054c13c5caf2ed7b812a0d260527e167ef6.zip | |
HTML validation
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/views/AbstractViewTest.php | 98 | ||||
| -rw-r--r-- | tests/views/VerifySuccessPageTest.php | 36 |
2 files changed, 134 insertions, 0 deletions
diff --git a/tests/views/AbstractViewTest.php b/tests/views/AbstractViewTest.php new file mode 100644 index 0000000000..2994f9e5cd --- /dev/null +++ b/tests/views/AbstractViewTest.php @@ -0,0 +1,98 @@ +<?php + +/** + * webtrees: online genealogy + * Copyright (C) 2021 webtrees development team + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +declare(strict_types=1); + +namespace Fisharebest\Webtrees; + +use DOMDocument; +use Exception; + +use function str_starts_with; +use function trim; + +use const LIBXML_PEDANTIC; + +/** + * Common functions for testing views + */ +abstract class AbstractViewTest extends TestCase +{ + protected const EVIL_VALUE = '<script>evil()</script>'; + + /** + * Check the view runs without error and generates valid HTML + * + * @param string $view + * @param array<array<string,array<string,mixed>> $data + */ + protected function doTestView(string $view, array $data): void + { + foreach ($this->cartesian($data) as $datum) { + $html = view($view, $datum); + + $this->validateHTML($html); + } + } + + /** + * @param array<string,array<string,mixed>> $input + * + * @return array<array<string,array<string,mixed>> + */ + private function cartesian(array $input): array + { + $result = [[]]; + + foreach ($input as $key => $values) { + $append = []; + + foreach ($result as $product) { + foreach ($values as $item) { + $product[$key] = $item; + $append[] = $product; + } + } + + $result = $append; + } + + return $result; + } + + /** + * @param string $html + */ + private function validateHTML(string $html): void + { + if (str_starts_with($html, '<!DOCTYPE html>')) { + $xml = $html; + } else { + $xml = '<!DOCTYPE html><html lang="en"><body>' . $html . '</body></html>'; + } + + $doc = new DOMDocument(); + $doc->validateOnParse = true; + + try { + self::assertTrue($doc->loadXML($xml, LIBXML_PEDANTIC), $html); + } catch (Exception $ex) { + echo $html, PHP_EOL; + self::assertTrue(false); + } + } +} diff --git a/tests/views/VerifySuccessPageTest.php b/tests/views/VerifySuccessPageTest.php new file mode 100644 index 0000000000..db8fcc2520 --- /dev/null +++ b/tests/views/VerifySuccessPageTest.php @@ -0,0 +1,36 @@ +<?php + +/** + * webtrees: online genealogy + * Copyright (C) 2021 webtrees development team + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +declare(strict_types=1); + +namespace Fisharebest\Webtrees; + +/** + * Test harness for the view verify-success-page + */ +class VerifySuccessPageTest extends AbstractViewTest +{ + /** + * Standard tests for all elements. + */ + public function testView(): void + { + $this->doTestView('verify-success-page', [ + 'title' => ['title'] + ]); + } +} |
