1
0
Fork 0
mirror of https://github.com/buddypress/buddypress.git synced 2026-07-22 20:56:55 +08:00
buddypress/tests/phpunit/testcases/core/caps.php
Mathieu Viet 89a6666dc6 Fully enjoy Yoast’s PHPUnit polyfills
Using these polyfills let us use PHPUnit v9.x for our tests and add PHP 8.1 to our testing matrix. Some additional edits to our PHP unit tests suite were needed:

- Stop using PHPunit deprecated functions.
- Rename some `BP_UnitTestCase` methods to use Yoast's polyfills.
- Edit the PHP Unit test GH action and also run this action on pull requests.
- Update some composer dependencies, remove the one about `phpunit/phpunit:^7.5` and add a new composer script to use PHPUnit v9.x. 

Props renatonascalves, rafiahmedd

Closes https://github.com/buddypress/buddypress/pull/13
Fixes #8649


git-svn-id: https://buddypress.svn.wordpress.org/trunk@13314 cdf35c40-ae34-48e0-9cc9-0c9da1808c22
2022-08-13 08:58:51 +00:00

233 lines
5.9 KiB
PHP

<?php
/**
* @group core
* @group caps
*/
class BP_Tests_Core_Caps extends BP_UnitTestCase {
protected $reset_user_id;
protected $blog_id;
public function set_up() {
parent::set_up();
$this->reset_user_id = get_current_user_id();
if ( is_multisite() ) {
$this->blog_id = self::factory()->blog->create();
}
}
public function tear_down() {
parent::tear_down();
$this->set_current_user( $this->reset_user_id );
}
public function test_bp_current_user_can_should_interpret_integer_second_param_as_a_blog_id() {
if ( ! is_multisite() ) {
$this->markTestSkipped( __METHOD__ . ' requires multisite.' );
}
$b = self::factory()->blog->create();
$u = self::factory()->user->create();
$this->set_current_user( $u );
add_filter( 'user_has_cap', array( $this, 'grant_cap_foo' ), 10, 2 );
$can = bp_current_user_can( 'foo', bp_get_root_blog_id() );
$cant = bp_current_user_can( 'foo', $b );
remove_filter( 'user_has_cap', array( $this, 'grant_cap_foo' ), 10 );
$this->assertTrue( $can );
$this->assertFalse( $cant );
}
/**
* @ticket BP6501
*/
public function test_bp_current_user_can_should_respect_blog_id_passed_in_args_array() {
if ( ! is_multisite() ) {
$this->markTestSkipped( __METHOD__ . ' requires multisite.' );
}
$b = self::factory()->blog->create();
$u = self::factory()->user->create();
$this->set_current_user( $u );
add_filter( 'user_has_cap', array( $this, 'grant_cap_foo' ), 10, 2 );
$can = bp_current_user_can( 'foo', array( 'blog_id' => bp_get_root_blog_id() ) );
$cant = bp_current_user_can( 'foo', array( 'blog_id' => $b ) );
remove_filter( 'user_has_cap', array( $this, 'grant_cap_foo' ), 10 );
$this->assertTrue( $can );
$this->assertFalse( $cant );
}
public function grant_cap_foo( $allcaps, $caps ) {
if ( bp_is_root_blog() ) {
$allcaps['foo'] = 1;
}
return $allcaps;
}
public function check_cap_args( $caps, $cap, $user_id, $args ) {
$this->test_args = $args;
return $caps;
}
/**
* @group bp_moderate
*/
public function test_administrator_can_bp_moderate() {
$u = self::factory()->user->create(
array(
'role' => 'administrator',
)
);
$this->set_current_user( $u );
$this->assertTrue( bp_current_user_can( 'bp_moderate' ), 'Administrator can `bp_moderate` on default WordPress config' );
}
/**
* @group bp_moderate
*/
public function test_role_with_manage_options_cap_can_bp_moderate() {
add_role( 'random_role', 'Random Role', array( 'manage_options' => true ) );
// Reset roles.
wp_roles()->init_roles();
$u = self::factory()->user->create(
array(
'role' => 'random_role',
)
);
$this->set_current_user( $u );
$this->assertTrue( bp_current_user_can( 'bp_moderate' ), 'Users having a `manage_options` cap into their role can `bp_moderate`' );
remove_role( 'random_role' );
}
/**
* @group bp_moderate
*/
public function test_administrator_can_bp_moderate_emails() {
$u1 = self::factory()->user->create(
array(
'role' => 'administrator',
)
);
$u2 = self::factory()->user->create(
array(
'role' => 'administrator',
)
);
$this->set_current_user( $u1 );
$email = self::factory()->post->create(
array(
'post_type' => 'bp-email',
)
);
$this->assertTrue( current_user_can( 'edit_post', $email ), 'Administrator should be able to edit emails they created' );
$this->set_current_user( $u2 );
$this->assertTrue( current_user_can( 'edit_post', $email ), 'Administrator should be able to edit emails others created when BuddyPress is not network activated' );
}
/**
* @group bp_moderate
*/
public function test_administrator_can_bp_moderate_network_activated() {
if ( ! is_multisite() ) {
$this->markTestSkipped( __METHOD__ . ' requires multisite.' );
}
$u1 = self::factory()->user->create(
array(
'role' => 'administrator',
)
);
grant_super_admin( $u1 );
$u2 = self::factory()->user->create(
array(
'role' => 'administrator',
)
);
add_filter( 'bp_is_network_activated', '__return_true' );
// Swith & restore to reset the roles.
switch_to_blog( $this->blog_id );
$this->set_current_user( $u1 );
$this->assertTrue( bp_current_user_can( 'bp_moderate' ), 'Only Super Admins can `bp_moderate` when BuddyPress is network activated' );
$this->set_current_user( $u2 );
$this->assertFalse( bp_current_user_can( 'bp_moderate' ), 'Regular Admins cannot `bp_moderate` when BuddyPress is network activated' );
grant_super_admin( $u2 );
$this->assertTrue( bp_current_user_can( 'bp_moderate' ), 'Only Super Admins can `bp_moderate` when BuddyPress is network activated' );
restore_current_blog();
remove_filter( 'bp_is_network_activated', '__return_true' );
}
/**
* @group bp_moderate
*/
public function test_administrator_can_bp_moderate_emails_network_activated() {
if ( ! is_multisite() ) {
$this->markTestSkipped( __METHOD__ . ' requires multisite.' );
}
$u1 = self::factory()->user->create(
array(
'role' => 'administrator',
)
);
grant_super_admin( $u1 );
$u2 = self::factory()->user->create(
array(
'role' => 'administrator',
)
);
$email = self::factory()->post->create(
array(
'post_type' => 'bp-email',
)
);
add_filter( 'bp_is_network_activated', '__return_true' );
// Swith & restore to reset the roles.
switch_to_blog( $this->blog_id );
restore_current_blog();
$this->set_current_user( $u1 );
$this->assertTrue( current_user_can( 'edit_post', $email ), 'Super Admins should be able to edit emails they created' );
$this->set_current_user( $u2 );
$this->assertFalse( current_user_can( 'edit_post', $email ), 'Administrator should not be able to edit emails others created when BuddyPress is network activated' );
grant_super_admin( $u2 );
$this->assertTrue( current_user_can( 'edit_post', $email ), 'Super Admins should be able to edit emails others created' );
remove_filter( 'bp_is_network_activated', '__return_true' );
}
}