Make WordPress Core

Ticket #33968: 33968.diff

File 33968.diff, 1.9 KB (added by boonebgorges, 9 years ago)
  • tests/phpunit/includes/testcase-xmlrpc.php

    diff --git tests/phpunit/includes/testcase-xmlrpc.php tests/phpunit/includes/testcase-xmlrpc.php
    index bf0f22a..5f80e83 100644
    include_once(ABSPATH . WPINC . '/class-wp-xmlrpc-server.php'); 
    55
    66class WP_XMLRPC_UnitTestCase extends WP_UnitTestCase {
    77        protected $myxmlrpcserver;
     8        protected $username;
     9        protected $password;
     10        protected $role;
     11        protected $user_id;
     12        protected $mocked_auth = false;
    813
    914        function setUp() {
    1015                parent::setUp();
    1116
    1217                add_filter( 'pre_option_enable_xmlrpc', '__return_true' );
    1318
     19                add_filter( 'authenticate', array( $this, 'mock_authenticate' ), 10, 3 );
    1420                $this->myxmlrpcserver = new wp_xmlrpc_server();
    1521        }
    1622
    1723        function tearDown() {
     24                remove_filter( 'authenticate', array( $this, 'mock_authenticate' ), 10, 3 );
     25
     26                if ( $this->mocked_auth ) {
     27                        add_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 );
     28                        add_filter( 'authenticate', 'wp_authenticate_spam_check', 99 );
     29                }
     30
    1831                remove_filter( 'pre_option_enable_xmlrpc', '__return_true' );
    1932                $this->remove_added_uploads();
    2033
    2134                parent::tearDown();
    2235        }
    2336
     37        public function mock_authenticate( $user, $username, $password ) {
     38                if ( $username === $this->username && $password === $this->password ) {
     39                        $user = new WP_User( $this->user_id );
     40
     41                        // Don't let WP do its native authentication checks.
     42                        remove_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 );
     43                        remove_filter( 'authenticate', 'wp_authenticate_spam_check', 99 );
     44
     45                        $this->mocked_auth = true;
     46                }
     47
     48                return $user;
     49        }
     50
    2451        protected function make_user_by_role( $role ) {
    25                 return $this->factory->user->create( array(
     52                $this->user_id = $this->factory->user->create( array(
    2653                        'user_login' => $role,
    2754                        'user_pass'  => $role,
    2855                        'role'       => $role
    2956                ));
     57
     58                $this->username = $role;
     59                $this->password = $role;
     60                $this->role = $role;
     61
     62                return $this->user_id;
    3063        }
    3164}