Skip to content

Commit

Permalink
EWPP-3721: Theming CircaBC docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
upchuk committed Dec 15, 2023
1 parent 77ab92b commit 89bfff6
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"openeuropa/oe_corporate_blocks": "^4.14",
"openeuropa/oe_corporate_countries": "^2.0.0-alpha8",
"openeuropa/oe_corporate_site_info": "^1.0.0-alpha8",
"openeuropa/oe_media": "^1.23.1",
"openeuropa/oe_media": "dev-EWPP-3721 as 1.23.1",
"openeuropa/oe_multilingual": "^1.13",
"openeuropa/oe_paragraphs": "^1.20",
"openeuropa/oe_search": "^2.0@beta",
Expand Down
34 changes: 32 additions & 2 deletions src/DocumentMediaValueExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ public static function getFileValue(MediaInterface $media): ?FileValueObject {
return static::getFromLocalFile($media);
}

if ($file_type === 'circabc') {
return static::getFromCircaBcReference($media);
}

return NULL;
}

/**
* Returns a local FileValueObject from a Document media.
* Returns a local FileValueObject from a local Document media.
*
* @param \Drupal\media\MediaInterface $media
* The document media.
Expand All @@ -63,7 +67,7 @@ protected static function getFromLocalFile(MediaInterface $media): ?FileValueObj
}

/**
* Returns a remote FileValueObject from a Document media.
* Returns a remote FileValueObject from a remote Document media.
*
* @param \Drupal\media\MediaInterface $media
* The document media.
Expand All @@ -83,4 +87,30 @@ protected static function getFromRemoteFile(MediaInterface $media): ?FileValueOb
->setLanguageCode($media->language()->getId());
}

/**
* Returns a remote FileValueObject from a CircaBC Document media.
*
* @param \Drupal\media\MediaInterface $media
* The document media.
*
* @return \Drupal\oe_theme\ValueObject\FileValueObject|null
* The value object or NULL if could not be determined.
*/
protected static function getFromCircaBcReference(MediaInterface $media): ?FileValueObject {
if ($media->get('oe_media_circabc_reference')->isEmpty()) {
return NULL;
}

$reference = $media->get('oe_media_circabc_reference')->first();

return FileValueObject::fromArray([
'name' => $reference->filename,
'url' => $reference->getFileUrl()->toString(),
'mime' => $reference->mime,
'size' => $reference->size,
])
->setTitle($media->getName())
->setLanguageCode($media->language()->getId());
}

}
55 changes: 55 additions & 0 deletions tests/src/Kernel/MediaRenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Drupal\Tests\oe_theme\Kernel;

use Drupal\Core\Site\Settings;
use Drupal\Tests\oe_theme\PatternAssertions\FileTranslationAssert;
use Symfony\Component\DomCrawler\Crawler;

Expand Down Expand Up @@ -45,6 +46,8 @@ class MediaRenderTest extends MultilingualAbstractKernelTestBase {
'oe_webtools_media',
'json_field',
'oe_media_webtools',
'oe_media_circabc',
'oe_media_circabc_mock',
];

/**
Expand All @@ -64,11 +67,18 @@ protected function setUp(): void {
'oe_media_iframe',
'oe_media_webtools',
'oe_webtools_media',
'oe_media_circabc',
'json_field',
]);

$this->mediaStorage = $this->container->get('entity_type.manager')->getStorage('media');
$this->mediaViewBuilder = $this->container->get('entity_type.manager')->getViewBuilder('media');

$settings = Settings::getInstance() ? Settings::getAll() : [];
$settings['circabc'] = [
'url' => 'https://example.com/circabc-ewpp',
];
new Settings($settings);
}

/**
Expand All @@ -82,6 +92,7 @@ public function testDocumentMedia(): void {
'media.document.oe_media_file_type',
'media.document.oe_media_remote_file',
'media.document.oe_media_file',
'media.document.oe_media_circabc_reference',
];
foreach ($field_ids as $field_id) {
$field_config = $this->container->get('entity_type.manager')->getStorage('field_config')->load($field_id);
Expand Down Expand Up @@ -288,6 +299,50 @@ public function testDocumentMedia(): void {
$assert = new FileTranslationAssert();
$assert->assertPattern($expected, $output);
}

// Assert the rendering of the CircaBC document.
$media = $this->mediaStorage->create([
'name' => 'a document media',
'bundle' => 'document',
'oe_media_file_type' => 'circabc',
'oe_media_circabc_reference' => [
// UUID is enough to start, it will pull all the rest of the data.
'uuid' => 'e74e3bc0-a639-4e04-a839-3bbd60ed5688',
],
]);
$media->save();

foreach ($view_modes as $view_mode) {
$build = $this->mediaViewBuilder->view($media, $view_mode);
$output = $this->renderRoot($build);
$expected = [
'button_label' => 'Download',
'file' => [
'title' => 'Test sample file',
'url' => 'https://example.com/circabc-ewpp/d/d/workspace/SpacesStore/e74e3bc0-a639-4e04-a839-3bbd60ed5688/file.bin',
'language' => 'English',
'meta' => '(2.96 KB - PDF)',
'icon' => 'file',
],
'translations' => [
[
'title' => 'français',
'url' => 'https://example.com/circabc-ewpp/d/d/workspace/SpacesStore/5d634abd-fec1-452a-ae0b-62e4cf080506/file.bin',
'meta' => '(2.96 KB - PDF)',
'icon' => 'file',
],
[
'title' => 'português',
'url' => 'https://example.com/circabc-ewpp/d/d/workspace/SpacesStore/78634abd-fec1-452a-ae0b-62e4cf080578/file.bin',
'meta' => '(4.91 KB - PDF)',
'icon' => 'file',
],
],
];

$assert = new FileTranslationAssert();
$assert->assertPattern($expected, $output);
}
}

/**
Expand Down

0 comments on commit 89bfff6

Please sign in to comment.