1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318 |
- <?php
- require_once __DIR__ . '/factory.php';
- require_once __DIR__ . '/trac.php';
- abstract class WP_UnitTestCase_Base extends PHPUnit\Framework\TestCase {
- protected static $forced_tickets = array();
- protected $expected_deprecated = array();
- protected $caught_deprecated = array();
- protected $expected_doing_it_wrong = array();
- protected $caught_doing_it_wrong = array();
- protected static $hooks_saved = array();
- protected static $ignore_files;
- public function __isset( $name ) {
- return 'factory' === $name;
- }
- public function __get( $name ) {
- if ( 'factory' === $name ) {
- return self::factory();
- }
- }
-
- protected static function factory() {
- static $factory = null;
- if ( ! $factory ) {
- $factory = new WP_UnitTest_Factory();
- }
- return $factory;
- }
-
- public static function get_called_class() {
- return get_called_class();
- }
-
- public static function setUpBeforeClass() {
- global $wpdb;
- $wpdb->suppress_errors = false;
- $wpdb->show_errors = true;
- $wpdb->db_connect();
- ini_set( 'display_errors', 1 );
- parent::setUpBeforeClass();
- $class = get_called_class();
- if ( method_exists( $class, 'wpSetUpBeforeClass' ) ) {
- call_user_func( array( $class, 'wpSetUpBeforeClass' ), self::factory() );
- }
- self::commit_transaction();
- }
-
- public static function tearDownAfterClass() {
- parent::tearDownAfterClass();
- _delete_all_data();
- self::flush_cache();
- $class = get_called_class();
- if ( method_exists( $class, 'wpTearDownAfterClass' ) ) {
- call_user_func( array( $class, 'wpTearDownAfterClass' ) );
- }
- self::commit_transaction();
- }
-
- public function setUp() {
- set_time_limit( 0 );
- if ( ! self::$ignore_files ) {
- self::$ignore_files = $this->scan_user_uploads();
- }
- if ( ! self::$hooks_saved ) {
- $this->_backup_hooks();
- }
- global $wp_rewrite;
- $this->clean_up_global_scope();
-
- if ( defined( 'WP_RUN_CORE_TESTS' ) && WP_RUN_CORE_TESTS ) {
- $this->reset_post_types();
- $this->reset_taxonomies();
- $this->reset_post_statuses();
- $this->reset__SERVER();
- if ( $wp_rewrite->permalink_structure ) {
- $this->set_permalink_structure( '' );
- }
- }
- $this->start_transaction();
- $this->expectDeprecated();
- add_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) );
- }
-
- public function tearDown() {
- global $wpdb, $wp_query, $wp;
- $wpdb->query( 'ROLLBACK' );
- if ( is_multisite() ) {
- while ( ms_is_switched() ) {
- restore_current_blog();
- }
- }
- $wp_query = new WP_Query();
- $wp = new WP();
-
- $post_globals = array( 'post', 'id', 'authordata', 'currentday', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages' );
- foreach ( $post_globals as $global ) {
- $GLOBALS[ $global ] = null;
- }
-
- $GLOBALS['wp_sitemaps'] = null;
- $this->unregister_all_meta_keys();
- remove_theme_support( 'html5' );
- remove_filter( 'query', array( $this, '_create_temporary_tables' ) );
- remove_filter( 'query', array( $this, '_drop_temporary_tables' ) );
- remove_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) );
- $this->_restore_hooks();
- wp_set_current_user( 0 );
- }
-
- public function clean_up_global_scope() {
- $_GET = array();
- $_POST = array();
- self::flush_cache();
- }
-
- public function skipOnAutomatedBranches() {
-
- $travis_branch = getenv( 'TRAVIS_BRANCH' );
- $travis_pull_request = getenv( 'TRAVIS_PULL_REQUEST' );
-
- $github_event_name = getenv( 'GITHUB_EVENT_NAME' );
- $github_ref = getenv( 'GITHUB_REF' );
- if ( $github_event_name && 'false' !== $github_event_name ) {
-
- $skipped = array( 'pull_request', 'pull_request_target' );
- if ( in_array( $github_event_name, $skipped, true ) || 'refs/heads/master' !== $github_ref ) {
- $this->markTestSkipped( 'For automated test runs, this test is only run on trunk/master' );
- }
- } elseif ( $travis_branch && 'false' !== $travis_branch ) {
-
- if ( 'master' !== $travis_branch || 'false' !== $travis_pull_request ) {
- $this->markTestSkipped( 'For automated test runs, this test is only run on trunk/master' );
- }
- }
- }
-
- public function skipWithoutMultisite() {
- if ( ! is_multisite() ) {
- $this->markTestSkipped( 'Test only runs on Multisite' );
- }
- }
-
- public function skipWithMultisite() {
- if ( is_multisite() ) {
- $this->markTestSkipped( 'Test does not run on Multisite' );
- }
- }
-
- public function skipTestOnTimeout( $response ) {
- if ( ! is_wp_error( $response ) ) {
- return;
- }
- if ( 'connect() timed out!' === $response->get_error_message() ) {
- $this->markTestSkipped( 'HTTP timeout' );
- }
- if ( false !== strpos( $response->get_error_message(), 'timed out after' ) ) {
- $this->markTestSkipped( 'HTTP timeout' );
- }
- if ( 0 === strpos( $response->get_error_message(), 'stream_socket_client(): unable to connect to tcp://s.w.org:80' ) ) {
- $this->markTestSkipped( 'HTTP timeout' );
- }
- }
-
- protected function reset_post_types() {
- foreach ( get_post_types( array(), 'objects' ) as $pt ) {
- if ( empty( $pt->tests_no_auto_unregister ) ) {
- _unregister_post_type( $pt->name );
- }
- }
- create_initial_post_types();
- }
-
- protected function reset_taxonomies() {
- foreach ( get_taxonomies() as $tax ) {
- _unregister_taxonomy( $tax );
- }
- create_initial_taxonomies();
- }
-
- protected function reset_post_statuses() {
- foreach ( get_post_stati( array( '_builtin' => false ) ) as $post_status ) {
- _unregister_post_status( $post_status );
- }
- }
-
- protected function reset__SERVER() {
- tests_reset__SERVER();
- }
-
- protected function _backup_hooks() {
- $globals = array( 'wp_actions', 'wp_current_filter' );
- foreach ( $globals as $key ) {
- self::$hooks_saved[ $key ] = $GLOBALS[ $key ];
- }
- self::$hooks_saved['wp_filter'] = array();
- foreach ( $GLOBALS['wp_filter'] as $hook_name => $hook_object ) {
- self::$hooks_saved['wp_filter'][ $hook_name ] = clone $hook_object;
- }
- }
-
- protected function _restore_hooks() {
- $globals = array( 'wp_actions', 'wp_current_filter' );
- foreach ( $globals as $key ) {
- if ( isset( self::$hooks_saved[ $key ] ) ) {
- $GLOBALS[ $key ] = self::$hooks_saved[ $key ];
- }
- }
- if ( isset( self::$hooks_saved['wp_filter'] ) ) {
- $GLOBALS['wp_filter'] = array();
- foreach ( self::$hooks_saved['wp_filter'] as $hook_name => $hook_object ) {
- $GLOBALS['wp_filter'][ $hook_name ] = clone $hook_object;
- }
- }
- }
-
- public static function flush_cache() {
- global $wp_object_cache;
- $wp_object_cache->group_ops = array();
- $wp_object_cache->stats = array();
- $wp_object_cache->memcache_debug = array();
- $wp_object_cache->cache = array();
- if ( method_exists( $wp_object_cache, '__remoteset' ) ) {
- $wp_object_cache->__remoteset();
- }
- wp_cache_flush();
- wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'blog_meta' ) );
- wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
- }
-
- public function unregister_all_meta_keys() {
- global $wp_meta_keys;
- if ( ! is_array( $wp_meta_keys ) ) {
- return;
- }
- foreach ( $wp_meta_keys as $object_type => $type_keys ) {
- foreach ( $type_keys as $object_subtype => $subtype_keys ) {
- foreach ( $subtype_keys as $key => $value ) {
- unregister_meta_key( $object_type, $key, $object_subtype );
- }
- }
- }
- }
-
- public function start_transaction() {
- global $wpdb;
- $wpdb->query( 'SET autocommit = 0;' );
- $wpdb->query( 'START TRANSACTION;' );
- add_filter( 'query', array( $this, '_create_temporary_tables' ) );
- add_filter( 'query', array( $this, '_drop_temporary_tables' ) );
- }
-
- public static function commit_transaction() {
- global $wpdb;
- $wpdb->query( 'COMMIT;' );
- }
-
- public function _create_temporary_tables( $query ) {
- if ( 0 === strpos( trim( $query ), 'CREATE TABLE' ) ) {
- return substr_replace( trim( $query ), 'CREATE TEMPORARY TABLE', 0, 12 );
- }
- return $query;
- }
-
- public function _drop_temporary_tables( $query ) {
- if ( 0 === strpos( trim( $query ), 'DROP TABLE' ) ) {
- return substr_replace( trim( $query ), 'DROP TEMPORARY TABLE', 0, 10 );
- }
- return $query;
- }
-
- public function get_wp_die_handler( $handler ) {
- return array( $this, 'wp_die_handler' );
- }
-
- public function wp_die_handler( $message ) {
- if ( is_wp_error( $message ) ) {
- $message = $message->get_error_message();
- }
- if ( ! is_scalar( $message ) ) {
- $message = '0';
- }
- throw new WPDieException( $message );
- }
-
- public function expectDeprecated() {
- $annotations = $this->getAnnotations();
- foreach ( array( 'class', 'method' ) as $depth ) {
- if ( ! empty( $annotations[ $depth ]['expectedDeprecated'] ) ) {
- $this->expected_deprecated = array_merge( $this->expected_deprecated, $annotations[ $depth ]['expectedDeprecated'] );
- }
- if ( ! empty( $annotations[ $depth ]['expectedIncorrectUsage'] ) ) {
- $this->expected_doing_it_wrong = array_merge( $this->expected_doing_it_wrong, $annotations[ $depth ]['expectedIncorrectUsage'] );
- }
- }
- add_action( 'deprecated_function_run', array( $this, 'deprecated_function_run' ) );
- add_action( 'deprecated_argument_run', array( $this, 'deprecated_function_run' ) );
- add_action( 'deprecated_hook_run', array( $this, 'deprecated_function_run' ) );
- add_action( 'doing_it_wrong_run', array( $this, 'doing_it_wrong_run' ) );
- add_action( 'deprecated_function_trigger_error', '__return_false' );
- add_action( 'deprecated_argument_trigger_error', '__return_false' );
- add_action( 'deprecated_hook_trigger_error', '__return_false' );
- add_action( 'doing_it_wrong_trigger_error', '__return_false' );
- }
-
- public function expectedDeprecated() {
- $errors = array();
- $not_caught_deprecated = array_diff( $this->expected_deprecated, $this->caught_deprecated );
- foreach ( $not_caught_deprecated as $not_caught ) {
- $errors[] = "Failed to assert that $not_caught triggered a deprecated notice";
- }
- $unexpected_deprecated = array_diff( $this->caught_deprecated, $this->expected_deprecated );
- foreach ( $unexpected_deprecated as $unexpected ) {
- $errors[] = "Unexpected deprecated notice for $unexpected";
- }
- $not_caught_doing_it_wrong = array_diff( $this->expected_doing_it_wrong, $this->caught_doing_it_wrong );
- foreach ( $not_caught_doing_it_wrong as $not_caught ) {
- $errors[] = "Failed to assert that $not_caught triggered an incorrect usage notice";
- }
- $unexpected_doing_it_wrong = array_diff( $this->caught_doing_it_wrong, $this->expected_doing_it_wrong );
- foreach ( $unexpected_doing_it_wrong as $unexpected ) {
- $errors[] = "Unexpected incorrect usage notice for $unexpected";
- }
-
- if ( ! empty( $this->expected_deprecated ) ||
- ! empty( $this->expected_doing_it_wrong ) ||
- ! empty( $this->caught_deprecated ) ||
- ! empty( $this->caught_doing_it_wrong ) ) {
- $this->assertEmpty( $errors, implode( "\n", $errors ) );
- }
- }
-
- protected function assertPostConditions() {
- $this->expectedDeprecated();
- }
-
- public function setExpectedDeprecated( $deprecated ) {
- $this->expected_deprecated[] = $deprecated;
- }
-
- public function setExpectedIncorrectUsage( $doing_it_wrong ) {
- $this->expected_doing_it_wrong[] = $doing_it_wrong;
- }
-
- public function setExpectedException( $exception, $message = '', $code = null ) {
- if ( method_exists( 'PHPUnit_Framework_TestCase', 'setExpectedException' ) ) {
- parent::setExpectedException( $exception, $message, $code );
- } else {
- $this->expectException( $exception );
- if ( '' !== $message ) {
- $this->expectExceptionMessage( $message );
- }
- if ( null !== $code ) {
- $this->expectExceptionCode( $code );
- }
- }
- }
-
- public function deprecated_function_run( $function ) {
- if ( ! in_array( $function, $this->caught_deprecated, true ) ) {
- $this->caught_deprecated[] = $function;
- }
- }
-
- public function doing_it_wrong_run( $function ) {
- if ( ! in_array( $function, $this->caught_doing_it_wrong, true ) ) {
- $this->caught_doing_it_wrong[] = $function;
- }
- }
-
- public function assertWPError( $actual, $message = '' ) {
- $this->assertInstanceOf( 'WP_Error', $actual, $message );
- }
-
- public function assertNotWPError( $actual, $message = '' ) {
- if ( '' === $message && is_wp_error( $actual ) ) {
- $message = $actual->get_error_message();
- }
- $this->assertNotInstanceOf( 'WP_Error', $actual, $message );
- }
-
- public function assertIXRError( $actual, $message = '' ) {
- $this->assertInstanceOf( 'IXR_Error', $actual, $message );
- }
-
- public function assertNotIXRError( $actual, $message = '' ) {
- if ( $actual instanceof IXR_Error && '' === $message ) {
- $message = $actual->message;
- }
- $this->assertNotInstanceOf( 'IXR_Error', $actual, $message );
- }
-
- public function assertEqualFields( $object, $fields ) {
- foreach ( $fields as $field_name => $field_value ) {
- if ( $object->$field_name !== $field_value ) {
- $this->fail();
- }
- }
- }
-
- public function assertDiscardWhitespace( $expected, $actual ) {
- $this->assertEquals( preg_replace( '/\s*/', '', $expected ), preg_replace( '/\s*/', '', $actual ) );
- }
-
- public function assertSameIgnoreEOL( $expected, $actual ) {
- $this->assertSame( str_replace( "\r\n", "\n", $expected ), str_replace( "\r\n", "\n", $actual ) );
- }
-
- public function assertEqualsIgnoreEOL( $expected, $actual ) {
- $this->assertSameIgnoreEOL( $expected, $actual );
- }
-
- public function assertSameSets( $expected, $actual ) {
- sort( $expected );
- sort( $actual );
- $this->assertSame( $expected, $actual );
- }
-
- public function assertEqualSets( $expected, $actual ) {
- sort( $expected );
- sort( $actual );
- $this->assertEquals( $expected, $actual );
- }
-
- public function assertSameSetsWithIndex( $expected, $actual ) {
- ksort( $expected );
- ksort( $actual );
- $this->assertSame( $expected, $actual );
- }
-
- public function assertEqualSetsWithIndex( $expected, $actual ) {
- ksort( $expected );
- ksort( $actual );
- $this->assertEquals( $expected, $actual );
- }
-
- public function assertNonEmptyMultidimensionalArray( $array ) {
- $this->assertTrue( is_array( $array ) );
- $this->assertNotEmpty( $array );
- foreach ( $array as $sub_array ) {
- $this->assertTrue( is_array( $sub_array ) );
- $this->assertNotEmpty( $sub_array );
- }
- }
-
- public function go_to( $url ) {
-
- $_GET = array();
- $_POST = array();
- foreach ( array( 'query_string', 'id', 'postdata', 'authordata', 'day', 'currentmonth', 'page', 'pages', 'multipage', 'more', 'numpages', 'pagenow', 'current_screen' ) as $v ) {
- if ( isset( $GLOBALS[ $v ] ) ) {
- unset( $GLOBALS[ $v ] );
- }
- }
- $parts = parse_url( $url );
- if ( isset( $parts['scheme'] ) ) {
- $req = isset( $parts['path'] ) ? $parts['path'] : '';
- if ( isset( $parts['query'] ) ) {
- $req .= '?' . $parts['query'];
-
- parse_str( $parts['query'], $_GET );
- }
- } else {
- $req = $url;
- }
- if ( ! isset( $parts['query'] ) ) {
- $parts['query'] = '';
- }
- $_SERVER['REQUEST_URI'] = $req;
- unset( $_SERVER['PATH_INFO'] );
- self::flush_cache();
- unset( $GLOBALS['wp_query'], $GLOBALS['wp_the_query'] );
- $GLOBALS['wp_the_query'] = new WP_Query();
- $GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
- $public_query_vars = $GLOBALS['wp']->public_query_vars;
- $private_query_vars = $GLOBALS['wp']->private_query_vars;
- $GLOBALS['wp'] = new WP();
- $GLOBALS['wp']->public_query_vars = $public_query_vars;
- $GLOBALS['wp']->private_query_vars = $private_query_vars;
- _cleanup_query_vars();
- $GLOBALS['wp']->main( $parts['query'] );
- }
-
- protected function checkRequirements() {
- parent::checkRequirements();
- $annotations = $this->getAnnotations();
- $groups = array();
- if ( ! empty( $annotations['class']['group'] ) ) {
- $groups = array_merge( $groups, $annotations['class']['group'] );
- }
- if ( ! empty( $annotations['method']['group'] ) ) {
- $groups = array_merge( $groups, $annotations['method']['group'] );
- }
- if ( ! empty( $groups ) ) {
- if ( in_array( 'ms-required', $groups, true ) ) {
- $this->skipWithoutMultisite();
- }
- if ( in_array( 'ms-excluded', $groups, true ) ) {
- $this->skipWithMultisite();
- }
- }
-
-
- if ( defined( 'WP_RUN_CORE_TESTS' ) && WP_RUN_CORE_TESTS ) {
- return;
- }
- if ( WP_TESTS_FORCE_KNOWN_BUGS ) {
- return;
- }
- $tickets = PHPUnit_Util_Test::getTickets( get_class( $this ), $this->getName( false ) );
- foreach ( $tickets as $ticket ) {
- if ( is_numeric( $ticket ) ) {
- $this->knownWPBug( $ticket );
- } elseif ( 0 === strpos( $ticket, 'Plugin' ) ) {
- $ticket = substr( $ticket, 6 );
- if ( $ticket && is_numeric( $ticket ) ) {
- $this->knownPluginBug( $ticket );
- }
- }
- }
- }
-
- public function knownWPBug( $ticket_id ) {
- if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( $ticket_id, self::$forced_tickets, true ) ) {
- return;
- }
- if ( ! TracTickets::isTracTicketClosed( 'https://core.trac.wordpress.org', $ticket_id ) ) {
- $this->markTestSkipped( sprintf( 'WordPress Ticket #%d is not fixed', $ticket_id ) );
- }
- }
-
- public function knownUTBug( $ticket_id ) {
- return;
- }
-
- public function knownPluginBug( $ticket_id ) {
- if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( 'Plugin' . $ticket_id, self::$forced_tickets, true ) ) {
- return;
- }
- if ( ! TracTickets::isTracTicketClosed( 'https://plugins.trac.wordpress.org', $ticket_id ) ) {
- $this->markTestSkipped( sprintf( 'WordPress Plugin Ticket #%d is not fixed', $ticket_id ) );
- }
- }
-
- public static function forceTicket( $ticket ) {
- self::$forced_tickets[] = $ticket;
- }
-
- public function prepareTemplate( Text_Template $template ) {
- $template->setVar( array( 'constants' => '' ) );
- $template->setVar( array( 'wp_constants' => PHPUnit_Util_GlobalState::getConstantsAsString() ) );
- parent::prepareTemplate( $template );
- }
-
- public function temp_filename() {
- $tmp_dir = '';
- $dirs = array( 'TMP', 'TMPDIR', 'TEMP' );
- foreach ( $dirs as $dir ) {
- if ( isset( $_ENV[ $dir ] ) && ! empty( $_ENV[ $dir ] ) ) {
- $tmp_dir = $dir;
- break;
- }
- }
- if ( empty( $tmp_dir ) ) {
- $tmp_dir = get_temp_dir();
- }
- $tmp_dir = realpath( $tmp_dir );
- return tempnam( $tmp_dir, 'wpunit' );
- }
-
- public function assertQueryTrue( ...$prop ) {
- global $wp_query;
- $all = array(
- 'is_404',
- 'is_admin',
- 'is_archive',
- 'is_attachment',
- 'is_author',
- 'is_category',
- 'is_comment_feed',
- 'is_date',
- 'is_day',
- 'is_embed',
- 'is_feed',
- 'is_front_page',
- 'is_home',
- 'is_privacy_policy',
- 'is_month',
- 'is_page',
- 'is_paged',
- 'is_post_type_archive',
- 'is_posts_page',
- 'is_preview',
- 'is_robots',
- 'is_favicon',
- 'is_search',
- 'is_single',
- 'is_singular',
- 'is_tag',
- 'is_tax',
- 'is_time',
- 'is_trackback',
- 'is_year',
- );
- foreach ( $prop as $true_thing ) {
- $this->assertContains( $true_thing, $all, "Unknown conditional: {$true_thing}." );
- }
- $passed = true;
- $message = '';
- foreach ( $all as $query_thing ) {
- $result = is_callable( $query_thing ) ? call_user_func( $query_thing ) : $wp_query->$query_thing;
- if ( in_array( $query_thing, $prop, true ) ) {
- if ( ! $result ) {
- $message .= $query_thing . ' is false but is expected to be true. ' . PHP_EOL;
- $passed = false;
- }
- } elseif ( $result ) {
- $message .= $query_thing . ' is true but is expected to be false. ' . PHP_EOL;
- $passed = false;
- }
- }
- if ( ! $passed ) {
- $this->fail( $message );
- }
- }
-
- public function unlink( $file ) {
- $exists = is_file( $file );
- if ( $exists && ! in_array( $file, self::$ignore_files, true ) ) {
-
- unlink( $file );
- } elseif ( ! $exists ) {
- $this->fail( "Trying to delete a file that doesn't exist: $file" );
- }
- }
-
- public function rmdir( $path ) {
- $files = $this->files_in_dir( $path );
- foreach ( $files as $file ) {
- if ( ! in_array( $file, self::$ignore_files, true ) ) {
- $this->unlink( $file );
- }
- }
- }
-
- public function remove_added_uploads() {
- $uploads = wp_upload_dir();
- $this->rmdir( $uploads['basedir'] );
- }
-
- public function files_in_dir( $dir ) {
- $files = array();
- $iterator = new RecursiveDirectoryIterator( $dir );
- $objects = new RecursiveIteratorIterator( $iterator );
- foreach ( $objects as $name => $object ) {
- if ( is_file( $name ) ) {
- $files[] = $name;
- }
- }
- return $files;
- }
-
- public function scan_user_uploads() {
- static $files = array();
- if ( ! empty( $files ) ) {
- return $files;
- }
- $uploads = wp_upload_dir();
- $files = $this->files_in_dir( $uploads['basedir'] );
- return $files;
- }
-
- public function delete_folders( $path ) {
- $this->matched_dirs = array();
- if ( ! is_dir( $path ) ) {
- return;
- }
- $this->scandir( $path );
- foreach ( array_reverse( $this->matched_dirs ) as $dir ) {
- rmdir( $dir );
- }
- rmdir( $path );
- }
-
- public function scandir( $dir ) {
- foreach ( scandir( $dir ) as $path ) {
- if ( 0 !== strpos( $path, '.' ) && is_dir( $dir . '/' . $path ) ) {
- $this->matched_dirs[] = $dir . '/' . $path;
- $this->scandir( $dir . '/' . $path );
- }
- }
- }
-
- protected function _microtime_to_float( $microtime ) {
- $time_array = explode( ' ', $microtime );
- return array_sum( $time_array );
- }
-
- public static function delete_user( $user_id ) {
- if ( is_multisite() ) {
- return wpmu_delete_user( $user_id );
- }
- return wp_delete_user( $user_id );
- }
-
- public function set_permalink_structure( $structure = '' ) {
- global $wp_rewrite;
- $wp_rewrite->init();
- $wp_rewrite->set_permalink_structure( $structure );
- $wp_rewrite->flush_rules();
- }
-
- public function _make_attachment( $upload, $parent_post_id = 0 ) {
- $type = '';
- if ( ! empty( $upload['type'] ) ) {
- $type = $upload['type'];
- } else {
- $mime = wp_check_filetype( $upload['file'] );
- if ( $mime ) {
- $type = $mime['type'];
- }
- }
- $attachment = array(
- 'post_title' => wp_basename( $upload['file'] ),
- 'post_content' => '',
- 'post_type' => 'attachment',
- 'post_parent' => $parent_post_id,
- 'post_mime_type' => $type,
- 'guid' => $upload['url'],
- );
- $id = wp_insert_attachment( $attachment, $upload['file'], $parent_post_id );
- wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
- return $id;
- }
-
- protected function update_post_modified( $post_id, $date ) {
- global $wpdb;
- return $wpdb->update(
- $wpdb->posts,
- array(
- 'post_modified' => $date,
- 'post_modified_gmt' => $date,
- ),
- array(
- 'ID' => $post_id,
- ),
- array(
- '%s',
- '%s',
- ),
- array(
- '%d',
- )
- );
- }
-
- public static function touch( $file ) {
- if ( file_exists( $file ) ) {
- return;
- }
- $dir = dirname( $file );
- if ( ! file_exists( $dir ) ) {
- mkdir( $dir, 0777, true );
- }
- touch( $file );
- }
- }
|