Completed
Push — renovate/slack-web-api-5.x ( 42edfe...6ad45d )
by
unknown
184:59 queued 173:57
created

Test_Admin_Menu::test_add_tools_menu()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 45
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * Tests for Admin_Menu class.
4
 *
5
 * @package Jetpack
6
 */
7
8
use Automattic\Jetpack\Dashboard_Customizations\Admin_Menu;
9
use Automattic\Jetpack\Status;
10
11
require_jetpack_file( 'modules/masterbar/admin-menu/class-admin-menu.php' );
12
require_jetpack_file( 'tests/php/modules/masterbar/data/admin-menu.php' );
13
14
/**
15
 * Class Test_Admin_Menu
16
 *
17
 * @coversDefaultClass Automattic\Jetpack\Dashboard_Customizations\Admin_Menu
18
 */
19
class Test_Admin_Menu extends WP_UnitTestCase {
20
21
	/**
22
	 * Menu data fixture.
23
	 *
24
	 * @var array
25
	 */
26
	public static $menu_data;
27
28
	/**
29
	 * Submenu data fixture.
30
	 *
31
	 * @var array
32
	 */
33
	public static $submenu_data;
34
35
	/**
36
	 * Test domain.
37
	 *
38
	 * @var string
39
	 */
40
	public static $domain;
41
42
	/**
43
	 * Admin menu instance.
44
	 *
45
	 * @var Admin_Menu
46
	 */
47
	public static $admin_menu;
48
49
	/**
50
	 * Mock user ID.
51
	 *
52
	 * @var int
53
	 */
54
	private static $user_id = 0;
55
56
	/**
57
	 * Create shared fixtures.
58
	 *
59
	 * @param WP_UnitTest_Factory $factory Fixture factory.
60
	 */
61 View Code Duplication
	public static function wpSetUpBeforeClass( $factory ) {
62
		static::$domain  = ( new Status() )->get_site_suffix();
63
		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...
64
65
		static::$menu_data    = get_menu_fixture();
66
		static::$submenu_data = get_submenu_fixture();
67
	}
68
69
	/**
70
	 * Set up data.
71
	 */
72 View Code Duplication
	public function setUp() {
73
		parent::setUp();
74
		global $menu, $submenu;
75
76
		// Initialize in setUp so it registers hooks for every test.
77
		static::$admin_menu = Admin_Menu::get_instance();
78
79
		$menu    = static::$menu_data;
80
		$submenu = static::$submenu_data;
81
82
		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...
83
	}
84
85
	/**
86
	 * Test get_instance.
87
	 *
88
	 * @covers ::get_instance
89
	 * @covers ::__construct
90
	 */
91 View Code Duplication
	public function test_get_instance() {
92
		$instance = Admin_Menu::get_instance();
93
94
		$this->assertInstanceOf( Admin_Menu::class, $instance );
95
		$this->assertSame( $instance, static::$admin_menu );
96
97
		$this->assertSame( 99999, has_action( 'admin_menu', array( $instance, 'reregister_menu_items' ) ) );
98
		$this->assertSame( 10, has_action( 'admin_enqueue_scripts', array( $instance, 'enqueue_scripts' ) ) );
99
	}
100
101
	/**
102
	 * Tests add_admin_menu_separator
103
	 *
104
	 * @covers ::add_admin_menu_separator
105
	 */
106
	public function test_add_admin_menu_separator() {
107
		global $menu;
108
109
		// Start with a clean slate.
110
		$temp_menu = $menu;
111
		$menu      = array();
112
113
		static::$admin_menu->add_admin_menu_separator( 15 );
114
		static::$admin_menu->add_admin_menu_separator( 10, 'manage_options' );
115
116
		$this->assertSame( array( 10, 15 ), array_keys( $menu ), 'Menu should be ordered by position parameter.' );
117
		$this->assertSame( 'manage_options', $menu[10][1] );
118
		$this->assertSame( 'separator-custom-5', $menu[10][2] );
119
		$this->assertSame( 'read', $menu[15][1] );
120
		$this->assertSame( 'separator-custom-4', $menu[15][2] );
121
122
		// Restore filtered $menu.
123
		$menu = $temp_menu;
124
	}
125
126
	/**
127
	 * Test_Admin_Menu.
128
	 *
129
	 * @covers ::reregister_menu_items
130
	 */
131
	public function test_admin_menu_output() {
132
		global $menu, $submenu;
133
134
		static::$admin_menu->reregister_menu_items();
135
136
		$this->assertSame(
137
			array_keys( $menu ),
138
			array( 2, 3, '3.86682', 4, 5, 10, 15, 20, 25, 59, 60, 65, 70, 75, 80 ),
139
			'Admin menu should not have unexpected top menu items.'
140
		);
141
142
		$this->assertEquals( static::$submenu_data[''], $submenu[''], 'Submenu items without parent should stay the same.' );
143
	}
144
145
	/**
146
	 * Shim wpcomsh fallback site icon.
147
	 *
148
	 * @return string
149
	 */
150
	public function wpcomsh_site_icon_url() {
151
		return 'https://s0.wp.com/i/webclip.png';
152
	}
153
154
	/**
155
	 * Custom site icon.
156
	 *
157
	 * @return string
158
	 */
159
	public function custom_site_icon_url() {
160
		return 'https://s0.wp.com/i/jetpack.png';
161
	}
162
163
	/**
164
	 * Tests add_my_home_menu
165
	 *
166
	 * @covers ::add_my_home_menu
167
	 */
168
	public function test_add_my_home_menu() {
169
		global $menu, $submenu;
170
171
		static::$admin_menu->add_my_home_menu( static::$domain );
0 ignored issues
show
Documentation introduced by
static::$domain is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
172
173
		$slug = 'https://wordpress.com/home/' . static::$domain;
174
175
		$my_home_menu_item = array(
176
			'My Home',
177
			'read',
178
			$slug,
179
			'My Home',
180
			'menu-top toplevel_page_' . $slug,
181
			'toplevel_page_' . $slug,
182
			'dashicons-admin-home',
183
		);
184
		$this->assertSame( $menu[2], $my_home_menu_item );
185
186
		// Has My Home submenu item when there are other submenu items.
187
		$my_home_submenu_item = array(
188
			'My Home',
189
			'read',
190
			$slug,
191
			'My Home',
192
		);
193
		$this->assertContains( $my_home_submenu_item, $submenu[ $slug ] );
194
		// Reset data.
195
		$menu    = static::$menu_data;
196
		$submenu = static::$submenu_data;
197
198
		// Has no ny Home submenu when there are no other submenus.
199
		$submenu['index.php'] = array(
200
			0 => array( 'Home', 'read', 'index.php' ),
201
		);
202
203
		static::$admin_menu->add_my_home_menu( static::$domain );
0 ignored issues
show
Documentation introduced by
static::$domain is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
204
205
		$this->assertArrayNotHasKey( 'https://wordpress.com/home/' . static::$domain, $submenu );
206
	}
207
208
	/**
209
	 * Tests add_stats_menu
210
	 *
211
	 * @covers ::add_stats_menu
212
	 */
213
	public function test_add_stats_menu() {
214
		global $menu, $submenu;
215
216
		static::$admin_menu->add_stats_menu( static::$domain );
0 ignored issues
show
Unused Code introduced by
The call to Admin_Menu::add_stats_menu() has too many arguments starting with static::$domain.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
217
218
		$menu_title = __( 'Stats', 'jetpack' );
219
220
		if ( ! defined( 'TESTING_IN_JETPACK' ) || ! TESTING_IN_JETPACK ) {
221
			$menu_title .= sprintf(
222
				'<img class="sidebar-unified__sparkline" width="80" height="20" src="%1$s" alt="%2$s">',
223
				esc_url( home_url( 'wp-includes/charts/admin-bar-hours-scale-2x.php?masterbar=1&s=' . get_current_blog_id() ) ),
224
				esc_attr__( 'Hourly views', 'jetpack' )
225
			);
226
		}
227
		$stats_menu_item = array(
228
			$menu_title,
229
			'edit_posts',
230
			'https://wordpress.com/stats/day/' . static::$domain,
231
			'Stats',
232
			'menu-top toplevel_page_https://wordpress.com/stats/day/' . static::$domain,
233
			'toplevel_page_https://wordpress.com/stats/day/' . static::$domain,
234
			'dashicons-chart-bar',
235
		);
236
237
		$this->assertSame( $menu['3.86682'], $stats_menu_item );
238
		$this->assertArrayNotHasKey( 'https://wordpress.com/stats/day/' . static::$domain, $submenu );
239
	}
240
241
	/**
242
	 * Tests add_upgrades_menu
243
	 *
244
	 * @covers ::add_upgrades_menu
245
	 */
246
	public function test_add_wpcom_upgrades_menu() {
247
		global $menu, $submenu;
248
249
		static::$admin_menu->add_upgrades_menu();
250
251
		$slug = 'https://wordpress.com/plans/' . static::$domain;
252
253
		$upgrades_menu_item = array(
254
			'Upgrades',
255
			'manage_options',
256
			$slug,
257
			'Upgrades',
258
			'menu-top toplevel_page_https://wordpress.com/plans/' . static::$domain,
259
			'toplevel_page_https://wordpress.com/plans/' . static::$domain,
260
			'dashicons-cart',
261
		);
262
		$this->assertSame( $menu['4.80608'], $upgrades_menu_item );
263
264
		$plans_submenu_item = array(
265
			'Plans',
266
			'manage_options',
267
			$slug,
268
			'Plans',
269
		);
270
		$this->assertContains( $plans_submenu_item, $submenu[ $slug ] );
271
272
		$purchases_submenu_item = array(
273
			'Purchases',
274
			'manage_options',
275
			'https://wordpress.com/purchases/subscriptions/' . static::$domain,
276
			'Purchases',
277
		);
278
		$this->assertContains( $purchases_submenu_item, $submenu[ $slug ] );
279
	}
280
281
	/**
282
	 * Tests add_jetpack_upgrades_menu
283
	 *
284
	 * @covers ::add_jetpack_upgrades_menu
285
	 */
286 View Code Duplication
	public function test_add_jetpack_upgrades_menu() {
287
		global $menu, $submenu;
288
289
		static::$admin_menu->add_upgrades_menu( static::$domain );
0 ignored issues
show
Unused Code introduced by
The call to Admin_Menu::add_upgrades_menu() has too many arguments starting with static::$domain.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
290
291
		$slug = 'https://wordpress.com/plans/' . static::$domain;
292
293
		$upgrades_menu_item = array(
294
			'Upgrades',
295
			'manage_options',
296
			$slug,
297
			'Upgrades',
298
			'menu-top toplevel_page_https://wordpress.com/plans/' . static::$domain,
299
			'toplevel_page_https://wordpress.com/plans/' . static::$domain,
300
			'dashicons-cart',
301
		);
302
		$this->assertSame( $menu['4.80608'], $upgrades_menu_item );
303
		$this->assertArrayNotHasKey( 'https://wordpress.com/domains/manage/' . static::$domain, $submenu );
304
	}
305
306
	/**
307
	 * Tests add_posts_menu
308
	 *
309
	 * @covers ::add_posts_menu
310
	 */
311 View Code Duplication
	public function test_add_posts_menu() {
312
		global $menu, $submenu;
313
314
		static::$admin_menu->add_posts_menu( static::$domain );
0 ignored issues
show
Documentation introduced by
static::$domain is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
315
316
		$posts_menu_item = array(
317
			'Posts',
318
			'edit_posts',
319
			'https://wordpress.com/posts/' . static::$domain,
320
			'Posts',
321
			'menu-top toplevel_page_https://wordpress.com/posts/' . static::$domain,
322
			'toplevel_page_https://wordpress.com/posts/' . static::$domain,
323
			'dashicons-admin-post',
324
		);
325
326
		$this->assertSame( $menu[5], $posts_menu_item );
327
		$this->assertArrayNotHasKey( 'edit.php', $submenu );
328
	}
329
330
	/**
331
	 * Tests add_media_menu
332
	 *
333
	 * @covers ::add_media_menu
334
	 */
335
	public function test_add_media_menu() {
336
		global $menu, $submenu;
337
338
		static::$admin_menu->add_media_menu( static::$domain );
0 ignored issues
show
Documentation introduced by
static::$domain is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
339
340
		$slug = 'https://wordpress.com/media/' . static::$domain;
341
342
		$media_menu_item = array(
343
			'Media',
344
			'upload_files',
345
			$slug,
346
			'Media',
347
			'menu-top toplevel_page_' . $slug,
348
			'toplevel_page_' . $slug,
349
			'dashicons-admin-media',
350
		);
351
352
		$this->assertSame( $menu[10], $media_menu_item );
353
		$this->assertArrayNotHasKey( $slug, $submenu );
354
355
		$library_submenu_item = array(
356
			'Library',
357
			'upload_files',
358
			'upload.php',
359
		);
360
		$this->assertNotContains( $library_submenu_item, $submenu['upload.php'] );
361
362
		$add_new_submenu_item = array(
363
			'Add New',
364
			'upload_files',
365
			'media-new.php',
366
		);
367
		$this->assertNotContains( $add_new_submenu_item, $submenu['upload.php'] );
368
	}
369
370
	/**
371
	 * Tests add_page_menu
372
	 *
373
	 * @covers ::add_page_menu
374
	 */
375 View Code Duplication
	public function test_add_page_menu() {
376
		global $menu, $submenu;
377
378
		static::$admin_menu->add_page_menu( static::$domain );
0 ignored issues
show
Documentation introduced by
static::$domain is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
379
380
		$posts_menu_item = array(
381
			'Pages',
382
			'edit_pages',
383
			'https://wordpress.com/pages/' . static::$domain,
384
			'Pages',
385
			'menu-top toplevel_page_https://wordpress.com/pages/' . static::$domain,
386
			'toplevel_page_https://wordpress.com/pages/' . static::$domain,
387
			'dashicons-admin-page',
388
		);
389
390
		$this->assertSame( $menu[20], $posts_menu_item );
391
		$this->assertEmpty( $submenu['edit.php?post_type=page'] );
392
	}
393
394
	/**
395
	 * Tests add_custom_post_type_menu
396
	 *
397
	 * @covers ::add_custom_post_type_menu
398
	 */
399
	public function test_add_custom_post_type_menu() {
400
		global $menu, $submenu;
401
402
		// Don't show post types that don't want to be shown.
403
		$revision = get_post_type_object( 'revision' );
404
		static::$admin_menu->add_custom_post_type_menu( $revision, true );
405
406
		$last_item = array_pop( $menu );
407
		$this->assertNotSame( 'https://wordpress.com/types/revision/' . static::$domain, $last_item[2] );
408
409
		register_post_type(
410
			'custom_test_type',
411
			array(
412
				'label'         => 'Custom Test Types',
413
				'show_ui'       => true,
414
				'menu_position' => 2020,
415
			)
416
		);
417
		static::$admin_menu->add_custom_post_type_menu( 'custom_test_type', true );
418
419
		// Clean up.
420
		unregister_post_type( 'custom_test_type' );
421
422
		$slug = 'https://wordpress.com/types/custom_test_type/' . static::$domain;
423
424
		$custom_menu_item = array(
425
			'Custom Test Types',
426
			'edit_posts',
427
			$slug,
428
			'Custom Test Types',
429
			'menu-top toplevel_page_' . $slug,
430
			'toplevel_page_' . $slug,
431
			'dashicons-admin-post',
432
		);
433
434
		$this->assertSame( $menu[2020], $custom_menu_item );
435
436
		$custom_submenu_item = array(
437
			'Custom Test Types',
438
			'edit_posts',
439
			'https://wordpress.com/types/custom_test_type/' . static::$domain,
440
			'Custom Test Types',
441
		);
442
		$this->assertContains( $custom_submenu_item, $submenu[ $slug ] );
443
444
		$add_new_submenu_item = array(
445
			'Add New',
446
			'edit_posts',
447
			'https://wordpress.com/edit/custom_test_type/' . static::$domain,
448
			'Add New',
449
		);
450
		$this->assertContains( $add_new_submenu_item, $submenu[ $slug ] );
451
	}
452
453
	/**
454
	 * Tests add_comments_menu
455
	 *
456
	 * @covers ::add_comments_menu
457
	 */
458
	public function test_add_comments_menu() {
459
		global $menu, $submenu;
460
461
		// Only users that can edit posts get to see the comments menu.
462
		wp_set_current_user( $this->factory->user->create( array( 'role' => 'subscriber' ) ) );
463
		$menu = array();
464
		static::$admin_menu->add_comments_menu( static::$domain );
0 ignored issues
show
Documentation introduced by
static::$domain is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
465
		$this->assertEmpty( $menu );
466
467
		// Reset.
468
		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...
469
		$menu = static::$menu_data;
470
471
		static::$admin_menu->add_comments_menu( static::$domain );
0 ignored issues
show
Documentation introduced by
static::$domain is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
472
473
		$comments_menu_item = array(
474
			'Comments <span class="awaiting-mod count-0"><span class="pending-count" aria-hidden="true">0</span><span class="comments-in-moderation-text screen-reader-text">0 Comments in moderation</span></span>',
475
			'edit_posts',
476
			'https://wordpress.com/comments/all/' . static::$domain,
477
			'Comments',
478
			'menu-top toplevel_page_https://wordpress.com/comments/all/' . static::$domain,
479
			'toplevel_page_https://wordpress.com/comments/all/' . static::$domain,
480
			'dashicons-admin-comments',
481
		);
482
483
		$this->assertSame( $menu[25], $comments_menu_item );
484
		$this->assertEmpty( $submenu['edit-comments.php'] );
485
	}
486
487
	/**
488
	 * Tests add_appearance_menu
489
	 *
490
	 * @covers ::add_appearance_menu
491
	 */
492
	public function test_add_appearance_menu() {
493
		global $menu, $submenu;
494
495
		static::$admin_menu->add_appearance_menu( static::$domain );
0 ignored issues
show
Documentation introduced by
static::$domain is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
496
497
		$slug = 'https://wordpress.com/themes/' . static::$domain;
498
499
		$appearance_menu_item = array(
500
			'Appearance',
501
			'switch_themes',
502
			$slug,
503
			'Appearance',
504
			'menu-top toplevel_page_' . $slug,
505
			'menu-appearance',
506
			'dashicons-admin-appearance',
507
		);
508
509
		$this->assertSame( $menu[60], $appearance_menu_item );
510
		$this->assertArrayNotHasKey( 'themes.php', $submenu );
511
512
		$themes_submenu_item = array(
513
			'Themes',
514
			'switch_themes',
515
			'https://wordpress.com/themes/' . static::$domain,
516
			'Themes',
517
		);
518
		$this->assertContains( $themes_submenu_item, $submenu[ $slug ] );
519
520
		$customize_submenu_item = array(
521
			'Customize',
522
			'customize',
523
			'https://wordpress.com/customize/' . static::$domain,
524
			'Customize',
525
		);
526
		$this->assertContains( $customize_submenu_item, $submenu[ $slug ] );
527
528
		$widgets_submenu_item = array(
529
			'Widgets',
530
			'customize',
531
			'https://wordpress.com/customize/' . static::$domain . '?autofocus%5Bpanel%5D=widgets',
532
			'Widgets',
533
		);
534
		$this->assertContains( $widgets_submenu_item, $submenu[ $slug ] );
535
536
		$menus_submenu_item = array(
537
			'Menus',
538
			'customize',
539
			'https://wordpress.com/customize/' . static::$domain . '?autofocus%5Bpanel%5D=nav_menus',
540
			'Menus',
541
		);
542
		$this->assertContains( $menus_submenu_item, $submenu[ $slug ] );
543
	}
544
545
	/**
546
	 * Tests add_users_menu
547
	 *
548
	 * @covers ::add_users_menu
549
	 */
550 View Code Duplication
	public function test_add_users_menu() {
551
		global $menu, $submenu;
552
553
		// Current user can't list users.
554
		wp_set_current_user( $this->factory->user->create( array( 'role' => 'editor' ) ) );
555
		$menu = array();
556
557
		static::$admin_menu->add_users_menu( true );
558
559
		$this->assertEmpty( $menu );
560
561
		// Reset.
562
		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...
563
		$menu = static::$menu_data;
564
565
		static::$admin_menu->add_users_menu( static::$domain );
0 ignored issues
show
Documentation introduced by
static::$domain is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
566
567
		$slug = 'https://wordpress.com/people/team/' . static::$domain;
568
569
		$users_menu_item = array(
570
			'Users',
571
			'list_users',
572
			$slug,
573
			'Users',
574
			'menu-top toplevel_page_' . $slug,
575
			'toplevel_page_' . $slug,
576
			'dashicons-admin-users',
577
		);
578
		$this->assertSame( $menu[70], $users_menu_item );
579
		$this->assertEmpty( $submenu['users.php'] );
580
581
		$all_people_submenu_item = array(
582
			'All People',
583
			'list_users',
584
			$slug,
585
			'All People',
586
		);
587
		$this->assertContains( $all_people_submenu_item, $submenu[ $slug ] );
588
589
		$add_new_submenu_item = array(
590
			'Add New',
591
			'promote_users',
592
			'https://wordpress.com/people/new/' . static::$domain,
593
			'Add New',
594
		);
595
		$this->assertContains( $add_new_submenu_item, $submenu[ $slug ] );
596
597
		$profile_submenu_item = array(
598
			'My Profile',
599
			'read',
600
			'https://wordpress.com/me',
601
			'My Profile',
602
		);
603
		$this->assertContains( $profile_submenu_item, $submenu[ $slug ] );
604
605
		$account_submenu_item = array(
606
			'Account Settings',
607
			'read',
608
			'https://wordpress.com/me/account',
609
			'Account Settings',
610
		);
611
		$this->assertContains( $account_submenu_item, $submenu[ $slug ] );
612
	}
613
614
	/**
615
	 * Tests add_tools_menu
616
	 *
617
	 * @covers ::add_tools_menu
618
	 */
619
	public function test_add_tools_menu() {
620
		global $menu, $submenu;
621
622
		$slug = 'https://wordpress.com/marketing/tools/' . static::$domain;
623
		static::$admin_menu->add_tools_menu( static::$domain );
0 ignored issues
show
Documentation introduced by
static::$domain is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
624
625
		$tools_menu_item = array(
626
			'Tools',
627
			'manage_options',
628
			$slug,
629
			'Tools',
630
			'menu-top toplevel_page_' . $slug,
631
			'toplevel_page_' . $slug,
632
			'dashicons-admin-tools',
633
		);
634
635
		$this->assertSame( $menu[75], $tools_menu_item );
636
		$this->assertArrayNotHasKey( 'tools.php', $submenu );
637
638
		// Contains the following menu items.
639
640
		$import_submenu_item = array(
641
			'Import',
642
			'import',
643
			'https://wordpress.com/import/' . static::$domain,
644
			'Import',
645
		);
646
		$this->assertContains( $import_submenu_item, $submenu[ $slug ] );
647
648
		// NOT contains the following menu items.
649
650
		$tools_submenu_item = array(
651
			'Available Tools',
652
			'edit_posts',
653
			'tools.php',
654
		);
655
		$this->assertNotContains( $tools_submenu_item, $submenu[ $slug ] );
656
657
		$import_submenu_item = array(
658
			'Import',
659
			'import',
660
			'import.php',
661
		);
662
		$this->assertNotContains( $import_submenu_item, $submenu[ $slug ] );
663
	}
664
665
	/**
666
	 * Tests add_options_menu
667
	 *
668
	 * @covers ::add_options_menu
669
	 */
670
	public function test_add_options_menu() {
671
		global $submenu;
672
673
		$slug = 'https://wordpress.com/settings/general/' . static::$domain;
674
		static::$admin_menu->add_options_menu( static::$domain );
0 ignored issues
show
Documentation introduced by
static::$domain is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
675
676
		$this->assertNotContains( 'options-discussion.php', $submenu[ $slug ] );
677
		$this->assertNotContains( 'options-writing.php', $submenu[ $slug ] );
678
679
		$general_submenu_item = array(
680
			'General',
681
			'manage_options',
682
			$slug,
683
			'General',
684
		);
685
		$this->assertContains( $general_submenu_item, $submenu[ $slug ] );
686
	}
687
688
	/**
689
	 * Tests migrate_submenus
690
	 *
691
	 * @covers ::migrate_submenus
692
	 */
693
	public function test_migrate_submenus() {
694
		global $submenu;
695
696
		$new_slug = 'made-up-slug';
697
698
		// Start with a clean slate.
699
		$temp_submenu = $submenu;
700
		$submenu      = static::$submenu_data;
701
702
		// New slug doesn't exist yet.
703
		static::$admin_menu->migrate_submenus( 'edit.php', $new_slug );
704
		$this->assertArrayNotHasKey( 'edit.php', $submenu );
705
		$this->assertSame( static::$submenu_data['edit.php'], $submenu[ $new_slug ] );
706
707
		// New slug exists.
708
		static::$admin_menu->migrate_submenus( 'upload.php', $new_slug );
709
		$this->assertArrayNotHasKey( 'upload.php', $submenu );
710
		$expected = array_replace( static::$submenu_data['edit.php'], static::$submenu_data['upload.php'] );
711
		$this->assertSame( $expected, $submenu[ $new_slug ] );
712
713
		// Old slug doesn't exist.
714
		$this->assertArrayNotHasKey( 'unkown', $submenu );
715
		$pre_migration = $submenu;
716
		static::$admin_menu->migrate_submenus( 'unkown', $new_slug );
717
		$this->assertSame( $pre_migration, $submenu );
718
719
		// Slugs are the same.
720
		$this->assertArrayHasKey( 'index.php', $submenu );
721
		$pre_migration = $submenu;
722
		static::$admin_menu->migrate_submenus( 'index.php', 'index.php' );
723
		$this->assertSame( $pre_migration, $submenu );
724
725
		// Restore filtered $submenu.
726
		$submenu = $temp_submenu;
727
	}
728
}
729