Commit f61dbad2 authored by Dave Reid's avatar Dave Reid
Browse files

Issue #3260837 by Dave Reid, kevin.pfeifer: Fixed sitemap file generation...

Issue #3260837 by Dave Reid, kevin.pfeifer: Fixed sitemap file generation causes exceptions with the config_readonly module enabled. Added a new XmlSitemap::saveState() method to bypass the save() method when needed.
parent 173ee587
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@
        "ext-xmlwriter": "*"
    },
    "require-dev": {
        "drupal/config_readonly":  "^1.0",
        "drupal/metatag": "^1.0",
        "drupal/robotstxt": "^1.0"
    },
+12 −0
Original line number Diff line number Diff line
@@ -211,4 +211,16 @@ class XmlSitemap extends ConfigEntityBase implements XmlSitemapInterface {
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function saveState(): void {
    \Drupal::state()->set('xmlsitemap.' . $this->id(), [
      'chunks' => $this->getChunks(),
      'links' => $this->getLinks(),
      'max_filesize' => $this->getMaxFileSize(),
      'updated' => $this->getUpdated(),
    ]);
  }

}
+1 −1
Original line number Diff line number Diff line
@@ -457,7 +457,7 @@ class XmlSitemapGenerator implements XmlSitemapGeneratorInterface {
      $context['sandbox']['max'] = $sitemap->getChunks();
      $sitemap->setUpdated($this->time->getRequestTime());
      xmlsitemap_sitemap_get_max_filesize($sitemap);
      xmlsitemap_sitemap_save($sitemap);
      $sitemap->saveState();

      $context['finished'] = 1;
      return;
+5 −0
Original line number Diff line number Diff line
@@ -146,4 +146,9 @@ interface XmlSitemapInterface extends ConfigEntityInterface {
   */
  public static function loadByContext(array $context = NULL);

  /**
   * Save the state information about the sitemap.
   */
  public function saveState(): void;

}
+64 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\xmlsitemap\Kernel;

use Drupal\config_readonly\Exception\ConfigReadonlyStorageException;
use Drupal\Core\Site\Settings;
use Drupal\xmlsitemap\Entity\XmlSitemap;

/**
 * Tests integration with the Configuration Read-only mode module.
 *
 * @group xmlsitemap
 */
class ConfigReadOnlyTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'config_readonly'
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();

    // Turn on config_readonly via settings manually.
    $settings = Settings::getInstance() ? Settings::getAll() : [];
    $settings['config_readonly'] = TRUE;
    new Settings($settings);
  }

  /**
   * Test to make sure config_readonly is working as expected in the test.
   */
  public function testConfigReadOnly() {
    // Prove that saving the config entity results in an exception.
    $sitemap = XmlSitemap::loadByContext();
    $sitemap->setLinks(1);
    $this->expectException(ConfigReadonlyStorageException::class);
    $sitemap->save();
  }

  /**
   * Tests that generating the sitemaps will not throw a config exception.
   */
  public function testSitemapGeneration() {
    $sitemap = XmlSitemap::loadByContext();
    $this->assertNull($sitemap->getLinks());
    $this->assertNull($sitemap->getChunks());
    $this->assertNull($sitemap->getMaxFileSize());

    xmlsitemap_run_unprogressive_batch('xmlsitemap_regenerate_batch');

    // Test that the state was updated correctly after generation.
    $sitemap = Xmlsitemap::load($sitemap->id());
    $this->assertSame(1, $sitemap->getLinks());
    $this->assertSame(1, $sitemap->getChunks());
    $this->assertGreaterThan(0, $sitemap->getMaxFileSize());
  }

}
Loading