Skip to content

Commit 0773712

Browse files
authored
Update Jetpack connection check to use Manager class (#2249)
1 parent 99e6c0b commit 0773712

File tree

3 files changed

+87
-12
lines changed

3 files changed

+87
-12
lines changed

integration/class-jetpack.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Activitypub\Collection\Followers;
1111
use Activitypub\Collection\Following;
1212
use Activitypub\Comment;
13+
use Automattic\Jetpack\Connection\Manager;
1314

1415
/**
1516
* Jetpack integration class.
@@ -30,7 +31,7 @@ public static function init() {
3031

3132
if (
3233
( \defined( 'IS_WPCOM' ) && IS_WPCOM ) ||
33-
( \class_exists( '\Jetpack' ) && \Jetpack::is_connection_ready() )
34+
( \class_exists( '\Automattic\Jetpack\Connection\Manager' ) && ( new Manager() )->is_user_connected() )
3435
) {
3536
\add_filter( 'activitypub_following_row_actions', array( self::class, 'add_reader_link' ), 10, 2 );
3637
\add_filter( 'pre_option_activitypub_following_ui', array( self::class, 'pre_option_activitypub_following_ui' ) );

tests/data/class-manager.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Mock Jetpack class for testing.
4+
*
5+
* @package Activitypub
6+
*/
7+
8+
namespace Automattic\Jetpack\Connection;
9+
10+
/**
11+
* Mock Jetpack class for testing.
12+
*/
13+
if ( ! class_exists( 'Automattic\Jetpack\Connection\Manager' ) ) {
14+
/**
15+
* Mock Jetpack class for testing purposes.
16+
*/
17+
class Manager {
18+
/**
19+
* Mock method to simulate Jetpack connection status.
20+
*
21+
* @return bool Always returns true for testing.
22+
*/
23+
public function is_user_connected() {
24+
return true;
25+
}
26+
}
27+
}

tests/integration/class-test-jetpack.php

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ public static function wpSetUpBeforeClass( $factory ) {
5454
);
5555
}
5656

57+
/**
58+
* Load mock Manager class for specific tests.
59+
*/
60+
private function load_mock_manager() {
61+
if ( ! class_exists( '\Automattic\Jetpack\Connection\Manager' ) ) {
62+
require_once AP_TESTS_DIR . '/data/class-manager.php';
63+
}
64+
}
65+
5766
/**
5867
* Clean up after tests.
5968
*/
@@ -71,42 +80,80 @@ public function tear_down() {
7180
}
7281

7382
/**
74-
* Test init method registers sync hooks correctly.
83+
* Test init method registers sync hooks without Manager class.
84+
*
85+
* This test must run before the Manager class is loaded to test the behavior
86+
* when the class doesn't exist.
7587
*
7688
* @covers ::init
7789
*/
78-
public function test_init_registers_sync_hooks() {
90+
public function test_a_init_registers_sync_hooks_without_manager() {
91+
// Verify Manager class is not yet loaded.
92+
$this->assertFalse( class_exists( '\Automattic\Jetpack\Connection\Manager' ), 'Manager class should not exist yet' );
93+
7994
// Ensure hooks are not already registered.
8095
$this->assertFalse( has_filter( 'jetpack_sync_post_meta_whitelist' ) );
96+
$this->assertFalse( has_filter( 'activitypub_following_row_actions' ) );
97+
$this->assertFalse( has_filter( 'pre_option_activitypub_following_ui' ) );
8198

82-
// Initialize Jetpack integration.
99+
// Initialize Jetpack integration without Manager class loaded.
83100
Jetpack::init();
84101

85-
// Check that sync hooks are registered (when not on WordPress.com).
102+
// Check that sync hooks are registered regardless of Manager class.
86103
$this->assertTrue( has_filter( 'jetpack_sync_post_meta_whitelist' ) );
87104
$this->assertTrue( has_filter( 'jetpack_sync_comment_meta_whitelist' ) );
88105
$this->assertTrue( has_filter( 'jetpack_sync_whitelisted_comment_types' ) );
89106
$this->assertTrue( has_filter( 'jetpack_json_api_comment_types' ) );
90107
$this->assertTrue( has_filter( 'jetpack_api_include_comment_types_count' ) );
108+
109+
// Following UI hooks should NOT be registered without Manager class.
110+
$this->assertFalse( has_filter( 'activitypub_following_row_actions' ) );
111+
$this->assertFalse( has_filter( 'pre_option_activitypub_following_ui' ) );
91112
}
92113

93114
/**
94-
* Test that following UI hooks are not registered without proper conditions.
115+
* Test init method registers all hooks with Manager class available.
95116
*
96117
* @covers ::init
97118
*/
98-
public function test_init_skips_following_ui_hooks_without_conditions() {
119+
public function test_b_init_registers_hooks_with_manager() {
120+
// Load mock Manager class.
121+
$this->load_mock_manager();
122+
99123
// Ensure hooks are not already registered.
124+
$this->assertFalse( has_filter( 'jetpack_sync_post_meta_whitelist' ) );
100125
$this->assertFalse( has_filter( 'activitypub_following_row_actions' ) );
101126
$this->assertFalse( has_filter( 'pre_option_activitypub_following_ui' ) );
102127

103-
// Initialize Jetpack integration.
128+
// Initialize Jetpack integration with Manager class.
104129
Jetpack::init();
105130

106-
// Following UI hooks should NOT be registered in test environment
107-
// because neither IS_WPCOM is defined nor is Jetpack connected.
108-
$this->assertFalse( has_filter( 'activitypub_following_row_actions' ) );
109-
$this->assertFalse( has_filter( 'pre_option_activitypub_following_ui' ) );
131+
// Check that sync hooks are registered.
132+
$this->assertTrue( has_filter( 'jetpack_sync_post_meta_whitelist' ) );
133+
$this->assertTrue( has_filter( 'jetpack_sync_comment_meta_whitelist' ) );
134+
$this->assertTrue( has_filter( 'jetpack_sync_whitelisted_comment_types' ) );
135+
$this->assertTrue( has_filter( 'jetpack_json_api_comment_types' ) );
136+
$this->assertTrue( has_filter( 'jetpack_api_include_comment_types_count' ) );
137+
138+
// Following UI hooks should also be registered (mock Manager returns connected).
139+
$this->assertTrue( has_filter( 'activitypub_following_row_actions' ) );
140+
$this->assertTrue( has_filter( 'pre_option_activitypub_following_ui' ) );
141+
}
142+
143+
/**
144+
* Test that Manager class connection check works when available.
145+
*
146+
* @covers ::init
147+
*/
148+
public function test_c_manager_connection_check() {
149+
// Load mock Manager class.
150+
$this->load_mock_manager();
151+
152+
// Test that our mock Manager class exists and works.
153+
$this->assertTrue( class_exists( '\Automattic\Jetpack\Connection\Manager' ), 'Mock Manager class should exist' );
154+
155+
$manager = new \Automattic\Jetpack\Connection\Manager();
156+
$this->assertTrue( $manager->is_user_connected(), 'Mock Manager should return connected' );
110157
}
111158

112159
/**

0 commit comments

Comments
 (0)