Changeset 53545 for trunk/tests/phpunit/includes/abstract-testcase.php
- Timestamp:
- 06/21/2022 11:12:02 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/includes/abstract-testcase.php
r53536 r53545 878 878 $this->assertNotEmpty( $sub_array, $message . ' Subitem of the array is empty.' ); 879 879 } 880 }881 882 /**883 * Helper function to convert a single-level array containing text strings to a named data provider.884 *885 * The value of the data set will also be used as the name of the data set.886 *887 * Typical usage of this method:888 *889 * public function data_provider_for_test_name() {890 * $array = array(891 * 'value1',892 * 'value2',893 * );894 *895 * return $this->text_array_to_dataprovider( $array );896 * }897 *898 * The returned result will look like:899 *900 * array(901 * 'value1' => array( 'value1' ),902 * 'value2' => array( 'value2' ),903 * )904 *905 * @since 6.1.0906 *907 * @param array $input Input array.908 * @return array Array which is usable as a test data provider with named data sets.909 */910 public static function text_array_to_dataprovider( $input ) {911 $data = array();912 913 foreach ( $input as $value ) {914 if ( ! is_string( $value ) ) {915 throw new Exception(916 'All values in the input array should be text strings. Fix the input data.'917 );918 }919 920 if ( isset( $data[ $value ] ) ) {921 throw new Exception(922 "Attempting to add a duplicate data set for value $value to the data provider. Fix the input data."923 );924 }925 926 $data[ $value ] = array( $value );927 }928 929 return $data;930 }931 932 /**933 * Sets the global state to as if a given URL has been requested.934 *935 * This sets:936 * - The super globals.937 * - The globals.938 * - The query variables.939 * - The main query.940 *941 * @since 3.5.0942 *943 * @param string $url The URL for the request.944 */945 public function go_to( $url ) {946 /*947 * Note: the WP and WP_Query classes like to silently fetch parameters948 * from all over the place (globals, GET, etc), which makes it tricky949 * to run them more than once without very carefully clearing everything.950 */951 $_GET = array();952 $_POST = array();953 foreach ( array( 'query_string', 'id', 'postdata', 'authordata', 'day', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages', 'pagenow', 'current_screen' ) as $v ) {954 if ( isset( $GLOBALS[ $v ] ) ) {955 unset( $GLOBALS[ $v ] );956 }957 }958 $parts = parse_url( $url );959 if ( isset( $parts['scheme'] ) ) {960 $req = isset( $parts['path'] ) ? $parts['path'] : '';961 if ( isset( $parts['query'] ) ) {962 $req .= '?' . $parts['query'];963 // Parse the URL query vars into $_GET.964 parse_str( $parts['query'], $_GET );965 }966 } else {967 $req = $url;968 }969 if ( ! isset( $parts['query'] ) ) {970 $parts['query'] = '';971 }972 973 $_SERVER['REQUEST_URI'] = $req;974 unset( $_SERVER['PATH_INFO'] );975 976 self::flush_cache();977 unset( $GLOBALS['wp_query'], $GLOBALS['wp_the_query'] );978 $GLOBALS['wp_the_query'] = new WP_Query();979 $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];980 981 $public_query_vars = $GLOBALS['wp']->public_query_vars;982 $private_query_vars = $GLOBALS['wp']->private_query_vars;983 984 $GLOBALS['wp'] = new WP();985 $GLOBALS['wp']->public_query_vars = $public_query_vars;986 $GLOBALS['wp']->private_query_vars = $private_query_vars;987 988 _cleanup_query_vars();989 990 $GLOBALS['wp']->main( $parts['query'] );991 }992 993 /**994 * Allows tests to be skipped on single or multisite installs by using @group annotations.995 *996 * This is a custom extension of the PHPUnit requirements handling.997 *998 * @since 3.5.0999 * @deprecated 5.9.0 This method has not been functional since PHPUnit 7.0.1000 */1001 protected function checkRequirements() {1002 // For PHPUnit 5/6, as we're overloading a public PHPUnit native method in those versions.1003 if ( is_callable( 'PHPUnit\Framework\TestCase', 'checkRequirements' ) ) {1004 parent::checkRequirements();1005 }1006 }1007 1008 /**1009 * Skips the current test if there is an open Trac ticket associated with it.1010 *1011 * @since 3.5.01012 *1013 * @param int $ticket_id Ticket number.1014 */1015 public function knownWPBug( $ticket_id ) {1016 if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( $ticket_id, self::$forced_tickets, true ) ) {1017 return;1018 }1019 if ( ! TracTickets::isTracTicketClosed( 'https://core.trac.wordpress.org', $ticket_id ) ) {1020 $this->markTestSkipped( sprintf( 'WordPress Ticket #%d is not fixed', $ticket_id ) );1021 }1022 }1023 1024 /**1025 * Skips the current test if there is an open Unit Test Trac ticket associated with it.1026 *1027 * @since 3.5.01028 * @deprecated No longer used since the Unit Test Trac was merged into the Core Trac.1029 *1030 * @param int $ticket_id Ticket number.1031 */1032 public function knownUTBug( $ticket_id ) {1033 return;1034 }1035 1036 /**1037 * Skips the current test if there is an open Plugin Trac ticket associated with it.1038 *1039 * @since 3.5.01040 *1041 * @param int $ticket_id Ticket number.1042 */1043 public function knownPluginBug( $ticket_id ) {1044 if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( 'Plugin' . $ticket_id, self::$forced_tickets, true ) ) {1045 return;1046 }1047 if ( ! TracTickets::isTracTicketClosed( 'https://plugins.trac.wordpress.org', $ticket_id ) ) {1048 $this->markTestSkipped( sprintf( 'WordPress Plugin Ticket #%d is not fixed', $ticket_id ) );1049 }1050 }1051 1052 /**1053 * Adds a Trac ticket number to the `$forced_tickets` property.1054 *1055 * @since 3.5.01056 *1057 * @param int $ticket Ticket number.1058 */1059 public static function forceTicket( $ticket ) {1060 self::$forced_tickets[] = $ticket;1061 }1062 1063 /**1064 * Custom preparations for the PHPUnit process isolation template.1065 *1066 * When restoring global state between tests, PHPUnit defines all the constants that were already defined, and then1067 * includes included files. This does not work with WordPress, as the included files define the constants.1068 *1069 * This method defines the constants after including files.1070 *1071 * @param Text_Template $template The template to prepare.1072 */1073 public function prepareTemplate( Text_Template $template ) {1074 $template->setVar( array( 'constants' => '' ) );1075 $template->setVar( array( 'wp_constants' => PHPUnit_Util_GlobalState::getConstantsAsString() ) );1076 parent::prepareTemplate( $template );1077 }1078 1079 /**1080 * Creates a unique temporary file name.1081 *1082 * The directory in which the file is created depends on the environment configuration.1083 *1084 * @since 3.5.01085 *1086 * @return string|bool Path on success, else false.1087 */1088 public function temp_filename() {1089 $tmp_dir = '';1090 $dirs = array( 'TMP', 'TMPDIR', 'TEMP' );1091 1092 foreach ( $dirs as $dir ) {1093 if ( isset( $_ENV[ $dir ] ) && ! empty( $_ENV[ $dir ] ) ) {1094 $tmp_dir = $dir;1095 break;1096 }1097 }1098 1099 if ( empty( $tmp_dir ) ) {1100 $tmp_dir = get_temp_dir();1101 }1102 1103 $tmp_dir = realpath( $tmp_dir );1104 1105 return tempnam( $tmp_dir, 'wpunit' );1106 880 } 1107 881 … … 1183 957 1184 958 /** 959 * Helper function to convert a single-level array containing text strings to a named data provider. 960 * 961 * The value of the data set will also be used as the name of the data set. 962 * 963 * Typical usage of this method: 964 * 965 * public function data_provider_for_test_name() { 966 * $array = array( 967 * 'value1', 968 * 'value2', 969 * ); 970 * 971 * return $this->text_array_to_dataprovider( $array ); 972 * } 973 * 974 * The returned result will look like: 975 * 976 * array( 977 * 'value1' => array( 'value1' ), 978 * 'value2' => array( 'value2' ), 979 * ) 980 * 981 * @since 6.1.0 982 * 983 * @param array $input Input array. 984 * @return array Array which is usable as a test data provider with named data sets. 985 */ 986 public static function text_array_to_dataprovider( $input ) { 987 $data = array(); 988 989 foreach ( $input as $value ) { 990 if ( ! is_string( $value ) ) { 991 throw new Exception( 992 'All values in the input array should be text strings. Fix the input data.' 993 ); 994 } 995 996 if ( isset( $data[ $value ] ) ) { 997 throw new Exception( 998 "Attempting to add a duplicate data set for value $value to the data provider. Fix the input data." 999 ); 1000 } 1001 1002 $data[ $value ] = array( $value ); 1003 } 1004 1005 return $data; 1006 } 1007 1008 /** 1009 * Sets the global state to as if a given URL has been requested. 1010 * 1011 * This sets: 1012 * - The super globals. 1013 * - The globals. 1014 * - The query variables. 1015 * - The main query. 1016 * 1017 * @since 3.5.0 1018 * 1019 * @param string $url The URL for the request. 1020 */ 1021 public function go_to( $url ) { 1022 /* 1023 * Note: the WP and WP_Query classes like to silently fetch parameters 1024 * from all over the place (globals, GET, etc), which makes it tricky 1025 * to run them more than once without very carefully clearing everything. 1026 */ 1027 $_GET = array(); 1028 $_POST = array(); 1029 foreach ( array( 'query_string', 'id', 'postdata', 'authordata', 'day', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages', 'pagenow', 'current_screen' ) as $v ) { 1030 if ( isset( $GLOBALS[ $v ] ) ) { 1031 unset( $GLOBALS[ $v ] ); 1032 } 1033 } 1034 $parts = parse_url( $url ); 1035 if ( isset( $parts['scheme'] ) ) { 1036 $req = isset( $parts['path'] ) ? $parts['path'] : ''; 1037 if ( isset( $parts['query'] ) ) { 1038 $req .= '?' . $parts['query']; 1039 // Parse the URL query vars into $_GET. 1040 parse_str( $parts['query'], $_GET ); 1041 } 1042 } else { 1043 $req = $url; 1044 } 1045 if ( ! isset( $parts['query'] ) ) { 1046 $parts['query'] = ''; 1047 } 1048 1049 $_SERVER['REQUEST_URI'] = $req; 1050 unset( $_SERVER['PATH_INFO'] ); 1051 1052 self::flush_cache(); 1053 unset( $GLOBALS['wp_query'], $GLOBALS['wp_the_query'] ); 1054 $GLOBALS['wp_the_query'] = new WP_Query(); 1055 $GLOBALS['wp_query'] = $GLOBALS['wp_the_query']; 1056 1057 $public_query_vars = $GLOBALS['wp']->public_query_vars; 1058 $private_query_vars = $GLOBALS['wp']->private_query_vars; 1059 1060 $GLOBALS['wp'] = new WP(); 1061 $GLOBALS['wp']->public_query_vars = $public_query_vars; 1062 $GLOBALS['wp']->private_query_vars = $private_query_vars; 1063 1064 _cleanup_query_vars(); 1065 1066 $GLOBALS['wp']->main( $parts['query'] ); 1067 } 1068 1069 /** 1070 * Allows tests to be skipped on single or multisite installs by using @group annotations. 1071 * 1072 * This is a custom extension of the PHPUnit requirements handling. 1073 * 1074 * @since 3.5.0 1075 * @deprecated 5.9.0 This method has not been functional since PHPUnit 7.0. 1076 */ 1077 protected function checkRequirements() { 1078 // For PHPUnit 5/6, as we're overloading a public PHPUnit native method in those versions. 1079 if ( is_callable( 'PHPUnit\Framework\TestCase', 'checkRequirements' ) ) { 1080 parent::checkRequirements(); 1081 } 1082 } 1083 1084 /** 1085 * Skips the current test if there is an open Trac ticket associated with it. 1086 * 1087 * @since 3.5.0 1088 * 1089 * @param int $ticket_id Ticket number. 1090 */ 1091 public function knownWPBug( $ticket_id ) { 1092 if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( $ticket_id, self::$forced_tickets, true ) ) { 1093 return; 1094 } 1095 if ( ! TracTickets::isTracTicketClosed( 'https://core.trac.wordpress.org', $ticket_id ) ) { 1096 $this->markTestSkipped( sprintf( 'WordPress Ticket #%d is not fixed', $ticket_id ) ); 1097 } 1098 } 1099 1100 /** 1101 * Skips the current test if there is an open Unit Test Trac ticket associated with it. 1102 * 1103 * @since 3.5.0 1104 * @deprecated No longer used since the Unit Test Trac was merged into the Core Trac. 1105 * 1106 * @param int $ticket_id Ticket number. 1107 */ 1108 public function knownUTBug( $ticket_id ) { 1109 return; 1110 } 1111 1112 /** 1113 * Skips the current test if there is an open Plugin Trac ticket associated with it. 1114 * 1115 * @since 3.5.0 1116 * 1117 * @param int $ticket_id Ticket number. 1118 */ 1119 public function knownPluginBug( $ticket_id ) { 1120 if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( 'Plugin' . $ticket_id, self::$forced_tickets, true ) ) { 1121 return; 1122 } 1123 if ( ! TracTickets::isTracTicketClosed( 'https://plugins.trac.wordpress.org', $ticket_id ) ) { 1124 $this->markTestSkipped( sprintf( 'WordPress Plugin Ticket #%d is not fixed', $ticket_id ) ); 1125 } 1126 } 1127 1128 /** 1129 * Adds a Trac ticket number to the `$forced_tickets` property. 1130 * 1131 * @since 3.5.0 1132 * 1133 * @param int $ticket Ticket number. 1134 */ 1135 public static function forceTicket( $ticket ) { 1136 self::$forced_tickets[] = $ticket; 1137 } 1138 1139 /** 1140 * Custom preparations for the PHPUnit process isolation template. 1141 * 1142 * When restoring global state between tests, PHPUnit defines all the constants that were already defined, and then 1143 * includes included files. This does not work with WordPress, as the included files define the constants. 1144 * 1145 * This method defines the constants after including files. 1146 * 1147 * @param Text_Template $template The template to prepare. 1148 */ 1149 public function prepareTemplate( Text_Template $template ) { 1150 $template->setVar( array( 'constants' => '' ) ); 1151 $template->setVar( array( 'wp_constants' => PHPUnit_Util_GlobalState::getConstantsAsString() ) ); 1152 parent::prepareTemplate( $template ); 1153 } 1154 1155 /** 1156 * Creates a unique temporary file name. 1157 * 1158 * The directory in which the file is created depends on the environment configuration. 1159 * 1160 * @since 3.5.0 1161 * 1162 * @return string|bool Path on success, else false. 1163 */ 1164 public function temp_filename() { 1165 $tmp_dir = ''; 1166 $dirs = array( 'TMP', 'TMPDIR', 'TEMP' ); 1167 1168 foreach ( $dirs as $dir ) { 1169 if ( isset( $_ENV[ $dir ] ) && ! empty( $_ENV[ $dir ] ) ) { 1170 $tmp_dir = $dir; 1171 break; 1172 } 1173 } 1174 1175 if ( empty( $tmp_dir ) ) { 1176 $tmp_dir = get_temp_dir(); 1177 } 1178 1179 $tmp_dir = realpath( $tmp_dir ); 1180 1181 return tempnam( $tmp_dir, 'wpunit' ); 1182 } 1183 1184 /** 1185 1185 * Selectively deletes a file. 1186 1186 *
Note: See TracChangeset
for help on using the changeset viewer.