xref: /dokuwiki/_test/tests/Feed/FeedCreatorValidationTest.php (revision 2d1b030690419760067aa1b583ec928afef0f5ab)
1*2d1b0306SAndreas Gohr<?php
2*2d1b0306SAndreas Gohr
3*2d1b0306SAndreas Gohrnamespace dokuwiki\test\Feed;
4*2d1b0306SAndreas Gohr
5*2d1b0306SAndreas Gohruse dokuwiki\Feed\FeedCreator;
6*2d1b0306SAndreas Gohruse dokuwiki\Feed\FeedCreatorOptions;
7*2d1b0306SAndreas Gohruse dokuwiki\HTTP\DokuHTTPClient;
8*2d1b0306SAndreas Gohr
9*2d1b0306SAndreas Gohr/**
10*2d1b0306SAndreas Gohr * @group internet
11*2d1b0306SAndreas Gohr */
12*2d1b0306SAndreas Gohrclass FeedCreatorTest extends \DokuWikiTest
13*2d1b0306SAndreas Gohr{
14*2d1b0306SAndreas Gohr
15*2d1b0306SAndreas Gohr    /**
16*2d1b0306SAndreas Gohr     * @todo This only test the default feed, various configurations could be tested
17*2d1b0306SAndreas Gohr     *
18*2d1b0306SAndreas Gohr     * @return void
19*2d1b0306SAndreas Gohr     * @throws \Exception
20*2d1b0306SAndreas Gohr     */
21*2d1b0306SAndreas Gohr    public function testValidate()
22*2d1b0306SAndreas Gohr    {
23*2d1b0306SAndreas Gohr
24*2d1b0306SAndreas Gohr        $options = new FeedCreatorOptions();
25*2d1b0306SAndreas Gohr        $creator = new FeedCreator($options);
26*2d1b0306SAndreas Gohr        $feed = $creator->build();
27*2d1b0306SAndreas Gohr
28*2d1b0306SAndreas Gohr        $http = new DokuHTTPClient();
29*2d1b0306SAndreas Gohr        $result = $http->post('https://validator.w3.org/feed/check.cgi', [
30*2d1b0306SAndreas Gohr            'rawdata' => $feed,
31*2d1b0306SAndreas Gohr            'output' => 'soap12',
32*2d1b0306SAndreas Gohr        ]);
33*2d1b0306SAndreas Gohr
34*2d1b0306SAndreas Gohr        if (!$result) {
35*2d1b0306SAndreas Gohr            $this->markTestSkipped('Could not validate feed');
36*2d1b0306SAndreas Gohr        }
37*2d1b0306SAndreas Gohr        //print($result);
38*2d1b0306SAndreas Gohr
39*2d1b0306SAndreas Gohr        $xml = new \SimpleXMLElement($result);
40*2d1b0306SAndreas Gohr        $ns = $xml->getNamespaces(true);
41*2d1b0306SAndreas Gohr        foreach ($ns as $key => $value) {
42*2d1b0306SAndreas Gohr            $xml->registerXPathNamespace($key, $value);
43*2d1b0306SAndreas Gohr        }
44*2d1b0306SAndreas Gohr
45*2d1b0306SAndreas Gohr        $warningCount = (int)$xml->xpath('//m:warnings/m:warningcount')[0];
46*2d1b0306SAndreas Gohr        if ($warningCount > 0) {
47*2d1b0306SAndreas Gohr            $line = (int)$xml->xpath('//m:warnings/m:warninglist/warning/line')[0];
48*2d1b0306SAndreas Gohr            $text = (string)$xml->xpath('//m:warnings/m:warninglist/warning/text')[0];
49*2d1b0306SAndreas Gohr            $element = (string)$xml->xpath('//m:warnings/m:warninglist/warning/element')[0];
50*2d1b0306SAndreas Gohr            $parent = (string)$xml->xpath('//m:warnings/m:warninglist/warning/parent')[0];
51*2d1b0306SAndreas Gohr
52*2d1b0306SAndreas Gohr            $lines = explode("\n", $feed);
53*2d1b0306SAndreas Gohr            $show = trim($lines[$line - 1]);
54*2d1b0306SAndreas Gohr
55*2d1b0306SAndreas Gohr            $this->addWarning(
56*2d1b0306SAndreas Gohr                "Feed validation produced a warning:\n" .
57*2d1b0306SAndreas Gohr                "Line $line: $text\n" .
58*2d1b0306SAndreas Gohr                "$parent -> $element\n" .
59*2d1b0306SAndreas Gohr                $show
60*2d1b0306SAndreas Gohr            );
61*2d1b0306SAndreas Gohr        }
62*2d1b0306SAndreas Gohr
63*2d1b0306SAndreas Gohr        $errorCount = (int)$xml->xpath('//m:errors/m:errorcount')[0];
64*2d1b0306SAndreas Gohr        if ($errorCount > 0) {
65*2d1b0306SAndreas Gohr            $line = (int)$xml->xpath('//m:errors/m:errorlist/error/line')[0];
66*2d1b0306SAndreas Gohr            $text = (string)$xml->xpath('//m:errors/m:errorlist/error/text')[0];
67*2d1b0306SAndreas Gohr            $element = (string)$xml->xpath('//m:errors/m:errorlist/error/element')[0];
68*2d1b0306SAndreas Gohr            $parent = (string)$xml->xpath('//m:errors/m:errorlist/error/parent')[0];
69*2d1b0306SAndreas Gohr
70*2d1b0306SAndreas Gohr            $lines = explode("\n", $feed);
71*2d1b0306SAndreas Gohr            $show = trim($lines[$line - 1]);
72*2d1b0306SAndreas Gohr
73*2d1b0306SAndreas Gohr            $this->fail(
74*2d1b0306SAndreas Gohr                "Feed validation produced an error:\n" .
75*2d1b0306SAndreas Gohr                "Line $line: $text\n" .
76*2d1b0306SAndreas Gohr                "$parent -> $element\n" .
77*2d1b0306SAndreas Gohr                $show
78*2d1b0306SAndreas Gohr            );
79*2d1b0306SAndreas Gohr        }
80*2d1b0306SAndreas Gohr
81*2d1b0306SAndreas Gohr        $this->assertTrue(true);
82*2d1b0306SAndreas Gohr    }
83*2d1b0306SAndreas Gohr
84*2d1b0306SAndreas Gohr
85*2d1b0306SAndreas Gohr}
86