@@ -221,6 +221,45 @@ public function test_returns_no_srcs_with_block_in_post_with_no_attached_images(
221221 );
222222 }
223223
224+ /**
225+ * Ensures that the function only returns up to the requested count of galleries.
226+ *
227+ * @dataProvider data_unique_gallery_type_content
228+ *
229+ * @param $gallery
230+ */
231+ public function tests_returns_requested_max_number_of_galleries ( $ gallery ) {
232+ // @todo Why is this necessary?
233+ // add_shortcode( 'gallery', 'gallery_shortcode' );
234+
235+ $ post_id = self ::factory ()->post ->create (
236+ array ( 'post_content ' => str_repeat ( "{$ gallery }\n" , 5 ) )
237+ );
238+
239+ // Test negative counts, the zero count, and a max count above the total contained galleries.
240+ foreach ( range ( -5 , 10 ) as $ max_count ) {
241+ $ this ->assertCount (
242+ max ( 0 , min ( 5 , $ max_count ) ),
243+ get_post_galleries ( $ post_id , false , $ max_count ),
244+ 'Failed to fetch up to the max requested number of galleries. '
245+ );
246+ }
247+ }
248+
249+ /**
250+ * Data provider.
251+ *
252+ * @return array[]
253+ */
254+ public static function data_unique_gallery_type_content () {
255+ return array (
256+ 'Shortcode with ids ' => array ( '[gallery ids="11,12,13"] ' ),
257+ 'Shortcode without ids ' => array ( '[gallery]<figure><img src="image-15.jpg" data-id="15"></figure>[/gallery] ' ),
258+ 'Block with ids ' => array ( '<!-- wp:gallery {"ids":[14, 15, 16]} /--> ' ),
259+ 'Block with inner blocks ' => array ( '<!-- wp:gallery --><!-- wp:image {"id":11} --><img src="image-11.jpg" data-id="11"><!-- /wp:image --><!-- /wp:gallery --> ' ),
260+ );
261+ }
262+
224263 /**
225264 * Tests that no srcs are returned for a gallery block v2
226265 * in a post with no attached images.
0 commit comments