ImageFormatterTest.php in Facebook Instant Articles 3.x
File
tests/src/Kernel/Plugin/Field/FieldFormatter/ImageFormatterTest.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\Regions;
use Facebook\InstantArticles\Elements\Caption;
use Facebook\InstantArticles\Elements\Image;
use Facebook\InstantArticles\Elements\InstantArticle;
class ImageFormatterTest extends FormatterTestBase {
protected static $modules = [
'image',
'file',
];
protected function getFieldType() {
return 'image';
}
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('file');
$this
->installSchema('file', [
'file_usage',
]);
$this->display
->setComponent($this->fieldName, [
'type' => 'fbia_image',
'settings' => [],
]);
$this->display
->save();
}
public function testImageFormatter() {
$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);
$image = $article
->getHeader()
->getCover();
$this
->assertTrue($image instanceof Image);
$this
->assertNull($image
->getCaption());
$this
->assertNull($image
->getPresentation());
$this->display
->setComponent($this->fieldName, [
'type' => 'fbia_image',
'settings' => [
'caption' => TRUE,
'presentation' => Image::ASPECT_FIT,
],
]);
$this->display
->save();
$formatter = $this->display
->getRenderer($this->fieldName);
$article = InstantArticle::create();
$formatter
->viewInstantArticle($entity->{$this->fieldName}, $article, Regions::REGION_HEADER, $this->normalizerMock);
$image = $article
->getHeader()
->getCover();
$this
->assertTrue($image instanceof Image);
$this
->assertTrue($image
->getCaption() instanceof Caption);
$this
->assertEquals(Image::ASPECT_FIT, $image
->getPresentation());
$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 Image);
}
}