public function TextimageApiTest::testTextimageApi in Textimage 8.3
Same name and namespace in other branches
- 8.4 tests/src/Kernel/TextimageApiTest.php \Drupal\Tests\textimage\Kernel\TextimageApiTest::testTextimageApi()
Test basic functionality of the API.
File
- tests/
src/ Kernel/ TextimageApiTest.php, line 58
Class
- TextimageApiTest
- Kernel tests for Textimage API.
Namespace
Drupal\Tests\textimage\KernelCode
public function testTextimageApi() {
// Add more effects to the test style.
$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();
// Test Textimage API.
$textimage = $this->textimageFactory
->get();
// Check API is accepting input, but not providing output, before process.
$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();
// Process Textimage.
$text_array = [
'bingo',
'bongo',
'tengo',
'tango',
];
$expected_text_array = [
'bingo',
'bongo',
'tengo',
'tango',
];
$textimage
->process($text_array);
// Check API is providing output after processing.
$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');
// Build Textimage.
$textimage
->buildImage();
// Check API is not allowing changes after processing.
$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);
// Get textimage cache entry.
$stored_image = \Drupal::cache('textimage')
->get('tiid:' . $textimage
->id());
$image_data = $stored_image->data['imageData'];
$effects_outline = $stored_image->data['effects'];
// Check processed text is stored in image data.
$this
->assertSame($expected_text_array, array_values($image_data['text']), 'Processed text stored in image data');
// Check count of effects is as expected.
$this
->assertCount(6, $effects_outline, 'Expected number of effects in the outline');
// Check processed text is not stored in the effects 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');
}
}
}