Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
196 changes: 66 additions & 130 deletions tests/integration/class-test-jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,68 +156,6 @@ public function test_c_manager_connection_check() {
$this->assertTrue( $manager->is_user_connected(), 'Mock Manager should return connected' );
}

/**
* Test init method skips sync hooks when IS_WPCOM is defined.
*
* @covers ::init
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_init_skips_sync_hooks_on_wpcom() {
// Clear any existing hooks first.
remove_all_filters( 'jetpack_sync_post_meta_whitelist' );
remove_all_filters( 'jetpack_sync_comment_meta_whitelist' );
remove_all_filters( 'jetpack_sync_whitelisted_comment_types' );
remove_all_filters( 'jetpack_json_api_comment_types' );
remove_all_filters( 'jetpack_api_include_comment_types_count' );
remove_all_filters( 'activitypub_following_row_actions' );
remove_all_filters( 'pre_option_activitypub_following_ui' );

// Test the normal case first (IS_WPCOM not defined).
if ( ! defined( 'IS_WPCOM' ) ) {
Jetpack::init();

// Sync hooks should be registered when IS_WPCOM is not defined.
$this->assertTrue( has_filter( 'jetpack_sync_post_meta_whitelist' ) );
$this->assertTrue( has_filter( 'jetpack_sync_comment_meta_whitelist' ) );
$this->assertTrue( has_filter( 'jetpack_sync_whitelisted_comment_types' ) );
$this->assertTrue( has_filter( 'jetpack_json_api_comment_types' ) );
$this->assertTrue( has_filter( 'jetpack_api_include_comment_types_count' ) );

// Following UI hooks should NOT be registered in normal test environment.
$this->assertFalse( has_filter( 'activitypub_following_row_actions' ) );
$this->assertFalse( has_filter( 'pre_option_activitypub_following_ui' ) );

// Clear hooks again for the WPCOM simulation.
remove_all_filters( 'jetpack_sync_post_meta_whitelist' );
remove_all_filters( 'jetpack_sync_comment_meta_whitelist' );
remove_all_filters( 'jetpack_sync_whitelisted_comment_types' );
remove_all_filters( 'jetpack_json_api_comment_types' );
remove_all_filters( 'jetpack_api_include_comment_types_count' );
remove_all_filters( 'activitypub_following_row_actions' );
remove_all_filters( 'pre_option_activitypub_following_ui' );
}

// Now simulate IS_WPCOM behavior by defining the constant temporarily.
// We use a runInSeparateProcess annotation to isolate this test.
if ( ! defined( 'IS_WPCOM' ) ) {
define( 'IS_WPCOM', true );
}

Jetpack::init();

// When IS_WPCOM is defined, sync hooks should NOT be registered.
$this->assertFalse( has_filter( 'jetpack_sync_post_meta_whitelist' ) );
$this->assertFalse( has_filter( 'jetpack_sync_comment_meta_whitelist' ) );
$this->assertFalse( has_filter( 'jetpack_sync_whitelisted_comment_types' ) );
$this->assertFalse( has_filter( 'jetpack_json_api_comment_types' ) );
$this->assertFalse( has_filter( 'jetpack_api_include_comment_types_count' ) );

// But following UI hooks should be registered when IS_WPCOM is true.
$this->assertTrue( has_filter( 'activitypub_following_row_actions' ) );
$this->assertTrue( has_filter( 'pre_option_activitypub_following_ui' ) );
}

/**
* Test add_sync_meta method adds ActivityPub meta keys.
*
Expand Down Expand Up @@ -278,31 +216,91 @@ public function test_add_comment_types() {
$this->assertEquals( $updated_types, array_unique( $updated_types ) );
}

/**
* Test pre_option_activitypub_following_ui method forces UI to be enabled.
*
* @covers ::pre_option_activitypub_following_ui
*/
public function test_pre_option_activitypub_following_ui() {
$result = Jetpack::pre_option_activitypub_following_ui();

$this->assertEquals( '1', $result );
}

/**
* Test integration with actual WordPress filters.
*/
public function test_filter_integration() {
// Initialize Jetpack integration.
Jetpack::init();

// Test sync meta filter integration (only if not on WordPress.com).
if ( ! defined( 'IS_WPCOM' ) ) {
$sync_meta = apply_filters( 'jetpack_sync_post_meta_whitelist', array() );
$this->assertContains( Followers::FOLLOWER_META_KEY, $sync_meta );
$this->assertContains( Following::FOLLOWING_META_KEY, $sync_meta );

// Test comment meta filter integration.
$comment_meta = apply_filters( 'jetpack_sync_comment_meta_whitelist', array() );
$this->assertContains( 'avatar_url', $comment_meta );

// Test comment types filter integration.
$comment_types = apply_filters( 'jetpack_sync_whitelisted_comment_types', array() );
$expected_ap_types = Comment::get_comment_type_slugs();
foreach ( $expected_ap_types as $type ) {
$this->assertContains( $type, $comment_types );
}
} else {
// On WordPress.com, sync filters should not be registered.
// Test that they are indeed not registered.
$sync_meta = apply_filters( 'jetpack_sync_post_meta_whitelist', array() );
$this->assertNotContains( Followers::FOLLOWER_META_KEY, $sync_meta );
$this->assertNotContains( Following::FOLLOWING_META_KEY, $sync_meta );

$comment_meta = apply_filters( 'jetpack_sync_comment_meta_whitelist', array() );
$this->assertNotContains( 'avatar_url', $comment_meta );
}

// Test following UI filter integration - test direct method calls.
$ui_result = Jetpack::pre_option_activitypub_following_ui();
$this->assertEquals( '1', $ui_result );

// Test reader link method directly.
$test_item = array(
'id' => 123,
'status' => 'active',
'identifier' => 'https://example.com/feed',
);
$original_actions = array( 'edit' => '<a href="#">Edit</a>' );
$updated_actions = Jetpack::add_reader_link( $original_actions, $test_item );
$this->assertArrayHasKey( 'reader', $updated_actions );
}

/**
* Data provider for Reader link test scenarios.
*
* @return array Test cases with different following item configurations.
*/
public function reader_link_data() {
return array(
'active following with feed ID' => array(
'active following without feed ID' => array(
'item' => array(
'id' => 123,
'status' => 'active',
'identifier' => 'https://example.com/feed',
),
'feed_id' => 456,
'expected_url' => 'https://wordpress.com/reader/feeds/456',
'feed_id' => false,
'expected_url' => 'https://wordpress.com/reader/feeds/lookup/https%3A%2F%2Fexample.com%2Ffeed',
'should_have_reader_link' => true,
),
'active following without feed ID' => array(
'active following with feed ID' => array(
'item' => array(
'id' => 123,
'status' => 'active',
'identifier' => 'https://example.com/feed',
),
'feed_id' => false,
'expected_url' => 'https://wordpress.com/reader/feeds/lookup/https%3A%2F%2Fexample.com%2Ffeed',
'feed_id' => 456,
'expected_url' => 'https://wordpress.com/reader/feeds/456',
'should_have_reader_link' => true,
),
'pending following should not have reader link' => array(
Expand All @@ -323,8 +321,6 @@ public function reader_link_data() {
*
* @dataProvider reader_link_data
* @covers ::add_reader_link
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* @param array $item The following item.
* @param int|false $feed_id The feed ID or false.
Expand Down Expand Up @@ -375,64 +371,4 @@ function ( $value, $object_id, $meta_key ) use ( $item, $feed_id ) {
// Clean up filters.
remove_all_filters( 'get_post_metadata' );
}

/**
* Test pre_option_activitypub_following_ui method forces UI to be enabled.
*
* @covers ::pre_option_activitypub_following_ui
*/
public function test_pre_option_activitypub_following_ui() {
$result = Jetpack::pre_option_activitypub_following_ui();

$this->assertEquals( '1', $result );
}

/**
* Test integration with actual WordPress filters.
*/
public function test_filter_integration() {
// Initialize Jetpack integration.
Jetpack::init();

// Test sync meta filter integration (only if not on WordPress.com).
if ( ! defined( 'IS_WPCOM' ) ) {
$sync_meta = apply_filters( 'jetpack_sync_post_meta_whitelist', array() );
$this->assertContains( Followers::FOLLOWER_META_KEY, $sync_meta );
$this->assertContains( Following::FOLLOWING_META_KEY, $sync_meta );

// Test comment meta filter integration.
$comment_meta = apply_filters( 'jetpack_sync_comment_meta_whitelist', array() );
$this->assertContains( 'avatar_url', $comment_meta );

// Test comment types filter integration.
$comment_types = apply_filters( 'jetpack_sync_whitelisted_comment_types', array() );
$expected_ap_types = Comment::get_comment_type_slugs();
foreach ( $expected_ap_types as $type ) {
$this->assertContains( $type, $comment_types );
}
} else {
// On WordPress.com, sync filters should not be registered.
// Test that they are indeed not registered.
$sync_meta = apply_filters( 'jetpack_sync_post_meta_whitelist', array() );
$this->assertNotContains( Followers::FOLLOWER_META_KEY, $sync_meta );
$this->assertNotContains( Following::FOLLOWING_META_KEY, $sync_meta );

$comment_meta = apply_filters( 'jetpack_sync_comment_meta_whitelist', array() );
$this->assertNotContains( 'avatar_url', $comment_meta );
}

// Test following UI filter integration - test direct method calls.
$ui_result = Jetpack::pre_option_activitypub_following_ui();
$this->assertEquals( '1', $ui_result );

// Test reader link method directly.
$test_item = array(
'id' => 123,
'status' => 'active',
'identifier' => 'https://example.com/feed',
);
$original_actions = array( 'edit' => '<a href="#">Edit</a>' );
$updated_actions = Jetpack::add_reader_link( $original_actions, $test_item );
$this->assertArrayHasKey( 'reader', $updated_actions );
}
}