AnalyticsFormatterTest.php in Facebook Instant Articles 8.2
File
tests/src/Kernel/Plugin/Field/FieldFormatter/AnalyticsFormatterTest.php
View source
<?php
namespace Drupal\Tests\fb_instant_articles\Kernel\Plugin\Field\FieldFormatter;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\fb_instant_articles\Plugin\Field\FieldFormatter\FormatterBase;
use Drupal\fb_instant_articles\Regions;
use Facebook\InstantArticles\Elements\Analytics;
use Facebook\InstantArticles\Elements\InstantArticle;
class AnalyticsFormatterTest extends FormatterTestBase {
protected function setUp() : void {
parent::setUp();
$this->display
->setComponent($this->fieldName, [
'type' => 'fbia_analytics',
'settings' => [],
]);
$this->display
->save();
}
public function testAnalyticsFormatter() {
$value = 'http://example.com/analytics';
$entity = EntityTest::create([]);
$entity->{$this->fieldName}->value = $value;
$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);
$analytics = $children[0];
$this
->assertEquals($value, $analytics
->getSource());
$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();
$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);
}
}