View source
<?php
namespace Drupal\Tests\fb_instant_articles\Kernel;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\KernelTests\KernelTestBase;
use Drupal\user\Entity\User;
use Facebook\InstantArticles\Elements\Blockquote;
use Facebook\InstantArticles\Elements\Paragraph;
class InstantArticleContentEntityNormalizerTest extends KernelTestBase {
protected static $modules = [
'system',
'field',
'serialization',
'node',
'user',
'fb_instant_articles',
];
protected $display;
protected $serializer;
protected $userName;
protected $account;
protected function setUp() : void {
parent::setUp();
$this->serializer = $this->container
->get('serializer');
$this
->installSchema('system', 'sequences');
$this
->installConfig([
'system',
'field',
]);
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this->userName = $this
->randomMachineName();
$account = User::create([
'name' => $this->userName,
'status' => 1,
]);
$account
->enforceIsNew();
$account
->save();
$this->account = $account;
$type = NodeType::create([
'type' => 'article',
'name' => 'article',
]);
$type
->save();
foreach ([
'field_one',
'field_two',
] as $field_name) {
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'type' => 'string_long',
]);
$field_storage
->save();
$instance = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'article',
'label' => $this
->randomMachineName(),
]);
$instance
->save();
}
$this->display = EntityViewDisplay::create([
'targetEntityType' => 'node',
'bundle' => 'article',
'mode' => 'default',
'status' => TRUE,
]);
}
public function testNormalize() {
$title = $this
->randomString();
$node = Node::create([
'title' => $title,
'type' => 'article',
'field_one' => [
'value' => 'This is a value for the first field.',
],
'field_two' => [
'value' => 'This is a value for the second field.',
],
]);
$node
->setOwner($this->account);
$node
->save();
$this->display
->setComponent('field_one', [
'type' => 'fbia_paragraph',
'settings' => [],
]);
$this->display
->setComponent('field_two', [
'type' => 'fbia_blockquote',
'settings' => [],
]);
$this->display
->save();
$article = $this->serializer
->normalize($node, 'fbia', [
'entity_view_display' => $this->display,
]);
$this
->assertEquals($title, $article
->getHeader()
->getTitle()
->getPlainText());
$children = $article
->getChildren();
$this
->assertEquals(2, count($children));
$this
->assertTrue($children[0] instanceof Paragraph);
$this
->assertTrue($children[1] instanceof Blockquote);
$this->display
->setComponent('field_one', [
'type' => 'basic_string',
'settings' => [],
'label' => 'hidden',
]);
$this->display
->setComponent('field_two', [
'type' => 'basic_string',
'settings' => [],
'label' => 'hidden',
]);
$this->display
->save();
$article = $this->serializer
->normalize($node, 'fbia', [
'entity_view_display' => $this->display,
]);
$this
->assertEquals($title, $article
->getHeader()
->getTitle()
->getPlainText());
$children = $article
->getChildren();
$this
->assertEquals(2, count($children));
$this
->assertTrue($children[0] instanceof Paragraph);
$this
->assertTrue($children[1] instanceof Paragraph);
$this
->assertEquals('This is a value for the first field.', $children[0]
->getPlainText());
$this->display
->setComponent('field_one', [
'type' => 'basic_string',
'settings' => [],
]);
$this->display
->setComponent('field_two', [
'type' => 'basic_string',
'settings' => [],
]);
$this->display
->save();
$article = $this->serializer
->normalize($node, 'fbia', [
'entity_view_display' => $this->display,
]);
$this
->assertEquals($title, $article
->getHeader()
->getTitle()
->getPlainText());
$children = $article
->getChildren();
$this
->assertEquals(4, count($children));
}
}