Skip to content

Commit 5d908bc

Browse files
committed
Update tests
1 parent d6fd975 commit 5d908bc

File tree

4 files changed

+48
-27
lines changed

4 files changed

+48
-27
lines changed

projects/packages/admin-ui/src/class-admin-menu.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ class Admin_Menu {
4949
*/
5050
private static $menu_items = array();
5151

52+
/**
53+
* Optional connection manager dependency.
54+
*
55+
* @var object|null
56+
*/
57+
private static $connection_manager = null;
58+
5259
/**
5360
* Initialize the class and set up the main hook
5461
*
@@ -287,13 +294,28 @@ private static function should_show_upgrade_menu() {
287294
* @return bool True if the site has a connected blog ID.
288295
*/
289296
private static function is_site_connected() {
290-
if ( class_exists( '\Automattic\Jetpack\Connection\Manager' ) ) {
291-
return (bool) ( new \Automattic\Jetpack\Connection\Manager() )->is_connected();
297+
$connection_manager = self::$connection_manager;
298+
if ( ! $connection_manager && class_exists( '\Automattic\Jetpack\Connection\Manager' ) ) {
299+
$connection_manager = new \Automattic\Jetpack\Connection\Manager();
300+
}
301+
302+
if ( $connection_manager && is_callable( array( $connection_manager, 'is_connected' ) ) ) {
303+
return (bool) $connection_manager->is_connected();
292304
}
293305

294306
return false;
295307
}
296308

309+
/**
310+
* Sets the connection manager dependency.
311+
*
312+
* @param object|null $connection_manager Connection manager object.
313+
* @return void
314+
*/
315+
public static function set_connection_manager( $connection_manager ) {
316+
self::$connection_manager = $connection_manager;
317+
}
318+
297319
/**
298320
* Checks whether the current site is on a free Jetpack plan with no active paid license.
299321
*

projects/packages/admin-ui/tests/php/Admin_Menu_Test.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ public function setUp(): void {
9292
delete_option( 'jetpack_active_plan' );
9393
delete_option( 'jetpack_site_products' );
9494
update_option( 'jetpack_options', array( 'id' => 123456 ) );
95+
$connection = $this->getMockBuilder( 'Automattic\Jetpack\Connection\Manager' )
96+
->disableOriginalConstructor()
97+
->getMock();
98+
$connection->method( 'is_connected' )->willReturn( true );
99+
Admin_Menu::set_connection_manager( $connection );
95100
wp_dequeue_style( 'jetpack-admin-ui-upgrade-menu' );
96101
wp_deregister_style( 'jetpack-admin-ui-upgrade-menu' );
97102
wp_dequeue_script( 'jetpack-admin-ui-upgrade-menu' );
@@ -208,6 +213,13 @@ public function test_first_menu() {
208213
*/
209214
public function test_upgrade_menu_item_shown_for_free_plan_admin() {
210215
wp_set_current_user( self::$admin_user_id );
216+
$connection = $this->getMockBuilder( 'Automattic\Jetpack\Connection\Manager' )
217+
->disableOriginalConstructor()
218+
->getMock();
219+
$connection->expects( $this->once() )
220+
->method( 'is_connected' )
221+
->willReturn( true );
222+
Admin_Menu::set_connection_manager( $connection );
211223

212224
Admin_Menu::init();
213225
do_action( 'admin_menu' );
@@ -373,7 +385,13 @@ public function test_upgrade_menu_item_hidden_for_non_admin() {
373385
*/
374386
public function test_upgrade_menu_item_hidden_when_site_not_connected() {
375387
wp_set_current_user( self::$admin_user_id );
376-
update_option( 'jetpack_options', array() );
388+
$connection = $this->getMockBuilder( 'Automattic\Jetpack\Connection\Manager' )
389+
->disableOriginalConstructor()
390+
->getMock();
391+
$connection->expects( $this->once() )
392+
->method( 'is_connected' )
393+
->willReturn( false );
394+
Admin_Menu::set_connection_manager( $connection );
377395

378396
Admin_Menu::init();
379397
do_action( 'admin_menu' );

projects/packages/admin-ui/tests/php/bootstrap.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010
*/
1111
require_once __DIR__ . '/../../vendor/autoload.php';
1212

13+
if ( ! class_exists( 'Jetpack_Options' ) ) {
14+
require_once __DIR__ . '/../../../connection/legacy/class-jetpack-options.php';
15+
}
16+
1317
if ( ! class_exists( '\Automattic\Jetpack\Connection\Manager' ) ) {
14-
require_once __DIR__ . '/stubs/class-manager.php';
18+
require_once __DIR__ . '/../../../connection/src/class-manager.php';
1519
}
1620

1721
define( 'WP_DEBUG', true );

projects/packages/admin-ui/tests/php/stubs/class-manager.php

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)