@@ -15,6 +15,12 @@ class Tests_Improve_Calculate_Sizes extends WP_UnitTestCase {
15
15
*/
16
16
public static $ image_id ;
17
17
18
+ /**
19
+ * Post ID.
20
+ *
21
+ * @var int
22
+ */
23
+ public static $ post_id ;
18
24
/**
19
25
* Set up the environment for the tests.
20
26
*/
@@ -24,6 +30,13 @@ public static function set_up_before_class(): void {
24
30
switch_theme ( 'twentytwentyfour ' );
25
31
26
32
self ::$ image_id = self ::factory ()->attachment ->create_upload_object ( TESTS_PLUGIN_DIR . '/tests/data/images/leaves.jpg ' );
33
+
34
+ self ::$ post_id = self ::factory ()->post ->create (
35
+ array (
36
+ 'post_status ' => 'publish ' ,
37
+ 'post_name ' => 'test-post ' ,
38
+ )
39
+ );
27
40
}
28
41
29
42
public function set_up (): void {
@@ -108,7 +121,7 @@ public function test_image_block_with_wide_alignment( string $image_size ): void
108
121
*
109
122
* @return array<array<string>> The image sizes.
110
123
*/
111
- public function data_image_sizes (): array {
124
+ public static function data_image_sizes (): array {
112
125
return array (
113
126
'Return full or wideSize 1280px instead of medium size 300px ' => array (
114
127
'medium ' ,
@@ -164,7 +177,7 @@ public function test_image_block_with_default_alignment( string $image_size, str
164
177
*
165
178
* @return array<string, array<int, bool|string>> The image sizes.
166
179
*/
167
- public function data_image_sizes_for_default_alignment (): array {
180
+ public static function data_image_sizes_for_default_alignment (): array {
168
181
return array (
169
182
'Return medium image size 300px instead of contentSize 620px ' => array (
170
183
'medium ' ,
@@ -239,7 +252,7 @@ public function test_image_block_with_left_right_center_alignment( string $image
239
252
*
240
253
* @return array<string, array<int, bool|string>> The image sizes and alignments.
241
254
*/
242
- public function data_image_sizes_for_left_right_center_alignment (): array {
255
+ public static function data_image_sizes_for_left_right_center_alignment (): array {
243
256
return array (
244
257
'Return medium image size 300px with left alignment ' => array (
245
258
'medium ' ,
@@ -369,7 +382,7 @@ public function test_cover_block_with_left_right_center_alignment( string $align
369
382
*
370
383
* @return array<array<string>> The image sizes.
371
384
*/
372
- public function data_image_left_right_center_alignment (): array {
385
+ public static function data_image_left_right_center_alignment (): array {
373
386
return array (
374
387
array ( 'left ' , 'sizes="(max-width: 420px) 100vw, 420px ' ),
375
388
array ( 'right ' , 'sizes="(max-width: 420px) 100vw, 420px ' ),
@@ -417,7 +430,7 @@ public function test_ancestor_layout_is_passed_by_context( string $ancestor_bloc
417
430
*
418
431
* @return array<string, array<int, bool|string>> The ancestor and image alignments.
419
432
*/
420
- public function data_ancestor_and_image_block_alignment (): array {
433
+ public static function data_ancestor_and_image_block_alignment (): array {
421
434
return array (
422
435
// Parent default alignment.
423
436
'Return contentSize 620px, parent block default alignment, image block default alignment ' => array (
@@ -546,7 +559,7 @@ public function test_sizes_with_relative_layout_sizes( string $ancestor_alignmen
546
559
*
547
560
* @return array<string, array<int, bool|string>> The ancestor and image alignments.
548
561
*/
549
- public function data_image_blocks_with_relative_alignment (): array {
562
+ public static function data_image_blocks_with_relative_alignment (): array {
550
563
return array (
551
564
// Parent default alignment.
552
565
'Return contentSize 50vw, parent block default alignment, image block default alignment ' => array (
@@ -636,7 +649,7 @@ public function test_image_block_with_different_alignment_in_classic_theme( stri
636
649
*
637
650
* @return array<array<string>> The ancestor and image alignments.
638
651
*/
639
- public function data_image_blocks_with_relative_alignment_for_classic_theme (): array {
652
+ public static function data_image_blocks_with_relative_alignment_for_classic_theme (): array {
640
653
return array (
641
654
array ( '' ),
642
655
array ( 'wide ' ),
@@ -673,7 +686,7 @@ public function test_image_block_with_single_column_block( string $ancestor_bloc
673
686
*
674
687
* @return array<string, array<int, bool|string>> The ancestor and image alignments.
675
688
*/
676
- public function data_image_block_with_column_block (): array {
689
+ public static function data_image_block_with_column_block (): array {
677
690
return array (
678
691
// Parent default alignment.
679
692
'Return contentSize 620px, parent block default alignment, image block default alignment ' => array (
@@ -809,7 +822,7 @@ public function test_image_block_with_two_equal_column_block( string $ancestor_b
809
822
*
810
823
* @return array<string, array<int, bool|string>> The ancestor and image alignments.
811
824
*/
812
- public function data_image_block_with_two_equal_column_block (): array {
825
+ public static function data_image_block_with_two_equal_column_block (): array {
813
826
return array (
814
827
// Parent default alignment.
815
828
'Return half size of contentSize 310px, parent block default alignment, image block default alignment ' => array (
@@ -945,7 +958,7 @@ public function test_image_block_with_two_different_width_column_block( string $
945
958
*
946
959
* @return array<string, array<int, bool|string>> The ancestor and image alignments.
947
960
*/
948
- public function data_image_block_with_two_different_width_column_block (): array {
961
+ public static function data_image_block_with_two_different_width_column_block (): array {
949
962
return array (
950
963
// Parent default alignment.
951
964
'Return 66.66% width of contentSize 413px, parent block default alignment, image block default alignment ' => array (
@@ -1087,7 +1100,7 @@ public function test_image_block_with_parent_columns_and_its_parent_group_block(
1087
1100
*
1088
1101
* @return array<string, array<int, string>> The ancestor and image alignments.
1089
1102
*/
1090
- public function data_image_block_with_parent_columns_and_its_parent_group_block (): array {
1103
+ public static function data_image_block_with_parent_columns_and_its_parent_group_block (): array {
1091
1104
return array (
1092
1105
// Parent default alignment.
1093
1106
'Return 66.66% width of contentSize 413px, group block default alignment, columns block default alignment, image block default alignment ' => array (
@@ -1259,6 +1272,134 @@ public function data_image_block_with_parent_columns_and_its_parent_group_block(
1259
1272
);
1260
1273
}
1261
1274
1275
+ /**
1276
+ * Verifies that the post featured image block does not render when no featured image is set for the post.
1277
+ */
1278
+ public function test_post_featured_image_block_without_featured_image (): void {
1279
+ $ block_content = '<!-- wp:post-featured-image /--> ' ;
1280
+
1281
+ // Set up global $post so 'the_content' filter works as expected.
1282
+ global $ post ;
1283
+ $ post = get_post ( self ::$ post_id );
1284
+ setup_postdata ( $ post );
1285
+
1286
+ $ result = apply_filters ( 'the_content ' , $ block_content );
1287
+
1288
+ $ this ->assertStringContainsString ( '' , $ result );
1289
+
1290
+ wp_reset_postdata ();
1291
+ }
1292
+
1293
+ /**
1294
+ * Test that the post featured image block renders correctly with different image sizes.
1295
+ *
1296
+ * @dataProvider data_post_featured_image_block_image_sizes
1297
+ *
1298
+ * @param string $image_size Image size.
1299
+ * @param string $expected Expected output.
1300
+ */
1301
+ public function test_post_featured_image_block_with_different_image_size ( string $ image_size , string $ expected ): void {
1302
+ update_post_meta ( self ::$ post_id , '_thumbnail_id ' , self ::$ image_id );
1303
+
1304
+ $ block_content = '<!-- wp:post-featured-image {"sizeSlug":" ' . $ image_size . '"} /--> ' ;
1305
+
1306
+ // Set up global $post so 'the_content' filter works as expected.
1307
+ global $ post ;
1308
+ $ post = get_post ( self ::$ post_id );
1309
+ setup_postdata ( $ post );
1310
+
1311
+ $ result = apply_filters ( 'the_content ' , $ block_content );
1312
+
1313
+ // Check that the featured image block renders the image and has a sizes attribute.
1314
+ $ this ->assertStringContainsString ( 'wp-block-post-featured-image ' , $ result );
1315
+ $ this ->assertStringContainsString ( $ expected , $ result );
1316
+
1317
+ wp_reset_postdata ();
1318
+ }
1319
+
1320
+ /**
1321
+ * Data provider.
1322
+ *
1323
+ * @return array<array<string>> The image sizes.
1324
+ */
1325
+ public static function data_post_featured_image_block_image_sizes (): array {
1326
+ return array (
1327
+ 'Return medium image size 300px ' => array (
1328
+ 'medium ' ,
1329
+ 'sizes="(max-width: 300px) 100vw, 300px" ' ,
1330
+ ),
1331
+ 'Return contentSize 620px instead of large size 1024px ' => array (
1332
+ 'large ' ,
1333
+ 'sizes="(max-width: 620px) 100vw, 620px" ' ,
1334
+ ),
1335
+ 'Return contentSize 620px instead of full size 1080px ' => array (
1336
+ 'full ' ,
1337
+ 'sizes="(max-width: 620px) 100vw, 620px" ' ,
1338
+ ),
1339
+ );
1340
+ }
1341
+
1342
+ /**
1343
+ * Test that the post featured image block renders correctly with different alignment.
1344
+ *
1345
+ * @dataProvider data_post_featured_image_block_alignment
1346
+ *
1347
+ * @param string $alignment Alignment of the image.
1348
+ * @param string $expected Expected output.
1349
+ */
1350
+ public function test_post_featured_image_block_with_different_alignment ( string $ alignment , string $ expected ): void {
1351
+ update_post_meta ( self ::$ post_id , '_thumbnail_id ' , self ::$ image_id );
1352
+
1353
+ $ block_content = '<!-- wp:post-featured-image {"align":" ' . $ alignment . '"} /--> ' ;
1354
+
1355
+ // Set up global $post so 'the_content' filter works as expected.
1356
+ global $ post ;
1357
+ $ post = get_post ( self ::$ post_id );
1358
+ setup_postdata ( $ post );
1359
+
1360
+ $ result = apply_filters ( 'the_content ' , $ block_content );
1361
+
1362
+ // Check that the featured image block renders the image and has a sizes attribute.
1363
+ $ this ->assertStringContainsString ( 'wp-block-post-featured-image ' , $ result );
1364
+ $ this ->assertStringContainsString ( $ expected , $ result );
1365
+
1366
+ wp_reset_postdata ();
1367
+ }
1368
+
1369
+ /**
1370
+ * Data provider.
1371
+ *
1372
+ * @return array<array<string>> The image sizes.
1373
+ */
1374
+ public static function data_post_featured_image_block_alignment (): array {
1375
+ return array (
1376
+ 'Return contentSize 620px instead of image size 1080px, block default alignment ' => array (
1377
+ '' ,
1378
+ 'sizes="(max-width: 620px) 100vw, 620px" ' ,
1379
+ ),
1380
+ 'Return wideSize 1280px instead of image size 1080px, block wide alignment ' => array (
1381
+ 'wide ' ,
1382
+ 'sizes="(max-width: 1280px) 100vw, 1280px" ' ,
1383
+ ),
1384
+ 'Return full size instead of image size 1080px, block full alignment ' => array (
1385
+ 'full ' ,
1386
+ 'sizes="100vw" ' ,
1387
+ ),
1388
+ 'Return image size 1080px, block left alignment ' => array (
1389
+ 'left ' ,
1390
+ 'sizes="(max-width: 1080px) 100vw, 1080px" ' ,
1391
+ ),
1392
+ 'Return image size 1080px, block right alignment ' => array (
1393
+ 'right ' ,
1394
+ 'sizes="(max-width: 1080px) 100vw, 1080px" ' ,
1395
+ ),
1396
+ 'Return contentSize 620px instead of image size 1080px, block center alignment ' => array (
1397
+ 'center ' ,
1398
+ 'sizes="(max-width: 620px) 100vw, 620px" ' ,
1399
+ ),
1400
+ );
1401
+ }
1402
+
1262
1403
/**
1263
1404
* Filter the theme.json data to include relative layout sizes.
1264
1405
*
0 commit comments