public function TextimageFieldFormatterTest::testTextimageTextFieldFormatter in Textimage 8.4
Same name and namespace in other branches
- 8.3 tests/src/Functional/TextimageFieldFormatterTest.php \Drupal\Tests\textimage\Functional\TextimageFieldFormatterTest::testTextimageTextFieldFormatter()
Test Textimage formatter on node display and text field.
File
- tests/
src/ Functional/ TextimageFieldFormatterTest.php, line 33
Class
- TextimageFieldFormatterTest
- Test Textimage formatters on node display.
Namespace
Drupal\Tests\textimage\FunctionalCode
public function testTextimageTextFieldFormatter() {
// Create a text field for Textimage test.
$field_name = strtolower($this
->randomMachineName());
$this
->createTextField($field_name, 'article');
// Create a new node.
$field_value = '<p>Para1</p><!-- Comment --> Para2 "Title" One …';
$nid = $this
->createTextimageNode('text', $field_name, $field_value, 'article', 'Overly test');
$node = Node::load($nid);
// Get Textimage URL.
$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);
// Assert HTML tags are stripped and entities are decoded.
$this
->assertSame([
'Para1 Para2 "Title" One …',
], $textimage
->getText());
// Test the textimage formatter - no link.
$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'));
// Test the textimage formatter - linked to content.
$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'));
// Test the textimage formatter - linked to Textimage file.
$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());
// Check that alternate text and title tokens are resolved and their
// cacheability metadata added.
$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
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'config:image.style.textimage_test');
$this
->assertSession()
->responseHeaderContains('X-Drupal-Cache-Tags', 'config:system.site');
// Check URI token.
$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.');
// Check URL token.
$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);
}