Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
12/12/2013 08:43:28 PM (11 years ago)
Author:
boonebgorges
Message:

Always fake $_SERVERSERVER_NAME? when invoking wp_mail() during tests

This is a blanket fix for PHP errors that otherwise arise when calling any BP
function that results in an email notification being sent.

File:
1 edited

Legend:

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

    r7497 r7664  
    1212
    1313    protected $temp_has_bp_moderate = array();
     14    protected $cached_SERVER_NAME = null;
    1415
    1516    public function setUp() {
     
    2324        global $wpdb;
    2425        $wpdb->query( "TRUNCATE TABLE {$wpdb->users}" );
     26
     27        // Fake WP mail globals, to avoid errors
     28        add_filter( 'wp_mail', array( $this, 'setUp_wp_mail' ) );
     29        add_filter( 'wp_mail_from', array( $this, 'tearDown_wp_mail' ) );
    2530
    2631        $this->factory = new BP_UnitTest_Factory;
     
    305310     * Set up globals necessary to avoid errors when using wp_mail()
    306311     */
    307     public function setUp_wp_mail() {
     312    public function setUp_wp_mail( $args ) {
     313        if ( isset( $_SERVER['SERVER_NAME'] ) ) {
     314            $this->cached_SERVER_NAME = $_SERVER['SERVER_NAME'];
     315        }
     316
    308317        $_SERVER['SERVER_NAME'] = 'example.com';
     318
     319        // passthrough
     320        return $args;
    309321    }
    310322
     
    312324     * Tear down globals set up in setUp_wp_mail()
    313325     */
    314     public function tearDown_wp_mail() {
    315         unset( $_SERVER['SERVER_NAME'] );
     326    public function tearDown_wp_mail( $args ) {
     327        if ( ! empty( $this->cached_SERVER_NAME ) ) {
     328            $_SERVER['SERVER_NAME'] = $this->cached_SERVER_NAME;
     329            unset( $this->cached_SERVER_NAME );
     330        } else {
     331            unset( $_SERVER['SERVER_NAME'] );
     332        }
     333
     334        // passthrough
     335        return $args;
    316336    }
    317337}
Note: See TracChangeset for help on using the changeset viewer.