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 Roach/** 294a213054SGreg Roach * Common functions for testing views 304a213054SGreg Roach */ 314a213054SGreg Roachabstract class AbstractViewTest extends TestCase 324a213054SGreg Roach{ 334a213054SGreg Roach protected const EVIL_VALUE = '<script>evil()</script>'; 344a213054SGreg Roach 354a213054SGreg Roach /** 364a213054SGreg Roach * Check the view runs without error and generates valid HTML 374a213054SGreg Roach * 384a213054SGreg Roach * @param string $view 39*0acf1b4bSGreg Roach * @param array<string,array<int,mixed>> $data 404a213054SGreg Roach */ 414a213054SGreg Roach protected function doTestView(string $view, array $data): void 424a213054SGreg Roach { 434a213054SGreg Roach foreach ($this->cartesian($data) as $datum) { 444a213054SGreg Roach $html = view($view, $datum); 454a213054SGreg Roach 464a213054SGreg Roach $this->validateHTML($html); 474a213054SGreg Roach } 484a213054SGreg Roach } 494a213054SGreg Roach 504a213054SGreg Roach /** 51*0acf1b4bSGreg Roach * @param array<string,array<int,mixed>> $input 524a213054SGreg Roach * 53*0acf1b4bSGreg Roach * @return array<int,array<string,mixed>> 544a213054SGreg Roach */ 554a213054SGreg Roach private function cartesian(array $input): array 564a213054SGreg Roach { 574a213054SGreg Roach $result = [[]]; 584a213054SGreg Roach 594a213054SGreg Roach foreach ($input as $key => $values) { 604a213054SGreg Roach $append = []; 614a213054SGreg Roach 624a213054SGreg Roach foreach ($result as $product) { 634a213054SGreg Roach foreach ($values as $item) { 644a213054SGreg Roach $product[$key] = $item; 654a213054SGreg Roach $append[] = $product; 664a213054SGreg Roach } 674a213054SGreg Roach } 684a213054SGreg Roach 694a213054SGreg Roach $result = $append; 704a213054SGreg Roach } 714a213054SGreg Roach 724a213054SGreg Roach return $result; 734a213054SGreg Roach } 744a213054SGreg Roach 754a213054SGreg Roach /** 764a213054SGreg Roach * @param string $html 774a213054SGreg Roach */ 7873d58381SGreg Roach protected function validateHTML(string $html): void 794a213054SGreg Roach { 804a213054SGreg Roach if (str_starts_with($html, '<!DOCTYPE html>')) { 814a213054SGreg Roach $xml = $html; 824a213054SGreg Roach } else { 834a213054SGreg Roach $xml = '<!DOCTYPE html><html lang="en"><body>' . $html . '</body></html>'; 844a213054SGreg Roach } 854a213054SGreg Roach 864a213054SGreg Roach $doc = new DOMDocument(); 874a213054SGreg Roach $doc->validateOnParse = true; 884a213054SGreg Roach 894a213054SGreg Roach self::assertTrue($doc->loadXML($xml, LIBXML_PEDANTIC), $html); 904a213054SGreg Roach } 914a213054SGreg Roach} 92