function MediaSourceFileTest::testFileDeletion

Same name in other branches
  1. 9 core/modules/media/tests/src/Kernel/MediaSourceFileTest.php \Drupal\Tests\media\Kernel\MediaSourceFileTest::testFileDeletion()
  2. 8.9.x core/modules/media/tests/src/Kernel/MediaSourceFileTest.php \Drupal\Tests\media\Kernel\MediaSourceFileTest::testFileDeletion()
  3. 11.x core/modules/media/tests/src/Kernel/MediaSourceFileTest.php \Drupal\Tests\media\Kernel\MediaSourceFileTest::testFileDeletion()

Tests a media file can be deleted.

File

core/modules/media/tests/src/Kernel/MediaSourceFileTest.php, line 35

Class

MediaSourceFileTest
Tests the file media source.

Namespace

Drupal\Tests\media\Kernel

Code

public function testFileDeletion() : void {
    $mediaType = $this->createMediaType('file');
    $media = $this->generateMedia('test.txt', $mediaType);
    $media->save();
    $source_field_name = $mediaType->getSource()
        ->getSourceFieldDefinition($mediaType)
        ->getName();
    
    /** @var \Drupal\file\FileInterface $file */
    $file = $media->get($source_field_name)->entity;
    $file->delete();
    $this->assertEmpty($this->container
        ->get('entity_type.manager')
        ->getStorage('file')
        ->loadByProperties([
        'filename' => 'test.txt',
    ]));
}

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