Completed
Push — update/node-version ( 857559...c25464 )
by Jeremy
313:10 queued 285:45
created

Test_Base_Admin_Menu::test_get_instance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
1
<?php
2
/**
3
 * Tests for Base_Admin_Menu class.
4
 *
5
 * @package automattic/jetpack
6
 */
7
8
require_jetpack_file( 'modules/masterbar/admin-menu/class-base-admin-menu.php' );
9
10
use \Automattic\Jetpack\Dashboard_Customizations\Base_Admin_Menu;
11
12
/**
13
 * Class Test_Base_Admin_Menu
14
 *
15
 * @coversDefaultClass Automattic\Jetpack\Dashboard_Customizations\Base_Admin_Menu
16
 */
17
class Test_Base_Admin_Menu extends WP_UnitTestCase {
18
19
	/**
20
	 * Admin menu instance.
21
	 *
22
	 * @var Base_Admin_Menu
23
	 */
24
	public static $admin_menu;
25
26
	/**
27
	 * Mock user ID.
28
	 *
29
	 * @var int
30
	 */
31
	private static $user_id = 0;
32
33
	/**
34
	 * Menu data fixture.
35
	 *
36
	 * @var array
37
	 */
38
	public static $menu_data;
39
40
	/**
41
	 * Submenu data fixture.
42
	 *
43
	 * @var array
44
	 */
45
	public static $submenu_data;
46
47
	/**
48
	 * Create shared fixtures.
49
	 *
50
	 * @param WP_UnitTest_Factory $factory Fixture factory.
51
	 */
52
	public static function wpSetUpBeforeClass( $factory ) {
53
		static::$user_id      = $factory->user->create( array( 'role' => 'administrator' ) );
0 ignored issues
show
Bug introduced by
Since $user_id is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $user_id to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
54
		static::$menu_data    = get_menu_fixture();
55
		static::$submenu_data = get_submenu_fixture();
56
	}
57
58
	/**
59
	 * Set up data.
60
	 */
61
	public function setUp() {
62
		parent::setUp();
63
64
		$admin_menu = $this->get_concrete_menu_admin();
65
66
		// Initialize in setUp so it registers hooks for every test.
67
		static::$admin_menu = $admin_menu::get_instance();
68
69
		wp_set_current_user( static::$user_id );
0 ignored issues
show
Bug introduced by
Since $user_id is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $user_id to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
70
	}
71
72
	/**
73
	 * Test get_instance.
74
	 *
75
	 * @covers ::get_instance
76
	 * @covers ::__construct
77
	 */
78 View Code Duplication
	public function test_get_instance() {
79
80
		$admin_menu = $this->get_concrete_menu_admin();
81
82
		$instance = $admin_menu::get_instance();
83
84
		$this->assertInstanceOf( Base_Admin_Menu::class, $instance );
85
		$this->assertSame( $instance, static::$admin_menu );
86
87
		$this->assertSame( 99999, has_action( 'admin_menu', array( $instance, 'reregister_menu_items' ) ) );
88
		$this->assertSame( 10, has_action( 'admin_enqueue_scripts', array( $instance, 'enqueue_scripts' ) ) );
89
	}
90
91
	/**
92
	 * Tests add_admin_menu_separator
93
	 *
94
	 * @covers ::add_admin_menu_separator
95
	 */
96
	public function test_add_admin_menu_separator() {
97
		global $menu;
98
99
		// Start with a clean slate.
100
		$temp_menu = $menu;
101
		$menu      = array();
102
103
		static::$admin_menu->add_admin_menu_separator( 15 );
104
		static::$admin_menu->add_admin_menu_separator( 10, 'manage_options' );
105
106
		$this->assertSame( array( 10, 15 ), array_keys( $menu ), 'Menu should be ordered by position parameter.' );
107
		$this->assertSame( 'manage_options', $menu[10][1] );
108
		$this->assertContains( 'separator-custom-', $menu[10][2] );
109
		$this->assertSame( 'read', $menu[15][1] );
110
		$this->assertContains( 'separator-custom-', $menu[15][2] );
111
112
		// Restore filtered $menu.
113
		$menu = $temp_menu;
114
	}
115
116
	/**
117
	 * Get an object of Base_Admin_Menu
118
	 *
119
	 * @return Base_Admin_Menu
120
	 */
121
	private function get_concrete_menu_admin() {
122
		return $this->getMockBuilder( Base_Admin_Menu::class )->disableOriginalConstructor()->getMockForAbstractClass();
123
	}
124
}
125