Completed
Push — renovate/wordpress-monorepo ( 8cdadc...6203b6 )
by
unknown
552:54 queued 542:45
created

Test_Domain_Only_Admin_Menu::wpSetUpBeforeClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Tests for Domain_Only_Admin_Menu class.
4
 *
5
 * @package automattic/jetpack
6
 */
7
8
use Automattic\Jetpack\Dashboard_Customizations\Domain_Only_Admin_Menu;
9
use Automattic\Jetpack\Status;
10
11
require_jetpack_file( 'modules/masterbar/admin-menu/class-domain-only-admin-menu.php' );
12
require_jetpack_file( 'tests/php/modules/masterbar/data/admin-menu.php' );
13
14
/**
15
 * Class Test_Domain_Only_Admin_Menu.
16
 *
17
 * @coversDefaultClass Automattic\Jetpack\Dashboard_Customizations\Domain_Only_Admin_Menu
18
 */
19
class Test_Domain_Only_Admin_Menu extends WP_UnitTestCase {
20
21
	/**
22
	 * Menu data fixture.
23
	 *
24
	 * @var array
25
	 */
26
	public static $menu_data;
27
28
	/**
29
	 * Test domain.
30
	 *
31
	 * @var string
32
	 */
33
	public static $domain;
34
35
	/**
36
	 * Admin menu instance.
37
	 *
38
	 * @var Domain_Only_Admin_Menu
39
	 */
40
	public static $admin_menu;
41
42
	/**
43
	 * Mock user ID.
44
	 *
45
	 * @var int
46
	 */
47
	private static $user_id = 0;
48
49
	/**
50
	 * Create shared fixtures.
51
	 *
52
	 * @param WP_UnitTest_Factory $factory Fixture factory.
53
	 */
54
	public static function wpSetUpBeforeClass( $factory ) {
55
		static::$domain    = ( new Status() )->get_site_suffix();
56
		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...
57
		static::$menu_data = get_menu_fixture();
58
	}
59
60
	/**
61
	 * Set up data.
62
	 */
63 View Code Duplication
	public function setUp() {
64
		parent::setUp();
65
		global $menu;
66
67
		// Initialize in setUp so it registers hooks for every test.
68
		static::$admin_menu = Domain_Only_Admin_Menu::get_instance();
0 ignored issues
show
Documentation Bug introduced by
It seems like \Automattic\Jetpack\Dash...in_Menu::get_instance() of type object<Automattic\Jetpac...tomizations\Admin_Menu> is incompatible with the declared type object<Automattic\Jetpac...Domain_Only_Admin_Menu> of property $admin_menu.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
69
70
		$menu = static::$menu_data;
71
72
		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...
73
	}
74
75
	/**
76
	 * Tests reregister_menu_items.
77
	 *
78
	 * @covers ::reregister_menu_items
79
	 */
80
	public function test_reregister_menu_items() {
81
		global $menu;
82
83
		static::$admin_menu->reregister_menu_items();
84
85
		$this->assertCount( 1, $menu );
86
		$this->assertEquals( 'https://wordpress.com/domains/manage/' . static::$domain, $menu[0][2] );
87
	}
88
}
89