From da1837c803eeaa3aa126f8c65b8bf4ef48e61293 Mon Sep 17 00:00:00 2001 From: Colin Stewart <79332690+costdev@users.noreply.github.com> Date: Wed, 4 Dec 2024 19:02:18 +0000 Subject: [PATCH] Add tests for `Admin_Settings::register_admin_menu()`. (#236) --- .../AdminSettings_RegisterAdminMenuTest.php | 126 ++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 tests/phpunit/tests/AdminSettings/AdminSettings_RegisterAdminMenuTest.php diff --git a/tests/phpunit/tests/AdminSettings/AdminSettings_RegisterAdminMenuTest.php b/tests/phpunit/tests/AdminSettings/AdminSettings_RegisterAdminMenuTest.php new file mode 100644 index 0000000..8be7948 --- /dev/null +++ b/tests/phpunit/tests/AdminSettings/AdminSettings_RegisterAdminMenuTest.php @@ -0,0 +1,126 @@ +user->create( [ 'role' => 'administrator' ] ); + } + + /** + * Test that menu is not registered when AP_REMOVE_UI is enabled. + */ + public function test_should_not_register_menu_when_ap_remove_ui_is_enabled() { + global $submenu; + $original_submenu = $submenu; + + define( 'AP_REMOVE_UI', true ); + wp_set_current_user( self::$admin_id ); + + $admin_settings = new \AspireUpdate\Admin_Settings(); + $admin_settings->register_admin_menu(); + + $this->assertSame( $original_submenu, $submenu ); + } + + /** + * Test that menu is not registered when the user lacks appropriate permissions. + */ + public function test_should_not_register_menu_when_user_lacks_appropriate_permissions() { + global $submenu; + $original_submenu = $submenu; + + define( 'AP_REMOVE_UI', false ); + + $admin_settings = new \AspireUpdate\Admin_Settings(); + $admin_settings->register_admin_menu(); + + $this->assertSame( $original_submenu, $submenu ); + } + + /** + * Test that menu is registered when AP_REMOVE_UI is disabled. + */ + public function test_should_register_menu_when_ap_remove_ui_is_disabled() { + global $submenu; + + define( 'AP_REMOVE_UI', false ); + wp_set_current_user( self::$admin_id ); + grant_super_admin( self::$admin_id ); + + $admin_settings = new \AspireUpdate\Admin_Settings(); + $admin_settings->register_admin_menu(); + + $this->assertIsArray( + $submenu, + 'There are no submenus.' + ); + + $this->assertArrayHasKey( + 'index.php', + $submenu, + 'There is no dashboard section.' + ); + + $this->assertIsArray( + $submenu['index.php'], + 'There are no submenus for the dashboard.' + ); + + $last_menu_item = end( $submenu['index.php'] ); + $this->assertSame( + 'aspireupdate-settings', + $last_menu_item[2], + 'The menu was not registered.' + ); + } + + /** + * Test that AP_REMOVE_UI is defined when not already defined. + */ + public function test_should_define_ap_remove_ui_when_not_already_defined() { + $this->assertFalse( + defined( 'AP_REMOVE_UI' ), + 'AP_REMOVE_UI is already defined.' + ); + + $admin_settings = new AspireUpdate\Admin_Settings(); + $admin_settings->register_admin_menu(); + + $this->assertTrue( + defined( 'AP_REMOVE_UI' ), + 'AP_REMOVE_UI is not defined.' + ); + + $this->assertFalse( + AP_REMOVE_UI, + 'AP_REMOVE_UI is not defined as (bool) false.' + ); + } +}