| 1 | <?php |
| 2 | |
| 3 | if ( is_multisite() ) : |
| 4 | |
| 5 | /** |
| 6 | * Test get_site() wrapper of WP_Site in multisite. |
| 7 | * |
| 8 | * @group ms-site |
| 9 | * @group multisite |
| 10 | * @ticket 39944 |
| 11 | */ |
| 12 | class Tests_Multisite_Capabilties extends WP_UnitTestCase { |
| 13 | |
| 14 | public function test_capabilities_after_site_option_change() { |
| 15 | global $wp_roles, $wpdb; |
| 16 | |
| 17 | // Set user option |
| 18 | update_site_option( 'add_new_users', true ); |
| 19 | |
| 20 | // Create a blog |
| 21 | $blog1_id = self::factory()->blog->create(); |
| 22 | |
| 23 | // Change network caps option |
| 24 | update_site_option( 'add_new_users', false ); |
| 25 | |
| 26 | $blog2_id = self::factory()->blog->create(); |
| 27 | |
| 28 | $admin_user = self::factory()->user->create( array( |
| 29 | 'user_login' => 'testadmin', |
| 30 | 'role' => 'administrator', |
| 31 | ) ); |
| 32 | |
| 33 | add_user_to_blog( $blog1_id, $admin_user, 'administrator' ); |
| 34 | add_user_to_blog( $blog2_id, $admin_user, 'administrator' ); |
| 35 | |
| 36 | // Perform actions as admin |
| 37 | wp_set_current_user( $admin_user ); |
| 38 | |
| 39 | // Get roles & caps for blog1 |
| 40 | update_site_option( 'add_new_users', true ); |
| 41 | switch_to_blog( $blog1_id ); |
| 42 | $blog1_roles = $wp_roles->roles; |
| 43 | $db1_roles = get_option( $wpdb->get_blog_prefix() . 'user_roles' ); |
| 44 | |
| 45 | $this->assertTrue( current_user_can( 'list_users' ), 'Current user should be able to list users' ); |
| 46 | |
| 47 | restore_current_blog(); |
| 48 | |
| 49 | // Get roles & caps for blog2 |
| 50 | update_site_option( 'add_new_users', false ); |
| 51 | switch_to_blog( $blog2_id ); |
| 52 | $blog2_roles = $wp_roles->roles; |
| 53 | $db2_roles = get_option( $wpdb->get_blog_prefix() . 'user_roles' ); |
| 54 | |
| 55 | $this->assertFalse( current_user_can( 'list_users' ), 'Current user should not be able to list users' ); |
| 56 | |
| 57 | restore_current_blog(); |
| 58 | |
| 59 | foreach ( $blog1_roles as $key => $role ) { |
| 60 | $this->assertNotEquals( $db1_roles[ $key ]['capabilities'], $db2_roles[ $key ]['capabilities'], "Stored capabilities for {$role['name']} on Blog 1 match those on Blog 2" ); |
| 61 | $this->assertEquals( $role['capabilities'], $blog2_roles[ $key ]['capabilities'], "Generated capabilities for {$role['name']} on Blog 1 do not match those on Blog 2" ); |
| 62 | } |
| 63 | |
| 64 | // Clean up |
| 65 | wpmu_delete_blog( $blog1_id, true ); |
| 66 | wpmu_delete_blog( $blog2_id, true ); |
| 67 | } |
| 68 | |
| 69 | } |
| 70 | |
| 71 | endif; |