class InstantArticleContentEntityNormalizerTest in Facebook Instant Articles 8.2
Same name in this branch
- 8.2 tests/src/Unit/InstantArticleContentEntityNormalizerTest.php \Drupal\Tests\fb_instant_articles\Unit\InstantArticleContentEntityNormalizerTest
- 8.2 tests/src/Kernel/InstantArticleContentEntityNormalizerTest.php \Drupal\Tests\fb_instant_articles\Kernel\InstantArticleContentEntityNormalizerTest
Same name and namespace in other branches
- 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
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\fb_instant_articles\Unit\ContentEntityNormalizerTestBase
- class \Drupal\Tests\fb_instant_articles\Unit\InstantArticleContentEntityNormalizerTest
- class \Drupal\Tests\fb_instant_articles\Unit\ContentEntityNormalizerTestBase
Expanded class hierarchy of InstantArticleContentEntityNormalizerTest
File
- tests/
src/ Unit/ InstantArticleContentEntityNormalizerTest.php, line 27
Namespace
Drupal\Tests\fb_instant_articles\UnitView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContentEntityNormalizerTestBase:: |
protected | property | Mock current language. | |
ContentEntityNormalizerTestBase:: |
protected | function | Get a content entity to test with. | |
InstantArticleContentEntityNormalizerTest:: |
protected | function | Helper function to create a new ContentEntityNormalizer for testing. | |
InstantArticleContentEntityNormalizerTest:: |
public | function | Data provider for testSortComponents. | |
InstantArticleContentEntityNormalizerTest:: |
public | function | Tests the normalize() method. | |
InstantArticleContentEntityNormalizerTest:: |
public | function | Tests the normalize method on an RTL site. | |
InstantArticleContentEntityNormalizerTest:: |
public | function | Tests the sortComponents() method. | |
InstantArticleContentEntityNormalizerTest:: |
public | function | Tests the supportsNormalization() method. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 |