You are here

public function AdFormatterTest::testAdFormatter in Facebook Instant Articles 3.x

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

Test the instant article ad formatter.

File

tests/src/Kernel/Plugin/Field/FieldFormatter/AdFormatterTest.php, line 35

Class

AdFormatterTest
Tests the instant article ad formatter.

Namespace

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

Code

public function testAdFormatter() {
  $ad_url = 'http://example.com/ad';
  $entity = EntityTest::create([]);
  $entity->{$this->fieldName}->value = $ad_url;

  /** @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);
  $ads = $article
    ->getHeader()
    ->getAds();
  $this
    ->assertEquals(1, count($ads));
  $this
    ->assertTrue($ads[0] instanceof Ad);
  $this
    ->assertEquals(300, $ads[0]
    ->getWidth());
  $this
    ->assertEquals(250, $ads[0]
    ->getHeight());
  $this
    ->assertEquals($ad_url, $ads[0]
    ->getSource());

  // Test an embedded HTML ad.
  $ad_html = '<script src="http://example.com/ad.js"></script>';
  $entity->{$this->fieldName}->value = $ad_html;
  $this->display
    ->setComponent($this->fieldName, [
    'type' => 'fbia_ad',
    'settings' => [
      'source_type' => FormatterBase::SOURCE_TYPE_HTML,
    ],
  ]);
  $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);
  $ads = $article
    ->getHeader()
    ->getAds();
  $this
    ->assertEquals(1, count($ads));
  $this
    ->assertEquals(300, $ads[0]
    ->getWidth());
  $this
    ->assertEquals(250, $ads[0]
    ->getHeight());
  $this
    ->assertEquals($ad_html, $ads[0]
    ->getHtml()->textContent);
}