AuthorReferenceFormatterTest.php in Facebook Instant Articles 3.x
File
tests/src/Kernel/Plugin/Field/FieldFormatter/AuthorReferenceFormatterTest.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\Author;
use Facebook\InstantArticles\Elements\InstantArticle;
class AuthorReferenceFormatterTest extends FormatterTestBase {
protected static $modules = [
'user',
];
protected function setUp() : void {
parent::setUp();
$this
->installSchema('system', 'sequences');
$this
->installConfig([
'user',
]);
$this
->installEntitySchema('user');
$this->display
->setComponent($this->fieldName, [
'type' => 'fbia_author_reference',
'settings' => [],
]);
$this->display
->save();
}
protected function getFieldType() {
return 'entity_reference';
}
public function testAuthorReferenceFormatter() {
$value_alpha = 'Joe Mayo';
$value_beta = 'J. Peterman';
$referenced_entity_alpha = $this->container
->get('entity_type.manager')
->getStorage('user')
->create([
'name' => $value_alpha,
]);
$referenced_entity_alpha
->save();
$referenced_entity_beta = $this->container
->get('entity_type.manager')
->getStorage('user')
->create([
'name' => $value_beta,
]);
$referenced_entity_beta
->save();
$entity = EntityTest::create([]);
$entity->{$this->fieldName}[] = [
'entity' => $referenced_entity_alpha,
];
$entity->{$this->fieldName}[] = [
'entity' => $referenced_entity_beta,
];
$formatter = $this->display
->getRenderer($this->fieldName);
$article = InstantArticle::create();
$formatter
->viewInstantArticle($entity->{$this->fieldName}, $article, Regions::REGION_CONTENT, $this->normalizerMock);
$authors = $article
->getHeader()
->getAuthors();
$this
->assertEquals(2, count($authors));
$this
->assertTrue($authors[0] instanceof Author);
$this
->assertEquals($value_alpha, $authors[0]
->getName());
$this
->assertEquals('http://localhost/user/1', $authors[0]
->getUrl());
$this
->assertEquals($value_beta, $authors[1]
->getName());
$this
->assertEquals('http://localhost/user/2', $authors[1]
->getUrl());
$this->display
->setComponent($this->fieldName, [
'type' => 'fbia_author_reference',
'settings' => [
'link' => FALSE,
],
]);
$this->display
->save();
$formatter = $this->display
->getRenderer($this->fieldName);
$article = InstantArticle::create();
$formatter
->viewInstantArticle($entity->{$this->fieldName}, $article, Regions::REGION_CONTENT, $this->normalizerMock);
$authors = $article
->getHeader()
->getAuthors();
$this
->assertNull($authors[0]
->getUrl());
}
}