You are here

public function AuthorReferenceFormatterTest::testAuthorReferenceFormatter in Facebook Instant Articles 3.x

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/Plugin/Field/FieldFormatter/AuthorReferenceFormatterTest.php \Drupal\Tests\fb_instant_articles\Kernel\Plugin\Field\FieldFormatter\AuthorReferenceFormatterTest::testAuthorReferenceFormatter()

Test the instant article list formatter.

File

tests/src/Kernel/Plugin/Field/FieldFormatter/AuthorReferenceFormatterTest.php, line 52

Class

AuthorReferenceFormatterTest
Test the instant article author reference field formatter.

Namespace

Drupal\Tests\fb_instant_articles\Kernel\Plugin\Field\FieldFormatter

Code

public function testAuthorReferenceFormatter() {
  $value_alpha = 'Joe Mayo';
  $value_beta = 'J. Peterman';

  // Referenced entity.
  $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,
  ];

  /** @var \Drupal\fb_instant_articles\Plugin\Field\InstantArticleFormatterInterface $formatter */
  $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());

  // Test an un-linked configuration.
  $this->display
    ->setComponent($this->fieldName, [
    'type' => 'fbia_author_reference',
    'settings' => [
      'link' => FALSE,
    ],
  ]);
  $this->display
    ->save();

  /** @var \Drupal\fb_instant_articles\Plugin\Field\InstantArticleFormatterInterface $formatter */
  $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());
}