You are here

public function TextimageApiTest::testTargetExtension in Textimage 8.4

Same name and namespace in other branches
  1. 8.3 tests/src/Kernel/TextimageApiTest.php \Drupal\Tests\textimage\Kernel\TextimageApiTest::testTargetExtension()

Test output image file extension is consistent with source image.

File

tests/src/Kernel/TextimageApiTest.php, line 257

Class

TextimageApiTest
Kernel tests for Textimage API.

Namespace

Drupal\Tests\textimage\Kernel

Code

public function testTargetExtension() {
  $files = $this
    ->getTestFiles('image');

  // Get 'image-test.gif'.
  $file = File::create((array) $files[1]);
  $file
    ->save();
  $textimage = $this->textimageFactory
    ->get();
  $textimage
    ->setStyle(ImageStyle::load('textimage_test'))
    ->setSourceImageFile($file)
    ->process([
    'bingox',
  ])
    ->buildImage();
  $image = \Drupal::service('image.factory')
    ->get($textimage
    ->getUri());
  $this
    ->assertSame('image/gif', $image
    ->getMimeType());

  // Test loading the Textimage metadata.
  $id = $textimage
    ->id();
  $uri = $textimage
    ->getUri();
  $textimage = $this->textimageFactory
    ->load($id);
  $style = ImageStyle::load('textimage_test');

  // Check loaded data.
  $this
    ->assertSame($id, $textimage
    ->id());
  $this
    ->assertSame($uri, $textimage
    ->getUri());
  $this
    ->assertSame([
    'bingox',
  ], $textimage
    ->getText());
  try {
    $textimage
      ->setStyle($style);
    $this
      ->fail('Property \'style\' set when image was processed already');
  } catch (TextimageException $e) {

    // Countinue.
  }

  // File exists.
  $this
    ->assertFileExists($uri);

  // File deletion.
  $this
    ->assertTrue($this->fileSystem
    ->delete($uri));

  // Reload and rebuild.
  $textimage = $this->textimageFactory
    ->load($id);
  $textimage
    ->buildImage();
  $this
    ->assertFileExists($uri);
}