Skip to content
Merged
Show file tree
Hide file tree
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
79 changes: 79 additions & 0 deletions plugins/presence-api/tests/test-admin-bar.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* @covers ::wp_presence_admin_bar_assets
* @covers ::wp_presence_admin_bar_node_markup
* @covers ::wp_presence_admin_bar_heartbeat_received
* @covers ::wp_presence_refresh_screen_token
* @covers ::wp_presence_get_entry_screen
* @covers ::wp_presence_screen_object_id
* @covers ::wp_presence_get_user_color
* @covers ::wp_presence_online_users_url
Expand Down Expand Up @@ -328,6 +330,27 @@ public function test_someone_editing_a_post_links_to_it() {
$this->assertSame( 'Secret Draft', $place->title );
}

public function test_unparsable_post_room_is_skipped_and_first_post_room_wins() {
$this->put_editor_on_post( self::$post_id );

$second_post_id = self::factory()->post->create(
array(
'post_title' => 'Second Post',
'post_status' => 'draft',
)
);
wp_set_presence( wp_presence_post_room( $second_post_id ), 'second-' . self::$editor_id, array(), array( 'user_id' => self::$editor_id ) );

$second_user_id = self::factory()->user->create( array( 'role' => 'editor' ) );
wp_set_presence( 'postType/bad', 'bad-' . $second_user_id, array(), array( 'user_id' => $second_user_id ) );

wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
$place = $this->place_of( $this->render_nodes(), self::$editor_id );

$this->assertSame( get_edit_post_link( self::$post_id, 'raw' ), $place->href );
$this->assertSame( 'Secret Draft', $place->title );
}

public function test_someone_editing_a_template_in_the_site_editor_links_to_it() {
$template_id = self::factory()->post->create(
array(
Expand Down Expand Up @@ -531,6 +554,43 @@ public function test_readers_of_another_page_are_not_on_this_page() {
$this->assertSame( 'presence-elsewhere', $nodes[ 'presence-user-' . $there ]->parent );
}

public function test_people_on_the_front_end_link_to_posts_you_can_read() {
set_current_screen( 'dashboard' );
$published_id = self::factory()->post->create(
array(
'post_title' => 'Public Post',
'post_status' => 'publish',
)
);
$on_home = $this->put_user_on_screen( 'front', array( 'title' => 'Home' ) );
$on_published = $this->put_user_on_screen(
'front',
array(
'title' => 'Public Post',
'post_id' => $published_id,
)
);
$on_draft = $this->put_user_on_screen(
'front',
array(
'title' => 'Secret Draft',
'post_id' => self::$post_id,
)
);

wp_set_current_user( self::factory()->user->create( array( 'role' => 'contributor' ) ) );
$this->let_current_user_list_users();
$nodes = $this->render_nodes();

$this->assertArrayNotHasKey( 'presence-user-' . $on_published, $nodes );
$this->assertSame( get_permalink( $published_id ), $nodes['presence-place-0']->href );
$this->assertStringContainsString( '>Public Post</span>', $nodes['presence-place-0']->title );
$this->assertSame( 'Home', $this->place_of( $nodes, $on_home )->title );
$this->assertFalse( $this->place_of( $nodes, $on_home )->href );
$this->assertSame( esc_html( get_userdata( $on_draft )->display_name ), $nodes[ 'presence-user-' . $on_draft ]->title );
$this->assertFalse( $nodes[ 'presence-user-' . $on_draft ]->href );
}

public function test_rows_are_not_links() {
$this->view_admin_page( 'upload.php', 'upload' );
$user_id = $this->put_user_on_screen( 'upload' );
Expand Down Expand Up @@ -845,5 +905,24 @@ static function ( $is_agent, $user_id ) use ( $agent_id ) {
$title = $nodes[ 'presence-user-' . $agent_id ]->title;
$this->assertStringContainsString( $agent->display_name, $title );
$this->assertStringContainsString( 'presence-agent-badge', $title );

wp_remove_presence( wp_presence_post_room( self::$post_id ), 'agent-' . $agent_id );
wp_set_presence(
'admin/online',
'agent-screen-' . $agent_id,
array(
'screen' => 'users',
'title' => 'Users',
),
array( 'user_id' => $agent_id )
);
$nodes = $this->render_nodes();
$this->assertSame( $agent->display_name . ' (agent)', $nodes['presence-place-0']->meta['title'] );
}

public function test_node_markup_is_empty_for_a_user_without_edit_posts() {
wp_set_current_user( self::factory()->user->create( array( 'role' => 'subscriber' ) ) );

$this->assertSame( '', wp_presence_admin_bar_node_markup( 'dashboard' ) );
}
}
11 changes: 11 additions & 0 deletions plugins/presence-api/tests/test-user-list.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,17 @@ public function test_the_online_view_needs_its_nonce() {
$this->assertSame( array(), $this->tick_online_view( 'invalid' ) );
}

/**
* @covers ::wp_presence_users_online_count_heartbeat_received
* @covers ::wp_presence_users_list_heartbeat_received
*/
public function test_heartbeat_received_returns_empty_without_fragment_request() {
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );

$this->assertSame( array(), wp_presence_users_online_count_heartbeat_received( array(), array(), 'users' ) );
$this->assertSame( array(), wp_presence_users_list_heartbeat_received( array(), array(), 'users' ) );
Comment thread
josephfusco marked this conversation as resolved.
}

/**
* Every link to the Online view is built by the helper, so a URL from it
* has to open the filtered list rather than every user.
Expand Down
46 changes: 46 additions & 0 deletions plugins/presence-api/tests/widgets/test-widget-active-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,58 @@ public function test_excludes_non_post_rooms() {
wp_set_current_user( self::$editor_id );

wp_set_presence( 'admin/online', 'user-' . self::$editor_id, array(), array( 'user_id' => self::$editor_id ) );
wp_set_presence( 'postType/bad', 'bad-room', array(), array( 'user_id' => self::$editor_id ) );

$posts = $this->active_posts();

$this->assertCount( 0, $posts );
}

/**
* @covers WP_Presence_Widget_Active_Posts::build_active_posts_data
*/
public function test_active_posts_skips_users_that_no_longer_exist() {
wp_set_current_user( self::$editor_id );

wp_set_presence( wp_presence_post_room( self::$post_id ), 'deleted-user', array(), array( 'user_id' => 999901 ) );

$posts = $this->active_posts();

$this->assertCount( 0, $posts );
}

/**
* @covers WP_Presence_Widget_Active_Posts::build_active_posts_data
*/
public function test_active_posts_excludes_posts_without_presence_support() {
register_post_type(
'no_presence_support',
array(
'public' => true,
'supports' => array( 'title' ),
)
);
remove_post_type_support( 'no_presence_support', 'presence' );

$post_id = self::factory()->post->create(
array(
'post_type' => 'no_presence_support',
'post_status' => 'publish',
'post_author' => self::$editor_id,
)
);
wp_set_current_user( self::$editor_id );

$room = 'postType/no_presence_support:' . $post_id;
wp_set_presence( $room, 'user-' . self::$editor_id, array(), array( 'user_id' => self::$editor_id ) );

$posts = $this->active_posts();
$this->assertCount( 0, $posts );

_unregister_post_type( 'no_presence_support' );
wp_presence_forget_post_type( 'no_presence_support' );
}

/**
* A contributor has `edit_posts`, which is all the widget itself requires,
* but cannot edit someone else's post and must not learn it is being
Expand Down
Loading