Skip to content

Instantly share code, notes, and snippets.

@edutrul
Created April 6, 2026 02:25
Show Gist options
  • Select an option

  • Save edutrul/73e774ee870c4c73e2b030641e52d307 to your computer and use it in GitHub Desktop.

Select an option

Save edutrul/73e774ee870c4c73e2b030641e52d307 to your computer and use it in GitHub Desktop.
FileCreationTrait for DTT how this should be done very solid.
<?php
declare(strict_types=1);
namespace Drupal\Tests\server_general\Traits;
use Drupal\Core\File\FileExists;
use Drupal\file\FileInterface;
use weitzman\DrupalTestTraits\DrupalTrait;
/**
* Provides file entity creation for tests using committed test assets.
*
* Assets live in tests/assets/ and are copied to public:// with
* FileExists::Replace, so files never accumulate across test runs. The
* physical file persists in public:// intentionally — this is safe for
* parallel tests and avoids teardown race conditions.
*
* Available assets:
* - test-image.png
* - test-image.jpg
* - test-file.pdf
*/
trait FileCreationTrait {
use DrupalTrait;
const ASSETS_PATH = 'modules/custom/server_general/tests/assets/';
/**
* Creates a File entity from a committed test asset.
*
* @param string $filename
* The asset filename (e.g. 'test-image.png'). Must exist in tests/assets/.
*
* @return \Drupal\file\FileInterface
* The saved File entity, marked for automatic cleanup.
*/
protected function createFileEntity(string $filename): FileInterface {
$source = \Drupal::root() . '/' . self::ASSETS_PATH . $filename;
$uri = \Drupal::service('file_system')
->copy($source, 'public://' . $filename, FileExists::Replace);
/** @var \Drupal\file\FileInterface $file */
$file = \Drupal::entityTypeManager()
->getStorage('file')
->create([
'uri' => $uri,
'status' => 1,
]);
$file->setPermanent();
$file->save();
$this->markEntityForCleanup($file);
return $file;
}
}
<?php
declare(strict_types=1);
namespace Drupal\Tests\server_general\ExistingSite;
use Drupal\Tests\server_general\Traits\FileCreationTrait;
use weitzman\DrupalTestTraits\Entity\MediaCreationTrait;
/**
* Tests FileCreationTrait produces valid, clean File entities.
*/
class ServerGeneralFileCreationTraitTest extends ServerGeneralTestBase {
use FileCreationTrait;
use MediaCreationTrait;
/**
* Tests PNG file entity creation.
*/
public function testCreatePngFileEntity(): void {
$file = $this->createFileEntity('test-image.png');
$this->assertNotEmpty($file->id());
$this->assertEquals('public://test-image.png', $file->getFileUri());
$this->assertEquals('image/png', $file->getMimeType());
$this->assertTrue($file->isPermanent());
}
/**
* Tests JPG file entity creation.
*/
public function testCreateJpgFileEntity(): void {
$file = $this->createFileEntity('test-image.jpg');
$this->assertNotEmpty($file->id());
$this->assertEquals('public://test-image.jpg', $file->getFileUri());
$this->assertEquals('image/jpeg', $file->getMimeType());
$this->assertTrue($file->isPermanent());
}
/**
* Tests PDF file entity creation.
*/
public function testCreatePdfFileEntity(): void {
$file = $this->createFileEntity('test-file.pdf');
$this->assertNotEmpty($file->id());
$this->assertEquals('public://test-file.pdf', $file->getFileUri());
$this->assertEquals('application/pdf', $file->getMimeType());
$this->assertTrue($file->isPermanent());
}
/**
* Tests creating a media image entity referencing a file entity.
*/
public function testCreateMediaImage(): void {
$file = $this->createFileEntity('test-image.png');
$media = $this->createMedia([
'bundle' => 'image',
'name' => 'Test image',
'field_media_image' => [
'target_id' => $file->id(),
'alt' => 'Test alt',
],
]);
$this->assertNotEmpty($media->id());
$this->assertEquals('Test image', $media->getName());
$this->assertEquals($file->id(), $media->get('field_media_image')->target_id);
}
/**
* Tests FileExists::Replace — calling twice produces same URI, no duplicates.
*/
public function testFileExistsReplace(): void {
$file1 = $this->createFileEntity('test-image.png');
$file2 = $this->createFileEntity('test-image.png');
$this->assertEquals($file1->getFileUri(), $file2->getFileUri());
$this->assertNotEquals($file1->id(), $file2->id());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment