You are here

public function FileManagedTestBase::assertFileHooksCalled in Drupal 8

Same name in this branch
  1. 8 core/modules/file/src/Tests/FileManagedTestBase.php \Drupal\file\Tests\FileManagedTestBase::assertFileHooksCalled()
  2. 8 core/modules/file/tests/src/Functional/FileManagedTestBase.php \Drupal\Tests\file\Functional\FileManagedTestBase::assertFileHooksCalled()
Same name and namespace in other branches
  1. 9 core/modules/file/tests/src/Functional/FileManagedTestBase.php \Drupal\Tests\file\Functional\FileManagedTestBase::assertFileHooksCalled()
  2. 10 core/modules/file/tests/src/Functional/FileManagedTestBase.php \Drupal\Tests\file\Functional\FileManagedTestBase::assertFileHooksCalled()

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

Parameters

string[] $expected: An array of strings containing with the hook name; for example, 'load', 'save', 'insert', etc.

16 calls to FileManagedTestBase::assertFileHooksCalled()
SaveUploadFormTest::setUp in core/modules/file/tests/src/Functional/SaveUploadFormTest.php
SaveUploadFormTest::testExistingError in core/modules/file/tests/src/Functional/SaveUploadFormTest.php
Tests for failure when uploading over a file that already exists.
SaveUploadFormTest::testExistingRename in core/modules/file/tests/src/Functional/SaveUploadFormTest.php
Tests renaming when uploading over a file that already exists.
SaveUploadFormTest::testExistingReplace in core/modules/file/tests/src/Functional/SaveUploadFormTest.php
Tests replacement when uploading over a file that already exists.
SaveUploadFormTest::testHandleDangerousFile in core/modules/file/tests/src/Functional/SaveUploadFormTest.php
Tests dangerous file handling.

... See full list

File

core/modules/file/tests/src/Functional/FileManagedTestBase.php, line 37

Class

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

Namespace

Drupal\Tests\file\Functional

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.');
  }
}