View source
<?php
namespace Drupal\custom_formatters\Tests;
class CustomFormattersGeneralTest extends CustomFormattersTestBase {
public function testCustomFormattersUi() {
$this
->drupalGet('admin/structure');
$this
->assertLinkByHref('admin/structure/formatters');
$this
->assertText('Administer Formatters.');
$this
->drupalGet('admin/structure/formatters');
$expected_title = t(':title | :sitename', [
':title' => 'Formatters',
':sitename' => \Drupal::config('system.site')
->get('name'),
]);
$this
->assertTitle($expected_title);
$this
->assertLink(t('Settings'));
$this
->assertLinkByHref('admin/structure/formatters/settings');
$this
->assertText('Test Formatter');
$this
->assertLinkByHref('admin/structure/formatters/manage/test_formatter');
$this
->assertCustomFormatterExists('test_formatter');
$this
->drupalGet('admin/structure/types/manage/article/display');
$this
->assertRaw('custom_formatters:test_formatter');
$this
->assertRaw('Custom: Test Formatter');
$edit = [
'label_prefix_value' => $this
->randomMachineName(),
];
$this
->drupalPostForm('admin/structure/formatters/settings', $edit, t('Save configuration'));
$this
->assertText(t('Custom Formatters settings have been updated.'));
$this
->drupalGet('admin/structure/types/manage/article/display');
$this
->assertRaw(t('@prefix: Test Formatter', [
'@prefix' => $edit['label_prefix_value'],
]));
$edit = [
'label_prefix' => FALSE,
];
$this
->drupalPostForm('admin/structure/formatters/settings', $edit, t('Save configuration'));
$this
->assertText(t('Custom Formatters settings have been updated.'));
$this
->drupalGet('admin/structure/types/manage/article/display');
$this
->assertRaw('Test Formatter');
}
public function testFormatterTypeFormatterPreset() {
$this->formatter = $this
->createCustomFormatter([
'type' => 'formatter_preset',
'data' => [
'formatter' => 'text_trimmed',
'settings' => [
'trim_length' => 10,
],
],
]);
$this
->setCustomFormatter($this->formatter
->id(), 'body', 'article');
$this
->drupalGet($this->node
->toUrl());
$this
->assert(!strstr($this->content, $this->node
->get('body')[0]->value) && strstr($this->content, substr($this->node
->get('body')[0]->value, 0, 7)), t('Custom formatter output found.'));
}
public function testCustomFormatterTypePhp() {
$text = $this
->randomMachineName();
$this->formatter = $this
->createCustomFormatter([
'type' => 'php',
'data' => "return '{$text}';",
]);
$this
->setCustomFormatter($this->formatter
->id(), 'body', 'article');
$this
->drupalGet($this->node
->toUrl());
$this
->assertText($text, t('Custom formatter output found.'));
}
public function testCustomFormatterTypeTwig() {
$text = $this
->randomMachineName();
$this->formatter = $this
->createCustomFormatter([
'type' => 'twig',
'data' => $text,
]);
$this
->setCustomFormatter($this->formatter
->id(), 'body', 'article');
$this
->drupalGet($this->node
->toUrl());
$this
->assertText($text, t('Custom formatter output found.'));
}
public function testCustomFormatterTypeHtmlToken() {
$text = $this
->randomMachineName();
$this->formatter = $this
->createCustomFormatter([
'type' => 'html_token',
'data' => $text,
]);
$this
->setCustomFormatter($this->formatter
->id(), 'body', 'article');
$this
->drupalGet($this->node
->toUrl());
$this
->assertText($text, t('Custom formatter output found.'));
}
}