View source
<?php
namespace Drupal\Tests\textimage\Kernel;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
use Drupal\textimage\TextimageException;
use Drupal\Tests\TestFileCreationTrait;
use Drupal\KernelTests\KernelTestBase;
class TextimageApiTest extends KernelTestBase {
use TextimageTestTrait;
use TestFileCreationTrait;
protected static $modules = [
'system',
'textimage',
'image',
'image_effects',
'user',
'file',
'file_mdm',
'file_mdm_font',
];
public function setUp() {
parent::setUp();
$this
->installConfig([
'system',
'textimage',
'image',
'image_effects',
'user',
'file',
'file_mdm',
'file_mdm_font',
]);
$this
->installEntitySchema('user');
$this
->installEntitySchema('file');
$this
->initTextimageTest();
}
public function testTextimageApi() {
$style = ImageStyle::load('textimage_test');
$style
->addImageEffect([
'id' => 'image_effects_text_overlay',
'data' => [
'font' => [
'name' => 'Linux Libertine',
'uri' => drupal_get_path('module', 'image_effects') . '/tests/fonts/LinLibertineTTF_5.3.0_2012_07_02/LinLibertine_Rah.ttf',
'size' => 16,
'angle' => '90',
'color' => '#FF0000',
],
'text_string' => 'Eff 1',
],
]);
$style
->addImageEffect([
'id' => 'image_effects_text_overlay',
'data' => [
'font' => [
'name' => 'Linux Libertine',
'uri' => drupal_get_path('module', 'image_effects') . '/tests/fonts/LinLibertineTTF_5.3.0_2012_07_02/LinLibertine_Rah.ttf',
'size' => 16,
'angle' => '-90',
'color' => '#00FF00',
],
'text_string' => 'Eff 2',
],
]);
$style
->addImageEffect([
'id' => 'image_effects_text_overlay',
'data' => [
'font' => [
'name' => 'Linux Libertine',
'uri' => drupal_get_path('module', 'image_effects') . '/tests/fonts/LinLibertineTTF_5.3.0_2012_07_02/LinLibertine_Rah.ttf',
'size' => 16,
'angle' => '45',
'color' => '#0000FF',
],
'text_string' => 'Eff 3',
],
]);
$style
->addImageEffect([
'id' => 'image_desaturate',
'data' => [],
]);
$style
->addImageEffect([
'id' => 'image_scale_and_crop',
'data' => [
'width' => 120,
'height' => 121,
],
]);
$style
->save();
$textimage = $this->textimageFactory
->get();
$textimage
->setStyle($style);
$textimage
->setTemporary(FALSE);
$textimage
->setTokenData([
'user' => $this->adminUser,
]);
$this
->assertNull($textimage
->id(), 'ID is not available');
$this
->assertNull($textimage
->getUri(), 'URI is not available');
$this
->assertNull($textimage
->getUrl(), 'URL is not available');
$this
->assertNull($textimage
->getBubbleableMetadata(), 'Bubbleable metadata is not available');
$this
->assertEmpty($textimage
->getText(), 'Processed text is not available');
$this
->setExpectedException(TextimageException::class, 'Textimage error: Attempted to build Textimage before processing data');
$textimage
->buildImage();
$text_array = [
'bingo',
'bongo',
'tengo',
'tango',
];
$expected_text_array = [
'bingo',
'bongo',
'tengo',
'tango',
];
$textimage
->process($text_array);
$this
->assertNotNull($textimage
->id(), 'ID is available');
$this
->assertNotNull($textimage
->getUri(), 'URI is available');
$this
->assertNotNull($textimage
->getUrl(), 'URL is available');
$this
->assertNotNull($textimage
->getBubbleableMetadata(), 'Bubbleable metadata is available');
$this
->assertSame($expected_text_array, $textimage
->getText(), 'Processed text is available');
$textimage
->buildImage();
$this
->setExpectedException(TextimageException::class, 'Textimage error: Image style already set');
$textimage
->setStyle($style);
$this
->setExpectedException(TextimageException::class, 'Textimage error: Image effects already set');
$textimage
->setEffects([]);
$this
->setExpectedException(TextimageException::class, 'Textimage error: Extension already set');
$textimage
->setTargetExtension('png');
$this
->setExpectedException(TextimageException::class, 'Textimage error: URI already set');
$textimage
->setTemporary(TRUE);
$this
->setExpectedException(TextimageException::class, 'Textimage error: Token data already set');
$textimage
->setTokenData([
'user' => $this->adminUser,
]);
$this
->setExpectedException(TextimageException::class, 'Textimage error: URI already set');
$textimage
->setTargetUri('public://textimage-testing/bingo-bongo.png');
$this
->setExpectedException(TextimageException::class, 'Textimage error: Attempted to re-process an already processed Textimage');
$textimage
->process($text_array);
$stored_image = \Drupal::cache('textimage')
->get('tiid:' . $textimage
->id());
$image_data = $stored_image->data['imageData'];
$effects_outline = $stored_image->data['effects'];
$this
->assertSame($expected_text_array, array_values($image_data['text']), 'Processed text stored in image data');
$this
->assertCount(6, $effects_outline, 'Expected number of effects in the outline');
foreach ($effects_outline as $effect) {
if ($effect['id'] == 'image_effects_text_overlay') {
$this
->assertTrue(!isset($effect['data']['text_string']), 'Processed text not stored in the effects outline');
}
}
}
public function testForceTargetExtension() {
$files = $this
->getTestFiles('image');
$file = File::create((array) array_shift($files));
$file
->save();
$textimage = $this->textimageFactory
->get();
$textimage
->setStyle(ImageStyle::load('textimage_test'))
->setSourceImageFile($file)
->setTargetExtension('gif')
->process([
'bingox',
])
->buildImage();
$image = \Drupal::service('image.factory')
->get($textimage
->getUri());
$this
->assertSame('image/gif', $image
->getMimeType());
}
public function testTargetExtension() {
$files = $this
->getTestFiles('image');
$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());
$id = $textimage
->id();
$uri = $textimage
->getUri();
$textimage = $this->textimageFactory
->load($id);
$style = ImageStyle::load('textimage_test');
$this
->assertSame($id, $textimage
->id());
$this
->assertSame($uri, $textimage
->getUri());
$this
->assertSame([
'bingox',
], $textimage
->getText());
$this
->setExpectedException(TextimageException::class, 'Textimage error: Attempted to set property \'style\' when image was processed already');
$textimage
->setStyle($style);
$this
->assertFileExists($uri);
$this
->assertTrue($this->fileSystem
->delete($uri));
$textimage = $this->textimageFactory
->load($id);
$textimage
->buildImage();
$this
->assertFileExists($uri);
}
public function testFileExtensionCasing() {
$files = $this
->getTestFiles('image');
$file = File::create((array) array_shift($files));
$file
->save();
file_move($file, 'image-test.PNG');
$textimage = $this->textimageFactory
->get();
$textimage
->setStyle(ImageStyle::load('textimage_test'))
->setSourceImageFile($file)
->setTargetExtension('PNG')
->process([
'bingox',
])
->buildImage();
$image = \Drupal::service('image.factory')
->get($textimage
->getUri());
$this
->assertSame('image/png', $image
->getMimeType());
$this
->assertSame('png', pathinfo($textimage
->getUri(), PATHINFO_EXTENSION));
}
public function testFileExtensionChange() {
$textimage = $this->textimageFactory
->get();
$textimage
->setStyle(ImageStyle::load('textimage_test'))
->process('bingo')
->buildImage();
$image = \Drupal::service('image.factory')
->get($textimage
->getUri());
$this
->assertSame('image/png', $image
->getMimeType());
$style = ImageStyle::load('textimage_test');
$style
->addImageEffect([
'id' => 'image_convert',
'data' => [
'extension' => 'jpeg',
],
]);
$style
->save();
$textimage = $this->textimageFactory
->get();
$textimage
->setStyle(ImageStyle::load('textimage_test'))
->process('bingo')
->buildImage();
$image = \Drupal::service('image.factory')
->get($textimage
->getUri());
$this
->assertSame('image/jpeg', $image
->getMimeType());
}
public function testTextAlteration() {
$effects = [];
$effects[] = [
'id' => 'image_effects_text_overlay',
'data' => [
'text' => [
'strip_tags' => TRUE,
'decode_entities' => TRUE,
'maximum_chars' => 12,
'excess_chars_text' => ' [more]',
'case_format' => 'upper',
],
'text_string' => 'Test preview',
],
];
$textimage = $this->textimageFactory
->get();
$textimage
->setEffects($effects)
->process('the quick brown fox jumps over the lazy dog');
$this
->assertSame([
'THE QUICK BR [more]',
], $textimage
->getText());
$effects = [];
$effects[] = [
'id' => 'image_effects_text_overlay',
'data' => [
'text' => [
'strip_tags' => TRUE,
'decode_entities' => TRUE,
'case_format' => '',
'maximum_chars' => NULL,
],
'text_string' => 'Test preview',
],
];
$textimage = $this->textimageFactory
->get();
$textimage
->setEffects($effects)
->process('<p>Para1</p><!-- Comment --> Para2');
$this
->assertSame([
'Para1 Para2',
], $textimage
->getText());
$textimage = $this->textimageFactory
->get();
$textimage
->setEffects($effects)
->process('"Title" One …');
$this
->assertSame([
'"Title" One …',
], $textimage
->getText());
}
public function testSetInvalidTargetUri() {
$textimage = $this->textimageFactory
->get();
$this
->setExpectedException(TextimageException::class, 'Textimage error: Invalid target URI \'bingo://textimage-testing/bingo-bongo.png\' specified');
$textimage
->setTargetUri('bingo://textimage-testing/bingo-bongo.png');
$this
->setExpectedException(TextimageException::class, 'Textimage error: Invalid target URI \'public://textimage-testing/bingo' . chr(1) . '.png\' specified');
$textimage
->setTargetUri('public://textimage-testing/bingo' . chr(1) . '.png');
}
}