Make WordPress Core

Changeset 38285


Ignore:
Timestamp:
08/19/2016 03:44:37 PM (8 years ago)
Author:
boonebgorges
Message:

Tests: Move some utility classes to their own files.

Props Frank Klein.
Fixes #37523.

Location:
trunk/tests/phpunit
Files:
2 edited
2 copied

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/class-basic-object.php

    r38284 r38285  
    11<?php
    2 
    3 /**
    4  * Resets various `$_SERVER` variables that can get altered during tests.
    5  */
    6 function tests_reset__SERVER() {
    7     $_SERVER['HTTP_HOST']       = WP_TESTS_DOMAIN;
    8     $_SERVER['REMOTE_ADDR']     = '127.0.0.1';
    9     $_SERVER['REQUEST_METHOD']  = 'GET';
    10     $_SERVER['REQUEST_URI']     = '';
    11     $_SERVER['SERVER_NAME']     = WP_TESTS_DOMAIN;
    12     $_SERVER['SERVER_PORT']     = '80';
    13     $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
    14 
    15     unset( $_SERVER['HTTP_REFERER'] );
    16     unset( $_SERVER['HTTPS'] );
    17 }
    18 
    19 // For adding hooks before loading WP
    20 function tests_add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
    21     global $wp_filter, $merged_filters;
    22 
    23     $idx = _test_filter_build_unique_id($tag, $function_to_add, $priority);
    24     $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
    25     unset( $merged_filters[ $tag ] );
    26     return true;
    27 }
    28 
    29 function _test_filter_build_unique_id($tag, $function, $priority) {
    30     global $wp_filter;
    31     static $filter_id_count = 0;
    32 
    33     if ( is_string($function) )
    34         return $function;
    35 
    36     if ( is_object($function) ) {
    37         // Closures are currently implemented as objects
    38         $function = array( $function, '' );
    39     } else {
    40         $function = (array) $function;
    41     }
    42 
    43     if (is_object($function[0]) ) {
    44         return spl_object_hash($function[0]) . $function[1];
    45     } else if ( is_string($function[0]) ) {
    46         // Static Calling
    47         return $function[0].$function[1];
    48     }
    49 }
    50 
    51 function _delete_all_posts() {
    52     global $wpdb;
    53 
    54     $all_posts = $wpdb->get_col("SELECT ID from {$wpdb->posts}");
    55     if ($all_posts) {
    56         foreach ($all_posts as $id)
    57             wp_delete_post( $id, true );
    58     }
    59 }
    602
    613class Basic_Object {
     
    8628    }
    8729}
    88 
    89 class Basic_Subclass extends Basic_Object {}
    90 
    91 function _wp_die_handler( $message, $title = '', $args = array() ) {
    92     if ( !$GLOBALS['_wp_die_disabled'] ) {
    93         _wp_die_handler_txt( $message, $title, $args);
    94     } else {
    95         //Ignore at our peril
    96     }
    97 }
    98 
    99 function _disable_wp_die() {
    100     $GLOBALS['_wp_die_disabled'] = true;
    101 }
    102 
    103 function _enable_wp_die() {
    104     $GLOBALS['_wp_die_disabled'] = false;
    105 }
    106 
    107 function _wp_die_handler_filter() {
    108     return '_wp_die_handler';
    109 }
    110 
    111 function _wp_die_handler_txt( $message, $title, $args ) {
    112     echo "\nwp_die called\n";
    113     echo "Message : $message\n";
    114     echo "Title : $title\n";
    115     if ( ! empty( $args ) ) {
    116         echo "Args: \n";
    117         foreach( $args as $k => $v ){
    118             echo "\t $k : $v\n";
    119         }
    120     }
    121 }
    122 
    123 /**
    124  * Set a permalink structure.
    125  *
    126  * Hooked as a callback to the 'populate_options' action, we use this function to set a permalink structure during
    127  * `wp_install()`, so that WP doesn't attempt to do a time-consuming remote request.
    128  *
    129  * @since 4.2.0
    130  */
    131 function _set_default_permalink_structure_for_tests() {
    132     update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%postname%/' );
    133 }
    134 
    135 /**
    136  * Helper used with the `upload_dir` filter to remove the /year/month sub directories from the uploads path and URL.
    137  */
    138 function _upload_dir_no_subdir( $uploads ) {
    139     $subdir = $uploads['subdir'];
    140 
    141     $uploads['subdir'] = '';
    142     $uploads['path'] = str_replace( $subdir, '', $uploads['path'] );
    143     $uploads['url'] = str_replace( $subdir, '', $uploads['url'] );
    144 
    145     return $uploads;
    146 }
    147 
    148 /**
    149  * Helper used with the `upload_dir` filter to set https upload URL.
    150  */
    151 function _upload_dir_https( $uploads ) {
    152     $uploads['url'] = str_replace( 'http://', 'https://', $uploads['url'] );
    153     $uploads['baseurl'] = str_replace( 'http://', 'https://', $uploads['baseurl'] );
    154 
    155     return $uploads;
    156 }
  • trunk/tests/phpunit/includes/class-basic-subclass.php

    r38284 r38285  
    11<?php
    22
    3 /**
    4  * Resets various `$_SERVER` variables that can get altered during tests.
    5  */
    6 function tests_reset__SERVER() {
    7     $_SERVER['HTTP_HOST']       = WP_TESTS_DOMAIN;
    8     $_SERVER['REMOTE_ADDR']     = '127.0.0.1';
    9     $_SERVER['REQUEST_METHOD']  = 'GET';
    10     $_SERVER['REQUEST_URI']     = '';
    11     $_SERVER['SERVER_NAME']     = WP_TESTS_DOMAIN;
    12     $_SERVER['SERVER_PORT']     = '80';
    13     $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
    14 
    15     unset( $_SERVER['HTTP_REFERER'] );
    16     unset( $_SERVER['HTTPS'] );
    17 }
    18 
    19 // For adding hooks before loading WP
    20 function tests_add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
    21     global $wp_filter, $merged_filters;
    22 
    23     $idx = _test_filter_build_unique_id($tag, $function_to_add, $priority);
    24     $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
    25     unset( $merged_filters[ $tag ] );
    26     return true;
    27 }
    28 
    29 function _test_filter_build_unique_id($tag, $function, $priority) {
    30     global $wp_filter;
    31     static $filter_id_count = 0;
    32 
    33     if ( is_string($function) )
    34         return $function;
    35 
    36     if ( is_object($function) ) {
    37         // Closures are currently implemented as objects
    38         $function = array( $function, '' );
    39     } else {
    40         $function = (array) $function;
    41     }
    42 
    43     if (is_object($function[0]) ) {
    44         return spl_object_hash($function[0]) . $function[1];
    45     } else if ( is_string($function[0]) ) {
    46         // Static Calling
    47         return $function[0].$function[1];
    48     }
    49 }
    50 
    51 function _delete_all_posts() {
    52     global $wpdb;
    53 
    54     $all_posts = $wpdb->get_col("SELECT ID from {$wpdb->posts}");
    55     if ($all_posts) {
    56         foreach ($all_posts as $id)
    57             wp_delete_post( $id, true );
    58     }
    59 }
    60 
    61 class Basic_Object {
    62     private $foo = 'bar';
    63 
    64     public function __get( $name ) {
    65         return $this->$name;
    66     }
    67 
    68     public function __set( $name, $value ) {
    69         return $this->$name = $value;
    70     }
    71 
    72     public function __isset( $name ) {
    73         return isset( $this->$name );
    74     }
    75 
    76     public function __unset( $name ) {
    77         unset( $this->$name );
    78     }
    79 
    80     public function __call( $name, $arguments ) {
    81         return call_user_func_array( array( $this, $name ), $arguments );
    82     }
    83 
    84     private function callMe() {
    85         return 'maybe';
    86     }
    87 }
    88 
    893class Basic_Subclass extends Basic_Object {}
    90 
    91 function _wp_die_handler( $message, $title = '', $args = array() ) {
    92     if ( !$GLOBALS['_wp_die_disabled'] ) {
    93         _wp_die_handler_txt( $message, $title, $args);
    94     } else {
    95         //Ignore at our peril
    96     }
    97 }
    98 
    99 function _disable_wp_die() {
    100     $GLOBALS['_wp_die_disabled'] = true;
    101 }
    102 
    103 function _enable_wp_die() {
    104     $GLOBALS['_wp_die_disabled'] = false;
    105 }
    106 
    107 function _wp_die_handler_filter() {
    108     return '_wp_die_handler';
    109 }
    110 
    111 function _wp_die_handler_txt( $message, $title, $args ) {
    112     echo "\nwp_die called\n";
    113     echo "Message : $message\n";
    114     echo "Title : $title\n";
    115     if ( ! empty( $args ) ) {
    116         echo "Args: \n";
    117         foreach( $args as $k => $v ){
    118             echo "\t $k : $v\n";
    119         }
    120     }
    121 }
    122 
    123 /**
    124  * Set a permalink structure.
    125  *
    126  * Hooked as a callback to the 'populate_options' action, we use this function to set a permalink structure during
    127  * `wp_install()`, so that WP doesn't attempt to do a time-consuming remote request.
    128  *
    129  * @since 4.2.0
    130  */
    131 function _set_default_permalink_structure_for_tests() {
    132     update_option( 'permalink_structure', '/%year%/%monthnum%/%day%/%postname%/' );
    133 }
    134 
    135 /**
    136  * Helper used with the `upload_dir` filter to remove the /year/month sub directories from the uploads path and URL.
    137  */
    138 function _upload_dir_no_subdir( $uploads ) {
    139     $subdir = $uploads['subdir'];
    140 
    141     $uploads['subdir'] = '';
    142     $uploads['path'] = str_replace( $subdir, '', $uploads['path'] );
    143     $uploads['url'] = str_replace( $subdir, '', $uploads['url'] );
    144 
    145     return $uploads;
    146 }
    147 
    148 /**
    149  * Helper used with the `upload_dir` filter to set https upload URL.
    150  */
    151 function _upload_dir_https( $uploads ) {
    152     $uploads['url'] = str_replace( 'http://', 'https://', $uploads['url'] );
    153     $uploads['baseurl'] = str_replace( 'http://', 'https://', $uploads['baseurl'] );
    154 
    155     return $uploads;
    156 }
  • trunk/tests/phpunit/includes/functions.php

    r36721 r38285  
    5959}
    6060
    61 class Basic_Object {
    62     private $foo = 'bar';
    63 
    64     public function __get( $name ) {
    65         return $this->$name;
    66     }
    67 
    68     public function __set( $name, $value ) {
    69         return $this->$name = $value;
    70     }
    71 
    72     public function __isset( $name ) {
    73         return isset( $this->$name );
    74     }
    75 
    76     public function __unset( $name ) {
    77         unset( $this->$name );
    78     }
    79 
    80     public function __call( $name, $arguments ) {
    81         return call_user_func_array( array( $this, $name ), $arguments );
    82     }
    83 
    84     private function callMe() {
    85         return 'maybe';
    86     }
    87 }
    88 
    89 class Basic_Subclass extends Basic_Object {}
    90 
    9161function _wp_die_handler( $message, $title = '', $args = array() ) {
    9262    if ( !$GLOBALS['_wp_die_disabled'] ) {
     
    148118/**
    149119 * Helper used with the `upload_dir` filter to set https upload URL.
    150  */ 
     120 */
    151121function _upload_dir_https( $uploads ) {
    152122    $uploads['url'] = str_replace( 'http://', 'https://', $uploads['url'] );
  • trunk/tests/phpunit/tests/basic.php

    r36856 r38285  
    11<?php
     2
     3require_once dirname( dirname( __FILE__ ) ) . '/includes/class-basic-object.php';
     4require_once dirname( dirname( __FILE__ ) ) . '/includes/class-basic-subclass.php';
     5
    26/**
    37 * just make sure the test framework is working
Note: See TracChangeset for help on using the changeset viewer.