You are here

public function DirectoryTest::testClearDirectory in XML sitemap 2.x

Same name and namespace in other branches
  1. 8 tests/src/Kernel/DirectoryTest.php \Drupal\Tests\xmlsitemap\Kernel\DirectoryTest::testClearDirectory()

Test xmlsitemap_clear_directory().

@covers ::xmlsitemap_get_directory @covers ::xmlsitemap_clear_directory @covers ::_xmlsitemap_delete_recursive

File

tests/src/Kernel/DirectoryTest.php, line 19

Class

DirectoryTest
Tests directory functions.

Namespace

Drupal\Tests\xmlsitemap\Kernel

Code

public function testClearDirectory() {

  /** @var \Drupal\Core\File\FileSystemInterface $fileSystem */
  $fileSystem = $this->container
    ->get('file_system');

  // Set up a couple more directories and files.
  $directory = 'public://not-xmlsitemap';
  $fileSystem
    ->prepareDirectory($directory, $fileSystem::CREATE_DIRECTORY | $fileSystem::MODIFY_PERMISSIONS);
  $directory = 'public://xmlsitemap/test';
  $fileSystem
    ->prepareDirectory($directory, $fileSystem::CREATE_DIRECTORY | $fileSystem::MODIFY_PERMISSIONS);
  $fileSystem
    ->saveData('File unrelated to XML sitemap', 'public://not-xmlsitemap/file.txt');
  $fileSystem
    ->saveData('File unrelated to XML sitemap', 'public://file.txt');
  $fileSystem
    ->saveData('Test contents', 'public://xmlsitemap/test/index.xml');

  // Test that only the xmlsitemap directory was deleted.
  $result = xmlsitemap_clear_directory(NULL, TRUE);
  $this
    ->assertDirectoryNotExists('public://xmlsitemap/test');
  $this
    ->assertDirectoryExists('public://not-xmlsitemap');
  $this
    ->assertFileExists('public://file.txt');
  $this
    ->assertTrue($result);
}