/
test-amp-gallery-embed.php
244 lines (224 loc) · 11.6 KB
/
test-amp-gallery-embed.php
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
/**
* Tests for gallery embed.
*
* @package AMP
*/
/**
* Class AMP_Gallery_Embed_Test
*/
class AMP_Gallery_Embed_Test extends WP_UnitTestCase {
/**
* Tear down.
*/
public function tearDown() {
if ( did_action( 'add_attachment' ) ) {
$this->remove_added_uploads();
}
parent::tearDown();
}
/**
* Mock caption text.
*
* @var string
*/
const CAPTION_TEXT = 'Example caption';
/**
* Replacements.
*
* @var array Associative array of string replacements to process.
*/
private $replacements = [];
/**
* Get conversion data.
*
* @return array[]
*/
public function get_conversion_data() {
$amp_carousel_caption = '<span class="amp-wp-gallery-caption"><span>' . self::CAPTION_TEXT . '</span></span>';
$loading_attribute = version_compare( get_bloginfo( 'version' ), '5.4', '>' ) ? 'loading="lazy"' : '';
return [
'shortcode_with_invalid_id' => [
'[gallery ids=1]',
'',
],
'shortcode_with_valid_id' => [
'[gallery ids={{id1}}]',
'<amp-carousel width="50" height="50" type="slides" layout="responsive"><span class="slide"><img src="{{file1}}.jpg" width="50" height="50" layout="fill" alt="Alt text" object-fit="cover">' . $amp_carousel_caption . '</span></amp-carousel>',
],
'shortcode_with_multiple_ids' => [
'[gallery ids={{id1}},{{id2}},{{id3}}]',
'<amp-carousel width="600" height="450" type="slides" layout="responsive">' .
'<span class="slide"><img src="{{file1}}.jpg" width="50" height="50" layout="fill" alt="Alt text" object-fit="cover">' . $amp_carousel_caption . '</span>' .
'<span class="slide"><img src="{{file2}}.jpg" width="600" height="450" layout="fill" alt="Alt text" srcset="{{file2}}.jpg 640w, {{file2}}-300x225.jpg 300w" object-fit="cover"></span>' .
'<span class="slide"><img src="{{file3}}.jpg" width="100" height="100" layout="fill" alt="Alt text" object-fit="cover"></span>' .
'</amp-carousel>',
],
'shortcode_linking_to_file' => [
'[gallery link="file" ids={{id1}},{{id2}},{{id3}}]',
'<amp-carousel width="600" height="450" type="slides" layout="responsive">' .
'<span class="slide"><a href="{{file1}}.jpg"><img src="{{file1}}.jpg" width="50" height="50" layout="fill" alt="Alt text" object-fit="cover"></a>' . $amp_carousel_caption . '</span>' .
'<span class="slide"><a href="{{file2}}.jpg"><img src="{{file2}}.jpg" width="600" height="450" layout="fill" alt="Alt text" srcset="{{file2}}.jpg 640w, {{file2}}-300x225.jpg 300w" object-fit="cover"></a></span>' .
'<span class="slide"><a href="{{file3}}.jpg"><img src="{{file3}}.jpg" width="100" height="100" layout="fill" alt="Alt text" object-fit="cover"></a></span>' .
'</amp-carousel>',
],
'shortcode_with_carousel' => [
'[gallery amp-lightbox=false amp-carousel=true ids={{id1}},{{id2}},{{id3}}]',
'<amp-carousel width="600" height="450" type="slides" layout="responsive">' .
'<span class="slide"><img src="{{file1}}.jpg" width="50" height="50" layout="fill" alt="Alt text" object-fit="cover">' . $amp_carousel_caption . '</span>' .
'<span class="slide"><img src="{{file2}}.jpg" width="600" height="450" layout="fill" alt="Alt text" srcset="{{file2}}.jpg 640w, {{file2}}-300x225.jpg 300w" object-fit="cover"></span>' .
'<span class="slide"><img src="{{file3}}.jpg" width="100" height="100" layout="fill" alt="Alt text" object-fit="cover"></span>' .
'</amp-carousel>',
],
'shortcode_with_carousel_linking_to_file' => [
'[gallery amp-lightbox=false amp-carousel=true link="file" ids={{id1}},{{id2}},{{id3}}]',
'<amp-carousel width="600" height="450" type="slides" layout="responsive">' .
'<span class="slide"><a href="{{file1}}.jpg"><img src="{{file1}}.jpg" width="50" height="50" layout="fill" alt="Alt text" object-fit="cover"></a>' . $amp_carousel_caption . '</span>' .
'<span class="slide"><a href="{{file2}}.jpg"><img src="{{file2}}.jpg" width="600" height="450" layout="fill" alt="Alt text" srcset="{{file2}}.jpg 640w, {{file2}}-300x225.jpg 300w" object-fit="cover"></a></span>' .
'<span class="slide"><a href="{{file3}}.jpg"><img src="{{file3}}.jpg" width="100" height="100" layout="fill" alt="Alt text" object-fit="cover"></a></span>' .
'</amp-carousel>',
],
'shortcode_with_lightbox' => [
'[gallery amp-lightbox=true amp-carousel=false ids={{id1}},{{id2}},{{id3}}]',
'<style type=\'text/css\'> #gallery-8 { margin: auto; } #gallery-8 .gallery-item { float: left; margin-top: 10px; text-align: center; width: 33%; } #gallery-8 img { border: 2px solid #cfcfcf; } #gallery-8 .gallery-caption { margin-left: 0; } /* see gallery_shortcode() in wp-includes/media.php */ </style>
<div id=\'gallery-8\' class=\'gallery galleryid-0 gallery-columns-3 gallery-size-thumbnail\'>
<dl class=\'gallery-item\'><dt class=\'gallery-icon landscape\'>
<img width="50" height="50" src="{{file1}}.jpg" class="attachment-thumbnail size-thumbnail" alt="Alt text" ' . $loading_attribute . ' aria-describedby="gallery-8-{{id1}}" lightbox="" />
</dt><dd class=\'wp-caption-text gallery-caption\' id=\'gallery-8-{{id1}}\'> ' . self::CAPTION_TEXT . ' </dd></dl>
<dl class=\'gallery-item\'><dt class=\'gallery-icon landscape\'>
<img width="150" height="150" src="{{file2}}-150x150.jpg" class="attachment-thumbnail size-thumbnail" alt="Alt text" ' . $loading_attribute . ' lightbox="" />
</dt></dl>
<dl class=\'gallery-item\'><dt class=\'gallery-icon landscape\'>
<img width="100" height="100" src="{{file3}}.jpg" class="attachment-thumbnail size-thumbnail" alt="Alt text" ' . $loading_attribute . ' lightbox="" />
</dt></dl>
<br style="clear: both" />
</div>',
],
'shortcode_with_lightbox_linking_to_file' => [
'[gallery amp-lightbox=true amp-carousel=false link="file" ids={{id1}},{{id2}},{{id3}}]',
'<style type=\'text/css\'> #gallery-10 { margin: auto; } #gallery-10 .gallery-item { float: left; margin-top: 10px; text-align: center; width: 33%; } #gallery-10 img { border: 2px solid #cfcfcf; } #gallery-10 .gallery-caption { margin-left: 0; } /* see gallery_shortcode() in wp-includes/media.php */ </style>
<div id=\'gallery-10\' class=\'gallery galleryid-0 gallery-columns-3 gallery-size-thumbnail\'>
<dl class=\'gallery-item\'><dt class=\'gallery-icon landscape\'>
<img width="50" height="50" src="{{file1}}.jpg" class="attachment-thumbnail size-thumbnail" alt="Alt text" ' . $loading_attribute . ' aria-describedby="gallery-10-{{id1}}" lightbox="" />
</dt><dd class=\'wp-caption-text gallery-caption\' id=\'gallery-10-{{id1}}\'> ' . self::CAPTION_TEXT . ' </dd></dl>
<dl class=\'gallery-item\'><dt class=\'gallery-icon landscape\'>
<img width="150" height="150" src="{{file2}}-150x150.jpg" class="attachment-thumbnail size-thumbnail" alt="Alt text" ' . $loading_attribute . ' lightbox="" />
</dt></dl>
<dl class=\'gallery-item\'><dt class=\'gallery-icon landscape\'>
<img width="100" height="100" src="{{file3}}.jpg" class="attachment-thumbnail size-thumbnail" alt="Alt text" ' . $loading_attribute . ' lightbox="" />
</dt></dl>
<br style="clear: both" />
</div>',
],
'shortcode_with_lightbox_and_carousel' => [
'[gallery amp-lightbox=true amp-carousel=true ids={{id1}},{{id2}},{{id3}}]',
'<amp-carousel width="600" height="450" type="slides" layout="responsive">' .
'<span class="slide"><img src="{{file1}}.jpg" width="50" height="50" layout="fill" alt="Alt text" lightbox="" object-fit="cover">' . $amp_carousel_caption . '</span>' .
'<span class="slide"><img src="{{file2}}.jpg" width="600" height="450" layout="fill" alt="Alt text" srcset="{{file2}}.jpg 640w, {{file2}}-300x225.jpg 300w" lightbox="" object-fit="cover"></span>' .
'<span class="slide"><img src="{{file3}}.jpg" width="100" height="100" layout="fill" alt="Alt text" lightbox="" object-fit="cover"></span>' .
'</amp-carousel>',
],
'shortcode_with_lightbox_and_carousel_linking_to_file' => [
'[gallery amp-lightbox=true amp-carousel=true link="file" ids={{id1}},{{id2}},{{id3}}]',
'<amp-carousel width="600" height="450" type="slides" layout="responsive">' .
'<span class="slide"><img src="{{file1}}.jpg" width="50" height="50" layout="fill" alt="Alt text" lightbox="" object-fit="cover">' . $amp_carousel_caption . '</span>' .
'<span class="slide"><img src="{{file2}}.jpg" width="600" height="450" layout="fill" alt="Alt text" srcset="{{file2}}.jpg 640w, {{file2}}-300x225.jpg 300w" lightbox="" object-fit="cover"></span>' .
'<span class="slide"><img src="{{file3}}.jpg" width="100" height="100" layout="fill" alt="Alt text" lightbox="" object-fit="cover"></span>' .
'</amp-carousel>',
],
];
}
/**
* Test conversion.
*
* @dataProvider get_conversion_data
* @param string $source Source.
* @param string $expected Expected.
*/
public function test__conversion( $source, $expected ) {
$source_files = [
DIR_TESTDATA . '/images/test-image.jpg',
DIR_TESTDATA . '/images/canola.jpg',
DIR_TESTDATA . '/images/gradient-square.jpg',
];
// When we generate new attachments, we neither control the IDs being
// generated, which we need for the gallery shortcode, nor the actual
// filenames, which will have an index appended because of filename
// collisions. So we write the tests in a way that they don't rely on
// this, and str-replace the dynamic portions into the expected and
// actual output after the fact.
$ids = [];
$files = [];
foreach ( $source_files as $index => $source_file ) {
$ids[ $index ] = self::factory()->attachment
->create_upload_object( $source_file, 0 );
$files[ $index ] = rtrim(
wp_get_attachment_url( $ids[ $index ] ),
'.jpg'
);
update_post_meta(
$ids[ $index ],
'_wp_attachment_image_alt',
'Alt text'
);
// Add a caption to the first image.
if ( 0 === $index ) {
wp_update_post(
[
'ID' => $ids[ $index ],
'post_excerpt' => self::CAPTION_TEXT,
]
);
}
}
$this->initialize_replacements( $ids, $files );
$embed = new AMP_Gallery_Embed_Handler();
$embed->register_embed();
$filtered_content = apply_filters(
'the_content',
$this->normalize( $source )
);
$this->assertEquals(
$this->normalize( $expected ),
$this->normalize( $filtered_content )
);
}
/**
* Initialize the associative array of replacements to perform.
*
* @param array<int> $ids Array of attachment post IDs.
* @param array<string> $files Array of file URLs.
*/
private function initialize_replacements( $ids, $files ) {
$this->replacements = [
'{{id1}}' => $ids[0],
'{{id2}}' => $ids[1],
'{{id3}}' => $ids[2],
'{{file1}}' => $files[0],
'{{file2}}' => $files[1],
'{{file3}}' => $files[2],
"\n" => '', // Make tests ignore new lines.
'> <' => '><', // Remove left-over space between elements.
];
}
/**
* Normalize a piece of content by replacing placeholders with the related
* dynamic parts.
*
* @param string|array $content Content to normalize.
* @return string|array Normalized content.
*/
private function normalize( $content ) {
// We start by turning multiple whitespaces into one space, as the default WP gallery code
// creates a mess with lots of spaces.
$content = trim( preg_replace( '/\s+/', ' ', $content ) );
// Normalize attribute quote style for 5.3-alpha.
$content = str_replace( '<style type=\'text/css\'>', '<style type="text/css">', $content );
// Then we go through all previously defined replacements.
return str_replace(
array_keys( $this->replacements ),
$this->replacements,
$content
);
}
}