| 80 | |
| 81 | /** |
| 82 | * @group bp_moderate |
| 83 | */ |
| 84 | public function test_administrator_can_bp_moderate() { |
| 85 | $u = self::factory()->user->create( |
| 86 | array( |
| 87 | 'role' => 'administrator', |
| 88 | ) |
| 89 | ); |
| 90 | |
| 91 | $this->set_current_user( $u ); |
| 92 | |
| 93 | $this->assertTrue( bp_current_user_can( 'bp_moderate' ), 'Administrator can `bp_moderate` on default WordPress config' ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @group bp_moderate |
| 98 | */ |
| 99 | public function test_role_with_manage_options_cap_can_bp_moderate() { |
| 100 | add_role( 'random_role', 'Random Role', array( 'manage_options' => true ) ); |
| 101 | |
| 102 | // Reset roles. |
| 103 | wp_roles()->init_roles(); |
| 104 | |
| 105 | $u = self::factory()->user->create( |
| 106 | array( |
| 107 | 'role' => 'random_role', |
| 108 | ) |
| 109 | ); |
| 110 | |
| 111 | $this->set_current_user( $u ); |
| 112 | |
| 113 | $this->assertTrue( bp_current_user_can( 'bp_moderate' ), 'Users having a `manage_options` cap into their role can `bp_moderate`' ); |
| 114 | |
| 115 | remove_role( 'random_role' ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * @group bp_moderate |
| 120 | */ |
| 121 | public function test_administrator_can_bp_moderate_emails() { |
| 122 | $u1 = self::factory()->user->create( |
| 123 | array( |
| 124 | 'role' => 'administrator', |
| 125 | ) |
| 126 | ); |
| 127 | $u2 = self::factory()->user->create( |
| 128 | array( |
| 129 | 'role' => 'administrator', |
| 130 | ) |
| 131 | ); |
| 132 | |
| 133 | $this->set_current_user( $u1 ); |
| 134 | |
| 135 | $email = self::factory()->post->create( |
| 136 | array( |
| 137 | 'post_type' => 'bp-email', |
| 138 | ) |
| 139 | ); |
| 140 | |
| 141 | $this->assertTrue( current_user_can( 'edit_post', $email ), 'Administrator should be able to edit emails they created' ); |
| 142 | |
| 143 | $this->set_current_user( $u2 ); |
| 144 | |
| 145 | $this->assertTrue( current_user_can( 'edit_post', $email ), 'Administrator should be able to edit emails others created when BuddyPress is not network activated' ); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @group bp_moderate |
| 150 | */ |
| 151 | public function test_administrator_can_bp_moderate_network_activated() { |
| 152 | if ( ! is_multisite() ) { |
| 153 | $this->markTestSkipped( __METHOD__ . ' requires multisite.' ); |
| 154 | } |
| 155 | |
| 156 | $u1 = self::factory()->user->create( |
| 157 | array( |
| 158 | 'role' => 'administrator', |
| 159 | ) |
| 160 | ); |
| 161 | grant_super_admin( $u1 ); |
| 162 | |
| 163 | $u2 = self::factory()->user->create( |
| 164 | array( |
| 165 | 'role' => 'administrator', |
| 166 | ) |
| 167 | ); |
| 168 | |
| 169 | add_filter( 'bp_is_network_activated', '__return_true' ); |
| 170 | |
| 171 | // Swith & restore to reset the roles. |
| 172 | switch_to_blog( $this->blog_id ); |
| 173 | |
| 174 | $this->set_current_user( $u1 ); |
| 175 | $this->assertTrue( bp_current_user_can( 'bp_moderate' ), 'Only Super Admins can `bp_moderate` when BuddyPress is network activated' ); |
| 176 | |
| 177 | $this->set_current_user( $u2 ); |
| 178 | |
| 179 | $this->assertFalse( bp_current_user_can( 'bp_moderate' ), 'Regular Admins cannot `bp_moderate` when BuddyPress is network activated' ); |
| 180 | |
| 181 | grant_super_admin( $u2 ); |
| 182 | $this->assertTrue( bp_current_user_can( 'bp_moderate' ), 'Only Super Admins can `bp_moderate` when BuddyPress is network activated' ); |
| 183 | |
| 184 | restore_current_blog(); |
| 185 | |
| 186 | remove_filter( 'bp_is_network_activated', '__return_true' ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * @group bp_moderate |
| 191 | */ |
| 192 | public function test_administrator_can_bp_moderate_emails_network_activated() { |
| 193 | if ( ! is_multisite() ) { |
| 194 | $this->markTestSkipped( __METHOD__ . ' requires multisite.' ); |
| 195 | } |
| 196 | |
| 197 | $u1 = self::factory()->user->create( |
| 198 | array( |
| 199 | 'role' => 'administrator', |
| 200 | ) |
| 201 | ); |
| 202 | grant_super_admin( $u1 ); |
| 203 | |
| 204 | $u2 = self::factory()->user->create( |
| 205 | array( |
| 206 | 'role' => 'administrator', |
| 207 | ) |
| 208 | ); |
| 209 | |
| 210 | $email = self::factory()->post->create( |
| 211 | array( |
| 212 | 'post_type' => 'bp-email', |
| 213 | ) |
| 214 | ); |
| 215 | |
| 216 | add_filter( 'bp_is_network_activated', '__return_true' ); |
| 217 | |
| 218 | // Swith & restore to reset the roles. |
| 219 | switch_to_blog( $this->blog_id ); |
| 220 | restore_current_blog(); |
| 221 | |
| 222 | $this->set_current_user( $u1 ); |
| 223 | $this->assertTrue( current_user_can( 'edit_post', $email ), 'Super Admins should be able to edit emails they created' ); |
| 224 | |
| 225 | $this->set_current_user( $u2 ); |
| 226 | $this->assertFalse( current_user_can( 'edit_post', $email ), 'Administrator should not be able to edit emails others created when BuddyPress is network activated' ); |
| 227 | |
| 228 | grant_super_admin( $u2 ); |
| 229 | $this->assertTrue( current_user_can( 'edit_post', $email ), 'Super Admins should be able to edit emails others created' ); |
| 230 | |
| 231 | remove_filter( 'bp_is_network_activated', '__return_true' ); |
| 232 | } |