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