-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest_Co_Authors.php
More file actions
114 lines (91 loc) · 3.99 KB
/
Test_Co_Authors.php
File metadata and controls
114 lines (91 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* Tests for Co_Authors storage class.
*
* @package MultiAuthorPosts
*/
namespace MultiAuthorPosts\Tests;
use MultiAuthorPosts\Co_Authors;
use WP_UnitTestCase;
/**
* @covers \MultiAuthorPosts\Co_Authors
*/
class Test_Co_Authors extends WP_UnitTestCase {
private int $post_id;
private int $author_id;
private int $user_id;
public function set_up(): void {
parent::set_up();
$this->author_id = self::factory()->user->create( array( 'role' => 'author' ) );
$this->user_id = self::factory()->user->create( array( 'role' => 'subscriber' ) );
$this->post_id = self::factory()->post->create( array( 'post_author' => $this->author_id ) );
}
public function test_get_co_authors_returns_empty_array_by_default(): void {
$this->assertSame( array(), Co_Authors::get_co_authors( $this->post_id ) );
}
public function test_add_co_author_rejects_nonexistent_user(): void {
$this->assertFalse( Co_Authors::add_co_author( $this->post_id, 999999 ) );
$this->assertNotContains( 999999, Co_Authors::get_co_authors( $this->post_id ) );
}
public function test_add_co_author_returns_true(): void {
$this->assertTrue( Co_Authors::add_co_author( $this->post_id, $this->user_id ) );
}
public function test_add_co_author_stores_user_id(): void {
Co_Authors::add_co_author( $this->post_id, $this->user_id );
$this->assertContains( $this->user_id, Co_Authors::get_co_authors( $this->post_id ) );
}
public function test_add_co_author_is_idempotent(): void {
Co_Authors::add_co_author( $this->post_id, $this->user_id );
Co_Authors::add_co_author( $this->post_id, $this->user_id );
$this->assertCount( 1, Co_Authors::get_co_authors( $this->post_id ) );
}
public function test_post_author_is_not_stored_as_co_author(): void {
Co_Authors::add_co_author( $this->post_id, $this->author_id );
$this->assertNotContains( $this->author_id, Co_Authors::get_co_authors( $this->post_id ) );
}
public function test_remove_co_author(): void {
Co_Authors::add_co_author( $this->post_id, $this->user_id );
Co_Authors::remove_co_author( $this->post_id, $this->user_id );
$this->assertNotContains( $this->user_id, Co_Authors::get_co_authors( $this->post_id ) );
}
public function test_is_co_author_false_before_add(): void {
$this->assertFalse( Co_Authors::is_co_author( $this->post_id, $this->user_id ) );
}
public function test_is_co_author_true_after_add(): void {
Co_Authors::add_co_author( $this->post_id, $this->user_id );
$this->assertTrue( Co_Authors::is_co_author( $this->post_id, $this->user_id ) );
}
public function test_is_co_author_false_after_remove(): void {
Co_Authors::add_co_author( $this->post_id, $this->user_id );
Co_Authors::remove_co_author( $this->post_id, $this->user_id );
$this->assertFalse( Co_Authors::is_co_author( $this->post_id, $this->user_id ) );
}
public function test_get_co_author_data_returns_enriched_array(): void {
Co_Authors::add_co_author( $this->post_id, $this->user_id );
$data = Co_Authors::get_co_author_data( $this->post_id );
$this->assertCount( 1, $data );
$this->assertSame( $this->user_id, $data[0]['id'] );
$this->assertArrayHasKey( 'name', $data[0] );
$this->assertArrayHasKey( 'avatar', $data[0] );
}
public function test_previous_author_becomes_co_author_on_author_change(): void {
Co_Authors::init();
$author_b = self::factory()->user->create( array( 'role' => 'author' ) );
wp_update_post(
array(
'ID' => $this->post_id,
'post_author' => $author_b,
)
);
$this->assertTrue( Co_Authors::is_co_author( $this->post_id, $this->author_id ) );
}
public function test_multiple_co_authors(): void {
$second_user = self::factory()->user->create( array( 'role' => 'subscriber' ) );
Co_Authors::add_co_author( $this->post_id, $this->user_id );
Co_Authors::add_co_author( $this->post_id, $second_user );
$co_authors = Co_Authors::get_co_authors( $this->post_id );
$this->assertCount( 2, $co_authors );
$this->assertContains( $this->user_id, $co_authors );
$this->assertContains( $second_user, $co_authors );
}
}