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