You are here

class InstantArticleContentEntityNormalizerTest in Facebook Instant Articles 8.2

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

Tests the fbia content entity normalizer class.

@coversDefaultClass \Drupal\fb_instant_articles\Normalizer\InstantArticleContentEntityNormalizer

@group fb_instant_articles

Hierarchy

Expanded class hierarchy of InstantArticleContentEntityNormalizerTest

File

tests/src/Unit/InstantArticleContentEntityNormalizerTest.php, line 27

Namespace

Drupal\Tests\fb_instant_articles\Unit
View source
class InstantArticleContentEntityNormalizerTest 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
      ->assertTrue($normalizer
      ->supportsNormalization($content_entity, 'fbia'));
    $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();
    $entity = $this
      ->getContentEntity(NodeInterface::class, '/node/1', 'Test entity', $now, $now, 'Joe Mayo');
    $article = $normalizer
      ->normalize($entity, 'fbia');
    $this
      ->assertTrue($article instanceof InstantArticle);
    $this
      ->assertEquals('http://example.com/node/1', $article
      ->getCanonicalURL());
    $this
      ->assertEquals('Test entity', $article
      ->getHeader()
      ->getTitle()
      ->getPlainText());
    $this
      ->assertEquals($now, $article
      ->getHeader()
      ->getPublished()
      ->getDatetime()
      ->format('U'));
    $this
      ->assertEquals($now, $article
      ->getHeader()
      ->getModified()
      ->getDatetime()
      ->format('U'));
    $this
      ->assertEquals('Joe Mayo', $article
      ->getHeader()
      ->getAuthors()[0]
      ->getName());
    $children = $article
      ->getChildren();

    /** @var \Facebook\InstantArticles\Elements\Analytics $analytics */
    $analytics = $children[0];
    $this
      ->assertTrue($analytics instanceof Analytics);
    $this
      ->assertEquals('analytics embed code', $analytics
      ->getHtml()->ownerDocument
      ->saveHTML($analytics
      ->getHtml()));
    $ads = $article
      ->getHeader()
      ->getAds();
    $this
      ->assertEquals(1, count($ads));
    $this
      ->assertEquals($ads[0]
      ->getWidth(), 300);
    $this
      ->assertEquals($ads[0]
      ->getHeight(), 250);
    $this
      ->assertEquals($ads[0]
      ->getSource(), 'http://example.com');
  }

  /**
   * Tests the normalize method on an RTL site.
   *
   * @covers ::normalize
   */
  public function testNormalizeRtl() {
    $normalizer = $this
      ->getContentEntityNormalizer([], [], LanguageInterface::DIRECTION_RTL);
    $now = time();
    $entity = $this
      ->getContentEntity(NodeInterface::class, '/node/1', 'Test entity', $now, $now, 'Joe Mayo');
    $article = $normalizer
      ->normalize($entity, 'fbia');
    $this
      ->assertTrue($article
      ->isRTLEnabled());
  }

  /**
   * Tests the sortComponents() method.
   *
   * @dataProvider sortComponentsProvider
   * @covers ::sortComponents
   */
  public function testSortComponents($components, $expected) {
    uasort($components, [
      InstantArticleContentEntityNormalizer::class,
      'sortComponents',
    ]);

    // Re-key the array for equality check.
    $components = array_values($components);
    $this
      ->assertEquals($expected, $components);
  }

  /**
   * Data provider for testSortComponents.
   *
   * @return array
   *   Return an array or arrays of arguments to testSortComponents.
   */
  public function sortComponentsProvider() {
    return [
      [
        [
          [
            'region' => 'header',
            'weight' => 0,
          ],
          [
            'region' => 'content',
            'weight' => 1,
          ],
          [
            'region' => 'footer',
            'weigth' => 2,
          ],
        ],
        [
          [
            'region' => 'header',
            'weight' => 0,
          ],
          [
            'region' => 'content',
            'weight' => 1,
          ],
          [
            'region' => 'footer',
            'weigth' => 2,
          ],
        ],
      ],
      [
        [
          [
            'region' => 'header',
            'weight' => 0,
          ],
          [
            'region' => 'footer',
            'weight' => 2,
          ],
          [
            'region' => 'content',
            'weight' => 1,
          ],
        ],
        [
          [
            'region' => 'header',
            'weight' => 0,
          ],
          [
            'region' => 'content',
            'weight' => 1,
          ],
          [
            'region' => 'footer',
            'weight' => 2,
          ],
        ],
      ],
      [
        [
          [
            'region' => 'footer',
            'weight' => 2,
          ],
          [
            'region' => 'header',
            'weight' => 0,
          ],
          [
            'region' => 'content',
            'weight' => 1,
          ],
        ],
        [
          [
            'region' => 'header',
            'weight' => 0,
          ],
          [
            'region' => 'content',
            'weight' => 1,
          ],
          [
            'region' => 'footer',
            'weight' => 2,
          ],
        ],
      ],
      [
        [
          [
            'region' => 'header',
            'weight' => 100,
          ],
          [
            'region' => 'content',
            'weight' => -100,
          ],
          [
            'region' => 'footer',
            'weight' => 0,
          ],
        ],
        [
          [
            'region' => 'header',
            'weight' => 100,
          ],
          [
            'region' => 'content',
            'weight' => -100,
          ],
          [
            'region' => 'footer',
            'weight' => 0,
          ],
        ],
      ],
      [
        [
          [
            'region' => 'footer',
          ],
          [
            'region' => 'content',
          ],
          [
            'region' => 'header',
          ],
        ],
        [
          [
            'region' => 'header',
          ],
          [
            'region' => 'content',
          ],
          [
            'region' => 'footer',
          ],
        ],
      ],
    ];
  }

  /**
   * Helper function to create a new ContentEntityNormalizer for testing.
   *
   * @param array $settings
   *   Global config settings.
   * @param array $components
   *   Entity view display components.
   * @param string $language_direction
   *   Language direction.
   *
   * @return \Drupal\fb_instant_articles\Normalizer\InstantArticleContentEntityNormalizer
   *   Content entity normalizer object to test against.
   */
  protected function getContentEntityNormalizer(array $settings = [], array $components = [], $language_direction = LanguageInterface::DIRECTION_LTR) {
    $config_factory = $this
      ->getConfigFactoryStub([
      'fb_instant_articles.settings' => $settings,
    ]);
    $entity_field_manager = $this
      ->getMockBuilder(EntityFieldManagerInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $entity_storage = $this
      ->createMock(EntityStorageInterface::class);
    $entity_type_manager = $this
      ->getMockBuilder(EntityTypeManagerInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $entity_type_manager
      ->method('getStorage')
      ->willReturn($entity_storage);
    $info_parser = $this
      ->createMock(InfoParserInterface::class);
    $module_handler = $this
      ->createMock(ModuleHandlerInterface::class);
    $this->currentLanguage = $this
      ->getMockBuilder(Language::class)
      ->disableOriginalConstructor()
      ->onlyMethods([
      'getDirection',
    ])
      ->getMock();
    $this->currentLanguage
      ->expects($this
      ->any())
      ->method('getDirection')
      ->willReturn($language_direction);
    $language_manager = $this
      ->getMockBuilder(LanguageManager::class)
      ->disableOriginalConstructor()
      ->onlyMethods([
      'getCurrentLanguage',
    ])
      ->getMock();
    $language_manager
      ->expects($this
      ->once())
      ->method('getCurrentLanguage')
      ->willReturn($this->currentLanguage);
    $content_entity_normalizer = $this
      ->getMockBuilder(InstantArticleContentEntityNormalizer::class)
      ->setConstructorArgs([
      $config_factory,
      $entity_field_manager,
      $entity_type_manager,
      $info_parser,
      $module_handler,
      $language_manager,
    ])
      ->onlyMethods([
      'getApplicableComponents',
      'getApplicationVersion',
    ])
      ->getMock();
    $content_entity_normalizer
      ->method('getApplicableComponents')
      ->willReturn($components);
    $content_entity_normalizer
      ->method('getApplicationVersion')
      ->willReturn('8.x-2.x');
    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.
InstantArticleContentEntityNormalizerTest::getContentEntityNormalizer protected function Helper function to create a new ContentEntityNormalizer for testing.
InstantArticleContentEntityNormalizerTest::sortComponentsProvider public function Data provider for testSortComponents.
InstantArticleContentEntityNormalizerTest::testNormalize public function Tests the normalize() method.
InstantArticleContentEntityNormalizerTest::testNormalizeRtl public function Tests the normalize method on an RTL site.
InstantArticleContentEntityNormalizerTest::testSortComponents public function Tests the sortComponents() method.
InstantArticleContentEntityNormalizerTest::testSupportsNormalization public function Tests the supportsNormalization() method.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
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 340