function LoadTest::testMultiple

Same name and namespace in other branches
  1. 9 core/modules/file/tests/src/Kernel/LoadTest.php \Drupal\Tests\file\Kernel\LoadTest::testMultiple()
  2. 10 core/modules/file/tests/src/Kernel/LoadTest.php \Drupal\Tests\file\Kernel\LoadTest::testMultiple()
  3. 11.x core/modules/file/tests/src/Kernel/LoadTest.php \Drupal\Tests\file\Kernel\LoadTest::testMultiple()

This will test loading file data from the database.

File

core/modules/file/tests/src/Kernel/LoadTest.php, line 61

Class

LoadTest
Tests <a href="/api/drupal/core%21lib%21Drupal%21Core%21Entity%21EntityBase.php/function/EntityBase%3A%3Aload/8.9.x" title="Loads an entity." class="local">\Drupal\file\Entity\File::load</a>().

Namespace

Drupal\Tests\file\Kernel

Code

public function testMultiple() {
    // Create a new file entity.
    $file = $this->createFile('druplicon.txt', NULL, 'public');
    // Load by path.
    file_test_reset();
    $by_path_files = \Drupal::entityTypeManager()->getStorage('file')
        ->loadByProperties([
        'uri' => $file->getFileUri(),
    ]);
    $this->assertFileHookCalled('load');
    $this->assertCount(1, $by_path_files, '\\Drupal::entityTypeManager()->getStorage(\'file\')->loadByProperties() returned an array of the correct size.');
    $by_path_file = reset($by_path_files);
    $this->assertTrue($by_path_file->file_test['loaded'], 'file_test_file_load() was able to modify the file during load.');
    $this->assertEqual($by_path_file->id(), $file->id(), 'Loading by filepath got the correct fid.', 'File');
    // Load by fid.
    file_test_reset();
    $by_fid_files = File::loadMultiple([
        $file->id(),
    ]);
    $this->assertFileHooksCalled([]);
    $this->assertCount(1, $by_fid_files, '\\Drupal\\file\\Entity\\File::loadMultiple() returned an array of the correct size.');
    $by_fid_file = reset($by_fid_files);
    $this->assertTrue($by_fid_file->file_test['loaded'], 'file_test_file_load() was able to modify the file during load.');
    $this->assertEqual($by_fid_file->getFileUri(), $file->getFileUri(), 'Loading by fid got the correct filepath.', 'File');
}

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