Make WordPress Core

Changeset 29860


Ignore:
Timestamp:
10/09/2014 12:57:26 AM (10 years ago)
Author:
boonebgorges
Message:

Reset post types and taxonomies before each unit test.

Registering a post type or taxonomy during a unit test causes modifications to
global variables. If the test fails to clean up these globals - either by
neglecting to call _unregister_post_type()/_unregister_taxonomy() at all or by
failing before getting a chance to do so - tests that run later in the suite
can fail, leading to much gnashing of teeth. Wiping all taxonomies and
restoring to the defaults before each test ensures that we always start with a
clean slate.

Fixes #29827.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/testcase.php

    r29503 r29860  
    3838        $this->factory = new WP_UnitTest_Factory;
    3939        $this->clean_up_global_scope();
     40        $this->reset_post_types();
     41        $this->reset_taxonomies();
    4042        $this->start_transaction();
    4143        $this->expectDeprecated();
     
    6567        $_POST = array();
    6668        $this->flush_cache();
     69    }
     70
     71    /**
     72     * Unregister existing post types and register defaults.
     73     *
     74     * Run before each test in order to clean up the global scope, in case
     75     * a test forgets to unregister a post type on its own, or fails before
     76     * it has a chance to do so.
     77     */
     78    protected function reset_post_types() {
     79        foreach ( get_post_types() as $pt ) {
     80            _unregister_post_type( $pt );
     81        }
     82        create_initial_post_types();
     83    }
     84
     85    /**
     86     * Unregister existing taxonomies and register defaults.
     87     *
     88     * Run before each test in order to clean up the global scope, in case
     89     * a test forgets to unregister a taxonomy on its own, or fails before
     90     * it has a chance to do so.
     91     */
     92    protected function reset_taxonomies() {
     93        foreach ( get_taxonomies() as $tax ) {
     94            _unregister_taxonomy( $tax );
     95        }
     96        create_initial_taxonomies();
    6797    }
    6898
Note: See TracChangeset for help on using the changeset viewer.