You are here

public function AnalyticsFormatterTest::testAnalyticsFormatter in Facebook Instant Articles 8.2

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

Test the instant article analytics formatter.

File

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

Class

AnalyticsFormatterTest
Test instant articles analytics field formatter.

Namespace

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

Code

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

  /** @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);
  $children = $article
    ->getChildren();
  $this
    ->assertEquals(1, count($children));
  $this
    ->assertTrue($children[0] instanceof Analytics);

  /** @var \Facebook\InstantArticles\Elements\Analytics $analytics */
  $analytics = $children[0];
  $this
    ->assertEquals($value, $analytics
    ->getSource());

  // Test an embedded HTML ad.
  $analytics_html = '<script src="http://example.com/analytics.js"></script>';
  $entity->{$this->fieldName}->value = $analytics_html;
  $this->display
    ->setComponent($this->fieldName, [
    'type' => 'fbia_analytics',
    '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);
  $children = $article
    ->getChildren();
  $this
    ->assertEquals($analytics_html, $children[0]
    ->getHtml()->textContent);
}