You are here

private function SitemapWarmerTest::generateSitemaps in Warmer 2.x

Same name and namespace in other branches
  1. 8 modules/warmer_cdn/tests/src/Functional/SitemapWarmerTest.php \Drupal\Tests\warmer_cdn\Functional\SitemapWarmerTest::generateSitemaps()

Generates the sitemaps.

This will generate a sitemap like:


<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
  <url><loc>/lorem</loc><changefreq>monthly</changefreq><priority>0.8</priority></url>
  <url><loc>/ipsum</loc><changefreq>monthly</changefreq><priority>0.8</priority></url>
  <url><loc>/dolor</loc><changefreq>monthly</changefreq><priority>0.3</priority></url>
  <url><changefreq>monthly</changefreq><priority>0.3</priority></url>
  <url><loc>/sit</loc></url>
</urlset>

Where only 2 of the items are valid and have priority higher than 0.7.

Parameters

\Drupal\node\NodeInterface[] $nodes: The nodes to add to the sitemap.

Return value

\Drupal\file\Entity\File The file entity behind the sitemap.

Throws

\Drupal\Core\Entity\EntityStorageException

\Drupal\Core\Entity\EntityMalformedException

1 call to SitemapWarmerTest::generateSitemaps()
SitemapWarmerTest::setUp in modules/warmer_cdn/tests/src/Functional/SitemapWarmerTest.php

File

modules/warmer_cdn/tests/src/Functional/SitemapWarmerTest.php, line 137

Class

SitemapWarmerTest
Tests for the sitemap warmer.

Namespace

Drupal\Tests\warmer_cdn\Functional

Code

private function generateSitemaps(array $nodes) {
  $xml = new \DOMDocument('1.0', 'utf-8');
  $urlset = $xml
    ->createElement('urlset');
  $urlset
    ->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
  $urlset
    ->setAttribute('xmlns:xhtml', 'http://www.w3.org/1999/xhtml');
  $index = 1;
  foreach ($nodes as $alias => $node) {
    assert($node instanceof NodeInterface);
    $url = $xml
      ->createElement('url');
    if ($index !== 4) {
      $loc = $node
        ->toUrl('canonical', [
        'absolute' => TRUE,
      ])
        ->toString();
      $url
        ->appendChild($xml
        ->createElement('loc', $loc));
    }
    $url
      ->appendChild($xml
      ->createElement('changefreq', 'monthly'));
    if ($index !== 5) {
      $priority = $index === 3 ? '0.2' : '0.8';
      $url
        ->appendChild($xml
        ->createElement('priority', $priority));
    }
    $urlset
      ->appendChild($url);
    $index++;
  }
  $xml
    ->appendChild($urlset);
  $fs = \Drupal::service('file_system');
  assert($fs instanceof FileSystem);
  mkdir('public://sitemaps');
  $filename = 'public://sitemaps/0.xml';
  file_put_contents($fs
    ->realpath($filename), $xml
    ->saveXML());
  $file = File::create([
    'uri' => $filename,
  ]);
  $file
    ->setOwnerId($this->adminUser
    ->id());
  $file
    ->setPermanent();
  $file
    ->save();
  $xml = new \DOMDocument('1.0', 'utf-8');
  $sitemapindex = $xml
    ->createElement('sitemapindex');
  $sitemap = $xml
    ->createElement('sitemap');
  $sitemap
    ->appendChild($xml
    ->createElement('loc', $file
    ->createFileUrl(FALSE)));
  $sitemapindex
    ->appendChild($sitemap);
  $xml
    ->appendChild($sitemapindex);
  $filename = 'public://sitemaps/sitemap.xml';
  file_put_contents($fs
    ->realpath($filename), $xml
    ->saveXML());
  $file = File::create([
    'uri' => $filename,
  ]);
  $file
    ->setOwnerId($this->adminUser
    ->id());
  $file
    ->setPermanent();
  $file
    ->save();
  return $file;
}