public function TextimageFieldFormatterTest::testTextimageMultiValueTextFieldFormatter in Textimage 8.4
Same name and namespace in other branches
- 8.3 tests/src/Functional/TextimageFieldFormatterTest.php \Drupal\Tests\textimage\Functional\TextimageFieldFormatterTest::testTextimageMultiValueTextFieldFormatter()
Test Textimage formatter on multi-value text fields.
File
- tests/
src/ Functional/ TextimageFieldFormatterTest.php, line 127
Class
- TextimageFieldFormatterTest
- Test Textimage formatters on node display.
Namespace
Drupal\Tests\textimage\FunctionalCode
public function testTextimageMultiValueTextFieldFormatter() {
// Create a multi-value text field for Textimage test.
$field_name = strtolower($this
->randomMachineName());
$this
->createTextField($field_name, 'article', [
'cardinality' => 4,
]);
// Create a new node, with 4 text values for the field.
$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);
// Test the textimage formatter - one image.
$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'));
// Test the textimage formatter - multiple images.
$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'));
}
}