You are here

public function AuditFilesUsedNotReferencedTest::testFileEntityCanBeDeleted in Audit Files 8.2

Same name and namespace in other branches
  1. 8.3 tests/src/Functional/AuditFilesUsedNotReferencedTest.php \Drupal\Tests\auditfiles\Functional\AuditFilesUsedNotReferencedTest::testFileEntityCanBeDeleted()

Tests that an orphan file can be deleted.

An "orphan" file entity is one with an entry in the file_managed table that has no corresponding file in the file_usage table.

File

tests/src/Functional/AuditFilesUsedNotReferencedTest.php, line 156

Class

AuditFilesUsedNotReferencedTest
Tests that the "Managed not used" report is reachable with no errors.

Namespace

Drupal\Tests\auditfiles\Functional

Code

public function testFileEntityCanBeDeleted() {

  // Delete file_usage entry.
  \Drupal::database()
    ->query("DELETE FROM {node__field_image} WHERE field_image_target_id='1'")
    ->execute();
  \Drupal::database()
    ->query("DELETE FROM {node_revision__field_image} WHERE field_image_target_id='1'")
    ->execute();

  // Form to test.
  $path = URL::fromRoute('auditfiles.audit_files_usednotreferenced');

  // Establish session.
  $session = $this
    ->assertSession();

  // Log in as admin user.
  $this
    ->drupalLogin($this->user);

  // Load the report page.
  $this
    ->drupalGet($path);

  // Check for the report title.
  $session
    ->pageTextContains("Used not referenced");
  $session
    ->elementExists('css', '#audit-files-used-not-referenced');
  $session
    ->elementExists('css', '#edit-files-1');

  // Check boxes for file IDs to delete from database, and delete.
  $edit = [
    'edit-files-1' => TRUE,
  ];
  $this
    ->submitForm($edit, 'Delete selected items from the file_usage table');

  // Check for correct confirmation page and submit.
  $session
    ->pageTextContains("Delete these items from the file_usage table?");
  $edit = [];
  $this
    ->submitForm($edit, 'Confirm');

  // Check that target file is no longer listed.
  $session
    ->pageTextContains("Used not referenced");
  $session
    ->pageTextContains("Sucessfully deleted File ID : 1 from the file_usage table.");
  $session
    ->elementNotExists('css', '#edit-files-1');
  $session
    ->pageTextContains("No items found.");
}