View source
<?php
namespace Drupal\Tests\textimage\Functional;
use Drupal\image\Entity\ImageStyle;
use Drupal\Tests\Traits\Core\CronRunTrait;
class TextimageTest extends TextimageTestBase {
use CronRunTrait;
public function testTextimage() {
$public_directory_path = \Drupal::service('stream_wrapper_manager')
->getViaScheme('public')
->getDirectoryPath();
$private_directory_path = \Drupal::service('stream_wrapper_manager')
->getViaScheme('private')
->getDirectoryPath();
$input = [
[
'text' => [
'preview text image',
],
'width' => 171,
'height' => 24,
],
[
'text' => [
'Предварительный просмотр текста',
],
'width' => 335,
'height' => 24,
],
[
'text' => [
'προεπισκόπηση της εικόνας κείμενο',
],
'width' => 325,
'height' => 24,
],
[
'text' => [
'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
],
'width' => 1104,
'height' => 24,
],
];
foreach ($input as $item) {
$textimage = $this->textimageFactory
->get()
->setStyle(ImageStyle::load('textimage_test'))
->process($item['text']);
$element = [
'#theme' => 'textimage_formatter',
'#uri' => $textimage
->getUri(),
'#width' => $textimage
->getWidth(),
'#height' => $textimage
->getHeight(),
];
$textimage
->getBubbleableMetadata()
->applyTo($element);
$this->renderer
->renderRoot($element);
$this
->assertFileNotExists($textimage
->getUri());
$this
->drupalGet($textimage
->getUrl());
$this
->assertFileExists($textimage
->getUri());
$this
->assertTextimage($textimage
->getUri(), $item['width'], $item['height']);
}
$this
->assertCount(4, file_scan_directory($public_directory_path . '/textimage_store/cache/styles/textimage_test', '/.*/'));
foreach ($input as $item) {
$textimage = $this->textimageFactory
->get()
->setStyle(ImageStyle::load('textimage_test'))
->process($item['text']);
$cached = \Drupal::cache('textimage')
->get('tiid:' . $textimage
->id());
$this
->assertSame($textimage
->getUri(), $cached->data['uri']);
}
\Drupal::cache('textimage')
->deleteAll();
foreach ($input as $item) {
$textimage = $this->textimageFactory
->get()
->setStyle(ImageStyle::load('textimage_test'))
->process($item['text']);
$this
->assertFileExists($textimage
->getUri());
}
$edit = [
'textimage_options[uri_scheme]' => 'private',
];
$this
->drupalPostForm('admin/config/media/image-styles/manage/textimage_test', $edit, t('Save'));
foreach ($input as $item) {
$textimage = $this->textimageFactory
->get()
->setStyle(ImageStyle::load('textimage_test'))
->process($item['text']);
$element = [
'#theme' => 'textimage_formatter',
'#uri' => $textimage
->getUri(),
'#width' => $textimage
->getWidth(),
'#height' => $textimage
->getHeight(),
];
$textimage
->getBubbleableMetadata()
->applyTo($element);
$this->renderer
->renderRoot($element);
$this
->assertFileNotExists($textimage
->getUri());
$this
->drupalGet($textimage
->getUrl());
$this
->assertFileExists($textimage
->getUri());
$this
->assertTextimage($textimage
->getUri(), $item['width'], $item['height']);
}
$this
->assertCount(4, file_scan_directory($private_directory_path . '/textimage_store/cache/styles/textimage_test', '/.*/'));
$this
->drupalGet($public_directory_path . '/textimage_store/cache/styles/textimage_test/8/8f/8f3f0c1a0d01c0487f97d068b2a77c792964eedfbe7e2f24eb1207429118aaff.png');
$this
->assertResponse(404);
$this
->drupalGet($public_directory_path . '/textimage/textimage_test/url_preview_text_image---additional text.png');
$this
->assertResponse(403);
$edit = [
'textimage_options[uri_scheme]' => 'public',
];
$this
->drupalPostForm('admin/config/media/image-styles/manage/textimage_test', $edit, t('Save'));
$this
->drupalGet($public_directory_path . '/textimage/textimage_test/url_preview_text_image---additional text.png');
$this
->assertResponse(200);
$this
->assertCount(1, file_scan_directory($public_directory_path . '/textimage/textimage_test', '/.*/'), 'Textimage generation via request URL.');
$this
->assertTextimage('public://textimage/textimage_test/url_preview_text_image---additional text.png', 217, 24);
$this->textimageFactory
->get()
->setStyle(ImageStyle::load('textimage_test'))
->setTargetUri('public://textimage-testing/bingo-bongo.png')
->process('test')
->buildImage();
$this
->assertCount(1, file_scan_directory('public://textimage-testing', '/.*/'), 'Textimage generation at target URI via API.');
$this
->assertTextimage('public://textimage-testing/bingo-bongo.png', 33, 24);
$this->textimageFactory
->get()
->setStyle(ImageStyle::load('textimage_test'))
->setTargetUri('public://textimage-testing/bingo-bongo.png')
->process('another test')
->buildImage();
$this
->assertCount(1, file_scan_directory('public://textimage-testing', '/.*/'), 'Textimage replaced at target URI via API.');
$this
->assertTextimage('public://textimage-testing/bingo-bongo.png', 107, 24);
}
public function testTextimageCronRun() {
$textimage = $this->textimageFactory
->get();
$textimage
->setStyle(ImageStyle::load('textimage_test'))
->setTemporary(TRUE)
->process([
'text image for cron run',
])
->buildImage();
$this
->assertCount(1, file_scan_directory('public://textimage_store/temp', '/.*/'));
$this
->cronRun();
$this
->assertDirectoryNotExists('public://textimage_store/temp');
}
}