Skip to content

Commit c1331ef

Browse files
committed
Add test coverage for admin bar link changes
1 parent b5b50d5 commit c1331ef

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed

projects/packages/jetpack-mu-wpcom/tests/lib/functions-wordpress.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,17 @@ function wpcom_rest_api_v2_load_plugin( $class_name ) {
5151
}
5252
}
5353
}
54+
55+
if ( ! function_exists( 'get_user_attribute' ) ) {
56+
/**
57+
* A drop-in for a WordPress.com function.
58+
* Tests can mock return value using the global $test_user_attributes variable.
59+
*/
60+
function get_user_attribute( $user_id, $attribute ) {
61+
global $test_user_attributes;
62+
if ( ! isset( $test_user_attributes ) || ! is_array( $test_user_attributes ) ) {
63+
return null;
64+
}
65+
return $test_user_attributes[ $attribute ] ?? null;
66+
}
67+
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<?php
2+
/**
3+
* Test class for admin bar changes.
4+
*
5+
* @package automattic/jetpack-mu-wpcom
6+
*/
7+
8+
use Automattic\Jetpack\Jetpack_Mu_Wpcom;
9+
10+
require_once Jetpack_Mu_Wpcom::PKG_DIR . 'src/features/wpcom-admin-bar/wpcom-admin-bar.php';
11+
require_once ABSPATH . 'wp-includes/class-wp-admin-bar.php';
12+
13+
const WPCOM_ADMIN_BAR_TEST_OPT_IN = 1;
14+
const WPCOM_ADMIN_BAR_TEST_OPT_OUT = 2;
15+
const WPCOM_ADMIN_BAR_TEST_UNSET = 3;
16+
17+
/**
18+
* Class WPCOM_Admin_Bar_Test
19+
*/
20+
class WPCOM_Admin_Bar_Test extends \WorDBless\BaseTestCase {
21+
private static function make_test_admin_bar() {
22+
$admin_bar = new \WP_Admin_Bar();
23+
24+
$admin_bar->add_node(
25+
array(
26+
'id' => 'wp-logo',
27+
'title' => 'WordPress Logo',
28+
'href' => 'https://wordpress.org/',
29+
)
30+
);
31+
$admin_bar->add_node(
32+
array(
33+
'id' => 'about',
34+
'parent' => 'wp-logo',
35+
'title' => 'About WordPress',
36+
'href' => 'https://wordpress.org/about/',
37+
)
38+
);
39+
$admin_bar->add_node(
40+
array(
41+
'id' => 'contribute',
42+
'parent' => 'wp-logo',
43+
'title' => 'Get Involved',
44+
'href' => 'https://wordpress.org/contribute/',
45+
)
46+
);
47+
48+
return $admin_bar;
49+
}
50+
51+
/**
52+
* Mock the user's hosting dashboard opt-in preference.
53+
*
54+
* @param int $value One of the WPCOM_ADMIN_BAR_TEST_* constants to define the test scenario.
55+
*/
56+
private static function mock_hosting_dashboard_opt_in_preference( int $value ) {
57+
// See tests/lib/functions-wordpress.php for the get_user_attribute mock.
58+
global $test_user_attributes;
59+
$test_user_attributes = array();
60+
61+
// Set up preferences based on test scenario
62+
if ( $value === WPCOM_ADMIN_BAR_TEST_OPT_IN ) {
63+
$test_user_attributes = array(
64+
'calypso_preferences' => array(
65+
'hosting-dashboard-opt-in' => array(
66+
'value' => 'opt-in',
67+
),
68+
),
69+
);
70+
} elseif ( $value === WPCOM_ADMIN_BAR_TEST_OPT_OUT ) {
71+
$test_user_attributes = array(
72+
'calypso_preferences' => array(
73+
'hosting-dashboard-opt-in' => array(
74+
'value' => 'opt-out',
75+
),
76+
),
77+
);
78+
}
79+
}
80+
81+
public function test_wp_logo_replaced_by_wpcom_logo() {
82+
self::mock_hosting_dashboard_opt_in_preference( WPCOM_ADMIN_BAR_TEST_UNSET );
83+
84+
$admin_bar = self::make_test_admin_bar();
85+
wpcom_replace_wp_logo_with_wpcom_logo_menu( $admin_bar );
86+
87+
$this->assertNull( $admin_bar->get_node( 'wp-logo' ) );
88+
$this->assertNotNull( $admin_bar->get_node( 'wpcom-logo' ) );
89+
}
90+
91+
public function test_hosting_dashboard_opt_out_menu_links() {
92+
self::mock_hosting_dashboard_opt_in_preference( WPCOM_ADMIN_BAR_TEST_OPT_OUT );
93+
94+
$admin_bar = self::make_test_admin_bar();
95+
wpcom_replace_wp_logo_with_wpcom_logo_menu( $admin_bar );
96+
97+
$wpcom_logo_node = $admin_bar->get_node( 'wpcom-logo' );
98+
$this->assertStringContainsString( 'https://wordpress.com/sites', $wpcom_logo_node->href );
99+
100+
$sites_node = $admin_bar->get_node( 'wpcom-sites' );
101+
$this->assertNotNull( $sites_node );
102+
$this->assertStringContainsString( 'https://wordpress.com/sites', $sites_node->href );
103+
$this->assertEquals( 'wpcom-logo', $sites_node->parent );
104+
105+
$domains_node = $admin_bar->get_node( 'wpcom-domains' );
106+
$this->assertNotNull( $domains_node );
107+
$this->assertStringContainsString( 'https://wordpress.com/domains', $domains_node->href );
108+
$this->assertEquals( 'wpcom-logo', $domains_node->parent );
109+
}
110+
111+
public function test_hosting_dashboard_preference_unset_menu_links() {
112+
self::mock_hosting_dashboard_opt_in_preference( WPCOM_ADMIN_BAR_TEST_UNSET );
113+
114+
$admin_bar = self::make_test_admin_bar();
115+
wpcom_replace_wp_logo_with_wpcom_logo_menu( $admin_bar );
116+
117+
$wpcom_logo_node = $admin_bar->get_node( 'wpcom-logo' );
118+
$this->assertStringContainsString( 'https://wordpress.com/sites', $wpcom_logo_node->href );
119+
120+
$sites_node = $admin_bar->get_node( 'wpcom-sites' );
121+
$this->assertNotNull( $sites_node );
122+
$this->assertStringContainsString( 'https://wordpress.com/sites', $sites_node->href );
123+
$this->assertEquals( 'wpcom-logo', $sites_node->parent );
124+
125+
$domains_node = $admin_bar->get_node( 'wpcom-domains' );
126+
$this->assertNotNull( $domains_node );
127+
$this->assertStringContainsString( 'https://wordpress.com/domains', $domains_node->href );
128+
$this->assertEquals( 'wpcom-logo', $domains_node->parent );
129+
}
130+
131+
public function test_hosting_dashboard_opt_in_menu_links() {
132+
self::mock_hosting_dashboard_opt_in_preference( WPCOM_ADMIN_BAR_TEST_OPT_IN );
133+
134+
$admin_bar = self::make_test_admin_bar();
135+
wpcom_replace_wp_logo_with_wpcom_logo_menu( $admin_bar );
136+
137+
$wpcom_logo_node = $admin_bar->get_node( 'wpcom-logo' );
138+
$this->assertStringContainsString( 'https://wordpress.com/v2/sites', $wpcom_logo_node->href );
139+
140+
$sites_node = $admin_bar->get_node( 'wpcom-sites' );
141+
$this->assertNotNull( $sites_node );
142+
$this->assertStringContainsString( 'https://wordpress.com/v2/sites', $sites_node->href );
143+
$this->assertEquals( 'wpcom-logo', $sites_node->parent );
144+
145+
$domains_node = $admin_bar->get_node( 'wpcom-domains' );
146+
$this->assertNotNull( $domains_node );
147+
$this->assertStringContainsString( 'https://wordpress.com/v2/domains', $domains_node->href );
148+
$this->assertEquals( 'wpcom-logo', $domains_node->parent );
149+
}
150+
}

0 commit comments

Comments
 (0)