You are here

public function FileManagedUnitTestBase::assertFileHooksCalled in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/file/tests/src/Kernel/FileManagedUnitTestBase.php \Drupal\Tests\file\Kernel\FileManagedUnitTestBase::assertFileHooksCalled()

Assert that all of the specified hook_file_* hooks were called once, other values result in failure.

Parameters

array $expected: Array with string containing with the hook name, e.g. 'load', 'save', 'insert', etc.

23 calls to FileManagedUnitTestBase::assertFileHooksCalled()
CopyTest::testExistingError in core/modules/file/tests/src/Kernel/CopyTest.php
Tests that copying over an existing file fails when instructed to do so.
CopyTest::testExistingRename in core/modules/file/tests/src/Kernel/CopyTest.php
Tests renaming when copying over a file that already exists.
CopyTest::testExistingReplace in core/modules/file/tests/src/Kernel/CopyTest.php
Tests replacement when copying over a file that already exists.
CopyTest::testNormal in core/modules/file/tests/src/Kernel/CopyTest.php
Tests file copying in the normal, base case.
DeleteTest::testInUse in core/modules/file/tests/src/Kernel/DeleteTest.php
Tries deleting a file that is in use.

... See full list

File

core/modules/file/tests/src/Kernel/FileManagedUnitTestBase.php, line 50

Class

FileManagedUnitTestBase
Base class for file unit tests that use the file_test module to test uploads and hooks.

Namespace

Drupal\Tests\file\Kernel

Code

public function assertFileHooksCalled($expected) {
  \Drupal::state()
    ->resetCache();

  // Determine which hooks were called.
  $actual = array_keys(array_filter(file_test_get_all_calls()));

  // Determine if there were any expected that were not called.
  $uncalled = array_diff($expected, $actual);
  if (count($uncalled)) {
    $this
      ->assertTrue(FALSE, new FormattableMarkup('Expected hooks %expected to be called but %uncalled was not called.', [
      '%expected' => implode(', ', $expected),
      '%uncalled' => implode(', ', $uncalled),
    ]));
  }
  else {
    $this
      ->assertTrue(TRUE, new FormattableMarkup('All the expected hooks were called: %expected', [
      '%expected' => empty($expected) ? '(none)' : implode(', ', $expected),
    ]));
  }

  // Determine if there were any unexpected calls.
  $unexpected = array_diff($actual, $expected);
  if (count($unexpected)) {
    $this
      ->assertTrue(FALSE, new FormattableMarkup('Unexpected hooks were called: %unexpected.', [
      '%unexpected' => empty($unexpected) ? '(none)' : implode(', ', $unexpected),
    ]));
  }
  else {
    $this
      ->assertTrue(TRUE, 'No unexpected hooks were called.');
  }
}