You are here

public function VideoFormatterTest::testVideoFormatter in Facebook Instant Articles 8.2

Same name and namespace in other branches
  1. 3.x tests/src/Kernel/Plugin/Field/FieldFormatter/VideoFormatterTest.php \Drupal\Tests\fb_instant_articles\Kernel\Plugin\Field\FieldFormatter\VideoFormatterTest::testVideoFormatter()

Tests the instant article video formatter.

@covers ::viewInstantArticle

File

tests/src/Kernel/Plugin/Field/FieldFormatter/VideoFormatterTest.php, line 55

Class

VideoFormatterTest
Tests for the VideoFormatter.

Namespace

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

Code

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

  // Handy method to populate the field with a real value.
  // @see FileItem::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 video was added.
  $video = $article
    ->getHeader()
    ->getCover();
  $this
    ->assertTrue($video instanceof Video);

  // Assert default settings for the video field formatter.
  $this
    ->assertNull($video
    ->getPresentation());
  $this
    ->assertFalse($video
    ->isControlsShown());
  $this
    ->assertTrue($video
    ->isAutoplay());

  // Test config with everything inverted.
  $this->display
    ->setComponent($this->fieldName, [
    'type' => 'fbia_video',
    'settings' => [
      'presentation' => Video::ASPECT_FIT,
      'controls' => TRUE,
      'autoplay' => FALSE,
      'feed_cover' => TRUE,
    ],
  ]);
  $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 video was added.
  $video = $article
    ->getHeader()
    ->getCover();
  $this
    ->assertTrue($video instanceof Video);

  // Assert settings are reflected in the output.
  $this
    ->assertEquals(Video::ASPECT_FIT, $video
    ->getPresentation());
  $this
    ->assertTrue($video
    ->isControlsShown());
  $this
    ->assertFalse($video
    ->isAutoplay());

  // Test adding the video 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 Video);
}