Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
declare(strict_types = 1);
namespace Drupal\Tests\oe_whitelabel_paragraphs\Functional;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\ParagraphInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\oe_bootstrap_theme\PatternAssertion\GalleryPatternAssert;
use Drupal\Tests\oe_whitelabel\Traits\MediaCreationTrait;
use Symfony\Component\DomCrawler\Crawler;
/**
* Tests the gallery paragraph.
*
* A browser test was used, instead of a kernel test, because of the optional
* configurations that are installed only when oe_paragraphs_gallery is
* installed. This requires a precise order of installation in a kernel test,
* and the added complexity doesn't bring any value.
*/
class GalleryParagraphTest extends BrowserTestBase {
use MediaCreationTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'oe_paragraphs_gallery',
'oe_whitelabel_paragraphs',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'oe_whitelabel';
/**
* Tests the paragraph rendering.
*/
public function testRendering(): void {
$image = $this->createImageMedia();
$avportal_photo = $this->createAvPortalPhotoMedia();
$avportal_video = $this->createAvPortalVideoMedia();
$video = $this->createRemoteVideoMedia();
$paragraph = Paragraph::create([
'type' => 'oe_gallery',
'field_oe_gallery_items' => [
$image,
$avportal_photo,
$avportal_video,
$video,
],
]);
$paragraph->save();
$file_url_generator = \Drupal::service('file_url_generator');
$expected_items = [
[
'thumbnail' => [
'caption_title' => 'Image title',
'rendered' => sprintf(
'<img loading="lazy" src="%s" width="200" height="89" alt="Alt text" class="img-fluid">',
$file_url_generator->generate($image->get('oe_media_image')->entity->getFileUri())
->toString()
),
],
'media' => [
'caption_title' => 'Image title',
'rendered' => sprintf(
'<img loading="lazy" data-src="%s" width="200" height="89" alt="Alt text" class="img-fluid">',
$file_url_generator->generate($image->get('oe_media_image')->entity->getFileUri())
->toString()
),
],
],
[
'thumbnail' => [
'caption_title' => 'Euro with miniature figurines',
'rendered' => sprintf(
'<img loading="lazy" src="%s" width="639" height="426" alt="Euro with miniature figurines" class="img-fluid">',
$file_url_generator->generate($avportal_photo->get('thumbnail')->entity->getFileUri())
->toString()
),
],
'media' => [
'caption_title' => 'Euro with miniature figurines',
'rendered' => '<img class="avportal-photo img-fluid" alt="Euro with miniature figurines" data-src="//ec.europa.eu/avservices/avs/files/video6/repository/prod/photo/store/store2/4/P038924-352937.jpg">',
],
],
[
'thumbnail' => [
'caption_title' => 'Economic and Financial Affairs Council - Arrivals',
'rendered' => sprintf(
'<img loading="lazy" src="%s" width="352" height="200" alt="" class="img-fluid">',
$file_url_generator->generate($avportal_video->get('thumbnail')->entity->getFileUri())
->toString()
),
'play_icon' => TRUE,
],
'media' => [
'caption_title' => 'Economic and Financial Affairs Council - Arrivals',
'rendered' => '<iframe id="videoplayerI-163162" data-src="//ec.europa.eu/avservices/play.cfm?ref=I-163162&lg=EN&sublg=none&autoplay=true&tin=10&tout=59" frameborder="0" allowtransparency allowfullscreen webkitallowfullscreen mozallowfullscreen width="640" height="390" class="media-avportal-content"></iframe>',
],
],
[
'thumbnail' => [
'caption_title' => 'Energy, let\'s save it!',
'rendered' => sprintf(
'<img loading="lazy" src="%s" width="480" height="360" alt="" class="img-fluid">',
$file_url_generator->generate($video->get('thumbnail')->entity->getFileUri())
->toString()
),
'play_icon' => TRUE,
],
'media' => [
'caption_title' => 'Energy, let\'s save it!',
'rendered' => sprintf(
'<iframe data-src="/build/media/oembed?url=https%%3A//www.youtube.com/watch%%3Fv%%3D1-g73ty9v04&max_width=0&max_height=0&hash=%s" frameborder="0" allowtransparency width="200" height="150" class="media-oembed-content" title="Energy, let\'s save it!"></iframe>',
\Drupal::service('media.oembed.iframe_url_helper')
->getHash('https://www.youtube.com/watch?v=1-g73ty9v04', 0, 0)
),
],
],
];
$this->assertParagraphRendering([
'items' => $expected_items,
], $paragraph);
// Add a title.
$paragraph->set('field_oe_title', 'Gallery paragraph title')->save();
$this->assertParagraphRendering([
'title' => 'Gallery paragraph title',
'items' => $expected_items,
], $paragraph);
// Set also a description.
$description = $this->getRandomGenerator()->sentences(20);
$paragraph->set('field_oe_plain_text_long', $description)->save();
$this->assertParagraphRendering([
'title' => 'Gallery paragraph title',
'description' => $description,
'items' => $expected_items,
], $paragraph);
}
/**
* Asserts the gallery paragraph rendering.
*
* @param array $expected
* A list of expected values:
* - title: the gallery title.
* - description: the gallery description.
* - items: the gallery items in a format suitable for GalleryPatternAssert.
* @param \Drupal\paragraphs\ParagraphInterface $paragraph
* The paragraph being rendered.
*/
protected function assertParagraphRendering(array $expected, ParagraphInterface $paragraph): void {
$expected += [
'title' => NULL,
'description' => NULL,
'items' => [],
];
$html = $this->renderParagraph($paragraph);
$crawler = new Crawler($html);
$this->assertCount(1, $crawler->filter('body > div.paragraph--type--oe-gallery'));
$this->assertCount(1, $crawler->filter('div.paragraph--type--oe-gallery'));
if ($expected['title'] !== NULL) {
$this->assertCount(1, $crawler->filter('div.paragraph--type--oe-gallery > h2'));
$this->assertEquals($expected['title'], trim($crawler->filter('div.paragraph--type--oe-gallery > h2.mb-4.bcl-heading')->text()));
}
else {
$this->assertCount(0, $crawler->filter('div.paragraph--type--oe-gallery > h2'));
}
if ($expected['description'] !== NULL) {
$this->assertCount(1, $crawler->filter('div.paragraph--type--oe-gallery > p'));
$this->assertEquals($expected['description'], trim($crawler->filter('div.paragraph--type--oe-gallery > p')->text()));
}
else {
$this->assertCount(0, $crawler->filter('div.paragraph--type--oe-gallery > p'));
}
$gallery_element = $crawler->filter('div.paragraph--type--oe-gallery > div.bcl-gallery');
$this->assertCount(1, $gallery_element);
$assert = new GalleryPatternAssert();
$assert->assertPattern([
'title' => NULL,
'items' => $expected['items'],
], $gallery_element->outerHtml());
}
/**
* Render a paragraph.
*
* @param \Drupal\paragraphs\ParagraphInterface $paragraph
* Paragraph entity.
* @param string|null $langcode
* Rendering language code, defaults to 'en'.
*
* @return string
* Rendered output.
*
* @throws \Exception
*/
protected function renderParagraph(ParagraphInterface $paragraph, string $langcode = NULL): string {
$render = \Drupal::entityTypeManager()
->getViewBuilder('paragraph')
->view($paragraph, 'default', $langcode);
return (string) $this->container->get('renderer')->renderRoot($render);
}
}