FileDeleteRecursiveTest.php

Same filename and directory in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/File/FileDeleteRecursiveTest.php
  2. 8.9.x core/tests/Drupal/KernelTests/Core/File/FileDeleteRecursiveTest.php
  3. 11.x core/tests/Drupal/KernelTests/Core/File/FileDeleteRecursiveTest.php

Namespace

Drupal\KernelTests\Core\File

File

core/tests/Drupal/KernelTests/Core/File/FileDeleteRecursiveTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\KernelTests\Core\File;


/**
 * Tests the unmanaged file delete recursive function.
 *
 * @group File
 */
class FileDeleteRecursiveTest extends FileTestBase {
  
  /**
   * Delete a normal file.
   */
  public function testSingleFile() : void {
    // Create a file for testing
    $filepath = 'public://' . $this->randomMachineName();
    file_put_contents($filepath, '');
    // Delete the file.
    $this->assertTrue(\Drupal::service('file_system')->deleteRecursive($filepath), 'Function reported success.');
    $this->assertFileDoesNotExist($filepath);
  }
  
  /**
   * Try deleting an empty directory.
   */
  public function testEmptyDirectory() : void {
    // A directory to operate on.
    $directory = $this->createDirectory();
    // Delete the directory.
    $this->assertTrue(\Drupal::service('file_system')->deleteRecursive($directory), 'Function reported success.');
    $this->assertDirectoryDoesNotExist($directory);
  }
  
  /**
   * Try deleting a directory with some files.
   */
  public function testDirectory() : void {
    // A directory to operate on.
    $directory = $this->createDirectory();
    $filepathA = $directory . '/A';
    $filepathB = $directory . '/B';
    file_put_contents($filepathA, '');
    file_put_contents($filepathB, '');
    // Delete the directory.
    $this->assertTrue(\Drupal::service('file_system')->deleteRecursive($directory), 'Function reported success.');
    $this->assertFileDoesNotExist($filepathA);
    $this->assertFileDoesNotExist($filepathB);
    $this->assertDirectoryDoesNotExist($directory);
  }
  
  /**
   * Try deleting subdirectories with some files.
   */
  public function testSubDirectory() : void {
    // A directory to operate on.
    $directory = $this->createDirectory();
    $subdirectory = $this->createDirectory($directory . '/sub');
    $filepathA = $directory . '/A';
    $filepathB = $subdirectory . '/B';
    file_put_contents($filepathA, '');
    file_put_contents($filepathB, '');
    // Delete the directory.
    $this->assertTrue(\Drupal::service('file_system')->deleteRecursive($directory), 'Function reported success.');
    $this->assertFileDoesNotExist($filepathA);
    $this->assertFileDoesNotExist($filepathB);
    $this->assertDirectoryDoesNotExist($subdirectory);
    $this->assertDirectoryDoesNotExist($directory);
  }

}

Classes

Title Deprecated Summary
FileDeleteRecursiveTest Tests the unmanaged file delete recursive function.

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