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