MediaSourceFileTest.php

Same filename in this branch
  1. 10 core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php
  2. 10 core/modules/media/tests/src/Functional/MediaSourceFileTest.php
Same filename in other branches
  1. 9 core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php
  2. 9 core/modules/media/tests/src/Kernel/MediaSourceFileTest.php
  3. 9 core/modules/media/tests/src/Functional/MediaSourceFileTest.php
  4. 8.9.x core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php
  5. 8.9.x core/modules/media/tests/src/Kernel/MediaSourceFileTest.php
  6. 8.9.x core/modules/media/tests/src/Functional/MediaSourceFileTest.php
  7. 11.x core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php
  8. 11.x core/modules/media/tests/src/Kernel/MediaSourceFileTest.php
  9. 11.x core/modules/media/tests/src/Functional/MediaSourceFileTest.php

Namespace

Drupal\Tests\media\Kernel

File

core/modules/media/tests/src/Kernel/MediaSourceFileTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\media\Kernel;


/**
 * Tests the file media source.
 *
 * @group media
 */
class MediaSourceFileTest extends MediaKernelTestBase {
    
    /**
     * Tests the file extension constraint.
     */
    public function testFileExtensionConstraint() : void {
        $mediaType = $this->createMediaType('file');
        // Create a random file that should fail.
        $media = $this->generateMedia('test.patch', $mediaType);
        $result = $media->validate();
        $this->assertCount(1, $result);
        $this->assertSame('field_media_file.0', $result->get(0)
            ->getPropertyPath());
        $this->assertStringContainsString('Only files with the following extensions are allowed:', (string) $result->get(0)
            ->getMessage());
        // Create a random file that should pass.
        $media = $this->generateMedia('test.txt', $mediaType);
        $result = $media->validate();
        $this->assertCount(0, $result);
    }
    
    /**
     * Tests a media file can be deleted.
     */
    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',
        ]));
    }

}

Classes

Title Deprecated Summary
MediaSourceFileTest Tests the file media source.

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