From d864ed717b1a35069148a2ee49e8f1fcbe7732c2 Mon Sep 17 00:00:00 2001 From: Grzegorz Pietrzak Date: Fri, 11 Oct 2024 12:26:01 +0200 Subject: [PATCH] EWPP-4825: Preview extension for file patterns. --- .../file_teaser/file_teaser.ui_patterns.yml | 18 ++++++++++++- .../file_teaser/pattern-file-teaser.html.twig | 2 ++ .../file_translation.ui_patterns.yml | 7 ++++++ .../pattern-file-translation.html.twig | 1 + tests/src/Kernel/fixtures/rendering.yml | 25 +++++++++++++++++++ .../PatternAssertions/FileTeaserAssert.php | 24 ++++++++++++++++++ 6 files changed, 76 insertions(+), 1 deletion(-) diff --git a/templates/patterns/file_teaser/file_teaser.ui_patterns.yml b/templates/patterns/file_teaser/file_teaser.ui_patterns.yml index 87e575ede..f6ccb5ca6 100644 --- a/templates/patterns/file_teaser/file_teaser.ui_patterns.yml +++ b/templates/patterns/file_teaser/file_teaser.ui_patterns.yml @@ -23,6 +23,13 @@ file_teaser: size: "140000" name: "document.pdf" language_code: "en" + link_attributes: + type: "array" + label: "The download link attributes" + description: "Any additional attributes to be added to the file download link." + preview: + - "name": "data-attribute-foo" + "value": "bar" thumbnail: type: "array" label: "Thumbnail" @@ -43,7 +50,7 @@ file_teaser: translations: type: "array" label: "Files" - description: "List of teaser (teaser is deprecated - to be removed in 5.x) text/FileValueObject pairs, respectively keyed with 'teaser' and 'file'." + description: "List of items with the following properties: teaser (teaser is deprecated - to be removed in 5.x) text, FileValueObject and link attributes, respectively keyed with 'teaser', 'file' and 'link_attributes'." preview: - teaser: "Morbi fringilla turpis augue, et interdum ipsum egestas sed. Proin tristique, ante id aliquet malesuada, lorem dolor vulputate magna, a commodo purus ante nec massa." file: @@ -53,6 +60,9 @@ file_teaser: size: "150000" name: "document.pdf" language_code: "fr" + link_attributes: + - "name": "data-attribute-translation-foo" + "value": "bar" - teaser: "Proin sagittis nisi hendrerit purus porta, at suscipit est hendrerit. Duis facilisis augue imperdiet, pharetra nisl sed, molestie nulla." file: title: "Italian translation" @@ -61,6 +71,9 @@ file_teaser: size: "160000" name: "document.doc" language_code: "it" + link_attributes: + - "name": "data-attribute-translation-foo" + "value": "bar" - teaser: "Duis eget lacinia arcu. Nullam mattis ornare nibh. Proin tristique, ante id aliquet malesuada. Pellentesque porttitor commodo libero sed fringilla. Curabitur varius sodales elit, id tincidunt erat. Aenean tincidunt luctus molestie." file: title: "Hungarian translation" @@ -69,6 +82,9 @@ file_teaser: size: "170000" name: "document.pdf" language_code: "hu" + link_attributes: + - "name": "data-attribute-translation-foo" + "value": "bar" translation_toggle_label: type: "text" label: "Translation toggle label" diff --git a/templates/patterns/file_teaser/pattern-file-teaser.html.twig b/templates/patterns/file_teaser/pattern-file-teaser.html.twig index ed2344d9d..732214898 100644 --- a/templates/patterns/file_teaser/pattern-file-teaser.html.twig +++ b/templates/patterns/file_teaser/pattern-file-teaser.html.twig @@ -29,6 +29,7 @@ 'icon': { 'path': ecl_icon_path, }, + 'extra_attributes': item.link_attributes|default([]), }, 'lang_full': item.file.language_code|to_native_language }]) %} @@ -116,6 +117,7 @@ 'icon': { 'path': ecl_icon_path, }, + 'extra_attributes': link_attributes|default([]), }, 'translation': { 'toggle': { diff --git a/templates/patterns/file_translation/file_translation.ui_patterns.yml b/templates/patterns/file_translation/file_translation.ui_patterns.yml index c4867fd5a..995210da9 100644 --- a/templates/patterns/file_translation/file_translation.ui_patterns.yml +++ b/templates/patterns/file_translation/file_translation.ui_patterns.yml @@ -18,6 +18,13 @@ file_translation: size: "150000" name: "document.pdf" language_code: "en" + link_attributes: + type: "array" + label: "The download link attributes" + description: "Any additional attributes to be added to the file download link." + preview: + - "name": "data-attribute-foo" + "value": "bar" translations: type: "array" label: "Files" diff --git a/templates/patterns/file_translation/pattern-file-translation.html.twig b/templates/patterns/file_translation/pattern-file-translation.html.twig index e52ebeb17..f98981867 100644 --- a/templates/patterns/file_translation/pattern-file-translation.html.twig +++ b/templates/patterns/file_translation/pattern-file-translation.html.twig @@ -58,6 +58,7 @@ 'icon': { 'path': ecl_icon_path, }, + 'extra_attributes': link_attributes|default([]), }, 'translation': { 'toggle': { diff --git a/tests/src/Kernel/fixtures/rendering.yml b/tests/src/Kernel/fixtures/rendering.yml index c8f7f4f01..af614d88d 100644 --- a/tests/src/Kernel/fixtures/rendering.yml +++ b/tests/src/Kernel/fixtures/rendering.yml @@ -805,6 +805,9 @@ mime: "application/pdf" size: "10000" language_code: "en" + link_attributes: + - name: "data-attribute-foo" + value: "bar" translations: - mime: "application/vnd.ms-excel" url: "http://example.com/document_fr.xls" @@ -833,6 +836,7 @@ count: 'svg.ecl-file__icon': 1 'a.ecl-file__download[href="http://example.com"]': 1 + 'a.ecl-file__download[data-attribute-foo="bar"]': 1 'div.ecl-file__translation-container': 1 'li.ecl-file__translation-item a.ecl-file__translation-download': 4 'a.ecl-file__translation-download[href="http://example.com/document_fr.xls"]': 1 @@ -884,6 +888,7 @@ count: 'svg.ecl-file__icon': 1 'a.ecl-file__download[href="http://example.com"]': 1 + 'a.ecl-file__download[data-attribute-foo="bar"]': 0 'div.ecl-file__translation-container': 1 'li.ecl-file__translation-item a.ecl-file__translation-download': 1 'a.ecl-file__translation-download[href="http://example.com/document_fr.xls"]': 1 @@ -910,6 +915,9 @@ mime: "application/pdf" size: "140000" language_code: "en" + link_attributes: + - name: "data-attribute-foo" + value: "bar" thumbnail: src: "https://inno-ecl.s3.amazonaws.com/media/examples/example-image.jpg" alt: "thumbnail alt" @@ -924,6 +932,9 @@ size: "150000" name: "document.xls" language_code: "fr" + link_attributes: + - name: "data-attribute-translation-foo" + value: "bar" - teaser: "Proin sagittis nisi hendrerit purus porta, at suscipit est hendrerit. Duis facilisis augue imperdiet, pharetra nisl sed, molestie nulla." file: title: "Italian translation" @@ -932,6 +943,9 @@ size: "160000" name: "document.ppt" language_code: "it" + link_attributes: + - name: "data-attribute-translation-foo" + value: "bar" - teaser: "Duis eget lacinia arcu. Nullam mattis ornare nibh. Proin tristique, ante id aliquet malesuada. Pellentesque porttitor commodo libero sed fringilla. Curabitur varius sodales elit, id tincidunt erat. Aenean tincidunt luctus molestie." file: title: "Hungarian translation" @@ -940,6 +954,9 @@ size: "170000" name: "document.pdf" language_code: "hu" + link_attributes: + - name: "data-attribute-translation-foo" + value: "bar" - teaser: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce at finibus purus, eu ultricies nisl. Vivamus vitae ex lectus. In hac habitasse platea dictumst. Proin malesuada est eget vulputate malesuada." file: title: "Portugies translation" @@ -948,6 +965,9 @@ size: "180000" name: "document.doc" language_code: "pt-pt" + link_attributes: + - name: "data-attribute-translation-foo" + value: "bar" translation_toggle_label: "Other translations" more_info: "Looking for another language which is not on the list? Find out why." badges: @@ -962,6 +982,8 @@ 'img.ecl-file__image[alt="thumbnail alt"]': 1 'a.ecl-file__download[href="http://example.com"]': 1 'div.ecl-file.ecl-file--thumbnail[extra-attr="extra value"]': 1 + 'a.ecl-file__download[data-attribute-foo="bar"]': 1 + 'div.ecl-file.ecl-file--thumbnail': 1 'div.ecl-file__container div.ecl-file__info ul.ecl-file__detail-meta': 1 'div.ecl-file__container div.ecl-file__info': 1 'div.ecl-file__translation-container': 1 @@ -971,6 +993,7 @@ 'a.ecl-file__translation-download[href="http://example.com/document_it.ppt"]': 1 'a.ecl-file__translation-download[href="http://example.com/document_hu.pdf"]': 1 'a.ecl-file__translation-download[href="http://example.com/document_pt.doc"]': 1 + 'a.ecl-file__translation-download[data-attribute-translation-foo="bar"]': 4 'div.ecl-file__label span.ecl-label.ecl-label--highlight': 1 equals: 'div.ecl-file__info div.ecl-file__title': "Example document" @@ -1055,6 +1078,7 @@ 'img.ecl-file__image[src="https://inno-ecl.s3.amazonaws.com/media/examples/example-image.jpg"]': 1 'img.ecl-file__image[alt="thumbnail alt"]': 1 'a.ecl-file__download[href="http://example.com"]': 1 + 'a.ecl-file__download[data-attribute-foo="bar"]': 0 'div.ecl-file.ecl-file--thumbnail': 1 'div.ecl-file__container div.ecl-file__info ul.ecl-file__detail-meta': 1 'div.ecl-file__container div.ecl-file__info': 1 @@ -1062,6 +1086,7 @@ 'ul.ecl-file__translation-list': 1 'li.ecl-file__translation-item a.ecl-file__translation-download': 1 'a.ecl-file__translation-download[href="http://example.com/document_fr.xls"]': 1 + 'a.ecl-file__translation-download[data-attribute-translation-foo="bar"]': 0 'div.ecl-file__taxonomy': 1 'dl.ecl-description-list': 1 'dt.ecl-description-list__term': 3 diff --git a/tests/src/PatternAssertions/FileTeaserAssert.php b/tests/src/PatternAssertions/FileTeaserAssert.php index ea4c10c0d..cabf10baa 100644 --- a/tests/src/PatternAssertions/FileTeaserAssert.php +++ b/tests/src/PatternAssertions/FileTeaserAssert.php @@ -30,6 +30,9 @@ protected function getAssertions($variant): array { $assertions['lists'] = [ [$this, 'assertLists'], ]; + $assertions['link_attributes'] = [ + [$this, 'assertLinkAttributes'], + ]; $assertions['badge'] = [ [$this, 'assertBadge'], 'div.ecl-file--thumbnail div.ecl-file__container div.ecl-file__info div.ecl-file__label', @@ -100,6 +103,20 @@ protected function assertFile(array $expected_file, Crawler $crawler): void { $this->assertElementAttribute($expected_file['url'], 'a.ecl-file__download', 'href', $file_footer); } + /** + * Asserts the attributes of the download link. + * + * @param array $expected_link_attributes + * The expected link attributes. + * @param \Symfony\Component\DomCrawler\Crawler $crawler + * The DomCrawler where to check the element. + */ + protected function assertLinkAttributes(array $expected_link_attributes, Crawler $crawler): void { + foreach ($expected_link_attributes as $attribute => $value) { + $this->assertElementAttribute($value, 'a.ecl-file__download', $attribute, $crawler); + } + } + /** * {@inheritdoc} */ @@ -116,6 +133,13 @@ protected function assertTranslation(array $expected_file, Crawler $crawler): vo // Assert download link. $this->assertElementAttribute($expected_file['url'], 'a.ecl-file__translation-download', 'href', $crawler); + + // Assert download link attributes. + if (isset($expected_file['link_attributes'])) { + foreach ($expected_file['link_attributes'] as $attribute => $value) { + $this->assertElementAttribute($value, 'a.ecl-file__translation-download', $attribute, $crawler); + } + } } /**