Make WordPress Core

source: trunk/tests/phpunit/includes/functions.php @ 37475

Last change on this file since 37475 was 36721, checked in by johnbillion, 9 years ago

Remove (or at least reduce) the need to reset common $_SERVER variables before assertions or between tests, by introducing a method which automatically resets them during test setup.

See #35954

File size: 3.8 KB
Line 
1<?php
2
3/**
4 * Resets various `$_SERVER` variables that can get altered during tests.
5 */
6function 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
20function 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
29function _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
51function _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
61class 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
89class Basic_Subclass extends Basic_Object {}
90
91function _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
99function _disable_wp_die() {
100        $GLOBALS['_wp_die_disabled'] = true;
101}
102
103function _enable_wp_die() {
104        $GLOBALS['_wp_die_disabled'] = false;
105}
106
107function _wp_die_handler_filter() {
108        return '_wp_die_handler';
109}
110
111function _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 */
131function _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 */
138function _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 */ 
151function _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}
Note: See TracBrowser for help on using the repository browser.