Changeset 42343 for trunk/tests/phpunit/tests/user/capabilities.php
- Timestamp:
- 11/30/2017 11:09:33 PM (8 years ago)
- File:
-
- 1 edited
-
trunk/tests/phpunit/tests/user/capabilities.php (modified) (59 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/user/capabilities.php
r41635 r42343 9 9 class Tests_User_Capabilities extends WP_UnitTestCase { 10 10 11 protected static $users = array(11 protected static $users = array( 12 12 'administrator' => null, 13 13 'editor' => null, … … 19 19 20 20 public static function wpSetUpBeforeClass( $factory ) { 21 self::$users = array(21 self::$users = array( 22 22 'administrator' => $factory->user->create_and_get( array( 'role' => 'administrator' ) ), 23 23 'editor' => $factory->user->create_and_get( array( 'role' => 'editor' ) ), … … 40 40 // we want to make sure we're testing against the db, not just in-memory data 41 41 // this will flush everything and reload it from the db 42 unset( $GLOBALS['wp_user_roles']);42 unset( $GLOBALS['wp_user_roles'] ); 43 43 global $wp_roles; 44 44 $wp_roles = new WP_Roles(); … … 490 490 } 491 491 } 492 493 492 } 494 493 … … 610 609 public function test_user_can_edit_comment_on_own_post( $role, $can_edit_own_post ) { 611 610 $owner = self::$users[ $role ]; 612 $post = self::factory()->post->create_and_get( array( 613 'post_author' => $owner->ID, 614 ) ); 615 $comment = self::factory()->comment->create_and_get( array( 616 'comment_post_ID' => $post->ID, 617 ) ); 611 $post = self::factory()->post->create_and_get( 612 array( 613 'post_author' => $owner->ID, 614 ) 615 ); 616 $comment = self::factory()->comment->create_and_get( 617 array( 618 'comment_post_ID' => $post->ID, 619 ) 620 ); 618 621 619 622 $owner_can_edit = user_can( $owner->ID, 'edit_comment', $comment->comment_ID ); … … 629 632 public function test_user_can_edit_comment_on_others_post( $role, $can_edit_others_post ) { 630 633 $user = self::$users[ $role ]; 631 $owner = self::factory()->user->create_and_get( array( 632 'role' => 'editor', 633 ) ); 634 $post = self::factory()->post->create_and_get( array( 635 'post_author' => $owner->ID, 636 ) ); 637 $comment = self::factory()->comment->create_and_get( array( 638 'comment_post_ID' => $post->ID, 639 ) ); 634 $owner = self::factory()->user->create_and_get( 635 array( 636 'role' => 'editor', 637 ) 638 ); 639 $post = self::factory()->post->create_and_get( 640 array( 641 'post_author' => $owner->ID, 642 ) 643 ); 644 $comment = self::factory()->comment->create_and_get( 645 array( 646 'comment_post_ID' => $post->ID, 647 ) 648 ); 640 649 641 650 $user_can_edit = user_can( $user->ID, 'edit_comment', $comment->comment_ID ); … … 644 653 645 654 public function data_user_with_role_can_edit_own_post() { 646 $data = array();647 $caps = $this->getPrimitiveCapsAndRoles();655 $data = array(); 656 $caps = $this->getPrimitiveCapsAndRoles(); 648 657 649 658 foreach ( self::$users as $role => $null ) { … … 658 667 659 668 public function data_user_with_role_can_edit_others_posts() { 660 $data = array();661 $caps = $this->getPrimitiveCapsAndRoles();669 $data = array(); 670 $caps = $this->getPrimitiveCapsAndRoles(); 662 671 663 672 foreach ( self::$users as $role => $null ) { … … 685 694 } 686 695 687 $this->assertTrue( $user->has_cap( 'start_a_fire' ), "Super admins should have all custom capabilities");688 $this->assertTrue( user_can( $user, 'start_a_fire' ), "Super admins should have all custom capabilities");696 $this->assertTrue( $user->has_cap( 'start_a_fire' ), 'Super admins should have all custom capabilities' ); 697 $this->assertTrue( user_can( $user, 'start_a_fire' ), 'Super admins should have all custom capabilities' ); 689 698 690 699 $this->assertFalse( $user->has_cap( 'do_not_allow' ), 'Super Admins should not have the do_not_allow capability' ); … … 701 710 702 711 // make sure the user is valid 703 $this->assertTrue( $user->exists(), "User does not exist");712 $this->assertTrue( $user->exists(), 'User does not exist' ); 704 713 705 714 // make sure the role name is correct 706 $this->assertEquals( array(), $user->roles, "User should not have any roles");715 $this->assertEquals( array(), $user->roles, 'User should not have any roles' ); 707 716 708 717 $caps = $this->getAllCapsAndRoles(); … … 719 728 720 729 // make sure the user is valid 721 $this->assertTrue( $user->exists(), "User does not exist");730 $this->assertTrue( $user->exists(), 'User does not exist' ); 722 731 723 732 $user->add_role( 'contributor' ); … … 757 766 758 767 // make sure the user is valid 759 $this->assertTrue( $user->exists(), "User does not exist");768 $this->assertTrue( $user->exists(), 'User does not exist' ); 760 769 761 770 // make sure the role name is correct … … 779 788 global $wp_roles; 780 789 781 $role_name = 'janitor';790 $role_name = 'janitor'; 782 791 $expected_caps = array( 783 792 'edit_posts' => true, … … 794 803 795 804 // make sure the user is valid 796 $this->assertTrue( $user->exists(), "User does not exist");805 $this->assertTrue( $user->exists(), 'User does not exist' ); 797 806 798 807 // make sure the role name is correct … … 823 832 global $wp_roles; 824 833 $role_name = 'janitor'; 825 add_role( $role_name, 'Janitor', array( 'level_1'=>true) );834 add_role( $role_name, 'Janitor', array( 'level_1' => true ) ); 826 835 $this->_flush_roles(); 827 $this->assertTrue( $wp_roles->is_role( $role_name) );836 $this->assertTrue( $wp_roles->is_role( $role_name ) ); 828 837 829 838 // assign a user to that role … … 831 840 832 841 // now add a cap to the role 833 $wp_roles->add_cap( $role_name, 'sweep_floor');842 $wp_roles->add_cap( $role_name, 'sweep_floor' ); 834 843 $this->_flush_roles(); 835 844 836 $user = new WP_User( $id);837 $this->assertTrue( $user->exists(), "Problem getting user $id");838 $this->assertEquals( array($role_name), $user->roles);845 $user = new WP_User( $id ); 846 $this->assertTrue( $user->exists(), "Problem getting user $id" ); 847 $this->assertEquals( array( $role_name ), $user->roles ); 839 848 840 849 // the user should have all the above caps 841 $this->assertTrue( $user->has_cap($role_name));842 $this->assertTrue( $user->has_cap('level_1'));843 $this->assertTrue( $user->has_cap('sweep_floor'));850 $this->assertTrue( $user->has_cap( $role_name ) ); 851 $this->assertTrue( $user->has_cap( 'level_1' ) ); 852 $this->assertTrue( $user->has_cap( 'sweep_floor' ) ); 844 853 845 854 // shouldn't have any other caps … … 852 861 853 862 // clean up 854 remove_role( $role_name);863 remove_role( $role_name ); 855 864 $this->_flush_roles(); 856 $this->assertFalse( $wp_roles->is_role($role_name));865 $this->assertFalse( $wp_roles->is_role( $role_name ) ); 857 866 858 867 } … … 863 872 global $wp_roles; 864 873 $role_name = 'janitor'; 865 add_role( $role_name, 'Janitor', array('level_1'=>true, 'sweep_floor'=>true, 'polish_doorknobs'=>true) ); 874 add_role( 875 $role_name, 'Janitor', array( 876 'level_1' => true, 877 'sweep_floor' => true, 878 'polish_doorknobs' => true, 879 ) 880 ); 866 881 $this->_flush_roles(); 867 $this->assertTrue( $wp_roles->is_role( $role_name) );882 $this->assertTrue( $wp_roles->is_role( $role_name ) ); 868 883 869 884 // assign a user to that role … … 871 886 872 887 // now remove a cap from the role 873 $wp_roles->remove_cap( $role_name, 'polish_doorknobs');888 $wp_roles->remove_cap( $role_name, 'polish_doorknobs' ); 874 889 $this->_flush_roles(); 875 890 876 $user = new WP_User( $id);877 $this->assertTrue( $user->exists(), "Problem getting user $id");878 $this->assertEquals( array($role_name), $user->roles);891 $user = new WP_User( $id ); 892 $this->assertTrue( $user->exists(), "Problem getting user $id" ); 893 $this->assertEquals( array( $role_name ), $user->roles ); 879 894 880 895 // the user should have all the above caps 881 $this->assertTrue( $user->has_cap($role_name));882 $this->assertTrue( $user->has_cap('level_1'));883 $this->assertTrue( $user->has_cap('sweep_floor'));896 $this->assertTrue( $user->has_cap( $role_name ) ); 897 $this->assertTrue( $user->has_cap( 'level_1' ) ); 898 $this->assertTrue( $user->has_cap( 'sweep_floor' ) ); 884 899 885 900 // shouldn't have the removed cap 886 $this->assertFalse( $user->has_cap('polish_doorknobs'));901 $this->assertFalse( $user->has_cap( 'polish_doorknobs' ) ); 887 902 888 903 // clean up 889 remove_role( $role_name);904 remove_role( $role_name ); 890 905 $this->_flush_roles(); 891 $this->assertFalse( $wp_roles->is_role($role_name));906 $this->assertFalse( $wp_roles->is_role( $role_name ) ); 892 907 893 908 } … … 901 916 902 917 // user 1 has an extra capability 903 $user_1 = new WP_User( $id_1);904 $this->assertTrue( $user_1->exists(), "Problem getting user $id_1");905 $user_1->add_cap( 'publish_posts');918 $user_1 = new WP_User( $id_1 ); 919 $this->assertTrue( $user_1->exists(), "Problem getting user $id_1" ); 920 $user_1->add_cap( 'publish_posts' ); 906 921 907 922 // re-fetch both users from the db 908 $user_1 = new WP_User( $id_1);909 $this->assertTrue( $user_1->exists(), "Problem getting user $id_1");910 $user_2 = new WP_User( $id_2);911 $this->assertTrue( $user_2->exists(), "Problem getting user $id_2");923 $user_1 = new WP_User( $id_1 ); 924 $this->assertTrue( $user_1->exists(), "Problem getting user $id_1" ); 925 $user_2 = new WP_User( $id_2 ); 926 $this->assertTrue( $user_2->exists(), "Problem getting user $id_2" ); 912 927 913 928 // make sure they're both still contributors 914 $this->assertEquals( array('contributor'), $user_1->roles);915 $this->assertEquals( array('contributor'), $user_2->roles);929 $this->assertEquals( array( 'contributor' ), $user_1->roles ); 930 $this->assertEquals( array( 'contributor' ), $user_2->roles ); 916 931 917 932 // check the extra cap on both users 918 $this->assertTrue( $user_1->has_cap('publish_posts'));919 $this->assertFalse( $user_2->has_cap('publish_posts'));933 $this->assertTrue( $user_1->has_cap( 'publish_posts' ) ); 934 $this->assertFalse( $user_2->has_cap( 'publish_posts' ) ); 920 935 921 936 // make sure the other caps didn't get messed up … … 939 954 940 955 // user 1 has an extra capability 941 $user_1 = new WP_User( $id_1);942 $this->assertTrue( $user_1->exists(), "Problem getting user $id_1");943 $user_1->add_cap( 'publish_posts');956 $user_1 = new WP_User( $id_1 ); 957 $this->assertTrue( $user_1->exists(), "Problem getting user $id_1" ); 958 $user_1->add_cap( 'publish_posts' ); 944 959 945 960 // now remove the extra cap 946 $user_1->remove_cap( 'publish_posts');961 $user_1->remove_cap( 'publish_posts' ); 947 962 948 963 // re-fetch both users from the db 949 $user_1 = new WP_User( $id_1);950 $this->assertTrue( $user_1->exists(), "Problem getting user $id_1");951 $user_2 = new WP_User( $id_2);952 $this->assertTrue( $user_2->exists(), "Problem getting user $id_2");964 $user_1 = new WP_User( $id_1 ); 965 $this->assertTrue( $user_1->exists(), "Problem getting user $id_1" ); 966 $user_2 = new WP_User( $id_2 ); 967 $this->assertTrue( $user_2->exists(), "Problem getting user $id_2" ); 953 968 954 969 // make sure they're both still contributors 955 $this->assertEquals( array('contributor'), $user_1->roles);956 $this->assertEquals( array('contributor'), $user_2->roles);970 $this->assertEquals( array( 'contributor' ), $user_1->roles ); 971 $this->assertEquals( array( 'contributor' ), $user_2->roles ); 957 972 958 973 // check the removed cap on both users 959 $this->assertFalse( $user_1->has_cap('publish_posts'));960 $this->assertFalse( $user_2->has_cap('publish_posts'));974 $this->assertFalse( $user_1->has_cap( 'publish_posts' ) ); 975 $this->assertFalse( $user_2->has_cap( 'publish_posts' ) ); 961 976 962 977 } … … 966 981 967 982 // user starts as an author 968 $id = self::factory()->user->create( array( 'role' => 'author' ) );969 $user = new WP_User( $id);970 $this->assertTrue( $user->exists(), "Problem getting user $id");983 $id = self::factory()->user->create( array( 'role' => 'author' ) ); 984 $user = new WP_User( $id ); 985 $this->assertTrue( $user->exists(), "Problem getting user $id" ); 971 986 972 987 // author = user level 2 … … 974 989 975 990 // they get promoted to editor - level should get bumped to 7 976 $user->set_role( 'editor');991 $user->set_role( 'editor' ); 977 992 $this->assertEquals( 7, $user->user_level ); 978 993 979 994 // demoted to contributor - level is reduced to 1 980 $user->set_role( 'contributor');995 $user->set_role( 'contributor' ); 981 996 $this->assertEquals( 1, $user->user_level ); 982 997 983 998 // if they have two roles, user_level should be the max of the two 984 $user->add_role( 'editor');985 $this->assertEquals( array('contributor', 'editor'), $user->roles);999 $user->add_role( 'editor' ); 1000 $this->assertEquals( array( 'contributor', 'editor' ), $user->roles ); 986 1001 $this->assertEquals( 7, $user->user_level ); 987 1002 } … … 989 1004 function test_user_remove_all_caps() { 990 1005 // user starts as an author 991 $id = self::factory()->user->create( array( 'role' => 'author' ) );992 $user = new WP_User( $id);993 $this->assertTrue( $user->exists(), "Problem getting user $id");1006 $id = self::factory()->user->create( array( 'role' => 'author' ) ); 1007 $user = new WP_User( $id ); 1008 $this->assertTrue( $user->exists(), "Problem getting user $id" ); 994 1009 995 1010 // add some extra capabilities 996 $user->add_cap( 'make_coffee');997 $user->add_cap( 'drink_coffee');1011 $user->add_cap( 'make_coffee' ); 1012 $user->add_cap( 'drink_coffee' ); 998 1013 999 1014 // re-fetch 1000 $user = new WP_User( $id);1001 $this->assertTrue( $user->exists(), "Problem getting user $id");1002 1003 $this->assertTrue( $user->has_cap('make_coffee'));1004 $this->assertTrue( $user->has_cap('drink_coffee'));1015 $user = new WP_User( $id ); 1016 $this->assertTrue( $user->exists(), "Problem getting user $id" ); 1017 1018 $this->assertTrue( $user->has_cap( 'make_coffee' ) ); 1019 $this->assertTrue( $user->has_cap( 'drink_coffee' ) ); 1005 1020 1006 1021 // all caps are removed … … 1008 1023 1009 1024 // re-fetch 1010 $user = new WP_User( $id);1011 $this->assertTrue( $user->exists(), "Problem getting user $id");1025 $user = new WP_User( $id ); 1026 $this->assertTrue( $user->exists(), "Problem getting user $id" ); 1012 1027 1013 1028 // all capabilities for the user should be gone … … 1017 1032 1018 1033 // the extra capabilities should be gone 1019 $this->assertFalse( $user->has_cap('make_coffee'));1020 $this->assertFalse( $user->has_cap('drink_coffee'));1034 $this->assertFalse( $user->has_cap( 'make_coffee' ) ); 1035 $this->assertFalse( $user->has_cap( 'drink_coffee' ) ); 1021 1036 1022 1037 // user level should be empty 1023 1038 $this->assertEmpty( $user->user_level ); 1024 1039 1025 1026 1040 } 1027 1041 … … 1033 1047 1034 1048 // make a post 1035 $post = self::factory()->post->create( array( 'post_author' => $author->ID, 'post_type' => 'post' ) ); 1049 $post = self::factory()->post->create( 1050 array( 1051 'post_author' => $author->ID, 1052 'post_type' => 'post', 1053 ) 1054 ); 1036 1055 1037 1056 // the author of the post 1038 $this->assertTrue( $author->exists(), "Problem getting user $author->ID");1057 $this->assertTrue( $author->exists(), "Problem getting user $author->ID" ); 1039 1058 1040 1059 // add some other users 1041 $admin = new WP_User( self::factory()->user->create( array( 'role' => 'administrator' ) ) );1042 $author_2 = new WP_User( self::factory()->user->create( array( 'role' => 'author' ) ) );1043 $editor = new WP_User( self::factory()->user->create( array( 'role' => 'editor' ) ) );1060 $admin = new WP_User( self::factory()->user->create( array( 'role' => 'administrator' ) ) ); 1061 $author_2 = new WP_User( self::factory()->user->create( array( 'role' => 'author' ) ) ); 1062 $editor = new WP_User( self::factory()->user->create( array( 'role' => 'editor' ) ) ); 1044 1063 $contributor = new WP_User( self::factory()->user->create( array( 'role' => 'contributor' ) ) ); 1045 1064 1046 1065 // administrators, editors and the post owner can edit it 1047 $this->assertTrue( $admin->has_cap('edit_post', $post));1048 $this->assertTrue( $author->has_cap('edit_post', $post));1049 $this->assertTrue( $editor->has_cap('edit_post', $post));1066 $this->assertTrue( $admin->has_cap( 'edit_post', $post ) ); 1067 $this->assertTrue( $author->has_cap( 'edit_post', $post ) ); 1068 $this->assertTrue( $editor->has_cap( 'edit_post', $post ) ); 1050 1069 // other authors and contributors can't 1051 $this->assertFalse( $author_2->has_cap('edit_post', $post));1052 $this->assertFalse( $contributor->has_cap('edit_post', $post));1070 $this->assertFalse( $author_2->has_cap( 'edit_post', $post ) ); 1071 $this->assertFalse( $contributor->has_cap( 'edit_post', $post ) ); 1053 1072 1054 1073 // administrators, editors and the post owner can delete it 1055 $this->assertTrue( $admin->has_cap('delete_post', $post));1056 $this->assertTrue( $author->has_cap('delete_post', $post));1057 $this->assertTrue( $editor->has_cap('delete_post', $post));1074 $this->assertTrue( $admin->has_cap( 'delete_post', $post ) ); 1075 $this->assertTrue( $author->has_cap( 'delete_post', $post ) ); 1076 $this->assertTrue( $editor->has_cap( 'delete_post', $post ) ); 1058 1077 // other authors and contributors can't 1059 $this->assertFalse( $author_2->has_cap('delete_post', $post));1060 $this->assertFalse( $contributor->has_cap('delete_post', $post));1078 $this->assertFalse( $author_2->has_cap( 'delete_post', $post ) ); 1079 $this->assertFalse( $contributor->has_cap( 'delete_post', $post ) ); 1061 1080 1062 1081 // administrators, editors, and authors can publish it 1063 $this->assertTrue( $admin->has_cap('publish_post', $post));1064 $this->assertTrue( $author->has_cap('publish_post', $post));1065 $this->assertTrue( $editor->has_cap('publish_post', $post));1066 $this->assertTrue( $author_2->has_cap('publish_post', $post));1082 $this->assertTrue( $admin->has_cap( 'publish_post', $post ) ); 1083 $this->assertTrue( $author->has_cap( 'publish_post', $post ) ); 1084 $this->assertTrue( $editor->has_cap( 'publish_post', $post ) ); 1085 $this->assertTrue( $author_2->has_cap( 'publish_post', $post ) ); 1067 1086 // contributors can't 1068 $this->assertFalse( $contributor->has_cap('publish_post', $post));1087 $this->assertFalse( $contributor->has_cap( 'publish_post', $post ) ); 1069 1088 1070 1089 register_post_type( 'something', array( 'capabilities' => array( 'edit_posts' => 'draw_somethings' ) ) ); … … 1073 1092 $this->assertEquals( 'draw_somethings', $something->cap->create_posts ); 1074 1093 1075 register_post_type( 'something', array( 'capabilities' => 1076 array( 'edit_posts' => 'draw_somethings', 'create_posts' => 'create_somethings' ) ) ); 1094 register_post_type( 1095 'something', array( 1096 'capabilities' => 1097 array( 1098 'edit_posts' => 'draw_somethings', 1099 'create_posts' => 'create_somethings', 1100 ), 1101 ) 1102 ); 1077 1103 $something = get_post_type_object( 'something' ); 1078 1104 $this->assertEquals( 'draw_somethings', $something->cap->edit_posts ); … … 1081 1107 1082 1108 // Test meta authorization callbacks 1083 if ( function_exists( 'register_meta' ) ) {1084 $this->assertTrue( $admin->has_cap( 'edit_post_meta', $post) );1085 $this->assertTrue( $admin->has_cap( 'add_post_meta', $post) );1086 $this->assertTrue( $admin->has_cap( 'delete_post_meta', $post) );1087 1088 $this->assertFalse( $admin->has_cap( 'edit_post_meta', $post, '_protected') );1089 $this->assertFalse( $admin->has_cap( 'add_post_meta', $post, '_protected') );1090 $this->assertFalse( $admin->has_cap( 'delete_post_meta', $post, '_protected') );1109 if ( function_exists( 'register_meta' ) ) { 1110 $this->assertTrue( $admin->has_cap( 'edit_post_meta', $post ) ); 1111 $this->assertTrue( $admin->has_cap( 'add_post_meta', $post ) ); 1112 $this->assertTrue( $admin->has_cap( 'delete_post_meta', $post ) ); 1113 1114 $this->assertFalse( $admin->has_cap( 'edit_post_meta', $post, '_protected' ) ); 1115 $this->assertFalse( $admin->has_cap( 'add_post_meta', $post, '_protected' ) ); 1116 $this->assertFalse( $admin->has_cap( 'delete_post_meta', $post, '_protected' ) ); 1091 1117 1092 1118 register_meta( 'post', '_protected', array( $this, '_meta_filter' ), array( $this, '_meta_yes_you_can' ) ); 1093 $this->assertTrue( $admin->has_cap( 'edit_post_meta', $post, '_protected') );1094 $this->assertTrue( $admin->has_cap( 'add_post_meta', $post, '_protected') );1095 $this->assertTrue( $admin->has_cap( 'delete_post_meta', $post, '_protected') );1096 1097 $this->assertTrue( $admin->has_cap( 'edit_post_meta', $post, 'not_protected') );1098 $this->assertTrue( $admin->has_cap( 'add_post_meta', $post, 'not_protected') );1099 $this->assertTrue( $admin->has_cap( 'delete_post_meta', $post, 'not_protected') );1119 $this->assertTrue( $admin->has_cap( 'edit_post_meta', $post, '_protected' ) ); 1120 $this->assertTrue( $admin->has_cap( 'add_post_meta', $post, '_protected' ) ); 1121 $this->assertTrue( $admin->has_cap( 'delete_post_meta', $post, '_protected' ) ); 1122 1123 $this->assertTrue( $admin->has_cap( 'edit_post_meta', $post, 'not_protected' ) ); 1124 $this->assertTrue( $admin->has_cap( 'add_post_meta', $post, 'not_protected' ) ); 1125 $this->assertTrue( $admin->has_cap( 'delete_post_meta', $post, 'not_protected' ) ); 1100 1126 1101 1127 register_meta( 'post', 'not_protected', array( $this, '_meta_filter' ), array( $this, '_meta_no_you_cant' ) ); 1102 $this->assertFalse( $admin->has_cap( 'edit_post_meta', $post, 'not_protected') );1103 $this->assertFalse( $admin->has_cap( 'add_post_meta', $post, 'not_protected') );1104 $this->assertFalse( $admin->has_cap( 'delete_post_meta', $post, 'not_protected') );1128 $this->assertFalse( $admin->has_cap( 'edit_post_meta', $post, 'not_protected' ) ); 1129 $this->assertFalse( $admin->has_cap( 'add_post_meta', $post, 'not_protected' ) ); 1130 $this->assertFalse( $admin->has_cap( 'delete_post_meta', $post, 'not_protected' ) ); 1105 1131 } 1106 1132 } … … 1116 1142 function test_authorless_post( $status ) { 1117 1143 // Make a post without an author 1118 $post = self::factory()->post->create( array( 'post_author' => 0, 'post_type' => 'post', 'post_status' => $status ) ); 1144 $post = self::factory()->post->create( 1145 array( 1146 'post_author' => 0, 1147 'post_type' => 'post', 1148 'post_status' => $status, 1149 ) 1150 ); 1119 1151 1120 1152 // Add an editor and contributor 1121 $editor = self::$users['editor'];1153 $editor = self::$users['editor']; 1122 1154 $contributor = self::$users['contributor']; 1123 1155 … … 1146 1178 1147 1179 // create_posts isn't a real cap. 1148 $this->assertFalse( $admin->has_cap('create_posts'));1149 $this->assertFalse( $author->has_cap('create_posts'));1150 $this->assertFalse( $editor->has_cap('create_posts'));1151 $this->assertFalse( $contributor->has_cap('create_posts'));1152 $this->assertFalse( $subscriber->has_cap('create_posts'));1180 $this->assertFalse( $admin->has_cap( 'create_posts' ) ); 1181 $this->assertFalse( $author->has_cap( 'create_posts' ) ); 1182 $this->assertFalse( $editor->has_cap( 'create_posts' ) ); 1183 $this->assertFalse( $contributor->has_cap( 'create_posts' ) ); 1184 $this->assertFalse( $subscriber->has_cap( 'create_posts' ) ); 1153 1185 1154 1186 register_post_type( 'foobar' ); … … 1157 1189 $this->assertEquals( 'edit_posts', $cap->create_posts ); 1158 1190 1159 $this->assertTrue( $admin->has_cap( $cap->create_posts ));1160 $this->assertTrue( $author->has_cap( $cap->create_posts ));1161 $this->assertTrue( $editor->has_cap( $cap->create_posts ));1162 $this->assertTrue( $contributor->has_cap( $cap->create_posts ));1163 $this->assertFalse( $subscriber->has_cap( $cap->create_posts ));1191 $this->assertTrue( $admin->has_cap( $cap->create_posts ) ); 1192 $this->assertTrue( $author->has_cap( $cap->create_posts ) ); 1193 $this->assertTrue( $editor->has_cap( $cap->create_posts ) ); 1194 $this->assertTrue( $contributor->has_cap( $cap->create_posts ) ); 1195 $this->assertFalse( $subscriber->has_cap( $cap->create_posts ) ); 1164 1196 1165 1197 _unregister_post_type( 'foobar' ); … … 1171 1203 $this->assertEquals( 'edit_foobars', $cap->create_posts ); 1172 1204 1173 $this->assertFalse( $admin->has_cap( $cap->create_posts ));1174 $this->assertFalse( $author->has_cap( $cap->create_posts ));1175 $this->assertFalse( $editor->has_cap( $cap->create_posts ));1176 $this->assertFalse( $contributor->has_cap( $cap->create_posts ));1177 $this->assertFalse( $subscriber->has_cap( $cap->create_posts ));1205 $this->assertFalse( $admin->has_cap( $cap->create_posts ) ); 1206 $this->assertFalse( $author->has_cap( $cap->create_posts ) ); 1207 $this->assertFalse( $editor->has_cap( $cap->create_posts ) ); 1208 $this->assertFalse( $contributor->has_cap( $cap->create_posts ) ); 1209 $this->assertFalse( $subscriber->has_cap( $cap->create_posts ) ); 1178 1210 1179 1211 // Add edit_foobars primitive cap to a user. 1180 1212 $admin->add_cap( 'edit_foobars', true ); 1181 1213 $admin = new WP_User( $admin->ID ); 1182 $this->assertTrue( $admin->has_cap( $cap->create_posts ));1183 $this->assertFalse( $author->has_cap( $cap->create_posts ));1184 $this->assertFalse( $editor->has_cap( $cap->create_posts ));1185 $this->assertFalse( $contributor->has_cap( $cap->create_posts ));1186 $this->assertFalse( $subscriber->has_cap( $cap->create_posts ));1214 $this->assertTrue( $admin->has_cap( $cap->create_posts ) ); 1215 $this->assertFalse( $author->has_cap( $cap->create_posts ) ); 1216 $this->assertFalse( $editor->has_cap( $cap->create_posts ) ); 1217 $this->assertFalse( $contributor->has_cap( $cap->create_posts ) ); 1218 $this->assertFalse( $subscriber->has_cap( $cap->create_posts ) ); 1187 1219 1188 1220 $admin->remove_cap( 'edit_foobars' ); … … 1208 1240 1209 1241 // make a page 1210 $page = self::factory()->post->create( array( 'post_author' => $author->ID, 'post_type' => 'page' ) ); 1242 $page = self::factory()->post->create( 1243 array( 1244 'post_author' => $author->ID, 1245 'post_type' => 'page', 1246 ) 1247 ); 1211 1248 1212 1249 // the author of the page 1213 $this->assertTrue( $author->exists(), "Problem getting user " . $author->ID);1250 $this->assertTrue( $author->exists(), 'Problem getting user ' . $author->ID ); 1214 1251 1215 1252 // add some other users 1216 $admin = self::$users['administrator'];1217 $author_2 = new WP_User( self::factory()->user->create( array( 'role' => 'author' ) ) );1218 $editor = self::$users['editor'];1253 $admin = self::$users['administrator']; 1254 $author_2 = new WP_User( self::factory()->user->create( array( 'role' => 'author' ) ) ); 1255 $editor = self::$users['editor']; 1219 1256 $contributor = self::$users['contributor']; 1220 1257 1221 1258 // administrators, editors and the post owner can edit it 1222 $this->assertTrue( $admin->has_cap('edit_page', $page));1223 $this->assertTrue( $editor->has_cap('edit_page', $page));1259 $this->assertTrue( $admin->has_cap( 'edit_page', $page ) ); 1260 $this->assertTrue( $editor->has_cap( 'edit_page', $page ) ); 1224 1261 // other authors and contributors can't 1225 $this->assertFalse( $author->has_cap('edit_page', $page));1226 $this->assertFalse( $author_2->has_cap('edit_page', $page));1227 $this->assertFalse( $contributor->has_cap('edit_page', $page));1262 $this->assertFalse( $author->has_cap( 'edit_page', $page ) ); 1263 $this->assertFalse( $author_2->has_cap( 'edit_page', $page ) ); 1264 $this->assertFalse( $contributor->has_cap( 'edit_page', $page ) ); 1228 1265 1229 1266 // administrators, editors and the post owner can delete it 1230 $this->assertTrue( $admin->has_cap('delete_page', $page));1231 $this->assertTrue( $editor->has_cap('delete_page', $page));1267 $this->assertTrue( $admin->has_cap( 'delete_page', $page ) ); 1268 $this->assertTrue( $editor->has_cap( 'delete_page', $page ) ); 1232 1269 // other authors and contributors can't 1233 $this->assertFalse( $author->has_cap('delete_page', $page));1234 $this->assertFalse( $author_2->has_cap('delete_page', $page));1235 $this->assertFalse( $contributor->has_cap('delete_page', $page));1270 $this->assertFalse( $author->has_cap( 'delete_page', $page ) ); 1271 $this->assertFalse( $author_2->has_cap( 'delete_page', $page ) ); 1272 $this->assertFalse( $contributor->has_cap( 'delete_page', $page ) ); 1236 1273 } 1237 1274 … … 1259 1296 foreach ( $expected as $meta_cap => $primitive_cap ) { 1260 1297 $caps = map_meta_cap( $tax->cap->$meta_cap, $user->ID ); 1261 $this->assertEquals( array( 1262 $primitive_cap, 1263 ), $caps, "Meta cap: {$meta_cap}" ); 1298 $this->assertEquals( 1299 array( 1300 $primitive_cap, 1301 ), $caps, "Meta cap: {$meta_cap}" 1302 ); 1264 1303 } 1265 1304 } … … 1277 1316 $tax = get_taxonomy( $taxonomy ); 1278 1317 $user = self::$users['administrator']; 1279 $term = self::factory()->term->create_and_get( array( 1280 'taxonomy' => $taxonomy, 1281 ) ); 1318 $term = self::factory()->term->create_and_get( 1319 array( 1320 'taxonomy' => $taxonomy, 1321 ) 1322 ); 1282 1323 1283 1324 update_option( "default_{$taxonomy}", $term->term_id ); … … 1298 1339 1299 1340 $tax = get_taxonomy( $taxonomy ); 1300 $term = self::factory()->term->create_and_get( array( 1301 'taxonomy' => $taxonomy, 1302 ) ); 1341 $term = self::factory()->term->create_and_get( 1342 array( 1343 'taxonomy' => $taxonomy, 1344 ) 1345 ); 1303 1346 1304 1347 foreach ( self::$users as $role => $user ) { … … 1347 1390 ); 1348 1391 $taxonomy = 'custom_cap_taxo'; 1349 register_taxonomy( $taxonomy, 'post', array( 1350 'capabilities' => $expected, 1351 ) ); 1392 register_taxonomy( 1393 $taxonomy, 'post', array( 1394 'capabilities' => $expected, 1395 ) 1396 ); 1352 1397 1353 1398 $tax = get_taxonomy( $taxonomy ); … … 1356 1401 foreach ( $expected as $meta_cap => $primitive_cap ) { 1357 1402 $caps = map_meta_cap( $tax->cap->$meta_cap, $user->ID ); 1358 $this->assertEquals( array( 1359 $primitive_cap, 1360 ), $caps, "Meta cap: {$meta_cap}" ); 1403 $this->assertEquals( 1404 array( 1405 $primitive_cap, 1406 ), $caps, "Meta cap: {$meta_cap}" 1407 ); 1361 1408 } 1362 1409 } … … 1390 1437 1391 1438 $author->add_cap( 'foo', false ); 1392 $this->assertTrue ( isset( $author->caps['foo'] ) );1439 $this->assertTrue( isset( $author->caps['foo'] ) ); 1393 1440 $this->assertFalse( user_can( $author->ID, 'foo' ) ); 1394 1441 1395 1442 $author->remove_cap( 'foo' ); 1396 $this->assertFalse ( isset( $author->caps['foo'] ) );1443 $this->assertFalse( isset( $author->caps['foo'] ) ); 1397 1444 $this->assertFalse( user_can( $author->ID, 'foo' ) ); 1398 1445 } … … 1413 1460 global $wpdb; 1414 1461 1415 $user = self::$users['administrator'];1462 $user = self::$users['administrator']; 1416 1463 $old_uid = get_current_user_id(); 1417 1464 wp_set_current_user( $user->ID ); … … 1440 1487 function test_borked_current_user_can_for_blog() { 1441 1488 $orig_blog_id = get_current_blog_id(); 1442 $blog_id = self::factory()->blog->create();1489 $blog_id = self::factory()->blog->create(); 1443 1490 1444 1491 $this->_nullify_current_user(); … … 1498 1545 */ 1499 1546 function test_multisite_administrator_can_not_edit_users() { 1500 $user = self::$users['administrator'];1547 $user = self::$users['administrator']; 1501 1548 $other_user = self::$users['subscriber']; 1502 1549 … … 1515 1562 public function test_only_admins_and_super_admins_can_remove_users() { 1516 1563 if ( is_multisite() ) { 1517 $this->assertTrue( user_can( self::$super_admin->ID, 'remove_user', self::$users['subscriber']->ID ) );1564 $this->assertTrue( user_can( self::$super_admin->ID, 'remove_user', self::$users['subscriber']->ID ) ); 1518 1565 } 1519 1566 1520 1567 $this->assertTrue( user_can( self::$users['administrator']->ID, 'remove_user', self::$users['subscriber']->ID ) ); 1521 1568 1522 $this->assertFalse( user_can( self::$users['editor']->ID, 'remove_user', self::$users['subscriber']->ID ) );1523 $this->assertFalse( user_can( self::$users['author']->ID, 'remove_user', self::$users['subscriber']->ID ) );1524 $this->assertFalse( user_can( self::$users['contributor']->ID, 'remove_user', self::$users['subscriber']->ID ) );1525 $this->assertFalse( user_can( self::$users['subscriber']->ID, 'remove_user', self::$users['subscriber']->ID ) );1569 $this->assertFalse( user_can( self::$users['editor']->ID, 'remove_user', self::$users['subscriber']->ID ) ); 1570 $this->assertFalse( user_can( self::$users['author']->ID, 'remove_user', self::$users['subscriber']->ID ) ); 1571 $this->assertFalse( user_can( self::$users['contributor']->ID, 'remove_user', self::$users['subscriber']->ID ) ); 1572 $this->assertFalse( user_can( self::$users['subscriber']->ID, 'remove_user', self::$users['subscriber']->ID ) ); 1526 1573 } 1527 1574 … … 1530 1577 */ 1531 1578 public function test_only_super_admins_can_delete_users_on_multisite() { 1532 $this->assertTrue( user_can( self::$super_admin->ID, 'delete_user', self::$users['subscriber']->ID ) );1579 $this->assertTrue( user_can( self::$super_admin->ID, 'delete_user', self::$users['subscriber']->ID ) ); 1533 1580 1534 1581 $this->assertFalse( user_can( self::$users['administrator']->ID, 'delete_user', self::$users['subscriber']->ID ) ); 1535 $this->assertFalse( user_can( self::$users['editor']->ID, 'delete_user', self::$users['subscriber']->ID ) );1536 $this->assertFalse( user_can( self::$users['author']->ID, 'delete_user', self::$users['subscriber']->ID ) );1537 $this->assertFalse( user_can( self::$users['contributor']->ID, 'delete_user', self::$users['subscriber']->ID ) );1538 $this->assertFalse( user_can( self::$users['subscriber']->ID, 'delete_user', self::$users['subscriber']->ID ) );1582 $this->assertFalse( user_can( self::$users['editor']->ID, 'delete_user', self::$users['subscriber']->ID ) ); 1583 $this->assertFalse( user_can( self::$users['author']->ID, 'delete_user', self::$users['subscriber']->ID ) ); 1584 $this->assertFalse( user_can( self::$users['contributor']->ID, 'delete_user', self::$users['subscriber']->ID ) ); 1585 $this->assertFalse( user_can( self::$users['subscriber']->ID, 'delete_user', self::$users['subscriber']->ID ) ); 1539 1586 } 1540 1587 … … 1545 1592 $this->assertTrue( user_can( self::$users['administrator']->ID, 'delete_user', self::$users['subscriber']->ID ) ); 1546 1593 1547 $this->assertFalse( user_can( self::$users['editor']->ID, 'delete_user', self::$users['subscriber']->ID ) );1548 $this->assertFalse( user_can( self::$users['author']->ID, 'delete_user', self::$users['subscriber']->ID ) );1549 $this->assertFalse( user_can( self::$users['contributor']->ID, 'delete_user', self::$users['subscriber']->ID ) );1550 $this->assertFalse( user_can( self::$users['subscriber']->ID, 'delete_user', self::$users['subscriber']->ID ) );1594 $this->assertFalse( user_can( self::$users['editor']->ID, 'delete_user', self::$users['subscriber']->ID ) ); 1595 $this->assertFalse( user_can( self::$users['author']->ID, 'delete_user', self::$users['subscriber']->ID ) ); 1596 $this->assertFalse( user_can( self::$users['contributor']->ID, 'delete_user', self::$users['subscriber']->ID ) ); 1597 $this->assertFalse( user_can( self::$users['subscriber']->ID, 'delete_user', self::$users['subscriber']->ID ) ); 1551 1598 } 1552 1599 1553 1600 public function test_only_admins_and_super_admins_can_promote_users() { 1554 1601 if ( is_multisite() ) { 1555 $this->assertTrue( user_can( self::$super_admin->ID, 'promote_user', self::$users['subscriber']->ID ) );1602 $this->assertTrue( user_can( self::$super_admin->ID, 'promote_user', self::$users['subscriber']->ID ) ); 1556 1603 } 1557 1604 1558 1605 $this->assertTrue( user_can( self::$users['administrator']->ID, 'promote_user', self::$users['subscriber']->ID ) ); 1559 1606 1560 $this->assertFalse( user_can( self::$users['editor']->ID, 'promote_user', self::$users['subscriber']->ID ) );1561 $this->assertFalse( user_can( self::$users['author']->ID, 'promote_user', self::$users['subscriber']->ID ) );1562 $this->assertFalse( user_can( self::$users['contributor']->ID, 'promote_user', self::$users['subscriber']->ID ) );1563 $this->assertFalse( user_can( self::$users['subscriber']->ID, 'promote_user', self::$users['subscriber']->ID ) );1607 $this->assertFalse( user_can( self::$users['editor']->ID, 'promote_user', self::$users['subscriber']->ID ) ); 1608 $this->assertFalse( user_can( self::$users['author']->ID, 'promote_user', self::$users['subscriber']->ID ) ); 1609 $this->assertFalse( user_can( self::$users['contributor']->ID, 'promote_user', self::$users['subscriber']->ID ) ); 1610 $this->assertFalse( user_can( self::$users['subscriber']->ID, 'promote_user', self::$users['subscriber']->ID ) ); 1564 1611 } 1565 1612 … … 1573 1620 1574 1621 // Give them a scheduled post 1575 $post = $this->factory->post->create_and_get( array( 1576 'post_author' => $contributor->ID, 1577 'post_status' => 'future', 1578 ) ); 1622 $post = $this->factory->post->create_and_get( 1623 array( 1624 'post_author' => $contributor->ID, 1625 'post_status' => 'future', 1626 ) 1627 ); 1579 1628 1580 1629 // Ensure contributor can't edit or trash the post … … 1639 1688 1640 1689 $subscriber_id = self::$users['subscriber']->ID; 1641 $editor_id = self::$users['editor']->ID;1690 $editor_id = self::$users['editor']->ID; 1642 1691 1643 1692 foreach ( array( 'delete_post', 'edit_post', 'read_post', 'publish_post' ) as $cap ) { … … 1657 1706 function test_cpt_with_page_capability_type() { 1658 1707 1659 register_post_type( 'page_capability', array( 1660 'capability_type' => 'page', 1661 ) ); 1708 register_post_type( 1709 'page_capability', array( 1710 'capability_type' => 'page', 1711 ) 1712 ); 1662 1713 1663 1714 $cpt = get_post_type_object( 'page_capability' ); … … 1674 1725 $this->assertFalse( user_can( $contributor->ID, $cpt->cap->edit_posts ) ); 1675 1726 1676 $admin_post = self::factory()->post->create_and_get( array( 1677 'post_author' => $admin->ID, 1678 'post_type' => 'page_capability', 1679 ) ); 1727 $admin_post = self::factory()->post->create_and_get( 1728 array( 1729 'post_author' => $admin->ID, 1730 'post_type' => 'page_capability', 1731 ) 1732 ); 1680 1733 1681 1734 $this->assertTrue( user_can( $admin->ID, 'edit_post', $admin_post->ID ) ); … … 1684 1737 $this->assertFalse( user_can( $contributor->ID, 'edit_post', $admin_post->ID ) ); 1685 1738 1686 $author_post = self::factory()->post->create_and_get( array( 1687 'post_author' => $author->ID, 1688 'post_type' => 'page_capability', 1689 ) ); 1739 $author_post = self::factory()->post->create_and_get( 1740 array( 1741 'post_author' => $author->ID, 1742 'post_type' => 'page_capability', 1743 ) 1744 ); 1690 1745 1691 1746 $this->assertTrue( user_can( $admin->ID, 'edit_post', $author_post->ID ) ); … … 1709 1764 1710 1765 // Special cases for link manager and unfiltered uploads: 1711 $this->assertFalse( current_user_can( 'manage_links' ), "Non-logged-in user should not have the manage_links capability");1712 $this->assertFalse( current_user_can( 'unfiltered_upload' ), "Non-logged-in user should not have the unfiltered_upload capability");1713 1714 $this->assertFalse( current_user_can( 'start_a_fire' ), "Non-logged-in user should not have a custom capability");1715 $this->assertFalse( current_user_can( 'do_not_allow' ), "Non-logged-in user should not have the do_not_allow capability");1766 $this->assertFalse( current_user_can( 'manage_links' ), 'Non-logged-in user should not have the manage_links capability' ); 1767 $this->assertFalse( current_user_can( 'unfiltered_upload' ), 'Non-logged-in user should not have the unfiltered_upload capability' ); 1768 1769 $this->assertFalse( current_user_can( 'start_a_fire' ), 'Non-logged-in user should not have a custom capability' ); 1770 $this->assertFalse( current_user_can( 'do_not_allow' ), 'Non-logged-in user should not have the do_not_allow capability' ); 1716 1771 } 1717 1772 … … 1724 1779 'role' => 'test_wp_roles_init', 1725 1780 'info' => array( 1726 'name' => 'Test WP Roles Init',1781 'name' => 'Test WP Roles Init', 1727 1782 'capabilities' => array( 'testing_magic' => true ), 1728 1783 ), … … 1844 1899 1845 1900 $this->assertFalse( user_can( self::$users['administrator']->ID, 'remove_user', self::$users['administrator']->ID ) ); 1846 $this->assertFalse( user_can( self::$users['editor']->ID, 'remove_user', self::$users['editor']->ID ) );1847 $this->assertFalse( user_can( self::$users['author']->ID, 'remove_user', self::$users['author']->ID ) );1848 $this->assertFalse( user_can( self::$users['contributor']->ID, 'remove_user', self::$users['contributor']->ID ) );1849 $this->assertFalse( user_can( self::$users['subscriber']->ID, 'remove_user', self::$users['subscriber']->ID ) );1901 $this->assertFalse( user_can( self::$users['editor']->ID, 'remove_user', self::$users['editor']->ID ) ); 1902 $this->assertFalse( user_can( self::$users['author']->ID, 'remove_user', self::$users['author']->ID ) ); 1903 $this->assertFalse( user_can( self::$users['contributor']->ID, 'remove_user', self::$users['contributor']->ID ) ); 1904 $this->assertFalse( user_can( self::$users['subscriber']->ID, 'remove_user', self::$users['subscriber']->ID ) ); 1850 1905 } 1851 1906 … … 1862 1917 1863 1918 $role_name = 'uploader'; 1864 add_role( $role_name, 'Uploader', array( 1865 'read' => true, 1866 'upload_files' => true, 1867 ) ); 1919 add_role( 1920 $role_name, 'Uploader', array( 1921 'read' => true, 1922 'upload_files' => true, 1923 ) 1924 ); 1868 1925 add_user_to_blog( $site_id, self::$users['subscriber']->ID, $role_name ); 1869 1926 … … 1888 1945 1889 1946 $role_name = 'uploader'; 1890 add_role( $role_name, 'Uploader', array( 1891 'read' => true, 1892 'upload_files' => true, 1893 ) ); 1947 add_role( 1948 $role_name, 'Uploader', array( 1949 'read' => true, 1950 'upload_files' => true, 1951 ) 1952 ); 1894 1953 add_user_to_blog( $site_id, self::$users['subscriber']->ID, $role_name ); 1895 1954 … … 1935 1994 // which is unnecessary for this test. 1936 1995 $suppress = $wpdb->suppress_errors(); 1937 $user = new WP_User( self::$users['subscriber']->ID, '', 333 );1996 $user = new WP_User( self::$users['subscriber']->ID, '', 333 ); 1938 1997 $wpdb->suppress_errors( $suppress ); 1939 1998 … … 1953 2012 1954 2013 $role_name = 'uploader'; 1955 add_role( $role_name, 'Uploader', array( 1956 'read' => true, 1957 'upload_files' => true, 1958 ) ); 2014 add_role( 2015 $role_name, 'Uploader', array( 2016 'read' => true, 2017 'upload_files' => true, 2018 ) 2019 ); 1959 2020 1960 2021 restore_current_blog(); … … 2006 2067 // which is unnecessary for this test. 2007 2068 $suppress = $wpdb->suppress_errors(); 2008 $roles = new WP_Roles( 333 );2069 $roles = new WP_Roles( 333 ); 2009 2070 $wpdb->suppress_errors( $suppress ); 2010 2071
Note: See TracChangeset
for help on using the changeset viewer.