View source
<?php
namespace Drupal\Tests\fb_instant_articles\Kernel\Plugin\Field\FieldFormatter;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\fb_instant_articles\Regions;
use Facebook\InstantArticles\Elements\Blockquote;
use Facebook\InstantArticles\Elements\InstantArticle;
use Facebook\InstantArticles\Elements\Paragraph;
class TransformerFormatterTest extends FormatterTestBase {
protected static $modules = [
'text',
'filter',
'filter_test',
];
protected function getFieldType() {
return 'text_long';
}
protected function setUp() : void {
parent::setUp();
$this
->installConfig([
'text',
'filter',
'filter_test',
]);
$this->display
->setComponent($this->fieldName, [
'type' => 'fbia_transformer',
'settings' => [],
]);
$this->display
->save();
}
public function testTransformerFormatter($markup, $expected_child_count, array $expected_child_instances) {
$entity = EntityTest::create([]);
$entity->{$this->fieldName}[] = [
'value' => $markup,
'format' => 'full_html',
];
$formatter = $this->display
->getRenderer($this->fieldName);
$article = InstantArticle::create();
$formatter
->viewInstantArticle($entity->{$this->fieldName}, $article, Regions::REGION_CONTENT, $this->normalizerMock);
$children = $article
->getChildren();
$this
->assertEquals($expected_child_count, count($children));
foreach ($expected_child_instances as $i => $instance) {
$this
->assertInstanceOf($instance, $children[$i]);
}
}
public function transformerFormatterDataProvider() {
return [
[
'<p>I would drape myself in velvet if it were socially acceptable.</p><p>Puddy: I painted my face Elaine: You painted your face? Puddy: Yeah. Elaine: <strong>Why?</strong> Puddy: Well, you know, support the team.</p>',
2,
[
Paragraph::class,
Paragraph::class,
],
],
[
'<p>Newman: I mean parcels are rarely damaged during shipping. Jerry: Define rarely. Newman: Frequently.</p><blockquote>Six years I’ve had this t-shirt. It’s my best one. I call him Golden Boy.</blockquote>',
2,
[
Paragraph::class,
Blockquote::class,
],
],
[
'<div><a href="http://example.com"></a></div>',
1,
[
Paragraph::class,
],
],
];
}
}