|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Post featured image block tests. |
| 4 | + * |
| 5 | + * @package WordPress |
| 6 | + * @subpackage Blocks |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * Post featured image block tests. |
| 11 | + * |
| 12 | + * @group blocks |
| 13 | + */ |
| 14 | +class Tests_Blocks_RenderFeaturedImage extends WP_UnitTestCase { |
| 15 | + protected static $attachments = array(); |
| 16 | + |
| 17 | + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { |
| 18 | + $file = DIR_TESTDATA . '/images/canola.jpg'; |
| 19 | + $now = time(); |
| 20 | + |
| 21 | + for ( $i = 0; $i <= 3; $i ++ ) { |
| 22 | + $post_date = gmdate( 'Y-m-d H:i:s', $now - ( 10 * $i ) ); |
| 23 | + $post = $factory->post->create( |
| 24 | + array( |
| 25 | + 'post_date' => $post_date, |
| 26 | + ) |
| 27 | + ); |
| 28 | + |
| 29 | + self::$attachments[ $i ] = $factory->attachment->create_upload_object( $file, $post ); |
| 30 | + set_post_thumbnail( $post, self::$attachments[ $i ] ); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + public static function tear_down_after_class() { |
| 35 | + foreach ( self::$attachments as $attachment_id ) { |
| 36 | + wp_delete_post( $attachment_id, true ); |
| 37 | + } |
| 38 | + parent::tear_down_after_class(); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @covers render_block_core_post_featured_image |
| 43 | + */ |
| 44 | + public function test_render_block_core_post_featured_image() { |
| 45 | + $a = new MockAction(); |
| 46 | + add_filter( 'update_post_metadata_cache', array( $a, 'filter' ), 10, 2 ); |
| 47 | + query_posts( array() ); |
| 48 | + |
| 49 | + while ( have_posts() ) { |
| 50 | + the_post(); |
| 51 | + $block = new WP_Block( |
| 52 | + array( |
| 53 | + 'blockName' => 'core/post-featured-image', |
| 54 | + ), |
| 55 | + array( |
| 56 | + 'postId' => get_the_ID(), |
| 57 | + ) |
| 58 | + ); |
| 59 | + render_block_core_post_featured_image( |
| 60 | + array( |
| 61 | + 'height' => 200, |
| 62 | + 'sizeSlug' => 'post-thumbnail', |
| 63 | + 'width' => 200, |
| 64 | + ), |
| 65 | + '', |
| 66 | + $block |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + // Reset Query. |
| 71 | + wp_reset_query(); |
| 72 | + $args = $a->get_args(); |
| 73 | + $last_args = end( $args ); |
| 74 | + $this->assertSame( self::$attachments, $last_args[1] ); |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments