12d1b0306SAndreas Gohr<?php 22d1b0306SAndreas Gohr 32d1b0306SAndreas Gohrnamespace dokuwiki\test\Feed; 42d1b0306SAndreas Gohr 52d1b0306SAndreas Gohruse dokuwiki\Feed\FeedCreator; 62d1b0306SAndreas Gohruse dokuwiki\Feed\FeedCreatorOptions; 72d1b0306SAndreas Gohruse dokuwiki\HTTP\DokuHTTPClient; 82d1b0306SAndreas Gohr 92d1b0306SAndreas Gohr/** 102d1b0306SAndreas Gohr * @group internet 112d1b0306SAndreas Gohr */ 12*da1e6c6bSAndreas Gohrclass FeedCreatorValidationTest extends \DokuWikiTest 132d1b0306SAndreas Gohr{ 142d1b0306SAndreas Gohr 152d1b0306SAndreas Gohr /** 162d1b0306SAndreas Gohr * @todo This only test the default feed, various configurations could be tested 172d1b0306SAndreas Gohr * 182d1b0306SAndreas Gohr * @return void 192d1b0306SAndreas Gohr * @throws \Exception 202d1b0306SAndreas Gohr */ 212d1b0306SAndreas Gohr public function testValidate() 222d1b0306SAndreas Gohr { 232d1b0306SAndreas Gohr 242d1b0306SAndreas Gohr $options = new FeedCreatorOptions(); 252d1b0306SAndreas Gohr $creator = new FeedCreator($options); 262d1b0306SAndreas Gohr $feed = $creator->build(); 272d1b0306SAndreas Gohr 282d1b0306SAndreas Gohr $http = new DokuHTTPClient(); 292d1b0306SAndreas Gohr $result = $http->post('https://validator.w3.org/feed/check.cgi', [ 302d1b0306SAndreas Gohr 'rawdata' => $feed, 312d1b0306SAndreas Gohr 'output' => 'soap12', 322d1b0306SAndreas Gohr ]); 332d1b0306SAndreas Gohr 342d1b0306SAndreas Gohr if (!$result) { 352d1b0306SAndreas Gohr $this->markTestSkipped('Could not validate feed'); 362d1b0306SAndreas Gohr } 372d1b0306SAndreas Gohr //print($result); 382d1b0306SAndreas Gohr 392d1b0306SAndreas Gohr $xml = new \SimpleXMLElement($result); 402d1b0306SAndreas Gohr $ns = $xml->getNamespaces(true); 412d1b0306SAndreas Gohr foreach ($ns as $key => $value) { 422d1b0306SAndreas Gohr $xml->registerXPathNamespace($key, $value); 432d1b0306SAndreas Gohr } 442d1b0306SAndreas Gohr 452d1b0306SAndreas Gohr $warningCount = (int)$xml->xpath('//m:warnings/m:warningcount')[0]; 462d1b0306SAndreas Gohr if ($warningCount > 0) { 472d1b0306SAndreas Gohr $line = (int)$xml->xpath('//m:warnings/m:warninglist/warning/line')[0]; 482d1b0306SAndreas Gohr $text = (string)$xml->xpath('//m:warnings/m:warninglist/warning/text')[0]; 492d1b0306SAndreas Gohr $element = (string)$xml->xpath('//m:warnings/m:warninglist/warning/element')[0]; 502d1b0306SAndreas Gohr $parent = (string)$xml->xpath('//m:warnings/m:warninglist/warning/parent')[0]; 512d1b0306SAndreas Gohr 522d1b0306SAndreas Gohr $lines = explode("\n", $feed); 532d1b0306SAndreas Gohr $show = trim($lines[$line - 1]); 542d1b0306SAndreas Gohr 552d1b0306SAndreas Gohr $this->addWarning( 562d1b0306SAndreas Gohr "Feed validation produced a warning:\n" . 572d1b0306SAndreas Gohr "Line $line: $text\n" . 582d1b0306SAndreas Gohr "$parent -> $element\n" . 592d1b0306SAndreas Gohr $show 602d1b0306SAndreas Gohr ); 612d1b0306SAndreas Gohr } 622d1b0306SAndreas Gohr 632d1b0306SAndreas Gohr $errorCount = (int)$xml->xpath('//m:errors/m:errorcount')[0]; 642d1b0306SAndreas Gohr if ($errorCount > 0) { 652d1b0306SAndreas Gohr $line = (int)$xml->xpath('//m:errors/m:errorlist/error/line')[0]; 662d1b0306SAndreas Gohr $text = (string)$xml->xpath('//m:errors/m:errorlist/error/text')[0]; 672d1b0306SAndreas Gohr $element = (string)$xml->xpath('//m:errors/m:errorlist/error/element')[0]; 682d1b0306SAndreas Gohr $parent = (string)$xml->xpath('//m:errors/m:errorlist/error/parent')[0]; 692d1b0306SAndreas Gohr 702d1b0306SAndreas Gohr $lines = explode("\n", $feed); 712d1b0306SAndreas Gohr $show = trim($lines[$line - 1]); 722d1b0306SAndreas Gohr 732d1b0306SAndreas Gohr $this->fail( 742d1b0306SAndreas Gohr "Feed validation produced an error:\n" . 752d1b0306SAndreas Gohr "Line $line: $text\n" . 762d1b0306SAndreas Gohr "$parent -> $element\n" . 772d1b0306SAndreas Gohr $show 782d1b0306SAndreas Gohr ); 792d1b0306SAndreas Gohr } 802d1b0306SAndreas Gohr 812d1b0306SAndreas Gohr $this->assertTrue(true); 822d1b0306SAndreas Gohr } 832d1b0306SAndreas Gohr 842d1b0306SAndreas Gohr 852d1b0306SAndreas Gohr} 86