View source
<?php
namespace Drupal\Tests\fb_instant_articles\Kernel\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\fb_instant_articles\Regions;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Facebook\InstantArticles\Elements\InstantArticle;
class SubtitleFormatterTest extends FormatterTestBase {
protected static $modules = [
'text',
'filter',
'filter_test',
];
protected $textFieldName;
protected function setUp() : void {
parent::setUp();
$this->textFieldName = mb_strtolower($this
->randomMachineName());
$field_storage = FieldStorageConfig::create([
'field_name' => $this->textFieldName,
'entity_type' => $this->entityType,
'type' => 'text_long',
]);
$field_storage
->save();
$instance = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => $this->bundle,
'label' => $this
->randomMachineName(),
]);
$instance
->save();
$this
->installConfig([
'text',
'filter',
'filter_test',
]);
$this->display
->save();
$this->display = EntityViewDisplay::load($this->display
->id());
$this->display
->setComponent($this->textFieldName, [
'type' => 'fbia_subtitle',
'settings' => [],
]);
$this->display
->setComponent($this->fieldName, [
'type' => 'fbia_subtitle',
'settings' => [],
]);
$this->display
->save();
}
public function testSubtitleFormatter() {
$value_alpha = 'Inspiring subtitle to set the tone';
$value_beta = 'Another subtitle, which should never be seen.';
$entity = EntityTest::create([]);
$entity->{$this->fieldName}[] = [
'value' => $value_alpha,
];
$entity->{$this->fieldName}[] = [
'value' => $value_beta,
];
$formatter = $this->display
->getRenderer($this->fieldName);
$article = InstantArticle::create();
$formatter
->viewInstantArticle($entity->{$this->fieldName}, $article, Regions::REGION_HEADER, $this->normalizerMock);
$this
->assertEquals($value_alpha, $article
->getHeader()
->getSubtitle()
->getPlainText());
}
public function testSubtitleFormatterTextLong() {
$value = '<p>Inspiring <strong>subtitle</strong> to set <a href="https://en.wikipedia.org/wiki/Tone_(literature)">the tone</a>.</p>';
$entity = EntityTest::create([]);
$entity->{$this->textFieldName}[] = [
'value' => $value,
'format' => 'full_html',
];
$formatter = $this->display
->getRenderer($this->textFieldName);
$article = InstantArticle::create();
$formatter
->viewInstantArticle($entity->{$this->textFieldName}, $article, Regions::REGION_HEADER, $this->normalizerMock);
$subtitle = $article
->getHeader()
->getSubtitle();
$subtitle_dom = $subtitle
->toDOMElement();
$actual_string = $subtitle_dom->ownerDocument
->saveHTML($subtitle_dom);
$this
->assertEquals('<h2>Inspiring <b>subtitle</b> to set <a href="https://en.wikipedia.org/wiki/Tone_(literature)">the tone</a>.</h2>', $actual_string);
}
}