1<?php 2 3/** 4 * Check some page output for validity 5 * 6 * @group internet 7 */ 8class general_html_test extends DokuWikiTest 9{ 10 11 /** 12 * List of requests to check for validity 13 * 14 * @return array 15 */ 16 public function requestProvider() 17 { 18 return [ 19 ['/doku.php', 'GET', []], 20 ['/doku.php', 'GET', ['do' => 'recent']], 21 ['/doku.php', 'GET', ['do' => 'index']], 22 ['/doku.php', 'GET', ['do' => 'login']], 23 ['/doku.php', 'GET', ['do' => 'search', 'q' => 'wiki']], 24 ['/doku.php', 'GET', ['id' => 'wiki:syntax']], 25 ['/doku.php', 'GET', ['id' => 'wiki:syntax', 'ns' => 'wiki', 'image' => 'wiki:dokuwiki-128.png', 'do' => 'media']], 26 ['/lib/exe/detail.php', 'GET', ['id' => 'wiki:syntax', 'media' => 'wiki:dokuwiki-128.png']], 27 ]; 28 } 29 30 /** 31 * Sends the given HTML to the validator and returns the result 32 * 33 * @param string $html 34 * @return array 35 * @throws Exception when communication failed 36 */ 37 protected function validate($html) 38 { 39 $http = new HTTPClient(); 40 $http->headers['Content-Type'] = 'text/html; charset=utf-8'; 41 $result = $http->post('https://validator.w3.org/nu/?out=json&level=error', $html); 42 43 if ($result === false) { 44 throw new \Exception($http->error); 45 } 46 47 $result = json_decode($result, true); 48 if ($result === null) { 49 throw new \Exception('could not decode JSON'); 50 } 51 52 return $result; 53 } 54 55 /** 56 * Reformat the errors for nicer display in output 57 * 58 * @param array $result 59 * @return string[] 60 */ 61 protected function listErrors($result) 62 { 63 $errors = []; 64 foreach ($result['messages'] as $msg) { 65 $errors[] = "☛ " . $msg['message'] . "\n" . $msg['extract'] . "\n"; 66 } 67 return $errors; 68 } 69 70 71 /** 72 * @dataProvider requestProvider 73 * @param string $url 74 * @param string $method 75 * @param array $data 76 * @group internet 77 */ 78 public function test_Validity($url, $method, $data) 79 { 80 $request = new TestRequest(); 81 if ($method == 'GET') { 82 $response = $request->get($data, $url); 83 } elseif ($method == 'POST') { 84 $response = $request->post($data, $url); 85 } else { 86 throw new \RuntimeException("unknown method given: $method"); 87 } 88 89 $html = $response->getContent(); 90 try { 91 $result = $this->validate($html); 92 } catch (\Exception $e) { 93 $this->markTestSkipped($e->getMessage()); 94 return; 95 } 96 97 $errors = $this->listErrors($result); 98 $info = "Invalid HTML found:\n" . join("\n", $errors); 99 100 $this->assertEquals(0, count($errors), $info); 101 } 102} 103