View source
<?php
namespace Drupal\Tests\fb_instant_articles\Kernel\Plugin\Field\FieldFormatter;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\fb_instant_articles\Regions;
use Facebook\InstantArticles\Elements\InstantArticle;
use Facebook\InstantArticles\Elements\Video;
class VideoFormatterTest extends FormatterTestBase {
protected static $modules = [
'file',
];
protected function getFieldType() {
return 'file';
}
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('file');
$this
->installSchema('file', [
'file_usage',
]);
$this->display
->setComponent($this->fieldName, [
'type' => 'fbia_video',
'settings' => [],
]);
$this->display
->save();
}
public function testVideoFormatter() {
$entity = EntityTest::create([]);
$entity->{$this->fieldName}
->generateSampleItems(2);
$formatter = $this->display
->getRenderer($this->fieldName);
$article = InstantArticle::create();
$formatter
->viewInstantArticle($entity->{$this->fieldName}, $article, Regions::REGION_HEADER, $this->normalizerMock);
$video = $article
->getHeader()
->getCover();
$this
->assertTrue($video instanceof Video);
$this
->assertNull($video
->getPresentation());
$this
->assertFalse($video
->isControlsShown());
$this
->assertTrue($video
->isAutoplay());
$this->display
->setComponent($this->fieldName, [
'type' => 'fbia_video',
'settings' => [
'presentation' => Video::ASPECT_FIT,
'controls' => TRUE,
'autoplay' => FALSE,
'feed_cover' => TRUE,
],
]);
$this->display
->save();
$formatter = $this->display
->getRenderer($this->fieldName);
$article = InstantArticle::create();
$formatter
->viewInstantArticle($entity->{$this->fieldName}, $article, Regions::REGION_HEADER, $this->normalizerMock);
$video = $article
->getHeader()
->getCover();
$this
->assertTrue($video instanceof Video);
$this
->assertEquals(Video::ASPECT_FIT, $video
->getPresentation());
$this
->assertTrue($video
->isControlsShown());
$this
->assertFalse($video
->isAutoplay());
$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);
}
public function testVideoFormatterCanonicalUrl() {
$entity = EntityTest::create([]);
$entity->{$this->fieldName}
->generateSampleItems(1);
$config = $this
->config('fb_instant_articles.settings');
$config
->set('canonical_url_override', 'http://example.com/override')
->save();
$formatter = $this->display
->getRenderer($this->fieldName);
$article = InstantArticle::create();
$formatter
->viewInstantArticle($entity->{$this->fieldName}, $article, Regions::REGION_CONTENT, $this->normalizerMock);
$children = $article
->getChildren();
$this
->assertStringStartsWith('http://example.com/override', $children[0]
->getUrl());
}
}