You are here

class InstantArticleRssContentEntityNormalizerTest in Facebook Instant Articles 3.x

Same name and namespace in other branches
  1. 8.2 tests/src/Unit/InstantArticleRssContentEntityNormalizerTest.php \Drupal\Tests\fb_instant_articles\Unit\InstantArticleRssContentEntityNormalizerTest

Tests the fbia content entity normalizer class.

@coversDefaultClass \Drupal\fb_instant_articles\Normalizer\InstantArticleRssContentEntityNormalizer

@group fb_instant_articles

Hierarchy

Expanded class hierarchy of InstantArticleRssContentEntityNormalizerTest

File

tests/src/Unit/InstantArticleRssContentEntityNormalizerTest.php, line 19

Namespace

Drupal\Tests\fb_instant_articles\Unit
View source
class InstantArticleRssContentEntityNormalizerTest extends ContentEntityNormalizerTestBase {

  /**
   * Tests the supportsNormalization() method.
   *
   * @covers ::supportsNormalization
   */
  public function testSupportsNormalization() {
    $content_entity = $this
      ->getMockBuilder(ContentEntityInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $config_entity = $this
      ->getMockBuilder(ConfigEntityInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $normalizer = $this
      ->getContentEntityNormalizer();
    $this
      ->assertFalse($normalizer
      ->supportsNormalization($content_entity, 'fbia'));
    $this
      ->assertTrue($normalizer
      ->supportsNormalization($content_entity, 'fbia_rss'));
    $this
      ->assertFalse($normalizer
      ->supportsNormalization($content_entity, 'json'));
    $this
      ->assertFalse($normalizer
      ->supportsNormalization($config_entity, 'fbia'));
  }

  /**
   * Tests the normalize() method.
   *
   * @covers ::normalize
   */
  public function testNormalize() {

    // Test the global settings effect on the output.
    $normalizer = $this
      ->getContentEntityNormalizer([
      'canonical_url_override' => 'http://example.com',
      'analytics_embed_code' => 'analytics embed code',
      'ads.type' => 'source_url',
      'ads.iframe_url' => 'http://example.com',
      'ads.dimensions' => '300x250',
    ], []);
    $now = time();
    $now_date = \DateTime::createFromFormat('U', $now);
    $entity = $this
      ->getContentEntity(NodeInterface::class, '/node/1', 'Test entity', $now, $now, 'Joe Mayo');
    $normalized = $normalizer
      ->normalize($entity, 'fbia_rss');
    $this
      ->assertTrue(is_array($normalized));
    $this
      ->assertEquals('http://example.com/node/1', $normalized['link']);
    $this
      ->assertEquals('Test entity', $normalized['title']);
    $this
      ->assertEquals($now_date
      ->format('c'), $normalized['created']);
    $this
      ->assertEquals($now_date
      ->format('c'), $normalized['modified']);
    $this
      ->assertEquals('Joe Mayo', $normalized['author']);
    $this
      ->assertTrue($normalized['content:encoded'] instanceof InstantArticle);
  }

  /**
   * Helper function to create a new FBIA RSS normalizer for testing.
   *
   * @param array $settings
   *   Global config settings.
   * @param array $components
   *   Entity view display components.
   *
   * @return \Drupal\fb_instant_articles\Normalizer\InstantArticleContentEntityNormalizer
   *   Content entity normalizer object to test against.
   */
  protected function getContentEntityNormalizer(array $settings = [], array $components = []) {
    $config_factory = $this
      ->getConfigFactoryStub([
      'fb_instant_articles.settings' => $settings,
    ]);
    $content_entity_normalizer = $this
      ->getMockBuilder(InstantArticleRssContentEntityNormalizer::class)
      ->setConstructorArgs([
      $config_factory,
    ])
      ->addMethods([
      'getApplicableComponents',
      'getApplicationVersion',
    ])
      ->getMock();
    $content_entity_normalizer
      ->method('getApplicableComponents')
      ->willReturn($components);
    $content_entity_normalizer
      ->method('getApplicationVersion')
      ->willReturn('8.x-2.x');
    $serializer = $this
      ->createMock(Serializer::class);
    $serializer
      ->method('normalize')
      ->willReturn(InstantArticle::create());
    $content_entity_normalizer
      ->setSerializer($serializer);
    return $content_entity_normalizer;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentEntityNormalizerTestBase::$currentLanguage protected property Mock current language.
ContentEntityNormalizerTestBase::getContentEntity protected function Get a content entity to test with.
InstantArticleRssContentEntityNormalizerTest::getContentEntityNormalizer protected function Helper function to create a new FBIA RSS normalizer for testing.
InstantArticleRssContentEntityNormalizerTest::testNormalize public function Tests the normalize() method.
InstantArticleRssContentEntityNormalizerTest::testSupportsNormalization public function Tests the supportsNormalization() method.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 308
UnitTestCase::setUpBeforeClass public static function