class FileDeleteTest

Same name and namespace in other branches
  1. 7.x modules/simpletest/tests/file.test \FileDeleteTest
  2. 11.x core/tests/Drupal/KernelTests/Core/File/FileDeleteTest.php \Drupal\KernelTests\Core\File\FileDeleteTest
  3. 10 core/tests/Drupal/KernelTests/Core/File/FileDeleteTest.php \Drupal\KernelTests\Core\File\FileDeleteTest
  4. 8.9.x core/tests/Drupal/KernelTests/Core/File/FileDeleteTest.php \Drupal\KernelTests\Core\File\FileDeleteTest

Tests the unmanaged file delete function.

@group File

Hierarchy

Expanded class hierarchy of FileDeleteTest

File

core/tests/Drupal/KernelTests/Core/File/FileDeleteTest.php, line 12

Namespace

Drupal\KernelTests\Core\File
View source
class FileDeleteTest extends FileTestBase {
  
  /**
   * Delete a normal file.
   */
  public function testNormal() {
    // Create a file for testing
    $uri = $this->createUri();
    // Delete a regular file
    $this->assertTrue(\Drupal::service('file_system')->delete($uri), 'Deleted worked.');
    $this->assertFileDoesNotExist($uri);
  }
  
  /**
   * Try deleting a missing file.
   */
  public function testMissing() {
    // Try to delete a non-existing file
    $this->assertTrue(\Drupal::service('file_system')->delete('public://' . $this->randomMachineName()), 'Returns true when deleting a non-existent file.');
  }
  
  /**
   * Try deleting a directory.
   */
  public function testDirectory() {
    // A directory to operate on.
    $directory = $this->createDirectory();
    // Try to delete a directory.
    try {
      \Drupal::service('file_system')->delete($directory);
      $this->fail('Expected NotRegularFileException');
    } catch (NotRegularFileException $e) {
      // Ignore.
    }
    $this->assertDirectoryExists($directory);
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.