I am writing a few unit tests for a plugin that I have developed. However I seem to be hung up on testing one (seemingly) simple piece of code.
In the plugin I’ve written I have registered an admin menu page:
add_menu_page(
esc_html__( 'Places', 'wp-places' ),
esc_html__( 'Places', 'wp-places' ),
'manage_options',
'wp-places',
[ $this, 'places_page' ],
);
But I can’t seem to come up with a unit test to ensure this menu item exists. I’ve seen a few uses of global $menu
, but when I check the global it isn’t yet set.
Here is the very basics of what I have.
<?php
class AdminTests extends WP_UnitTestCase {
public function setUp() {
parent::setUp();
wp_set_current_user( self::factory()->user->create( [
'role' => 'administrator',
] ) );
$this->go_to( admin_url() );
}
public function test_admin_menu() {
global $menu;
print_r( $menu ); // not set
}
}