You are here

public function ImageFormatterTest::testImageFormatter in Facebook Instant Articles 3.x

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

Tests the instant article image formatter.

File

tests/src/Kernel/Plugin/Field/FieldFormatter/ImageFormatterTest.php, line 47

Class

ImageFormatterTest
Tests for the ImageFormatter.

Namespace

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

Code

public function testImageFormatter() {
  $entity = EntityTest::create([]);

  // Handy method to populate the field with a real value.
  // @see ImageItem::generateSampleValue()
  $entity->{$this->fieldName}
    ->generateSampleItems(2);

  /** @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_HEADER, $this->normalizerMock);

  // Assert that a cover image was added.
  $image = $article
    ->getHeader()
    ->getCover();
  $this
    ->assertTrue($image instanceof Image);

  // Default settings for the image formatter are no captions and an empty
  // presentation value.
  $this
    ->assertNull($image
    ->getCaption());
  $this
    ->assertNull($image
    ->getPresentation());

  // Test config with everything turned on.
  $this->display
    ->setComponent($this->fieldName, [
    'type' => 'fbia_image',
    'settings' => [
      'caption' => TRUE,
      'presentation' => Image::ASPECT_FIT,
    ],
  ]);
  $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_HEADER, $this->normalizerMock);

  // Assert that a cover image was added.
  $image = $article
    ->getHeader()
    ->getCover();
  $this
    ->assertTrue($image instanceof Image);

  // Assert settings are reflected in the output.
  $this
    ->assertTrue($image
    ->getCaption() instanceof Caption);
  $this
    ->assertEquals(Image::ASPECT_FIT, $image
    ->getPresentation());

  // Test adding the image to the body.
  $article = InstantArticle::create();
  $formatter
    ->viewInstantArticle($entity->{$this->fieldName}, $article, Regions::REGION_CONTENT, $this->normalizerMock);
  $children = $article
    ->getChildren();
  $this
    ->assertEquals(2, count($children));
  $this
    ->assertTrue($children[0] instanceof Image);
}