post->create( array( 'post_title' => 'Gallery dynamic mode test post' ) ); $file = DIR_TESTDATA . '/images/canola.jpg'; // Two images attached to the post. self::$attachment_ids[] = $factory->attachment->create_upload_object( $file, self::$post_id ); self::$attachment_ids[] = $factory->attachment->create_upload_object( $file, self::$post_id ); // Give the attachments distinct dates so `orderby=date` is deterministic // and actually reverses between asc and desc. Created back-to-back from // the same file they would otherwise share a `post_date`, which MySQL // tie-breaks by ID regardless of order direction. wp_update_post( array( 'ID' => self::$attachment_ids[0], 'post_date' => '2020-01-01 00:00:00', 'post_date_gmt' => '2020-01-01 00:00:00', ) ); wp_update_post( array( 'ID' => self::$attachment_ids[1], 'post_date' => '2020-01-02 00:00:00', 'post_date_gmt' => '2020-01-02 00:00:00', ) ); } public static function wpTearDownAfterClass() { foreach ( self::$attachment_ids as $attachment_id ) { wp_delete_attachment( $attachment_id, true ); } wp_delete_post( self::$post_id, true ); } /** * Renders a gallery block within the loop for self::$post_id so that the * `core/attached-media` source resolves against it (via the `get_the_ID()` * fallback). * * @param string $block_markup Serialized gallery block. * @return string Rendered HTML. */ private function render_in_loop( $block_markup ) { global $post; $post = get_post( self::$post_id ); setup_postdata( $post ); $output = do_blocks( $block_markup ); wp_reset_postdata(); return $output; } public function test_dynamic_attached_to_post_renders_attached_images() { $output = $this->render_in_loop( '' ); // The gallery wrapper is built from scratch (no markup is persisted in // dynamic mode), so assert it carries the gallery-specific classes plus // the flex layout class added by the layout render filter — i.e. the same // wrapper a static gallery would produce. foreach ( array( 'wp-block-gallery', 'has-nested-images', 'columns-default', 'is-cropped', 'is-layout-flex', ) as $expected_class ) { $this->assertStringContainsString( $expected_class, $output, "Constructed gallery wrapper should include the `$expected_class` class." ); } // One image figure per attached image. $this->assertSame( count( self::$attachment_ids ), substr_count( $output, 'wp-block-image' ), 'Should render one image block per attached image.' ); foreach ( self::$attachment_ids as $attachment_id ) { $this->assertStringContainsString( 'wp-image-' . $attachment_id, $output, "Rendered gallery should contain attachment $attachment_id." ); } } public function test_dynamic_grid_uses_only_standard_grid_layout_classes() { $output = $this->render_in_loop( '' ); $this->assertStringContainsString( 'is-layout-grid', $output ); $this->assertStringNotContainsString( 'is-layout-flex', $output ); $this->assertStringNotContainsString( 'columns-2', $output ); $this->assertStringNotContainsString( 'columns-default', $output ); $this->assertStringNotContainsString( 'is-cropped', $output ); $this->assertDoesNotMatchRegularExpression( '/\bwp-block-gallery-\d+\b/', $output, 'Grid galleries should not receive the unique class used by the custom Flex gap calculation.' ); } public function test_dynamic_attached_to_post_honours_order() { $asc = $this->render_in_loop( '' ); $desc = $this->render_in_loop( '' ); $first = self::$attachment_ids[0]; $second = self::$attachment_ids[1]; // Oldest to newest: the first-created attachment renders before the second. $this->assertLessThan( strpos( $asc, 'wp-image-' . $second ), strpos( $asc, 'wp-image-' . $first ), 'With order=asc the earlier attachment should render first.' ); // Newest to oldest reverses that order. $this->assertLessThan( strpos( $desc, 'wp-image-' . $first ), strpos( $desc, 'wp-image-' . $second ), 'With order=desc the later attachment should render first.' ); } public function test_dynamic_source_with_no_images_renders_nothing() { // An unrecognized source (like a valid source that resolves to no images) // should render nothing at all — not an empty gallery wrapper. $output = $this->render_in_loop( '' ); $this->assertStringNotContainsString( 'wp-block-image', $output ); $this->assertStringNotContainsString( 'wp-block-gallery', $output ); $this->assertSame( '', trim( $output ) ); } public function test_dynamic_gallery_ignores_malformed_dynamic_content() { // A `dynamicContent` that isn't the expected `{ source, args }` object // (e.g. corrupted or hand-edited markup) resolves to no source and renders // nothing, without reading array offsets off a non-array value. $non_object = $this->render_in_loop( '' ); $this->assertSame( '', trim( $non_object ), 'A non-object dynamicContent should resolve to no source and render nothing.' ); // A valid source with a malformed (non-array) `args` falls back to the // default ordering and still renders the attached images, again without // indexing a non-array. $malformed_args = $this->render_in_loop( '' ); $this->assertSame( count( self::$attachment_ids ), substr_count( $malformed_args, 'wp-block-image' ), 'A gallery with malformed args should still render the attached images using the default ordering.' ); } public function test_dynamic_gallery_sanitizes_image_aspect_ratio_style() { $valid_output = $this->render_in_loop( '' ); $this->assertStringContainsString( 'style="aspect-ratio:16/9;object-fit:cover"', $valid_output, 'A valid aspect ratio should be rendered as an image style.' ); // A value that tries to break out of the style attribute is run through // `safecss_filter_attr` and escaped on output, so it cannot introduce an // event handler or extra markup. $malicious_output = $this->render_in_loop( '' ); $this->assertStringNotContainsString( 'onload="alert(1)', $malicious_output, 'A malicious aspect ratio must not break out of the style attribute.' ); $this->assertStringNotContainsString( '`. The render callback builds the // wrapper, injects the resolved images, then appends the caption. $output = $this->render_in_loop( '' ); $this->assertStringContainsString( '', $output, 'The saved gallery caption should be rendered.' ); // The caption is appended after the images (mirroring a static gallery). $this->assertGreaterThan( strpos( $output, 'wp-image-' . self::$attachment_ids[0] ), strpos( $output, 'blocks-gallery-caption' ), 'The gallery caption should render after the images.' ); // It sits inside the gallery figure, not loose after it. $this->assertStringEndsWith( '', trim( $output ) ); } public function test_static_gallery_without_dynamic_source_is_unaffected() { $attachment_id = self::$attachment_ids[0]; $image_url = wp_get_attachment_image_url( $attachment_id, 'large' ); $markup = sprintf( '', $attachment_id, $image_url ); $output = $this->render_in_loop( $markup ); // The single, manually-added image renders; the dynamic source path is // not engaged, so no extra attached images are injected. $this->assertSame( 1, substr_count( $output, 'wp-block-image' ) ); $this->assertStringContainsString( 'wp-image-' . $attachment_id, $output ); } public function test_dynamic_caption_mirrors_raw_excerpt_and_ignores_filter() { $attachment_id = self::$attachment_ids[0]; // Give the attachment a raw caption (`post_excerpt`). wp_update_post( array( 'ID' => $attachment_id, 'post_excerpt' => 'Raw caption text', ) ); // The editor preview builds the image caption from the REST `caption.raw` // value and cannot see PHP filters, so a `wp_get_attachment_caption` filter // must NOT influence the rendered figcaption — otherwise the frontend would // diverge from the preview. $filter = static function () { return 'Filtered caption text'; }; add_filter( 'wp_get_attachment_caption', $filter ); try { $output = $this->render_in_loop( '' ); } finally { remove_filter( 'wp_get_attachment_caption', $filter ); // Reset so the shared fixture doesn't leak into other tests. wp_update_post( array( 'ID' => $attachment_id, 'post_excerpt' => '', ) ); } $this->assertStringContainsString( 'wp-element-caption', $output, 'Dynamic render should output a caption element.' ); $this->assertStringContainsString( 'Raw caption text', $output, 'Dynamic render should use the raw attachment caption, mirroring the editor preview.' ); $this->assertStringNotContainsString( 'Filtered caption text', $output, 'The wp_get_attachment_caption filter must not affect the dynamic render; it cannot be mirrored in the editor preview.' ); } public function test_gap_styles_skipped_when_theme_opts_out_of_layout_styles() { $attachment_id = self::$attachment_ids[0]; $image_url = wp_get_attachment_image_url( $attachment_id, 'large' ); $markup = sprintf( '', $attachment_id, $image_url ); // Baseline: the render generates per-instance gap styles, scoped by a // unique classname added to the gallery wrapper. WP_Style_Engine_CSS_Rules_Store_Gutenberg::remove_all_stores(); $output = $this->render_in_loop( $markup ); $stylesheet = gutenberg_style_engine_get_stylesheet_from_context( 'block-supports' ); $this->assertMatchesRegularExpression( '/wp-block-gallery-\d/', $output, 'By default the gallery wrapper should carry a unique gap-scoping classname.' ); $this->assertStringContainsString( '--wp--style--unstable-gallery-gap', $stylesheet, 'By default the gallery gap styles should be generated.' ); // A theme that opts out of layout styles opts out of the generated gap // styles too, and of the unique classname that exists only to scope them. add_theme_support( 'disable-layout-styles' ); WP_Style_Engine_CSS_Rules_Store_Gutenberg::remove_all_stores(); try { $output = $this->render_in_loop( $markup ); $stylesheet = gutenberg_style_engine_get_stylesheet_from_context( 'block-supports' ); } finally { remove_theme_support( 'disable-layout-styles' ); } $this->assertDoesNotMatchRegularExpression( '/wp-block-gallery-\d/', $output, 'Themes opting out of layout styles should not get the unique gap-scoping classname.' ); $this->assertStringNotContainsString( '--wp--style--unstable-gallery-gap', $stylesheet, 'Themes opting out of layout styles should not get generated gallery gap styles.' ); // The gallery itself still renders. $this->assertStringContainsString( 'wp-image-' . $attachment_id, $output, 'Opting out of layout styles should not affect the gallery markup itself.' ); } public function test_dynamic_lightbox_link_adds_interactivity_directives() { $output = $this->render_in_loop( '' ); // Lightbox-enabled images go through the image block's lightbox render, // which the gallery then wires up for navigation. $this->assertStringContainsString( 'data-wp-interactive="core/gallery"', $output ); $this->assertStringContainsString( 'lightbox-trigger', $output ); } public function test_static_gallery_outputs_viewport_columns_style() { WP_Style_Engine_CSS_Rules_Store_Gutenberg::remove_all_stores(); $output = do_blocks( '' ); $stylesheet = gutenberg_style_engine_get_stylesheet_from_context( 'block-supports', array( 'prettify' => false ) ); $this->assertMatchesRegularExpression( '/\bwp-block-gallery-\d+\b/', $output ); $this->assertStringContainsString( '@media (width <= 480px)', $stylesheet ); $this->assertStringContainsString( 'width:100%', $stylesheet ); } public function test_static_gallery_outputs_viewport_aspect_ratio_style_in_any_layout() { WP_Style_Engine_CSS_Rules_Store_Gutenberg::remove_all_stores(); // A Grid Gallery: the column and crop rules don't apply to it, but the // aspect ratio does, so it still needs the per-instance stylesheet. $output = do_blocks( '' ); $stylesheet = gutenberg_style_engine_get_stylesheet_from_context( 'block-supports', array( 'prettify' => false ) ); $this->assertMatchesRegularExpression( '/\bwp-block-gallery-\d+\b/', $output ); $this->assertStringContainsString( '@media (width <= 480px)', $stylesheet ); $this->assertStringContainsString( 'aspect-ratio:16/9 !important', $stylesheet ); $this->assertStringContainsString( 'object-fit:cover !important', $stylesheet ); $this->assertStringNotContainsString( 'is-layout-flex', $stylesheet ); } public function test_dynamic_gallery_outputs_viewport_aspect_ratio_style() { WP_Style_Engine_CSS_Rules_Store_Gutenberg::remove_all_stores(); // A dynamic gallery has no inner image blocks to carry the ratio, so // the Gallery's own responsive CSS has to supply it. $output = $this->render_in_loop( '' ); $stylesheet = gutenberg_style_engine_get_stylesheet_from_context( 'block-supports', array( 'prettify' => false ) ); $this->assertMatchesRegularExpression( '/\bwp-block-gallery-\d+\b/', $output ); $this->assertStringContainsString( '@media (480px < width <= 782px)', $stylesheet ); $this->assertStringContainsString( 'aspect-ratio:4/3 !important', $stylesheet ); } public function test_static_gallery_cancels_base_aspect_ratio_for_viewport() { WP_Style_Engine_CSS_Rules_Store_Gutenberg::remove_all_stores(); do_blocks( '' ); $stylesheet = gutenberg_style_engine_get_stylesheet_from_context( 'block-supports', array( 'prettify' => false ) ); /* * `auto` would override the `width`/`height` presentational hint a * lazy-loaded image relies on for its placeholder ratio, collapsing it to * zero height until it loads, so the declaration is rolled out of the * cascade instead. */ $this->assertStringContainsString( 'aspect-ratio:revert-layer !important', $stylesheet ); $this->assertStringNotContainsString( 'aspect-ratio:auto', $stylesheet ); } public function test_static_gallery_ignores_unsafe_viewport_aspect_ratio() { WP_Style_Engine_CSS_Rules_Store_Gutenberg::remove_all_stores(); // The value is interpolated into a generated rule rather than an inline // style, so anything that isn't a plain ratio is dropped instead of // being emitted and closing the rule early. do_blocks( '' ); $stylesheet = gutenberg_style_engine_get_stylesheet_from_context( 'block-supports', array( 'prettify' => false ) ); $this->assertStringNotContainsString( 'aspect-ratio', $stylesheet ); $this->assertStringNotContainsString( 'display:none', $stylesheet ); } public function test_static_gallery_outputs_viewport_crop_style() { WP_Style_Engine_CSS_Rules_Store_Gutenberg::remove_all_stores(); do_blocks( '' ); $stylesheet = gutenberg_style_engine_get_stylesheet_from_context( 'block-supports', array( 'prettify' => false ) ); $this->assertStringContainsString( '@media (480px < width <= 782px)', $stylesheet ); $this->assertStringContainsString( 'object-fit:fill', $stylesheet ); $this->assertStringContainsString( 'display:block', $stylesheet ); } }