How to create a test that calls is_front_page in phpunit?

I’m using WP_UnitTestCase but in my code I have this condition. is_front_page which I’m not sure how to simulate that in PHPUnit

This is the piece of code that I have

elseif ( is_front_page() ) {
    // logic
}

And this is how I go to a page

$this->go_to( $this->post_obj->get_permalink() );

How do I tell phpunit that this is the homepage?

1
1

The is_front_page() function calls wp_query::is_front_page(). If you scroll down to look at the source, you will see the code you’re looking to trigger:

elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) )
        return true;

To meet that condition, you’ll just need to do this in the code to set up for the test:

update_option( 'show_on_front', 'page' );
update_option( 'page_on_front', $post_id );

Leave a Comment