function EntityCrudHookTest::testFileHooks

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php \Drupal\KernelTests\Core\Entity\EntityCrudHookTest::testFileHooks()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php \Drupal\KernelTests\Core\Entity\EntityCrudHookTest::testFileHooks()
  3. 10 core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php \Drupal\KernelTests\Core\Entity\EntityCrudHookTest::testFileHooks()

Tests hook invocations for CRUD operations on files.

File

core/tests/Drupal/KernelTests/Core/Entity/EntityCrudHookTest.php, line 242

Class

EntityCrudHookTest
Tests entity CRUD via hooks.

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testFileHooks() : void {
    $this->installEntitySchema('file');
    $url = 'public://entity_crud_hook_test.file';
    file_put_contents($url, 'Test test test');
    $file = File::create([
        'fid' => NULL,
        'uid' => 1,
        'filename' => 'entity_crud_hook_test.file',
        'uri' => $url,
        'filemime' => 'text/plain',
        'filesize' => filesize($url),
        'status' => 1,
        'created' => \Drupal::time()->getRequestTime(),
        'changed' => \Drupal::time()->getRequestTime(),
    ]);
    $this->assertHookMessageOrder([
        'entity_crud_hook_test_file_create called',
        'entity_crud_hook_test_entity_create called for type file',
    ]);
    $GLOBALS['entity_crud_hook_test'] = [];
    $file->save();
    $this->assertHookMessageOrder([
        'entity_crud_hook_test_file_presave called',
        'entity_crud_hook_test_entity_presave called for type file',
        'entity_crud_hook_test_file_insert called',
        'entity_crud_hook_test_entity_insert called for type file',
    ]);
    $GLOBALS['entity_crud_hook_test'] = [];
    $file = File::load($file->id());
    $this->assertHookMessageOrder([
        'entity_crud_hook_test_entity_load called for type file',
        'entity_crud_hook_test_file_load called',
    ]);
    $GLOBALS['entity_crud_hook_test'] = [];
    $file->setFilename('new.entity_crud_hook_test.file');
    $file->save();
    $this->assertHookMessageOrder([
        'entity_crud_hook_test_file_presave called',
        'entity_crud_hook_test_entity_presave called for type file',
        'entity_crud_hook_test_file_update called',
        'entity_crud_hook_test_entity_update called for type file',
    ]);
    $GLOBALS['entity_crud_hook_test'] = [];
    $file->delete();
    $this->assertHookMessageOrder([
        'entity_crud_hook_test_file_predelete called',
        'entity_crud_hook_test_entity_predelete called for type file',
        'entity_crud_hook_test_file_delete called',
        'entity_crud_hook_test_entity_delete called for type file',
    ]);
}

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