@@ -221,6 +221,46 @@ 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+
234+ // add_shortcode( 'gallery', 'gallery_shortcode' );
235+
236+ $ post_id = self ::factory ()->post ->create (
237+ array ( 'post_content ' => str_repeat ( "{$ gallery }\n" , 5 ) )
238+ );
239+
240+ // Test negative counts, the zero count, and a max count above the total contained galleries.
241+ foreach ( range ( -5 , 10 ) as $ max_count ) {
242+ $ this ->assertCount (
243+ max ( 0 , min ( 5 , $ max_count ) ),
244+ get_post_galleries ( $ post_id , false , $ max_count ),
245+ 'Failed to fetch up to the max requested number of galleries. '
246+ );
247+ }
248+ }
249+
250+ /**
251+ * Data provider.
252+ *
253+ * @return array[]
254+ */
255+ public static function data_unique_gallery_type_content () {
256+ return array (
257+ 'Shortcode with ids ' => array ( '[gallery ids="11,12,13"] ' ),
258+ 'Shortcode without ids ' => array ( '[gallery]<figure><img src="image-15.jpg" data-id="15"></figure>[/gallery] ' ),
259+ 'Block with ids ' => array ( '<!-- wp:gallery {"ids":[14, 15, 16]} /--> ' ),
260+ 'Block with inner blocks ' => array ( '<!-- wp:gallery --><!-- wp:image {"id":11} --><img src="image-11.jpg" data-id="11"><!-- /wp:image --><!-- /wp:gallery --> ' ),
261+ );
262+ }
263+
224264 /**
225265 * Tests that no srcs are returned for a gallery block v2
226266 * in a post with no attached images.
0 commit comments