[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/_test/tests/Feed/ -> FeedCreatorValidationTest.php (source)

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