14a213054SGreg Roach<?php 24a213054SGreg Roach 34a213054SGreg Roach/** 44a213054SGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 64a213054SGreg Roach * This program is free software: you can redistribute it and/or modify 74a213054SGreg Roach * it under the terms of the GNU General Public License as published by 84a213054SGreg Roach * the Free Software Foundation, either version 3 of the License, or 94a213054SGreg Roach * (at your option) any later version. 104a213054SGreg Roach * This program is distributed in the hope that it will be useful, 114a213054SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 124a213054SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 134a213054SGreg Roach * GNU General Public License for more details. 144a213054SGreg Roach * You should have received a copy of the GNU General Public License 154a213054SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 164a213054SGreg Roach */ 174a213054SGreg Roach 184a213054SGreg Roachdeclare(strict_types=1); 194a213054SGreg Roach 204a213054SGreg Roachnamespace Fisharebest\Webtrees; 214a213054SGreg Roach 224a213054SGreg Roachuse DOMDocument; 234a213054SGreg Roach 244a213054SGreg Roachuse function str_starts_with; 254a213054SGreg Roach 264a213054SGreg Roachuse const LIBXML_PEDANTIC; 274a213054SGreg Roach 284a213054SGreg Roachabstract class AbstractViewTest extends TestCase 294a213054SGreg Roach{ 304a213054SGreg Roach protected const EVIL_VALUE = '<script>evil()</script>'; 314a213054SGreg Roach 324a213054SGreg Roach /** 334a213054SGreg Roach * Check the view runs without error and generates valid HTML 344a213054SGreg Roach * 350acf1b4bSGreg Roach * @param array<string,array<int,mixed>> $data 364a213054SGreg Roach */ 374a213054SGreg Roach protected function doTestView(string $view, array $data): void 384a213054SGreg Roach { 394a213054SGreg Roach foreach ($this->cartesian($data) as $datum) { 404a213054SGreg Roach $html = view($view, $datum); 414a213054SGreg Roach 424a213054SGreg Roach $this->validateHTML($html); 434a213054SGreg Roach } 444a213054SGreg Roach } 454a213054SGreg Roach 464a213054SGreg Roach /** 470acf1b4bSGreg Roach * @param array<string,array<int,mixed>> $input 484a213054SGreg Roach * 490acf1b4bSGreg Roach * @return array<int,array<string,mixed>> 504a213054SGreg Roach */ 514a213054SGreg Roach private function cartesian(array $input): array 524a213054SGreg Roach { 534a213054SGreg Roach $result = [[]]; 544a213054SGreg Roach 554a213054SGreg Roach foreach ($input as $key => $values) { 564a213054SGreg Roach $append = []; 574a213054SGreg Roach 584a213054SGreg Roach foreach ($result as $product) { 594a213054SGreg Roach foreach ($values as $item) { 604a213054SGreg Roach $product[$key] = $item; 614a213054SGreg Roach $append[] = $product; 624a213054SGreg Roach } 634a213054SGreg Roach } 644a213054SGreg Roach 654a213054SGreg Roach $result = $append; 664a213054SGreg Roach } 674a213054SGreg Roach 684a213054SGreg Roach return $result; 694a213054SGreg Roach } 704a213054SGreg Roach 71*a6d49169SGreg Roach protected function validateHtml(string $html): void 724a213054SGreg Roach { 734a213054SGreg Roach if (str_starts_with($html, '<!DOCTYPE html>')) { 744a213054SGreg Roach $xml = $html; 754a213054SGreg Roach } else { 764a213054SGreg Roach $xml = '<!DOCTYPE html><html lang="en"><body>' . $html . '</body></html>'; 774a213054SGreg Roach } 784a213054SGreg Roach 794a213054SGreg Roach $doc = new DOMDocument(); 804a213054SGreg Roach $doc->validateOnParse = true; 814a213054SGreg Roach 824a213054SGreg Roach self::assertTrue($doc->loadXML($xml, LIBXML_PEDANTIC), $html); 834a213054SGreg Roach } 844a213054SGreg Roach} 85