You are here

public function AggregatorTestBase::getValidOpml in Drupal 8

Same name in this branch
  1. 8 core/modules/aggregator/src/Tests/AggregatorTestBase.php \Drupal\aggregator\Tests\AggregatorTestBase::getValidOpml()
  2. 8 core/modules/aggregator/tests/src/Functional/AggregatorTestBase.php \Drupal\Tests\aggregator\Functional\AggregatorTestBase::getValidOpml()
Same name and namespace in other branches
  1. 9 core/modules/aggregator/tests/src/Functional/AggregatorTestBase.php \Drupal\Tests\aggregator\Functional\AggregatorTestBase::getValidOpml()

Creates a valid OPML file from an array of feeds.

Parameters

array $feeds: An array of feeds.

Return value

string Path to valid OPML file.

1 call to AggregatorTestBase::getValidOpml()
ImportOpmlTest::submitImportForm in core/modules/aggregator/tests/src/Functional/ImportOpmlTest.php
Submits form with invalid, empty, and valid OPML files.

File

core/modules/aggregator/tests/src/Functional/AggregatorTestBase.php, line 263

Class

AggregatorTestBase
Defines a base class for testing the Aggregator module.

Namespace

Drupal\Tests\aggregator\Functional

Code

public function getValidOpml(array $feeds) {

  // Properly escape URLs so that XML parsers don't choke on them.
  foreach ($feeds as &$feed) {
    $feed['url[0][value]'] = Html::escape($feed['url[0][value]']);
  }

  /**
   * Does not have an XML declaration, must pass the parser.
   */
  $opml = <<<EOF
<opml version="1.0">
  <head></head>
  <body>
    <!-- First feed to be imported. -->
    <outline text="{<span class="php-variable">$feeds</span>[<span class="php-constant">0</span>][<span class="php-string">'title[0][value]'</span>]}" xmlurl="{<span class="php-variable">$feeds</span>[<span class="php-constant">0</span>][<span class="php-string">'url[0][value]'</span>]}" />

    <!-- Second feed. Test string delimitation and attribute order. -->
    <outline xmlurl='{<span class="php-variable">$feeds</span>[<span class="php-constant">1</span>][<span class="php-string">'url[0][value]'</span>]}' text='{<span class="php-variable">$feeds</span>[<span class="php-constant">1</span>][<span class="php-string">'title[0][value]'</span>]}'/>

    <!-- Test for duplicate URL and title. -->
    <outline xmlurl="{<span class="php-variable">$feeds</span>[<span class="php-constant">0</span>][<span class="php-string">'url[0][value]'</span>]}" text="Duplicate URL"/>
    <outline xmlurl="http://duplicate.title" text="{<span class="php-variable">$feeds</span>[<span class="php-constant">1</span>][<span class="php-string">'title[0][value]'</span>]}"/>

    <!-- Test that feeds are only added with required attributes. -->
    <outline text="{<span class="php-variable">$feeds</span>[<span class="php-constant">2</span>][<span class="php-string">'title[0][value]'</span>]}" />
    <outline xmlurl="{<span class="php-variable">$feeds</span>[<span class="php-constant">2</span>][<span class="php-string">'url[0][value]'</span>]}" />
  </body>
</opml>
EOF;
  $path = 'public://valid-opml.xml';

  // Add the UTF-8 byte order mark.
  return \Drupal::service('file_system')
    ->saveData(chr(239) . chr(187) . chr(191) . $opml, $path);
}