.../src/Kernel/EntityEmbedFilterOverridesTest.php | 205 +++++++++++++++++++++ tests/src/Kernel/EntityEmbedFilterTest.php | 201 -------------------- tests/src/Kernel/EntityEmbedFilterTestBase.php | 15 ++ 3 files changed, 220 insertions(+), 201 deletions(-) diff --git a/tests/src/Kernel/EntityEmbedFilterOverridesTest.php b/tests/src/Kernel/EntityEmbedFilterOverridesTest.php new file mode 100644 index 0000000..7f18992 --- /dev/null +++ b/tests/src/Kernel/EntityEmbedFilterOverridesTest.php @@ -0,0 +1,205 @@ +installSchema('file', ['file_usage']); + $this->installEntitySchema('file'); + $this->installEntitySchema('media'); + $this->installConfig('image'); + $this->installConfig('media'); + $this->installConfig('system'); + } + + /** + * Tests overriding of `alt` and `title` for default image field formatter. + */ + public function testOverrideAltAndTitleForImage() { + /** @var \Drupal\file\FileInterface $image */ + $image = $this->getTestFile('image'); + $image->setPermanent(); + $image->save(); + $content = $this->createEmbedCode([ + 'data-entity-type' => 'file', + 'data-entity-uuid' => $image->uuid(), + 'data-entity-embed-display' => 'image:image', + 'data-entity-embed-display-settings' => '{"image_style":"","image_link":""}', + 'data-align' => 'left', + 'data-caption' => 'test caption', + 'alt' => 'This is alt text', + 'title' => 'This is title text', + ]); + + /** @var \Drupal\filter\FilterProcessResult $filter_result */ + $filter_result = $this->filter->process($content, 'en'); + $output = $filter_result->getProcessedText(); + $this->assertEntityEmbedFilterHasRun($output); + + $dom = Html::load($output); + $xpath = new \DOMXPath($dom); + /** @var \DOMNode $rendered_embed */ + $rendered_embed = $xpath->query('//div[contains(@class, "embedded-entity")]')[0]; + $this->assertHasAttributes($rendered_embed, [ + 'alt' => 'This is alt text', + 'data-align' => 'left', + 'data-entity-embed-display' => 'image:image', + 'data-entity-type' => 'file', + 'data-entity-uuid' => $image->uuid(), + 'title' => 'This is title text', + 'data-langcode' => 'en', + ]); + $img = $xpath->query('//div[contains(@class, "embedded-entity")]/img')[0]; + $this->assertHasAttributes($img, [ + 'alt' => 'This is alt text', + 'title' => 'This is title text', + ]); + } + + /** + * Tests overriding of `alt` and `title` for image media items. + */ + public function testOverridesAltAndTitleForImageMedia() { + $this->createMediaType('image', ['id' => 'image']); + // The `alt` field property is enabled by default, the `title` one is not. + // Since we want to test it, enable it. + $source_field = FieldConfig::load('media.image.field_media_image'); + $source_field->setSetting('title_field', TRUE); + $source_field->save(); + $this->container->get('current_user') + ->addRole($this->drupalCreateRole(['view media'])); + + \Drupal::service('file_system')->copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://batfish.jpg'); + /** @var \Drupal\file\FileInterface $file */ + $file = File::create([ + 'uri' => 'public://batfish.jpg', + 'uid' => 2, + ]); + $file->save(); + + $this->createNode([ + 'type' => 'article', + 'title' => 'Red-lipped batfish', + ]); + + $media = Media::create([ + 'bundle' => 'image', + 'name' => 'Screaming hairy armadillo', + 'field_media_image' => [ + [ + 'target_id' => $file->id(), + 'alt' => 'default alt', + 'title' => 'default title', + ], + ], + ]); + $media->save(); + + $input = $this->createEmbedCode([ + 'alt' => 'alt 1', + 'title' => 'title 1', + 'data-embed-button' => 'test_media_entity_embed', + 'data-entity-embed-display' => 'view_mode:media.full', + 'data-entity-embed-display-settings' => '', + 'data-entity-type' => 'media', + 'data-entity-uuid' => $media->uuid(), + 'data-langcode' => 'en', + ]); + $input .= $this->createEmbedCode([ + 'alt' => 'alt 2', + 'title' => 'title 2', + 'data-embed-button' => 'test_media_entity_embed', + 'data-entity-embed-display' => 'view_mode:media.full', + 'data-entity-embed-display-settings' => '', + 'data-entity-type' => 'media', + 'data-entity-uuid' => $media->uuid(), + 'data-langcode' => 'en', + ]); + $input .= $this->createEmbedCode([ + 'alt' => 'alt 3', + 'title' => 'title 3', + 'data-embed-button' => 'test_media_entity_embed', + 'data-entity-embed-display' => 'view_mode:media.full', + 'data-entity-embed-display-settings' => '', + 'data-entity-type' => 'media', + 'data-entity-uuid' => $media->uuid(), + 'data-langcode' => 'en', + ]); + + /** @var \Drupal\filter\FilterProcessResult $filter_result */ + $filter_result = $this->filter->process($input, 'en'); + $output = $filter_result->getProcessedText(); + $this->assertNotContains('drupal-entity data-entity-type="media" data-entity', $output); + $this->assertEntityEmbedFilterHasRun($output); + $dom = Html::load($output); + $xpath = new \DOMXPath($dom); + + $img1 = $xpath->query("//img[contains(@alt, 'alt 1')]")[0]; + $this->assertNotEmpty($img1); + $this->assertHasAttributes($img1, [ + 'alt' => 'alt 1', + 'title' => 'title 1', + ]); + + $img2 = $xpath->query("//img[contains(@alt, 'alt 2')]")[0]; + $this->assertNotEmpty($img2); + $this->assertHasAttributes($img2, [ + 'alt' => 'alt 2', + 'title' => 'title 2', + ]); + + $img3 = $xpath->query("//img[contains(@alt, 'alt 3')]")[0]; + $this->assertNotEmpty($img3); + $this->assertHasAttributes($img3, [ + 'alt' => 'alt 3', + 'title' => 'title 3', + ]); + } + + /** + * Retrieves a sample file of the specified type. + */ + protected function getTestFile($type_name, $size = NULL) { + // Get a file to upload. + $file = current($this->getTestFiles($type_name, $size)); + + // Add a filesize property to files as would be read by + // \Drupal\file\Entity\File::load(). + $file->filesize = filesize($file->uri); + + $file = File::create((array) $file); + $file->save(); + return $file; + } + +} diff --git a/tests/src/Kernel/EntityEmbedFilterTest.php b/tests/src/Kernel/EntityEmbedFilterTest.php index 7457d9c..4da5bec 100644 --- a/tests/src/Kernel/EntityEmbedFilterTest.php +++ b/tests/src/Kernel/EntityEmbedFilterTest.php @@ -5,12 +5,6 @@ namespace Drupal\Tests\entity_embed\Kernel; use Drupal\Component\Utility\Html; use Drupal\Core\Entity\TranslatableInterface; use Drupal\Core\Render\RenderContext; -use Drupal\field\Entity\FieldConfig; -use Drupal\file\Entity\File; -use Drupal\media\Entity\Media; -use Drupal\Tests\media\Traits\MediaTypeCreationTrait; -use Drupal\Tests\TestFileCreationTrait; -use Drupal\Tests\user\Traits\UserCreationTrait; /** * Tests the entity_embed filter. @@ -19,33 +13,12 @@ use Drupal\Tests\user\Traits\UserCreationTrait; */ class EntityEmbedFilterTest extends EntityEmbedFilterTestBase { - use UserCreationTrait { - createRole as drupalCreateRole; - } - use MediaTypeCreationTrait; - use TestFileCreationTrait; - - /** - * {@inheritdoc} - */ - protected static $modules = [ - 'media', - 'file', - 'image', - ]; - /** * {@inheritdoc} */ protected function setUp() { parent::setUp(); - $this->installSchema('file', ['file_usage']); - $this->installEntitySchema('file'); - $this->installEntitySchema('media'); - $this->installEntitySchema('date_format'); - $this->installConfig('image'); - $this->installConfig('media'); $this->installConfig('system'); } @@ -374,50 +347,6 @@ class EntityEmbedFilterTest extends EntityEmbedFilterTestBase { $this->assertEntityEmbedFilterHasRun($output); } - /** - * Test that attributes are correctly added when image formatter is used. - */ - public function testAttributesWhenUsingImageFormatter() { - /** @var \Drupal\file\FileInterface $image */ - $image = $this->getTestFile('image'); - $image->setPermanent(); - $image->save(); - $content = $this->createEmbedCode([ - 'data-entity-type' => 'file', - 'data-entity-uuid' => $image->uuid(), - 'data-entity-embed-display' => 'image:image', - 'data-entity-embed-display-settings' => '{"image_style":"","image_link":""}', - 'data-align' => 'left', - 'data-caption' => 'test caption', - 'alt' => 'This is alt text', - 'title' => 'This is title text', - ]); - - /** @var \Drupal\filter\FilterProcessResult $filter_result */ - $filter_result = $this->filter->process($content, 'en'); - $output = $filter_result->getProcessedText(); - $this->assertEntityEmbedFilterHasRun($output); - - $dom = Html::load($output); - $xpath = new \DOMXPath($dom); - /** @var \DOMNode $rendered_embed */ - $rendered_embed = $xpath->query('//div[contains(@class, "embedded-entity")]')[0]; - $this->assertHasAttributes($rendered_embed, [ - 'alt' => 'This is alt text', - 'data-align' => 'left', - 'data-entity-embed-display' => 'image:image', - 'data-entity-type' => 'file', - 'data-entity-uuid' => $image->uuid(), - 'title' => 'This is title text', - 'data-langcode' => 'en', - ]); - $img = $xpath->query('//div[contains(@class, "embedded-entity")]/img')[0]; - $this->assertHasAttributes($img, [ - 'alt' => 'This is alt text', - 'title' => 'This is title text', - ]); - } - /** * Tests BC for`data-entity-embed-display-settings`'s predecessor. * @@ -477,106 +406,6 @@ class EntityEmbedFilterTest extends EntityEmbedFilterTestBase { ]); } - /** - * Tests that embeds are not render cached, and can have per-embed overrides. - */ - public function testOverridesAndRenderCaching() { - $this->createMediaType('image', ['id' => 'image']); - // The `alt` field property is enabled by default, the `title` one is not. - // Since we want to test it, enable it. - $source_field = FieldConfig::load('media.image.field_media_image'); - $source_field->setSetting('title_field', TRUE); - $source_field->save(); - $this->container->get('current_user') - ->addRole($this->drupalCreateRole(['view media'])); - - \Drupal::service('file_system')->copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://batfish.jpg'); - /** @var \Drupal\file\FileInterface $file */ - $file = File::create([ - 'uri' => 'public://batfish.jpg', - 'uid' => 2, - ]); - $file->save(); - - $this->createNode([ - 'type' => 'article', - 'title' => 'Red-lipped batfish', - ]); - - $media = Media::create([ - 'bundle' => 'image', - 'name' => 'Screaming hairy armadillo', - 'field_media_image' => [ - [ - 'target_id' => $file->id(), - 'alt' => 'default alt', - 'title' => 'default title', - ], - ], - ]); - $media->save(); - - $input = $this->createEmbedCode([ - 'alt' => 'alt 1', - 'title' => 'title 1', - 'data-embed-button' => 'test_media_entity_embed', - 'data-entity-embed-display' => 'view_mode:media.full', - 'data-entity-embed-display-settings' => '', - 'data-entity-type' => 'media', - 'data-entity-uuid' => $media->uuid(), - 'data-langcode' => 'en', - ]); - $input .= $this->createEmbedCode([ - 'alt' => 'alt 2', - 'title' => 'title 2', - 'data-embed-button' => 'test_media_entity_embed', - 'data-entity-embed-display' => 'view_mode:media.full', - 'data-entity-embed-display-settings' => '', - 'data-entity-type' => 'media', - 'data-entity-uuid' => $media->uuid(), - 'data-langcode' => 'en', - ]); - $input .= $this->createEmbedCode([ - 'alt' => 'alt 3', - 'title' => 'title 3', - 'data-embed-button' => 'test_media_entity_embed', - 'data-entity-embed-display' => 'view_mode:media.full', - 'data-entity-embed-display-settings' => '', - 'data-entity-type' => 'media', - 'data-entity-uuid' => $media->uuid(), - 'data-langcode' => 'en', - ]); - - /** @var \Drupal\filter\FilterProcessResult $filter_result */ - $filter_result = $this->filter->process($input, 'en'); - $output = $filter_result->getProcessedText(); - $this->assertNotContains('drupal-entity data-entity-type="media" data-entity', $output); - $this->assertEntityEmbedFilterHasRun($output); - $dom = Html::load($output); - $xpath = new \DOMXPath($dom); - - $img1 = $xpath->query("//img[contains(@alt, 'alt 1')]")[0]; - $this->assertNotEmpty($img1); - $this->assertHasAttributes($img1, [ - 'alt' => 'alt 1', - 'title' => 'title 1', - ]); - - $img2 = $xpath->query("//img[contains(@alt, 'alt 2')]")[0]; - $this->assertNotEmpty($img2); - $this->assertHasAttributes($img2, [ - 'alt' => 'alt 2', - 'title' => 'title 2', - ]); - - $img3 = $xpath->query("//img[contains(@alt, 'alt 3')]")[0]; - $this->assertNotEmpty($img3); - $this->assertHasAttributes($img3, [ - 'alt' => 'alt 3', - 'title' => 'title 3', - ]); - } - /** * Loads an entity (in the appropriate translation) given HTML attributes. * @@ -608,20 +437,6 @@ class EntityEmbedFilterTest extends EntityEmbedFilterTestBase { return $entity; } - /** - * Assert that the DOMNode object has the given attributes. - * - * @param \DOMNode $node - * The DOMNode object to check. - * @param array $attributes - * An array of attributes. - */ - protected function assertHasAttributes(\DOMNode $node, array $attributes) { - foreach ($attributes as $attribute => $value) { - $this->assertEquals($value, $node->getAttribute($attribute)); - } - } - /** * Process text containing html for elements containing data-caption. * @@ -668,20 +483,4 @@ class EntityEmbedFilterTest extends EntityEmbedFilterTestBase { } } - /** - * Retrieves a sample file of the specified type. - */ - protected function getTestFile($type_name, $size = NULL) { - // Get a file to upload. - $file = current($this->getTestFiles($type_name, $size)); - - // Add a filesize property to files as would be read by - // \Drupal\file\Entity\File::load(). - $file->filesize = filesize($file->uri); - - $file = File::create((array) $file); - $file->save(); - return $file; - } - } diff --git a/tests/src/Kernel/EntityEmbedFilterTestBase.php b/tests/src/Kernel/EntityEmbedFilterTestBase.php index 6e2d628..7a33cf9 100644 --- a/tests/src/Kernel/EntityEmbedFilterTestBase.php +++ b/tests/src/Kernel/EntityEmbedFilterTestBase.php @@ -19,6 +19,7 @@ abstract class EntityEmbedFilterTestBase extends KernelTestBase { } use UserCreationTrait { createUser as drupalCreateUser; + createRole as drupalCreateRole; } use ContentTypeCreationTrait { createContentType as drupalCreateContentType; @@ -137,4 +138,18 @@ abstract class EntityEmbedFilterTestBase extends KernelTestBase { $this->assertNotContains('This placeholder should not be rendered.', $output); } + /** + * Assert that the DOMNode object has the given attributes. + * + * @param \DOMNode $node + * The DOMNode object to check. + * @param array $attributes + * An array of attributes. + */ + protected function assertHasAttributes(\DOMNode $node, array $attributes) { + foreach ($attributes as $attribute => $value) { + $this->assertEquals($value, $node->getAttribute($attribute)); + } + } + }