View source
<?php
namespace Drupal\Tests\image_effects\Functional\Effect;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Tests\image_effects\Functional\ImageEffectsTestBase;
class TextOverlayTest extends ImageEffectsTestBase {
public function setUp() : void {
static::$modules = array_merge(static::$modules, [
'file_mdm',
'file_mdm_font',
'file_test',
]);
parent::setUp();
}
public function providerToolkits() {
$toolkits = parent::providerToolkits();
unset($toolkits['ImageMagick-graphicsmagick']);
return $toolkits;
}
public function testTextOverlayEffect($toolkit_id, $toolkit_config, array $toolkit_settings) {
$this
->changeToolkit($toolkit_id, $toolkit_config, $toolkit_settings);
$this->fileSystem
->copy(drupal_get_path('module', 'image_effects') . '/tests/fonts/LinLibertineTTF_5.3.0_2012_07_02/LinLibertine_Rah.ttf', 'dummy-remote://', FileSystemInterface::EXISTS_REPLACE);
$effect_config = [
'id' => 'image_effects_text_overlay',
'data' => [
'text_default][text_string' => 'the quick brown fox jumps over the lazy dog',
'font][uri' => drupal_get_path('module', 'image_effects') . '/tests/fonts/LinLibertineTTF_5.3.0_2012_07_02/LinLibertine_Rah.ttf',
'font][size' => 40,
'layout][position][extended_color][container][transparent' => FALSE,
'layout][position][extended_color][container][hex' => '#FF00FF',
'layout][position][extended_color][container][opacity' => 100,
],
];
$this
->addEffectToTestStyle($effect_config);
$effect_config = [
'id' => 'image_effects_text_overlay',
'data' => [
'text_default][text_string' => 'the quick brown fox jumps over the lazy dog',
'font][uri' => 'public://LinLibertine_Rah.ttf',
'font][size' => 10,
'layout][position][extended_color][container][transparent' => FALSE,
'layout][position][extended_color][container][hex' => '#FF00FF',
'layout][position][extended_color][container][opacity' => 100,
],
];
$this
->addEffectToTestStyle($effect_config);
$effect_config = [
'id' => 'image_effects_text_overlay',
'data' => [
'text_default][text_string' => 'the quick brown fox jumps over the lazy dog',
'font][uri' => 'dummy-remote://LinLibertine_Rah.ttf',
'font][size' => 10,
'layout][position][extended_color][container][transparent' => FALSE,
'layout][position][extended_color][container][hex' => '#FF00FF',
'layout][position][extended_color][container][opacity' => 100,
],
];
$this
->addEffectToTestStyle($effect_config);
if ($toolkit_id === 'imagemagick') {
$directory_scan = $this->fileSystem
->scanDirectory('temporary://', '/ifx.*/');
$this
->assertEquals(0, count($directory_scan));
}
$test_data = [
[
'test_file' => $this
->getTestImageCopyUri('core/tests/fixtures/files/image-test.png'),
'derivative_width' => 984,
'derivative_height' => 61,
],
];
foreach ($test_data as $data) {
$original_uri = $data['test_file'];
$derivative_uri = $this->testImageStyle
->buildUri($original_uri);
$image = $this->imageFactory
->get($original_uri);
$derivative_url = file_url_transform_relative($this->testImageStyle
->buildUrl($original_uri));
$this->testImageStyle
->createDerivative($original_uri, $derivative_uri);
$derivative_image = $this->imageFactory
->get($derivative_uri, 'gd');
$this
->assertTextOverlay($derivative_image, $data['derivative_width'], $data['derivative_height']);
$this
->assertColorsAreEqual($this->fuchsia, $this
->getPixelColor($derivative_image, 0, 0));
$this
->assertColorsAreEqual($this->fuchsia, $this
->getPixelColor($derivative_image, $derivative_image
->getWidth() - 1, 0));
$this
->assertColorsAreEqual($this->fuchsia, $this
->getPixelColor($derivative_image, 0, $derivative_image
->getHeight() - 1));
$this
->assertColorsAreEqual($this->fuchsia, $this
->getPixelColor($derivative_image, $derivative_image
->getWidth() - 1, $derivative_image
->getHeight() - 1));
$variables = [
'#theme' => 'image_style',
'#style_name' => 'image_effects_test',
'#uri' => $original_uri,
'#width' => $image
->getWidth(),
'#height' => $image
->getHeight(),
];
$this
->assertRegExp("/\\<img src=\"" . preg_quote($derivative_url, '/') . "\" width=\"{$derivative_image->getWidth()}\" height=\"{$derivative_image->getHeight()}\" alt=\"\" .*class=\"image\\-style\\-image\\-effects\\-test\" \\/\\>/", $this
->getImageTag($variables));
}
}
public function testTextAlter() {
$effect_config = [
'id' => 'image_effects_text_overlay',
'data' => [
'text_default][text_string' => 'the quick brown fox jumps over the lazy dog',
'font][uri' => drupal_get_path('module', 'image_effects') . '/tests/fonts/LinLibertineTTF_5.3.0_2012_07_02/LinLibertine_Rah.ttf',
'font][size' => 40,
'layout][position][extended_color][container][transparent' => FALSE,
'layout][position][extended_color][container][hex' => '#FF00FF',
'layout][position][extended_color][container][opacity' => 100,
],
];
$uuid = $this
->addEffectToTestStyle($effect_config);
$effect = $this->testImageStyle
->getEffect($uuid);
$this
->assertEquals('the quick brown fox jumps over the lazy dog', $effect
->getAlteredText($effect
->getConfiguration()['data']['text_string']));
$this
->assertEquals('Para1 Para2', $effect
->getAlteredText('<p>Para1</p><!-- Comment --> Para2'));
$this
->assertEquals('"Title" One …', $effect
->getAlteredText('"Title" One …'));
$this
->removeEffectFromTestStyle($uuid);
$effect_config['data'] += [
'text_default][strip_tags' => FALSE,
'text_default][decode_entities' => FALSE,
];
$uuid = $this
->addEffectToTestStyle($effect_config);
$effect = $this->testImageStyle
->getEffect($uuid);
$this
->assertEquals('<p>Para1</p><!-- Comment --> Para2', $effect
->getAlteredText('<p>Para1</p><!-- Comment --> Para2'));
$this
->assertEquals('"Title" One …', $effect
->getAlteredText('"Title" One …'));
$this
->removeEffectFromTestStyle($uuid);
$effect_config['data'] += [
'text][maximum_chars' => 9,
'text][case_format' => 'upper',
];
$uuid = $this
->addEffectToTestStyle($effect_config);
$effect = $this->testImageStyle
->getEffect($uuid);
$this
->assertEquals('THE QUICK…', $effect
->getAlteredText($effect
->getConfiguration()['data']['text_string']));
$this
->removeEffectFromTestStyle($uuid);
$effect_config['data']['text][case_format'] = 'ucwords';
$effect_config['data']['text][maximum_chars'] = '';
$uuid = $this
->addEffectToTestStyle($effect_config);
$effect = $this->testImageStyle
->getEffect($uuid);
$this
->assertEquals('The Quick Brown Fox Jumps Over The Lazy Dog', $effect
->getAlteredText($effect
->getConfiguration()['data']['text_string']));
$this
->removeEffectFromTestStyle($uuid);
$effect_config['data']['text][case_format'] = 'ucfirst';
$uuid = $this
->addEffectToTestStyle($effect_config);
$effect = $this->testImageStyle
->getEffect($uuid);
$this
->assertEquals('The quick brown fox jumps over the lazy dog', $effect
->getAlteredText($effect
->getConfiguration()['data']['text_string']));
}
}