1

Since I need to have 100% test coverage for the client, I'm writing integration and unit tests in my plugin. And I have a method in a class that is hooked to admin_menu hook, which adds submenu.

Apart from the thing that it's absolutely stupid to test this imo (if it doesn't work I won't see the submenu anyways), I wrote a test like this

<?php
/**
 * Class Users submenu test
 *
 * @package My_Plugin\Tests
 */

use My_Plugin\Users\Menu;

/**
 * Class that tests users menu functionality.
 */
class Add_Submenu_Integration_Tests extends WP_UnitTestCase {
  /**
   * Initial set up for the test
   */
  public function setUp() {
    parent::setUp();

    \wp_set_current_user( self::factory()->user->create( [
        'role' => 'administrator',
    ] ) );

    \update_option( 'siteurl', 'http://example.com' );
  }

  /**
   * Tear down after test ends
   */
  public function tearDown() {
    parent::tearDown();
  }

  public function test_user_import_submenu() {
    $user_import = new Menu();
    $user_import->add_users_submenu();

    $this->assertTrue( function_exists( 'add_submenu_page' ) );
  }

}

The class Menu has the method

public function add_user_import_submenu() : void {
  add_submenu_page(
    'users.php',
    esc_html__( 'Test submenu', 'my-plugin' ),
    esc_html__( 'Test submenu', 'my-plugin' ),
    'manage_options',
    'user-test',
    [ $this, 'submenu_view' ]
  );
}

Now, my test passes and the coverage is green, but I feel like I'm cheating xD

I tried to test using global $submenu but $submenu was empty.

Is this ok to do?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.