tests/src/Kernel/EntityEmbedFilterTest.php | 75 +------------ tests/src/Kernel/EntityEmbedFilterTestBase.php | 124 +++++++++++++++++++++ .../Kernel/EntityEmbedFilterTranslationTest.php | 118 +------------------- 3 files changed, 134 insertions(+), 183 deletions(-) diff --git a/tests/src/Kernel/EntityEmbedFilterTest.php b/tests/src/Kernel/EntityEmbedFilterTest.php index 38f4e67..57bf092 100644 --- a/tests/src/Kernel/EntityEmbedFilterTest.php +++ b/tests/src/Kernel/EntityEmbedFilterTest.php @@ -6,11 +6,8 @@ use Drupal\Component\Utility\Html; use Drupal\Core\Entity\TranslatableInterface; use Drupal\Core\Render\RenderContext; use Drupal\file\Entity\File; -use Drupal\filter\FilterPluginCollection; -use Drupal\KernelTests\KernelTestBase; use Drupal\media\Entity\Media; use Drupal\Tests\node\Traits\ContentTypeCreationTrait; -use Drupal\Tests\node\Traits\NodeCreationTrait; use Drupal\Tests\TestFileCreationTrait; use Drupal\Tests\user\Traits\UserCreationTrait; @@ -19,17 +16,13 @@ use Drupal\Tests\user\Traits\UserCreationTrait; * * @group entity_embed */ -class EntityEmbedFilterTest extends KernelTestBase { +class EntityEmbedFilterTest extends EntityEmbedFilterTestBase { use ContentTypeCreationTrait { createContentType as drupalCreateContentType; } - use NodeCreationTrait { - createNode as drupalCreateNode; - } use UserCreationTrait { createRole as drupalCreateRole; - createUser as drupalCreateUser; } use TestFileCreationTrait; @@ -37,92 +30,32 @@ class EntityEmbedFilterTest extends KernelTestBase { * {@inheritdoc} */ protected static $modules = [ - 'system', - 'text', - 'field', - 'embed', - 'user', 'editor', 'media', - 'filter', 'file', 'image', - 'entity_embed', 'entity_embed_test', - 'node', 'ckeditor', ]; - /** - * The renderer service. - * - * @var \Drupal\Core\Render\RendererInterface - */ - protected $renderer; - - /** - * The entity embed filter. - * - * @var \Drupal\entity_embed\Plugin\Filter\EntityEmbedFilter - */ - protected $filter; - - /** - * The caption filter. - * - * @var \Drupal\filter\Plugin\Filter\FilterCaption - */ - protected $filterCaption; - /** * {@inheritdoc} */ protected function setUp() { parent::setUp(); - $this->installSchema('system', 'sequences'); - $this->installSchema('node', 'node_access'); $this->installSchema('file', ['file_usage']); $this->installEntitySchema('file'); - $this->installEntitySchema('user'); - $this->installEntitySchema('node'); $this->installEntitySchema('media'); $this->installEntitySchema('date_format'); - $this->installConfig('filter'); $this->installConfig('image'); $this->installConfig('media'); - $this->installConfig('node'); $this->installConfig('system'); $this->installConfig('entity_embed_test'); - // Enable the Classy theme. - \Drupal::service('theme_handler')->install(['classy']); + // Enable the Classy theme: test the caption filter's output for that theme. + $this->container->get('theme_handler')->install(['classy']); $this->config('system.theme')->set('default', 'classy')->save(); - - $this->renderer = $this->container->get('renderer'); - $manager = $this->container->get('plugin.manager.filter'); - $bag = new FilterPluginCollection($manager, []); - $this->filter = $bag->get('entity_embed'); - $this->filterCaption = $bag->get('filter_caption'); - - // Create a page content type. - $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']); - - // Create a user with required permissions. Ensure that we don't use user 1 - // because that user is treated in special ways by access control handlers. - $admin_user = $this->drupalCreateUser([]); - $this->webUser = $this->drupalCreateUser([ - 'access content', - ]); - $this->container->set('current_user', $this->webUser); - - // Create a sample node to be embedded. - $settings = []; - $settings['type'] = 'page'; - $settings['title'] = 'Embed Test Node'; - $settings['body'] = ['value' => 'This node is to be used for embedding in other nodes.']; - $settings['uuid'] = 'e7a3e1fe-b69b-417e-8ee4-c80cb7640e63'; - $this->node = $this->drupalCreateNode($settings); } /** @@ -602,7 +535,7 @@ class EntityEmbedFilterTest extends KernelTestBase { /** @var \Drupal\file\FileInterface $file */ $file = File::create([ 'uri' => 'public://batfish.jpg', - 'uid' => $this->webUser->id(), + 'uid' => 2, ]); $file->save(); diff --git a/tests/src/Kernel/EntityEmbedFilterTestBase.php b/tests/src/Kernel/EntityEmbedFilterTestBase.php new file mode 100644 index 0000000..f6cdd1f --- /dev/null +++ b/tests/src/Kernel/EntityEmbedFilterTestBase.php @@ -0,0 +1,124 @@ +installSchema('node', 'node_access'); + $this->installSchema('system', 'sequences'); + $this->installEntitySchema('node'); + $this->installEntitySchema('user'); + $this->installConfig('filter'); + $this->installConfig('node'); + + // Create a user with required permissions. Ensure that we don't use user 1 + // because that user is treated in special ways by access control handlers. + $admin_user = $this->drupalCreateUser([]); + $user = $this->drupalCreateUser([ + 'access content', + ]); + $this->container->set('current_user', $user); + + // Create a sample node to be embedded. + $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']); + $settings = []; + $settings['title'] = 'Embed Test Node'; + $settings['body'] = ['value' => 'This node is to be used for embedding in other nodes.']; + $settings['uuid'] = 'e7a3e1fe-b69b-417e-8ee4-c80cb7640e63'; + $this->node = $this->drupalCreateNode($settings); + + $this->renderer = $this->container->get('renderer'); + $manager = $this->container->get('plugin.manager.filter'); + $bag = new FilterPluginCollection($manager, []); + $this->filter = $bag->get('entity_embed'); + $this->filterCaption = $bag->get('filter_caption'); + } + + /** + * Gets an embed code with given attributes. + * + * @param array $attributes + * The attributes to add. + * + * @return string + * A string containing a drupal-entity dom element. + */ + public function createEmbedCode(array $attributes) { + $dom = Html::load('This placeholder should not be rendered.'); + $xpath = new \DOMXPath($dom); + $drupal_entity = $xpath->query('//drupal-entity')[0]; + foreach ($attributes as $attribute => $value) { + $drupal_entity->setAttribute($attribute, $value); + } + return Html::serialize($dom); + } + +} diff --git a/tests/src/Kernel/EntityEmbedFilterTranslationTest.php b/tests/src/Kernel/EntityEmbedFilterTranslationTest.php index 1a7c88d..8bd6702 100644 --- a/tests/src/Kernel/EntityEmbedFilterTranslationTest.php +++ b/tests/src/Kernel/EntityEmbedFilterTranslationTest.php @@ -2,119 +2,34 @@ namespace Drupal\Tests\entity_embed\Kernel; -use Drupal\Component\Utility\Html; -use Drupal\filter\FilterPluginCollection; -use Drupal\KernelTests\KernelTestBase; use Drupal\language\Entity\ConfigurableLanguage; -use Drupal\node\Entity\Node; -use Drupal\Tests\node\Traits\NodeCreationTrait; -use Drupal\Tests\user\Traits\UserCreationTrait; /** - * Tests the entity_embed filter. + * Tests the entity_embed filter with translated content. * * @group entity_embed */ -class EntityEmbedFilterTranslationTest extends KernelTestBase { - - use NodeCreationTrait { - createNode as drupalCreateNode; - } - use UserCreationTrait { - createUser as drupalCreateUser; - } +class EntityEmbedFilterTranslationTest extends EntityEmbedFilterTestBase { /** * {@inheritdoc} */ protected static $modules = [ - 'system', - 'text', - 'field', - 'embed', - 'user', - 'editor', - 'media', - 'filter', 'content_translation', - 'entity_embed', - 'entity_embed_test', - 'node', - 'ckeditor', - 'file', - 'image', 'language', ]; - /** - * The renderer service. - * - * @var \Drupal\Core\Render\RendererInterface - */ - protected $renderer; - - /** - * The entity embed filter. - * - * @var \Drupal\entity_embed\Plugin\Filter\EntityEmbedFilter - */ - protected $filter; - - /** - * The caption filter. - * - * @var \Drupal\filter\Plugin\Filter\FilterCaption - */ - protected $filterCaption; - /** * {@inheritdoc} */ protected function setUp() { parent::setUp(); - $this->installSchema('system', 'sequences'); - $this->installSchema('node', 'node_access'); - $this->installSchema('file', ['file_usage']); - $this->installEntitySchema('file'); - $this->installEntitySchema('user'); - $this->installEntitySchema('node'); - $this->installEntitySchema('media'); - $this->installEntitySchema('date_format'); - $this->installConfig('filter'); - $this->installConfig('image'); - $this->installConfig('media'); - $this->installConfig('node'); - $this->installConfig('system'); - $this->installConfig('entity_embed_test'); - - $this->installEntitySchema('language_content_settings'); - - $this->renderer = $this->container->get('renderer'); - - $manager = $this->container->get('plugin.manager.filter'); - $bag = new FilterPluginCollection($manager, []); - $this->filter = $bag->get('entity_embed'); - $this->filterCaption = $bag->get('filter_caption'); - - // Add pt-br lang for our tests below. ConfigurableLanguage::createFromLangcode('pt-br')->save(); - - // Create a user with required permissions. Ensure that we don't use user 1 - // because that user is treated in special ways by access control handlers. - $admin_user = $this->drupalCreateUser([]); - $this->webUser = $this->drupalCreateUser([ - 'access content', - ]); - $this->container->set('current_user', $this->webUser); - - // Create a sample node to be embedded. - $settings = []; - $settings['type'] = 'page'; - $settings['title'] = 'Embed Test Node'; - $settings['body'] = ['value' => 'This node is to be used for embedding in other nodes.']; - $settings['uuid'] = 'e7a3e1fe-b69b-417e-8ee4-c80cb7640e63'; - $this->node = $this->drupalCreateNode($settings); + // Reload the node to ensure it is aware of the newly created language. + $this->node = $this->container->get('entity.manager') + ->getStorage('node') + ->load($this->node->id()); } /** @@ -140,7 +55,6 @@ class EntityEmbedFilterTranslationTest extends KernelTestBase { $this->assertContains($expected_title, $output); // Translate the embedded entity to the same language as the context. - $this->node = Node::load($this->node->id()); $this->node->addTranslation('pt-br') ->getTranslation('pt-br') ->setTitle('Embed em portugues') @@ -162,7 +76,6 @@ class EntityEmbedFilterTranslationTest extends KernelTestBase { $content = $this->createEmbedCode($embed_attributes); // Translate the embedded entity to the same language as the context. - $this->node = Node::load($this->node->id()); $this->node->addTranslation('pt-br') ->getTranslation('pt-br') ->setTitle('Embed em portugues') @@ -248,23 +161,4 @@ class EntityEmbedFilterTranslationTest extends KernelTestBase { ]; } - /** - * Get an embed code with given attributes. - * - * @param array $attributes - * The attributes to add. - * - * @return string - * A string containing a drupal-entity dom element. - */ - public function createEmbedCode(array $attributes) { - $dom = Html::load('This placeholder should not be rendered.'); - $xpath = new \DOMXPath($dom); - $drupal_entity = $xpath->query('//drupal-entity')[0]; - foreach ($attributes as $attribute => $value) { - $drupal_entity->setAttribute($attribute, $value); - } - return Html::serialize($dom); - } - }