Changeset 55847 for trunk/tests/phpunit/tests/media.php
- Timestamp:
- 05/22/2023 07:11:36 PM (2 weeks ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/media.php
r55825 r55847 3560 3560 */ 3561 3561 public function test_wp_get_loading_attr_default( $context ) { 3562 global $wp_query, $wp_the_query;3563 3564 3562 // Return 'lazy' by default. 3565 3563 $this->assertSame( 'lazy', wp_get_loading_attr_default( 'test' ) ); … … 3569 3567 $this->assertSame( 'lazy', wp_get_loading_attr_default( $context ) ); 3570 3568 3571 $ wp_query = new WP_Query( array( 'post__in' => array( self::$post_ids['publish'] ) ));3569 $query = $this->get_new_wp_query_for_published_post(); 3572 3570 $this->reset_content_media_count(); 3573 3571 $this->reset_omit_loading_attr_filter(); … … 3580 3578 3581 3579 // Set as main query. 3582 $ wp_the_query = $wp_query;3580 $this->set_main_query( $query ); 3583 3581 3584 3582 // For contexts other than for the main content, still return 'lazy' even in the loop … … 3614 3612 */ 3615 3613 public function test_wp_omit_loading_attr_threshold_filter() { 3616 global $wp_query, $wp_the_query; 3617 3618 $wp_query = new WP_Query( array( 'post__in' => array( self::$post_ids['publish'] ) ) ); 3619 $wp_the_query = $wp_query; 3614 $query = $this->get_new_wp_query_for_published_post(); 3615 $this->set_main_query( $query ); 3620 3616 $this->reset_content_media_count(); 3621 3617 $this->reset_omit_loading_attr_filter(); … … 3641 3637 */ 3642 3638 public function test_wp_filter_content_tags_with_wp_get_loading_attr_default() { 3643 global $wp_query, $wp_the_query;3644 3645 3639 $img1 = get_image_tag( self::$large_id, '', '', '', 'large' ); 3646 3640 $iframe1 = '<iframe src="https://www.example.com" width="640" height="360"></iframe>'; … … 3660 3654 $content_expected = wp_img_tag_add_decoding_attr( $content_expected, 'the_content' ); 3661 3655 3662 $ wp_query = new WP_Query( array( 'post__in' => array( self::$post_ids['publish'] ) ));3663 $ wp_the_query = $wp_query;3656 $query = $this->get_new_wp_query_for_published_post(); 3657 $this->set_main_query( $query ); 3664 3658 $this->reset_content_media_count(); 3665 3659 $this->reset_omit_loading_attr_filter(); … … 3697 3691 $omit_threshold = wp_omit_loading_attr_threshold( true ); 3698 3692 $this->assertSame( 1, $omit_threshold ); 3693 } 3694 3695 /** 3696 * Tests that wp_get_loading_attr_default() returns the expected loading attribute value before loop but after get_header if not main query. 3697 * 3698 * @ticket 58211 3699 * 3700 * @covers ::wp_get_loading_attr_default 3701 * 3702 * @dataProvider data_wp_get_loading_attr_default_before_and_no_loop 3703 * 3704 * @param string $context Context for the element for which the `loading` attribute value is requested. 3705 */ 3706 public function test_wp_get_loading_attr_default_before_loop_if_not_main_query( $context ) { 3707 global $wp_query; 3708 3709 $wp_query = $this->get_new_wp_query_for_published_post(); 3710 $this->reset_content_media_count(); 3711 $this->reset_omit_loading_attr_filter(); 3712 3713 do_action( 'get_header' ); 3714 3715 // Lazy if not main query. 3716 $this->assertSame( 'lazy', wp_get_loading_attr_default( $context ) ); 3717 } 3718 3719 /** 3720 * Tests that wp_get_loading_attr_default() returns the expected loading attribute value before loop but after get_header in main query but header was not called. 3721 * 3722 * @ticket 58211 3723 * 3724 * @covers ::wp_get_loading_attr_default 3725 * 3726 * @dataProvider data_wp_get_loading_attr_default_before_and_no_loop 3727 * 3728 * @param string $context Context for the element for which the `loading` attribute value is requested. 3729 */ 3730 public function test_wp_get_loading_attr_default_before_loop_in_main_query_but_header_not_called( $context ) { 3731 global $wp_query; 3732 3733 $wp_query = $this->get_new_wp_query_for_published_post(); 3734 $this->set_main_query( $wp_query ); 3735 $this->reset_content_media_count(); 3736 $this->reset_omit_loading_attr_filter(); 3737 3738 // Lazy if header not called. 3739 $this->assertSame( 'lazy', wp_get_loading_attr_default( $context ) ); 3740 } 3741 3742 /** 3743 * Tests that wp_get_loading_attr_default() returns the expected loading attribute value before loop but after get_header for main query. 3744 * 3745 * @ticket 58211 3746 * 3747 * @covers ::wp_get_loading_attr_default 3748 * 3749 * @dataProvider data_wp_get_loading_attr_default_before_and_no_loop 3750 * 3751 * @param string $context Context for the element for which the `loading` attribute value is requested. 3752 */ 3753 public function test_wp_get_loading_attr_default_before_loop_if_main_query( $context ) { 3754 global $wp_query; 3755 3756 $wp_query = $this->get_new_wp_query_for_published_post(); 3757 $this->set_main_query( $wp_query ); 3758 $this->reset_content_media_count(); 3759 $this->reset_omit_loading_attr_filter(); 3760 3761 do_action( 'get_header' ); 3762 $this->assertFalse( wp_get_loading_attr_default( $context ) ); 3763 } 3764 3765 /** 3766 * Tests that wp_get_loading_attr_default() returns the expected loading attribute value after get_header and after loop. 3767 * 3768 * @ticket 58211 3769 * 3770 * @covers ::wp_get_loading_attr_default 3771 * 3772 * @dataProvider data_wp_get_loading_attr_default_before_and_no_loop 3773 * 3774 * @param string $context Context for the element for which the `loading` attribute value is requested. 3775 */ 3776 public function test_wp_get_loading_attr_default_after_loop( $context ) { 3777 global $wp_query; 3778 3779 $wp_query = $this->get_new_wp_query_for_published_post(); 3780 $this->set_main_query( $wp_query ); 3781 $this->reset_content_media_count(); 3782 $this->reset_omit_loading_attr_filter(); 3783 3784 do_action( 'get_header' ); 3785 3786 while ( have_posts() ) { 3787 the_post(); 3788 } 3789 $this->assertSame( 'lazy', wp_get_loading_attr_default( $context ) ); 3790 } 3791 3792 /** 3793 * Tests that wp_get_loading_attr_default() returns the expected loading attribute if no loop. 3794 * 3795 * @ticket 58211 3796 * 3797 * @covers ::wp_get_loading_attr_default 3798 * 3799 * @dataProvider data_wp_get_loading_attr_default_before_and_no_loop 3800 * 3801 * @param string $context Context for the element for which the `loading` attribute value is requested. 3802 */ 3803 public function test_wp_get_loading_attr_default_no_loop( $context ) { 3804 global $wp_query; 3805 3806 $wp_query = $this->get_new_wp_query_for_published_post(); 3807 $this->set_main_query( $wp_query ); 3808 $this->reset_content_media_count(); 3809 $this->reset_omit_loading_attr_filter(); 3810 3811 // Ensure header and footer is called. 3812 do_action( 'get_header' ); 3813 do_action( 'get_footer' ); 3814 3815 // Load lazy if the there is no loop and footer was called. 3816 $this->assertSame( 'lazy', wp_get_loading_attr_default( $context ) ); 3817 } 3818 3819 /** 3820 * Data provider. 3821 * 3822 * @return array[] 3823 */ 3824 public function data_wp_get_loading_attr_default_before_and_no_loop() { 3825 return array( 3826 array( 'wp_get_attachment_image' ), 3827 array( 'the_post_thumbnail' ), 3828 ); 3699 3829 } 3700 3830 … … 4167 4297 ); 4168 4298 } 4299 4300 /** 4301 * Returns a new WP_Query. 4302 * 4303 * @global WP_Query $wp_query WordPress Query object. 4304 * 4305 * @return WP_Query a new query. 4306 */ 4307 public function get_new_wp_query_for_published_post() { 4308 global $wp_query; 4309 4310 // New query to $wp_query. update global for the loop. 4311 $wp_query = new WP_Query( array( 'post__in' => array( self::$post_ids['publish'] ) ) ); 4312 4313 return $wp_query; 4314 } 4315 4316 /** 4317 * Sets a query as main query. 4318 * 4319 * @global WP_Query $wp_the_query WordPress Query object. 4320 * 4321 * @param WP_Query $query query to be set as main query. 4322 */ 4323 public function set_main_query( $query ) { 4324 global $wp_the_query; 4325 $wp_the_query = $query; 4326 } 4169 4327 } 4170 4328
Note: See TracChangeset
for help on using the changeset viewer.