Make WordPress Core


Ignore:
Timestamp:
10/03/2015 05:14:12 PM (10 years ago)
Author:
DrewAPicture
Message:

Tests: Introduce WP_UnitTestCase::reset_permalinks(), an attempt to DRY up logic for resetting and restoring default permalinks on setUp() and tearDown().

See #33968.

File:
1 edited

Legend:

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

    r34720 r34802  
    1616
    1717    protected $db_version;
     18
     19    public static $default_permalink_structure;
    1820
    1921    /**
     
    7173            add_filter( 'pre_option_db_version', array( $this, 'db_version' ) );
    7274        }
     75
     76        self::$default_permalink_structure = get_option( 'permalink_structure' );
     77
     78        $this->reset_permalinks();
    7379    }
    7480
     
    100106        $this->_restore_hooks();
    101107        wp_set_current_user( 0 );
     108
     109        $this->reset_permalinks( $restore = true );
    102110    }
    103111
     
    646654        return $this->db_version;
    647655    }
     656
     657    /**
     658     * Utility method that resets permalinks and flushes rewrites.
     659     *
     660     * Collects the current permalink structure and stores it in a class property for use
     661     * by sub-classes.
     662     *
     663     * @since 4.4.0
     664     *
     665     * @global WP_Rewrite $wp_rewrite
     666     *
     667     * @param bool $restore_default Optional. Whether to restore the default permalink structure.
     668     *                              Default false.
     669     */
     670    public function reset_permalinks( $restore_default = false ) {
     671        global $wp_rewrite;
     672
     673        if ( ! $restore_default ) {
     674            $wp_rewrite->init();
     675            $wp_rewrite->set_permalink_structure( '' );
     676        } else {
     677            $wp_rewrite->set_permalink_structure( self::$default_permalink_structure );
     678        }
     679
     680        $wp_rewrite->flush_rules();
     681    }
    648682}
Note: See TracChangeset for help on using the changeset viewer.