Skip to content

Commit

Permalink
EWPP-3296: Add cache invalidation test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
22Alexandra committed Jun 15, 2023
1 parent c79ce3e commit 288f195
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 6 deletions.
4 changes: 0 additions & 4 deletions oe_theme.theme
Original file line number Diff line number Diff line change
Expand Up @@ -2247,10 +2247,6 @@ function oe_theme_preprocess_paragraph__oe_av_media(array &$variables): void {
$source = $media->getSource();

if ($source instanceof MediaAvPortalVideoSource || $source instanceof OEmbed || $source instanceof Iframe) {
if ($source instanceof MediaAvPortalVideoSource) {
// Default video aspect ratio is set to 16:9 for AV Portal Video.
$variables['ratio'] = '16:9';
}

// Load information about the media and the display.
$media_type = \Drupal::entityTypeManager()->getStorage('media_type')->load($media->bundle());
Expand Down
102 changes: 102 additions & 0 deletions tests/src/Functional/ParagraphsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Drupal\Tests\oe_theme\Functional;

use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;

/**
Expand All @@ -23,6 +24,8 @@ class ParagraphsTest extends BrowserTestBase {
'oe_theme_helper',
'oe_paragraphs_demo',
'oe_content_timeline_field',
'oe_paragraphs_media',
'oe_multilingual',
];

/**
Expand Down Expand Up @@ -80,4 +83,103 @@ public function testAccordionItemParagraph(): void {
$this->assertSession()->pageTextContains('Accordion item body');
}

/**
* Tests the Media paragraph cache invalidation.
*/
public function testMediaParagraphCaching(): void {
// Set image media translatable.
$this->container->get('content_translation.manager')->setEnabled('media', 'image', TRUE);
// Make the image field translatable.
$field_config = $this->container->get('entity_type.manager')->getStorage('field_config')->load('media.image.oe_media_image');
$field_config->set('translatable', TRUE)->save();
$this->container->get('router.builder')->rebuild();

// Create English file.
$en_file = $this->container->get('file.repository')->writeData(file_get_contents($this->container->get('extension.list.theme')->getPath('oe_theme') . '/tests/fixtures/example_1.jpeg'), 'public://example_1_en.jpeg');
$en_file->setPermanent();
$en_file->save();

// Create Bulgarian file.
$bg_file = $this->container->get('file.repository')->writeData(file_get_contents($this->container->get('extension.list.theme')->getPath('oe_theme') . '/tests/fixtures/example_1.jpeg'), 'public://example_1_bg.jpeg');
$bg_file->setPermanent();
$bg_file->save();

// Create a media.
$media_storage = $this->container->get('entity_type.manager')->getStorage('media');
$media = $media_storage->create([
'bundle' => 'image',
'name' => 'test image en',
'oe_media_image' => [
'target_id' => $en_file->id(),
'alt' => 'Alt en',
],
]);
$media->save();
// Translate the media to Bulgarian.
$media_bg = $media->addTranslation('bg', [
'name' => 'test image bg',
'oe_media_image' => [
'target_id' => $bg_file->id(),
'alt' => 'Alt bg',
],
]);
$media_bg->save();

// Create a Media paragraph.
$paragraph = $this->container
->get('entity_type.manager')
->getStorage('paragraph')->create([
'type' => 'oe_av_media',
'field_oe_media' => [
'target_id' => $media->id(),
],
]);
$paragraph->save();

// Add Bulgarian translation.
$paragraph->addTranslation('bg', $paragraph->toArray())->save();

// Create a Demo page node referencing the paragraph.
$node = $this->container->get('entity_type.manager')->getStorage('node')->create([
'type' => 'oe_demo_landing_page',
'title' => 'Demo Media paragraph',
'field_oe_demo_body' => [$paragraph],
]);
$node->save();
// Add Bulgarian translation.
$node->addTranslation('bg', $node->toArray())->save();

// Assert the english translation.
$this->drupalGet($node->toUrl());
$this->assertSession()->elementAttributeContains('css', 'figure.ecl-media-container img.ecl-media-container__media', 'src', 'example_1_en.jpeg');
// Assert the bulgarian translation.
$this->drupalGet('/bg/node/' . $node->id(), ['external' => FALSE]);
$this->assertSession()->elementAttributeContains('css', 'figure.ecl-media-container img.ecl-media-container__media', 'src', 'example_1_bg.jpeg');

// Unpublish the media and assert it is not rendered anymore.
$media->set('status', 0);
$media->save();
$this->drupalGet($node->toUrl());
$this->assertSession()->elementNotExists('css', 'figure.ecl-media-container');

// Create a remote video and add it to the paragraph.
$media = $media_storage->create([
'bundle' => 'remote_video',
'oe_media_oembed_video' => [
'value' => 'https://www.youtube.com/watch?v=1-g73ty9v04',
],
]);
$media->save();
$paragraph->set('field_oe_media', ['target_id' => $media->id()]);
$paragraph->save();

$this->getSession()->reload();
$partial_iframe_url = Url::fromRoute('media.oembed_iframe', [], [
'query' => [
'url' => 'https://www.youtube.com/watch?v=1-g73ty9v04',
],
])->toString();
$this->assertSession()->elementAttributeContains('css', 'figure.ecl-media-container div.ecl-media-container__media iframe', 'src', $partial_iframe_url);
}

}
4 changes: 2 additions & 2 deletions tests/src/Kernel/Paragraphs/MediaParagraphsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1103,12 +1103,12 @@ public function testMediaParagraph(): void {
$this->container->get('router.builder')->rebuild();

// Create English file.
$en_file = file_save_data(file_get_contents(drupal_get_path('theme', 'oe_theme') . '/tests/fixtures/example_1.jpeg'), 'public://example_1_en.jpeg');
$en_file = $this->container->get('file.repository')->writeData(file_get_contents($this->container->get('extension.list.theme')->getPath('oe_theme') . '/tests/fixtures/example_1.jpeg'), 'public://example_1_en.jpeg');
$en_file->setPermanent();
$en_file->save();

// Create Bulgarian file.
$bg_file = file_save_data(file_get_contents(drupal_get_path('theme', 'oe_theme') . '/tests/fixtures/example_1.jpeg'), 'public://example_1_bg.jpeg');
$bg_file = $this->container->get('file.repository')->writeData(file_get_contents($this->container->get('extension.list.theme')->getPath('oe_theme') . '/tests/fixtures/example_1.jpeg'), 'public://example_1_bg.jpeg');
$bg_file->setPermanent();
$bg_file->save();

Expand Down

0 comments on commit 288f195

Please sign in to comment.