ListFormatterTest.php in Facebook Instant Articles 8.2
File
tests/src/Kernel/Plugin/Field/FieldFormatter/ListFormatterTest.php
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\InstantArticle;
use Facebook\InstantArticles\Elements\ListElement;
class ListFormatterTest extends FormatterTestBase {
protected function setUp() : void {
parent::setUp();
$this->display
->setComponent($this->fieldName, [
'type' => 'fbia_list',
'settings' => [],
]);
$this->display
->save();
}
public function testListFormatter() {
$value_alpha = 'I am a random value.';
$value_beta = 'I am another random value.';
$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_CONTENT, $this->normalizerMock);
$children = $article
->getChildren();
$list = $children[0];
$this
->assertEquals(1, count($children));
$this
->assertTrue($list instanceof ListElement);
$this
->assertFalse($list
->isOrdered());
$list_items = $list
->getItems();
$this
->assertEquals(2, count($list_items));
$this
->assertEquals($value_alpha, $list_items[0]
->getPlainText());
$this->display
->setComponent($this->fieldName, [
'type' => 'fbia_list',
'settings' => [
'is_ordered' => TRUE,
],
]);
$this->display
->save();
$formatter = $this->display
->getRenderer($this->fieldName);
$article = InstantArticle::create();
$formatter
->viewInstantArticle($entity->{$this->fieldName}, $article, Regions::REGION_CONTENT, $this->normalizerMock);
$children = $article
->getChildren();
$list = $children[0];
$this
->assertTrue($list
->isOrdered());
}
}