You are here

public function InstantArticleContentEntityNormalizerTest::testNormalize in Facebook Instant Articles 3.x

Same name in this branch
  1. 3.x tests/src/Unit/InstantArticleContentEntityNormalizerTest.php \Drupal\Tests\fb_instant_articles\Unit\InstantArticleContentEntityNormalizerTest::testNormalize()
  2. 3.x tests/src/Kernel/InstantArticleContentEntityNormalizerTest.php \Drupal\Tests\fb_instant_articles\Kernel\InstantArticleContentEntityNormalizerTest::testNormalize()
Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/InstantArticleContentEntityNormalizerTest.php \Drupal\Tests\fb_instant_articles\Kernel\InstantArticleContentEntityNormalizerTest::testNormalize()

Tests the normalize method with real node objects.

Validating that the end-to-end functionality of the normalizer works.

@covers ::normalize

File

tests/src/Kernel/InstantArticleContentEntityNormalizerTest.php, line 129

Class

InstantArticleContentEntityNormalizerTest
Test the Drupal Client wrapper.

Namespace

Drupal\Tests\fb_instant_articles\Kernel

Code

public function testNormalize() {

  // Setup a node with some simple values for testing.
  $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();

  // First test a case of using FBIA formatters.
  $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);

  // Next test with default field formatters.
  $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());

  // Re-run the same test with labels.
  $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));
}