WordPress.org

Make WordPress Core

Changeset 360 in tests


Ignore:
Timestamp:
05/23/11 11:11:08 (3 years ago)
Author:
westi
Message:

Add a simple test script for sending real emails with wp_mail to check that it actually works too.

File:
1 copied

Legend:

Unmodified
Added
Removed
  • wp-mail-real-test.php

    r358 r360  
    11<?php 
    22/** 
    3  * wp-test.php 
     3 * wp-mail-real-test.php 
    44 *  
    5  * WordPress Testrunner 
    6  *  
    7  * Example: 
    8  *  
    9  * # php wp-test.php -l 
    10  *  
     5 * Test script for wp_mail with real addresses. 
    116 */ 
    127 
    138// parse options 
    14 $options = 'v:t:r:sflndq'; 
     9$options = 'v:r:d'; 
    1510if (is_callable('getopt')) { 
    1611    $opts = getopt($options); 
     
    2116 
    2217define('DIR_TESTROOT', realpath(dirname(__FILE__))); 
    23 if (!defined('DIR_TESTCASE')) { 
    24     define('DIR_TESTCASE', './wp-testcase'); 
    25 } 
    26 if (!defined('DIR_TESTDATA')) 
    27     define('DIR_TESTDATA', './wp-testdata'); 
     18 
    2819define('TEST_WP', true); 
    29 define('TEST_MU', (@$opts['v'] == 'mu')); 
    30 define('TEST_SKIP_KNOWN_BUGS', array_key_exists('s', $opts)); 
    31 define('TEST_FORCE_KNOWN_BUGS', array_key_exists('f', $opts)); 
    3220define('WP_DEBUG', array_key_exists('d', $opts) ); 
    33 define('SAVEQUERIES', array_key_exists('q', $opts) ); 
    3421 
    3522if (!empty($opts['r'])) 
     
    4532ini_set('display_errors', true); 
    4633 
    47 require_once(DIR_TESTROOT.'/wp-testlib/base.php'); 
    4834require_once(DIR_TESTROOT.'/wp-testlib/utils.php'); 
    4935 
    5036// configure wp 
    51  
    5237require_once(DIR_TESTROOT.'/wp-config.php'); 
    5338define('ABSPATH', realpath(DIR_WP).'/'); 
    54  
    55 if (!defined('DIR_TESTPLUGINS')) 
    56     define('DIR_TESTPLUGINS', './wp-plugins'); 
    57  
    5839 
    5940// install wp 
     
    6243define('WP_USER_EMAIL', rand_str().'@example.com'); 
    6344 
    64  
    6545// initialize wp 
    6646define('WP_INSTALLING', 1); 
     
    6848require_once(ABSPATH.'wp-settings.php'); 
    6949 
    70 // override stuff 
    71 require_once(DIR_TESTROOT.'/wp-testlib/mock-mailer.php'); 
    72 $GLOBALS['phpmailer'] = new MockPHPMailer(); 
    73  
    74 // Allow tests to override wp_die 
    75 add_filter( 'wp_die_handler', '_wp_die_handler_filter' ); 
    76  
    7750drop_tables(); 
    7851 
    79 if (TEST_MU) 
    80     require_once(ABSPATH.'wp-admin/upgrade-functions.php'); 
    81 else 
    82     require_once(ABSPATH.'wp-admin/includes/upgrade.php'); 
     52require_once(ABSPATH.'wp-admin/includes/upgrade.php'); 
    8353wp_install(WP_BLOG_TITLE, WP_USER_NAME, WP_USER_EMAIL, true); 
    84  
    85 if (TEST_MU) { 
    86         // wp-settings.php would normally init this stuff, but that doesn't work because we've 
    87         // only just installed 
    88         $GLOBALS['blog_id'] = 1; 
    89         $GLOBALS['wpdb']->blogid = 1; 
    90         $GLOBALS['current_blog'] = $GLOBALS['wpdb']->get_results('SELECT * from wp_blogs where blog_id=1'); 
    91 } 
    9254 
    9355// make sure we're installed 
    9456assert(true == is_blog_installed()); 
    9557 
    96 // include plugins for testing, if any 
    97 if (is_dir(DIR_TESTPLUGINS)) { 
    98     $plugins = glob(realpath(DIR_TESTPLUGINS).'/*.php'); 
    99     foreach ($plugins as $plugin) 
    100         include_once($plugin); 
    101 } 
    102  
    103 // needed for jacob's tests 
    104 ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . ABSPATH . '/wp-includes'); 
    10558define('PHPUnit_MAIN_METHOD', false); 
    10659$original_wpdb = $GLOBALS['wpdb']; 
    10760 
    108 include_once(DIR_TESTDATA . '/sample_blogs.php'); 
    109 // include all files in DIR_TESTCASE, and fetch all the WPTestCase descendents 
    110 $files = wptest_get_all_test_files(DIR_TESTCASE); 
    111 foreach ($files as $file) { 
    112     require_once($file); 
     61// hide warnings during testing, since that's the normal WP behaviour 
     62if ( !WP_DEBUG ) { 
     63    error_reporting(E_ALL ^ E_NOTICE); 
    11364} 
    114 $classes = wptest_get_all_test_cases(); 
    11565 
    116 // some of jacob's tests clobber the wpdb object, so restore it 
    117 $GLOBALS['wpdb'] = $original_wpdb; 
     66$to = "To <wp.mail.testing@gmail.com>"; 
     67$from = "From <wp.mail.testing+from@gmail.com>"; 
     68$cc = "CC <wp.mail.testing+cc@gmail.com>"; 
     69$bcc = "BCC <wp.mail.testing+bcc@gmail.com>"; 
     70$subject = "RFC2822 Testing"; 
     71$message = "My RFC822 Test Message"; 
     72$headers[] = "From: {$from}"; 
     73$headers[] = "CC: {$cc}"; 
    11874 
    119 if ( isset($opts['l']) ) { 
    120     wptest_listall_testcases($classes); 
    121 } else { 
    122     do_action('test_start'); 
    123      
    124     // hide warnings during testing, since that's the normal WP behaviour 
    125     if ( !WP_DEBUG ) { 
    126         error_reporting(E_ALL ^ E_NOTICE); 
    127     } 
    128     // run the tests and print the results 
    129     list ($result, $printer) = wptest_run_tests($classes, isset($opts['t']) ? $opts['t'] : array()); 
    130     wptest_print_result($printer,$result); 
    131 } 
    132 if ( !isset($opts['n']) ) { 
    133     // clean up the database 
    134     drop_tables(); 
    135 } 
     75$_SERVER['SERVER_NAME'] = 'example.com'; 
     76wp_mail( $to, $subject, $message, $headers ); 
     77 
     78$headers = array(); 
     79$subject = "RFC2822 Testing 2"; 
     80$message = "My RFC822 Test Message 2"; 
     81$to = "To <wp.mail.testing+to@gmail.com>"; 
     82$headers[] = "BCC: {$bcc}"; 
     83wp_mail( '', $subject, $message, $headers ); 
     84echo "Test emails sent!\n" 
    13685?> 
Note: See TracChangeset for help on using the changeset viewer.