Make WordPress Core

Ticket #29712: 0001-clone-the-current_user-before-the-first-unit-test-an.patch

File 0001-clone-the-current_user-before-the-first-unit-test-an.patch, 2.3 KB (added by mnelson4, 11 years ago)

git patch

  • tests/phpunit/includes/testcase.php

    From ae7f39bab844fed61acb020b4cc18a8ea6339047 Mon Sep 17 00:00:00 2001
    From: Michael Nelson <michael@eventespresso.com>
    Date: Fri, 19 Sep 2014 12:17:24 -0700
    Subject: [PATCH] clone the current_user before the first unit test, and then
     restore the current_user to be a clone of it after each
     unit test
    
    ---
     tests/phpunit/includes/testcase.php | 17 ++++++++++++++---
     1 file changed, 14 insertions(+), 3 deletions(-)
    
    diff --git a/tests/phpunit/includes/testcase.php b/tests/phpunit/includes/testcase.php
    index 1e8ea11..5e58d2e 100644
    a b class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    1313
    1414        protected static $hooks_saved = array();
    1515        protected static $ignore_files;
     16        /**
     17         * The current user, as it was before the first unit test.
     18         * After each unit test $current_user gets restored to this.
     19         * @var WP_User
     20         */
     21        protected static $_original_current_user;
    1622
    1723        /**
    1824         * @var WP_UnitTest_Factory
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    2026        protected $factory;
    2127
    2228        function setUp() {
     29                global $wpdb, $current_user;
    2330                set_time_limit(0);
    2431
    2532                if ( ! self::$ignore_files ) {
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    3037                        $this->_backup_hooks();
    3138                }
    3239
    33                 global $wpdb;
     40                if ( ! self::$_original_current_user ){
     41                        self::$_original_current_user = clone $current_user;
     42                }
     43
    3444                $wpdb->suppress_errors = false;
    3545                $wpdb->show_errors = true;
    3646                $wpdb->db_connect();
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    4353        }
    4454
    4555        function tearDown() {
    46                 global $wpdb, $wp_query, $post;
     56                global $wpdb, $wp_query, $post, $current_user;
    4757                $this->expectedDeprecated();
    4858                $wpdb->query( 'ROLLBACK' );
    4959                if ( is_multisite() ) {
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    5868                remove_filter( 'query', array( $this, '_drop_temporary_tables' ) );
    5969                remove_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) );
    6070                $this->_restore_hooks();
     71                $current_user = clone self::$_original_current_user;
    6172        }
    6273
    6374        function clean_up_global_scope() {
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    103114                        }
    104115                }
    105116        }
    106        
     117
    107118        function flush_cache() {
    108119                global $wp_object_cache;
    109120                $wp_object_cache->group_ops = array();