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