View source
<?php
namespace Drupal\Tests\textimage\Functional;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
use Drupal\node\Entity\Node;
use Drupal\Tests\image\Kernel\ImageFieldCreationTrait;
use Drupal\Tests\TestFileCreationTrait;
class TextimageFieldFormatterTest extends TextimageTestBase {
use ImageFieldCreationTrait;
use TestFileCreationTrait;
protected $dumpHeaders = TRUE;
public function testTextimageTextFieldFormatter() {
$field_name = strtolower($this
->randomMachineName());
$this
->createTextField($field_name, 'article');
$field_value = '<p>Para1</p><!-- Comment --> Para2 "Title" One …';
$nid = $this
->createTextimageNode('text', $field_name, $field_value, 'article', 'Overly test');
$node = Node::load($nid);
$textimage = $this->textimageFactory
->get()
->setStyle(ImageStyle::load('textimage_test'))
->setTokenData([
'node' => $node,
])
->process($field_value);
$textimage_url = $textimage
->getUrl()
->toString();
$rel_url = file_url_transform_relative($textimage_url);
$this
->assertSame([
'Para1 Para2 "Title" One …',
], $textimage
->getText());
$display = $this->entityDisplayRepository
->getViewDisplay('node', $node
->getType(), 'default');
$display_options['type'] = 'textimage_text_field_formatter';
$display_options['settings']['image_style'] = 'textimage_test';
$display_options['settings']['image_link'] = '';
$display_options['settings']['image_alt'] = 'Alternate text: [node:title]';
$display_options['settings']['image_title'] = 'Title: [node:title]';
$display
->setComponent($field_name, $display_options)
->save();
$this
->drupalGet('node/' . $nid);
$elements = $this
->cssSelect("img[src='{$rel_url}']");
$this
->assertNotEmpty($elements, 'Unlinked Textimage displaying on full node view.');
$this
->assertSame('Alternate text: Overly test', $elements[0]
->getAttribute('alt'));
$this
->assertSame('Title: Overly test', $elements[0]
->getAttribute('title'));
$display_options['settings']['image_link'] = 'content';
$display
->setComponent($field_name, $display_options)
->save();
$href = $node
->toUrl()
->toString();
$this
->drupalGet($node
->toUrl());
$elements = $this
->cssSelect("a[href*='{$href}'] img[src='{$rel_url}']");
$this
->assertNotEmpty($elements, 'Textimage linked to content displaying on full node view.');
$this
->assertSame('Alternate text: Overly test', $elements[0]
->getAttribute('alt'));
$this
->assertSame('Title: Overly test', $elements[0]
->getAttribute('title'));
$display_options['settings']['image_link'] = 'file';
$display_options['settings']['image_alt'] = 'Alternate text: [node:author]';
$display_options['settings']['image_title'] = 'Title: [node:author]';
$display
->setComponent($field_name, $display_options)
->save();
$this
->drupalGet($node
->toUrl());
$elements = $this
->cssSelect("a[href='{$textimage_url}'] img[src='{$rel_url}']");
$this
->assertNotEmpty($elements, 'Textimage linked to image file displaying on full node view.');
$this
->assertSame($elements[0]
->getAttribute('alt'), 'Alternate text: ' . $this->adminUser
->getAccountName());
$this
->assertSame($elements[0]
->getAttribute('title'), 'Title: ' . $this->adminUser
->getAccountName());
$site_name = \Drupal::configFactory()
->get('system.site')
->get('name');
$display_options['settings']['image_alt'] = 'Alternate text: [node:author] [site:name]';
$display_options['settings']['image_title'] = 'Title: [node:author] [site:name]';
$display
->setComponent($field_name, $display_options)
->save();
$this
->drupalGet($node
->toUrl());
$elements = $this
->cssSelect("a[href='{$textimage_url}'] img[src='{$rel_url}']");
$this
->assertSame($elements[0]
->getAttribute('alt'), 'Alternate text: ' . $this->adminUser
->getAccountName() . ' ' . $site_name);
$this
->assertSame($elements[0]
->getAttribute('title'), 'Title: ' . $this->adminUser
->getAccountName() . ' ' . $site_name);
$this
->assertCacheTag('config:image.style.textimage_test');
$this
->assertCacheTag('config:system.site');
$bubbleable_metadata = new BubbleableMetadata();
$token_resolved = \Drupal::service('token')
->replace('[textimage:uri:' . $field_name . '] [site:name]', [
'node' => $node,
], [], $bubbleable_metadata);
$this
->assertSame($this
->getTextimageUriFromStyleAndText('textimage_test', $field_value) . ' ' . $site_name, $token_resolved);
$expected_tags = [
'config:image.style.textimage_test',
'config:system.site',
'node:' . $node
->id(),
];
$this
->assertSame($expected_tags, array_intersect($expected_tags, $bubbleable_metadata
->getCacheTags()), 'Token replace produced expected cache tags.');
$bubbleable_metadata = new BubbleableMetadata();
$token_resolved = \Drupal::service('token')
->replace('[textimage:url:' . $field_name . ']', [
'node' => $node,
], [], $bubbleable_metadata);
$this
->assertSame($this
->getTextimageUrlFromStyleAndText('textimage_test', $field_value)
->toString(), $token_resolved);
}
public function testTextimageMultiValueTextFieldFormatter() {
$field_name = strtolower($this
->randomMachineName());
$this
->createTextField($field_name, 'article', [
'cardinality' => 4,
]);
$field_value = [];
for ($i = 0; $i < 4; $i++) {
$field_value[] = $this
->randomMachineName(20);
}
$nid = $this
->createTextimageNode('text', $field_name, $field_value, 'article', 'Test Title');
$node = Node::load($nid);
$textimage_url = $this->textimageFactory
->get()
->setStyle(ImageStyle::load('textimage_test'))
->setTokenData([
'node' => $node,
])
->process($field_value)
->getUrl()
->toString();
$rel_url = file_url_transform_relative($textimage_url);
$display = $this->entityDisplayRepository
->getViewDisplay('node', $node
->getType(), 'default');
$display_options['type'] = 'textimage_text_field_formatter';
$display_options['settings']['image_style'] = 'textimage_test';
$display_options['settings']['image_text_values'] = 'merge';
$display_options['settings']['image_alt'] = 'Alternate text: [node:title]';
$display_options['settings']['image_title'] = 'Title: [node:title]';
$display
->setComponent($field_name, $display_options)
->save();
$this
->drupalGet('node/' . $nid);
$elements = $this
->cssSelect("div.field--name-{$field_name} div.field__items img");
$this
->assertCount(1, $elements);
$this
->assertSame($rel_url, $elements[0]
->getAttribute('src'));
$this
->assertSame('Alternate text: Test Title', $elements[0]
->getAttribute('alt'));
$this
->assertSame('Title: Test Title', $elements[0]
->getAttribute('title'));
$display = $this->entityDisplayRepository
->getViewDisplay('node', $node
->getType(), 'default');
$display_options['settings']['image_text_values'] = 'itemize';
$display
->setComponent($field_name, $display_options)
->save();
$this
->drupalGet('node/' . $nid);
$elements = $this
->cssSelect("div.field--name-{$field_name} div.field__items img");
$this
->assertCount(4, $elements);
for ($i = 0; $i < 4; $i++) {
$textimage_url = $this->textimageFactory
->get()
->setStyle(ImageStyle::load('textimage_test'))
->setTokenData([
'node' => $node,
])
->process($field_value[$i])
->getUrl()
->toString();
$rel_url = file_url_transform_relative($textimage_url);
$this
->assertSame($rel_url, $elements[$i]
->getAttribute('src'));
$this
->assertSame('Alternate text: Test Title', $elements[$i]
->getAttribute('alt'));
$this
->assertSame('Title: Test Title', $elements[$i]
->getAttribute('title'));
}
}
public function testTextimageImageFieldFormatter() {
$field_name = strtolower($this
->randomMachineName());
$min_resolution = 50;
$max_resolution = 100;
$field_settings = [
'max_resolution' => $max_resolution . 'x' . $max_resolution,
'min_resolution' => $min_resolution . 'x' . $min_resolution,
'alt_field' => 1,
];
$this
->createImageField($field_name, 'article', [], $field_settings);
$field_value = $this
->getTestFiles('image', 39325)[0];
$nid = $this
->createTextimageNode('image', $field_name, $field_value, 'article', $this
->randomMachineName());
$node = Node::load($nid);
$node_title = $node
->get('title')[0]
->get('value')
->getValue();
$fid = $node->{$field_name}[0]
->get('target_id')
->getValue();
$source_image_file = File::load($fid);
$source_image_file_url = file_create_url($source_image_file
->getFileUri());
$textimage_url = $this->textimageFactory
->get()
->setSourceImageFile($source_image_file)
->setStyle(ImageStyle::load('textimage_test'))
->setTokenData([
'node' => $node,
'file' => $source_image_file,
])
->process(NULL)
->getUrl()
->toString();
$rel_url = file_url_transform_relative($textimage_url);
$display = $this->entityDisplayRepository
->getViewDisplay('node', $node
->getType(), 'default');
$display_options['type'] = 'textimage_image_field_formatter';
$display_options['settings']['image_style'] = 'textimage_test';
$display_options['settings']['image_link'] = '';
$display_options['settings']['image_alt'] = 'Alternate text: [node:title]';
$display_options['settings']['image_title'] = 'Title: [node:title]';
$display
->setComponent($field_name, $display_options)
->save();
$this
->drupalGet('node/' . $nid);
$elements = $this
->cssSelect("img[src='{$rel_url}']");
$this
->assertNotEmpty($elements, 'Unlinked Textimage displaying on full node view.');
$this
->assertSame($elements[0]
->getAttribute('alt'), 'Alternate text: ' . $node_title);
$this
->assertSame($elements[0]
->getAttribute('title'), 'Title: ' . $node_title);
$display_options['settings']['image_link'] = 'content';
$display_options['settings']['image_alt'] = '';
$display
->setComponent($field_name, $display_options)
->save();
$href = $node
->toUrl()
->toString();
$this
->drupalGet($node
->toUrl());
$elements = $this
->cssSelect("a[href*='{$href}'] img[src='{$rel_url}']");
$this
->assertNotEmpty($elements, 'Textimage linked to content displaying on full node view.');
$this
->assertSame($elements[0]
->getAttribute('alt'), 'test alt text');
$this
->assertSame($elements[0]
->getAttribute('title'), 'Title: ' . $node_title);
$display_options['settings']['image_link'] = 'file';
$display_options['settings']['image_alt'] = 'Alternate text: [node:author]';
$display_options['settings']['image_title'] = 'Title: [node:author]';
$display
->setComponent($field_name, $display_options)
->save();
$this
->drupalGet($node
->toUrl());
$elements = $this
->cssSelect("a[href='{$source_image_file_url}'] img[src='{$rel_url}']");
$this
->assertNotEmpty($elements, 'Textimage linked to original image file.');
$this
->assertSame($elements[0]
->getAttribute('alt'), 'Alternate text: ' . $this->adminUser
->getAccountName());
$this
->assertSame($elements[0]
->getAttribute('title'), 'Title: ' . $this->adminUser
->getAccountName());
$display_options['settings']['image_link'] = 'derivative';
$display
->setComponent($field_name, $display_options)
->save();
$this
->drupalGet($node
->toUrl());
$elements = $this
->cssSelect("a[href='{$textimage_url}'] img[src='{$rel_url}']");
$this
->assertNotEmpty($elements, 'Textimage linked to derivative image file.');
$this
->assertSame($elements[0]
->getAttribute('alt'), 'Alternate text: ' . $this->adminUser
->getAccountName());
$this
->assertSame($elements[0]
->getAttribute('title'), 'Title: ' . $this->adminUser
->getAccountName());
$site_name = \Drupal::configFactory()
->get('system.site')
->get('name');
$display_options['settings']['image_alt'] = 'Alternate text: [node:author] [site:name]';
$display_options['settings']['image_title'] = 'Title: [node:author] [site:name]';
$display
->setComponent($field_name, $display_options)
->save();
$this
->drupalGet($node
->toUrl());
$this
->assertCacheTag('config:image.style.textimage_test');
$this
->assertCacheTag('config:system.site');
$this
->assertCacheTag('node:' . $node
->id());
$this
->assertCacheTag('file:' . $source_image_file
->id());
$this
->assertCacheTag('user:' . $this->adminUser
->id());
$bubbleable_metadata = new BubbleableMetadata();
$token_resolved = \Drupal::service('token')
->replace('[textimage:uri:' . $field_name . '] [site:name]', [
'node' => $node,
], [], $bubbleable_metadata);
$textimage = $this->textimageFactory
->get()
->setSourceImageFile($source_image_file)
->setStyle(ImageStyle::load('textimage_test'))
->setTokenData([
'node' => $node,
'file' => $source_image_file,
])
->process(NULL);
$this
->assertSame($textimage
->getUri() . ' ' . $site_name, $token_resolved);
$expected_tags = [
'config:image.style.textimage_test',
'config:system.site',
'node:' . $node
->id(),
'file:' . $source_image_file
->id(),
];
$this
->assertEquals($expected_tags, array_intersect($expected_tags, $bubbleable_metadata
->getCacheTags()), 'Token replace produced expected cache tags.');
$bubbleable_metadata = new BubbleableMetadata();
$token_resolved = \Drupal::service('token')
->replace('[textimage:url:' . $field_name . ']', [
'node' => $node,
], [], $bubbleable_metadata);
$this
->assertSame($textimage
->getUrl()
->toString(), $token_resolved);
}
public function testTextimageCaching() {
$field_name = 'test_caching';
$this
->createTextField($field_name, 'article');
$field_value = 'test for caching';
$nid = $this
->createTextimageNode('text', $field_name, $field_value, 'article', 'test');
$node = Node::load($nid);
$display = $this->entityDisplayRepository
->getViewDisplay('node', $node
->getType(), 'default');
$display_options['type'] = 'textimage_text_field_formatter';
$display_options['settings']['image_style'] = 'textimage_test';
$display_options['settings']['image_link'] = '';
$display_options['settings']['image_build_deferred'] = FALSE;
$display
->setComponent($field_name, $display_options)
->save();
$this
->drupalGet('node/' . $nid);
$this
->assertText('Built Textimage');
Cache::invalidateTags([
'rendered',
]);
$this
->drupalGet('node/' . $nid);
$this
->assertText('Cached Textimage');
Cache::invalidateTags([
'rendered',
]);
\Drupal::cache('textimage')
->deleteAll();
$this
->drupalGet('node/' . $nid);
$this
->assertText('Stored Textimage');
Cache::invalidateTags([
'rendered',
]);
$this
->drupalGet('node/' . $nid);
$this
->assertText('Cached Textimage');
}
}