/**
  * Test assigning a Co-Author to a post
  */
 public function test_add_coauthor_to_post()
 {
     global $coauthors_plus;
     $coauthors = \CoAuthorsPlusRoles\get_coauthors($this->author1_post1);
     $this->assertEquals(1, count($coauthors));
     // append = true, should preserve order
     $editor1 = get_user_by('id', $this->editor1);
     $coauthors_plus->add_coauthors($this->author1_post1, array($editor1->user_login), true);
     $coauthors = \CoAuthorsPlusRoles\get_coauthors($this->author1_post1);
     $this->assertEquals(array($this->author1, $this->editor1), wp_list_pluck($coauthors, 'ID'));
     // append = false, overrides existing authors
     $coauthors_plus->add_coauthors($this->author1_post1, array($editor1->user_login), false);
     $coauthors = \CoAuthorsPlusRoles\get_coauthors($this->author1_post1);
     $this->assertEquals(array($this->editor1), wp_list_pluck($coauthors, 'ID'));
 }
 /**
  * Test the functions called on update_post to edit the contributors on a post.
  */
 public function test_admin_set_contributors_on_post()
 {
     global $coauthors_plus;
     // Create a post with a WP user as author. Calling update_coauthors_on_post should reset it.
     $post = $this->factory->post->create(array('post_status' => 'publish', 'post_content' => rand_str(), 'post_title' => rand_str(), 'post_author' => $this->author1_post1));
     $guest_author = $coauthors_plus->guest_authors->create(array('display_name' => 'Guest Author through UI', 'user_login' => 'guest-author-through-ui'));
     $guest_author_nicename = $coauthors_plus->get_coauthor_by('id', $guest_author)->user_nicename;
     \CoAuthorsPlusRoles\update_coauthors_on_post($post, array("{$guest_author_nicename}|||author"));
     $updated_coauthors = \CoAuthorsPlusRoles\get_coauthors((int) $post, array('author_role' => 'any'));
     $this->assertCount(1, $updated_coauthors);
     $this->assertContains($guest_author, wp_list_pluck($updated_coauthors, 'ID'));
     $user1 = get_userdata($this->author1);
     // Calling update_coauthors_on_post with an array should add all the new authors
     // Using: one WP_User, and one of Guest Author post type
     \CoAuthorsPlusRoles\update_coauthors_on_post($post, array("{$user1->user_login}|||author", "guest-author-through-ui|||contributor"));
     // Both should show up in a query with author_role = any
     $updated_coauthors = \CoAuthorsPlusRoles\get_coauthors($post, array('author_role' => 'any'));
     $this->assertContains($user1->user_login, wp_list_pluck($updated_coauthors, 'user_nicename'));
     $this->assertContains('guest-author-through-ui', wp_list_pluck($updated_coauthors, 'user_nicename'));
     // Only the contributor should show up in this query where author_role = contributor
     $contributors = \CoAuthorsPlusRoles\get_coauthors($post, array('author_role' => 'contributor'));
     $this->assertNotContains($user1->user_login, wp_list_pluck($contributors, 'user_nicename'));
     $this->assertContains('guest-author-through-ui', wp_list_pluck($contributors, 'user_nicename'));
     // It should be possible to save more than one "byline" author on a post, through the UI.
     // Also, the string "byline" for role should behave identically to an empty string.
     \CoAuthorsPlusRoles\update_coauthors_on_post($post, array("{$user1->user_login}|||", "guest-author-through-ui|||byline"));
     $updated_coauthors = \CoAuthorsPlusRoles\get_coauthors($post);
     $this->assertcount(2, $updated_coauthors);
     $this->assertEquals($user1->user_login, $updated_coauthors[0]->user_nicename);
     $this->assertEquals('guest-author-through-ui', $updated_coauthors[1]->user_nicename);
     // Updating coauthors in a different order; the same tests should pass, but the new order should be preserved.
     \CoAuthorsPlusRoles\update_coauthors_on_post($post, array("guest-author-through-ui|||contributor", "{$user1->user_login}|||author"));
     // Both should show up in a query with author_role = any, and the first one posted should be first
     $updated_coauthors = \CoAuthorsPlusRoles\get_coauthors($post, array('author_role' => 'any'));
     $this->assertContains($user1->user_login, wp_list_pluck($updated_coauthors, 'user_nicename'));
     $this->assertContains('guest-author-through-ui', wp_list_pluck($updated_coauthors, 'user_nicename'));
     $this->assertEquals('guest-author-through-ui', $updated_coauthors[0]->user_nicename);
     // Updating an individual coauthor; should change role (sorting is presently undefined...)
     \CoAuthorsPlusRoles\set_author_on_post($post, 'guest-author-through-ui', 'author');
     $updated_coauthors = \CoAuthorsPlusRoles\get_coauthors($post, array('author_role' => 'author'));
     $this->assertcount(2, $updated_coauthors);
     // Removing an individual coauthor using `remove_coauthor_from_post()`
     \CoAuthorsPlusRoles\remove_author_from_post($post, 'guest-author-through-ui');
     $updated_coauthors = \CoAuthorsPlusRoles\get_coauthors($post, array('author_role' => 'any'));
     $this->assertcount(1, $updated_coauthors);
 }