xref: /webtrees/tests/views/AbstractViewTest.php (revision b11cdcd45131b1585d66693fab363cfeb18c51a4)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * 'Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees;
21
22use DOMDocument;
23
24use function str_starts_with;
25
26use const LIBXML_PEDANTIC;
27
28/**
29 * Common functions for testing views
30 */
31abstract class AbstractViewTest extends TestCase
32{
33    protected const EVIL_VALUE = '<script>evil()</script>';
34
35    /**
36     * Check the view runs without error and generates valid HTML
37     *
38     * @param string $view
39     * @param array<array<string,array<string,mixed>>>  $data
40     */
41    protected function doTestView(string $view, array $data): void
42    {
43        foreach ($this->cartesian($data) as $datum) {
44            $html = view($view, $datum);
45
46            $this->validateHTML($html);
47        }
48    }
49
50    /**
51     * @param array<string,array<string,mixed>> $input
52     *
53     * @return array<array<string,array<string,mixed>>>
54     */
55    private function cartesian(array $input): array
56    {
57        $result = [[]];
58
59        foreach ($input as $key => $values) {
60            $append = [];
61
62            foreach ($result as $product) {
63                foreach ($values as $item) {
64                    $product[$key] = $item;
65                    $append[]      = $product;
66                }
67            }
68
69            $result = $append;
70        }
71
72        return $result;
73    }
74
75    /**
76     * @param string $html
77     */
78    protected function validateHTML(string $html): void
79    {
80        if (str_starts_with($html, '<!DOCTYPE html>')) {
81            $xml = $html;
82        } else {
83            $xml = '<!DOCTYPE html><html lang="en"><body>' . $html . '</body></html>';
84        }
85
86        $doc = new DOMDocument();
87        $doc->validateOnParse = true;
88
89        self::assertTrue($doc->loadXML($xml, LIBXML_PEDANTIC), $html);
90    }
91}
92