Make WordPress Core

Changeset 38697


Ignore:
Timestamp:
09/30/2016 10:11:47 PM (10 years ago)
Author:
johnbillion
Message:

Role/Capability: Introduce tests that assert the primitive and meta capability tests test the correct capabilities.

Fixes #38191

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/user/capabilities.php

    r38696 r38697  
    308308        }
    309309
     310        /**
     311         * Test the tests. The administrator role has all primitive capabilities, therefore the
     312         * primitive capabilitity tests can be tested by checking that the list of tested
     313         * capabilities matches those of the administrator role.
     314         *
     315         * @group capTestTests
     316         */
     317        public function testPrimitiveCapsTestsAreCorrect() {
     318                $actual   = $this->getPrimitiveCapsAndRoles();
     319                $admin    = get_role( 'administrator' );
     320                $expected = $admin->capabilities;
     321
     322                unset(
     323                        // Role names as capabilities are a special case:
     324                        $actual['administrator'],
     325                        $actual['editor'],
     326                        $actual['author'],
     327                        $actual['subscriber'],
     328                        $actual['contributor']
     329                );
     330
     331                unset(
     332                        // `manage_links` is a special case in the caps tests:
     333                        $expected['manage_links'],
     334                        // `unfiltered_upload` is a special case in the caps tests:
     335                        $expected['unfiltered_upload']
     336                );
     337
     338                $expected = array_keys( $expected );
     339                $actual   = array_keys( $actual );
     340
     341                $missing_primitive_cap_checks = array_diff( $expected, $actual );
     342                $this->assertSame( array(), $missing_primitive_cap_checks, 'These primitive capabilities are not tested' );
     343
     344                $incorrect_primitive_cap_checks = array_diff( $actual, $expected );
     345                $this->assertSame( array(), $incorrect_primitive_cap_checks, 'These capabilities are not primitive' );
     346        }
     347
     348        /**
     349         * Test the tests. All meta capabilities should have a condition in the `map_meta_cap()`
     350         * function that handles the capability.
     351         *
     352         * @group capTestTests
     353         */
     354        public function testMetaCapsTestsAreCorrect() {
     355                $actual = $this->getMetaCapsAndRoles();
     356                $file   = file_get_contents( ABSPATH . WPINC . '/capabilities.php' );
     357
     358                $matched = preg_match( '/^function map_meta_cap\((.*?)^\}/ms', $file, $function );
     359                $this->assertSame( 1, $matched );
     360                $this->assertNotEmpty( $function );
     361
     362                $matched = preg_match_all( '/^[\t]case \'([^\']+)/m', $function[0], $cases );
     363                $this->assertNotEmpty( $matched );
     364                $this->assertNotEmpty( $cases );
     365
     366                $expected = array_flip( $cases[1] );
     367
     368                unset(
     369                        // These primitive capabilities have a 'case' in `map_meta_cap()` but aren't meta capabilities:
     370                        $expected['unfiltered_upload'],
     371                        $expected['unfiltered_html'],
     372                        $expected['edit_files'],
     373                        $expected['edit_plugins'],
     374                        $expected['edit_themes'],
     375                        $expected['update_plugins'],
     376                        $expected['delete_plugins'],
     377                        $expected['install_plugins'],
     378                        $expected['update_themes'],
     379                        $expected['delete_themes'],
     380                        $expected['install_themes'],
     381                        $expected['update_core'],
     382                        $expected['activate_plugins'],
     383                        $expected['edit_users'],
     384                        $expected['delete_users'],
     385                        $expected['create_users'],
     386                        $expected['manage_links'],
     387                        // Singular object meta capabilities (where an object ID is passed) are not tested:
     388                        $expected['remove_user'],
     389                        $expected['promote_user'],
     390                        $expected['edit_user'],
     391                        $expected['delete_post'],
     392                        $expected['delete_page'],
     393                        $expected['edit_post'],
     394                        $expected['edit_page'],
     395                        $expected['read_post'],
     396                        $expected['read_page'],
     397                        $expected['publish_post'],
     398                        $expected['edit_post_meta'],
     399                        $expected['delete_post_meta'],
     400                        $expected['add_post_meta'],
     401                        $expected['edit_comment'],
     402                        $expected['delete_user']
     403                );
     404
     405                $expected = array_keys( $expected );
     406                $actual   = array_keys( $actual );
     407
     408                $missing_meta_cap_checks = array_diff( $expected, $actual );
     409                $this->assertSame( array(), $missing_meta_cap_checks, 'These meta capabilities are not tested' );
     410
     411                $incorrect_meta_cap_checks = array_diff( $actual, $expected );
     412                $this->assertSame( array(), $incorrect_meta_cap_checks, 'These capabilities are not meta' );
     413        }
     414
    310415        // test the default roles and caps
    311416        function test_all_roles_and_caps() {
Note: See TracChangeset for help on using the changeset viewer.