-
-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathCapabilitySyncTest.php
More file actions
209 lines (178 loc) · 5.04 KB
/
Copy pathCapabilitySyncTest.php
File metadata and controls
209 lines (178 loc) · 5.04 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* Tests for Lazy Blocks role capability synchronization lifecycle.
*/
class CapabilitySyncTest extends WP_UnitTestCase {
/**
* Original lzb_db_version option value.
*
* @var string
*/
private $original_db_version = '__missing__';
/**
* Original role capability state before each test.
*
* @var array
*/
private $original_role_caps = array();
/**
* Capability matrix expected for Lazy Blocks roles.
*
* @return array
*/
private function get_role_capability_matrix() {
return array(
'administrator' => array(
'edit_lazyblock',
'edit_lazyblocks',
'edit_other_lazyblocks',
'publish_lazyblocks',
'read_lazyblock',
'read_private_lazyblocks',
'delete_lazyblocks',
'delete_lazyblock',
),
'editor' => array(
'read_lazyblock',
'read_private_lazyblocks',
),
'author' => array(
'read_lazyblock',
'read_private_lazyblocks',
),
'contributor' => array(
'read_lazyblock',
'read_private_lazyblocks',
),
);
}
/**
* Capture the current role capability state so tests can restore it.
*/
private function capture_role_capabilities() {
foreach ( $this->get_role_capability_matrix() as $role_name => $caps ) {
$role = get_role( $role_name );
if ( ! $role ) {
continue;
}
foreach ( $caps as $capability ) {
$this->original_role_caps[ $role_name ][ $capability ] = $role->has_cap( $capability );
}
}
}
/**
* Restore the role capability state captured before each test.
*/
private function restore_role_capabilities() {
foreach ( $this->original_role_caps as $role_name => $caps ) {
$role = get_role( $role_name );
if ( ! $role ) {
continue;
}
foreach ( $caps as $capability => $has_capability ) {
if ( $has_capability ) {
$role->add_cap( $capability );
} else {
$role->remove_cap( $capability );
}
}
}
}
/**
* Remove the Lazy Blocks capabilities from all affected roles.
*/
private function remove_lazyblocks_capabilities() {
foreach ( $this->get_role_capability_matrix() as $role_name => $caps ) {
$role = get_role( $role_name );
if ( ! $role ) {
continue;
}
foreach ( $caps as $capability ) {
$role->remove_cap( $capability );
}
}
}
/**
* Assert the expected Lazy Blocks capability matrix is applied.
*/
private function assert_lazyblocks_capability_matrix_applied() {
foreach ( $this->get_role_capability_matrix() as $role_name => $caps ) {
$role = get_role( $role_name );
$this->assertNotNull( $role, sprintf( 'Role %s should exist.', $role_name ) );
foreach ( $caps as $capability ) {
$this->assertTrue(
$role->has_cap( $capability ),
sprintf( 'Role %s should have capability %s.', $role_name, $capability )
);
}
}
}
/**
* Get a migration instance without registering its constructor hooks.
*
* @return LazyBlocks_Migration
*/
private function create_migration_instance() {
$reflection = new ReflectionClass( 'LazyBlocks_Migration' );
return $reflection->newInstanceWithoutConstructor();
}
/**
* Set up test state.
*/
protected function setUp(): void {
parent::setUp();
$this->original_db_version = get_option( 'lzb_db_version', '__missing__' );
$this->capture_role_capabilities();
}
/**
* Restore test state.
*/
protected function tearDown(): void {
$this->restore_role_capabilities();
if ( '__missing__' === $this->original_db_version ) {
delete_option( 'lzb_db_version' );
} else {
update_option( 'lzb_db_version', $this->original_db_version );
}
parent::tearDown();
}
/**
* Capabilities should not be synchronized via admin_init.
*/
public function test_role_caps_are_not_synced_on_admin_init() {
$this->assertFalse(
has_action( 'admin_init', array( lazyblocks()->blocks(), 'add_role_caps' ) ),
'Role capability sync should not be hooked to admin_init.'
);
}
/**
* Existing installs should receive capabilities through the migration path.
*/
public function test_migration_syncs_role_caps_for_existing_installs() {
$this->remove_lazyblocks_capabilities();
update_option( 'lzb_db_version', '4.2.9' );
$migration = $this->create_migration_instance();
$migration->init();
$this->assert_lazyblocks_capability_matrix_applied();
$this->assertSame( LAZY_BLOCKS_VERSION, get_option( 'lzb_db_version' ) );
}
/**
* Fresh installs should receive capabilities when migrations run for the first time.
*/
public function test_migration_syncs_role_caps_for_fresh_installs() {
$this->remove_lazyblocks_capabilities();
delete_option( 'lzb_db_version' );
$migration = $this->create_migration_instance();
$migration->init();
$this->assert_lazyblocks_capability_matrix_applied();
$this->assertSame( LAZY_BLOCKS_VERSION, get_option( 'lzb_db_version' ) );
}
/**
* Fresh installs should receive capabilities during plugin activation.
*/
public function test_activation_hook_syncs_role_caps_for_fresh_installs() {
$this->remove_lazyblocks_capabilities();
lazyblocks()->activation_hook();
$this->assert_lazyblocks_capability_matrix_applied();
}
}