Make WordPress Core

Ticket #42731: 42731-1.patch

File 42731-1.patch, 11.5 KB (added by Frank Klein, 6 years ago)
  • tests/phpunit/includes/testcase.php

    diff --git tests/phpunit/includes/testcase.php tests/phpunit/includes/testcase.php
    index 4b47ddca0a..914826a42f 100644
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    513513                $this->assertEquals( preg_replace( '/\s*/', '', $expected ), preg_replace( '/\s*/', '', $actual ) );
    514514        }
    515515
     516        /**
     517         * Assert that the contents of two un-keyed, single arrays are equal, without accounting for the order of elements.
     518         *
     519         * @since 3.5.0
     520         *
     521         * @param array $expected Expected array.
     522         * @param array $actual   Array to check.
     523         */
    516524        function assertEqualSets( $expected, $actual ) {
    517525                sort( $expected );
    518526                sort( $actual );
    519527                $this->assertEquals( $expected, $actual );
    520528        }
    521529
     530        /**
     531         * Assert that the contents of two keyed, single arrays are equal, without accounting for the order of elements.
     532         *
     533         * @since 4.1.0
     534         *
     535         * @param array $expected Expected array.
     536         * @param array $actual   Array to check.
     537         */
    522538        function assertEqualSetsWithIndex( $expected, $actual ) {
    523539                ksort( $expected );
    524540                ksort( $actual );
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    526542        }
    527543
    528544        /**
    529          * Asserts that the given variable is a multidimensional array, and that all arrays are non-empty.
     545         * Assert that the given variable is a multidimensional array, and that all arrays are non-empty.
    530546         *
    531          * @param array $array
     547         * @since 4.8.0
     548         *
     549         * @param array $array Array to check.
    532550         */
    533551        function assertNonEmptyMultidimensionalArray( $array ) {
    534552                $this->assertTrue( is_array( $array ) );
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    541559        }
    542560
    543561        /**
    544          * Asserts that a condition is not false.
     562         * Assert that a condition is not false.
     563         *
     564         * This method has been back ported from a more recent PHPUnit version, as tests running on PHP 5.2 use
     565         * PHPUnit 3.6.x.
    545566         *
    546          * @param bool   $condition
    547          * @param string $message
     567         * @since 4.7.4
     568         *
     569         * @param bool   $condition Condition to check.
     570         * @param string $message   Optional. Message to display when the assertion fails.
    548571         *
    549572         * @throws PHPUnit_Framework_AssertionFailedError
    550573         */
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    553576        }
    554577
    555578        /**
    556          * Modify WordPress's query internals as if a given URL has been requested.
     579         * Set the global state to as if a given URL has been requested.
     580         *
     581         * This sets:
     582         * - The super globals.
     583         * - The globals.
     584         * - The query variables.
     585         * - The main query.
     586         *
     587         * @since 3.5.0
    557588         *
    558589         * @param string $url The URL for the request.
    559590         */
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    600631                $GLOBALS['wp']->main($parts['query']);
    601632        }
    602633
     634        /**
     635         * Custom extension of the PHPUnit requirements handling.
     636         *
     637         * Allows tests to be skipped on single or multisite installs by using @group annotations.
     638         *
     639         * Contains legacy code for skipping tests that are associated with an open Trac ticket. Core tests do no longer
     640         * support this behaviour.
     641         *
     642         * @since 3.5.0
     643         */
    603644        protected function checkRequirements() {
    604645                parent::checkRequirements();
    605646
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    634675        }
    635676
    636677        /**
    637          * Skips the current test if there is an open WordPress ticket with id $ticket_id
     678         * Skips the current test if there is an open Trac ticket associated with it.
     679         *
     680         * @since 3.5.0
     681         *
     682         * @param int $ticket_id Ticket number.
    638683         */
    639684        function knownWPBug( $ticket_id ) {
    640685                if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( $ticket_id, self::$forced_tickets ) )
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    644689        }
    645690
    646691        /**
    647          * @deprecated No longer used since the unit test Trac was merged into Core's.
     692         * Skips the current test if there is an open Unit Test Trac ticket associated with it.
     693         *
     694         * @since 3.5.0
     695         *
     696         * @deprecated No longer used since the Unit Test Trac was merged into the Core Trac.
     697         *
     698         * @param int $ticket_id Ticket number.
    648699         */
    649700        function knownUTBug( $ticket_id ) {
    650701                return;
    651702        }
    652703
    653704        /**
    654          * Skips the current test if there is an open plugin ticket with id $ticket_id
     705         * Skips the current test if there is an open Plugin Trac ticket associated with it.
     706         *
     707         * @since 3.5.0
     708         *
     709         * @param int $ticket_id Ticket number.
    655710         */
    656711        function knownPluginBug( $ticket_id ) {
    657712                if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( 'Plugin' . $ticket_id, self::$forced_tickets ) )
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    660715                        $this->markTestSkipped( sprintf( 'WordPress Plugin Ticket #%d is not fixed', $ticket_id ) );
    661716        }
    662717
     718        /**
     719         * Add a Trac ticket number to the `$forced_tickets` property.
     720         *
     721         * @since 3.5.0
     722         *
     723         * @param int $ticket Ticket number.
     724         */
    663725        public static function forceTicket( $ticket ) {
    664726                self::$forced_tickets[] = $ticket;
    665727        }
    666728
    667729        /**
    668          * Define constants after including files.
     730         * Custom preparations for the PHPUnit process isolation template.
     731         *
     732         * When restoring global state between tests, PHPUnit defines all the constants that were already defined, and then
     733         * includes included files. This does not work with WordPress, as the included files define the constants.
     734         *
     735         * This method defines the constants after including files.
     736         *
     737         * @param Text_Template $template
    669738         */
    670739        function prepareTemplate( Text_Template $template ) {
    671740                $template->setVar( array( 'constants' => '' ) );
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    674743        }
    675744
    676745        /**
    677          * Returns the name of a temporary file
     746         * Create a unique temporary file name.
     747         *
     748         * The directory in which the file is created depends on the environment configuration.
     749         *
     750         * @since 3.5.0
     751         *
     752         * @return string|bool Path on success, else false.
    678753         */
    679754        function temp_filename() {
    680755                $tmp_dir = '';
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    698773         * expected to be false. For example, assertQueryTrue('is_single', 'is_feed') means is_single()
    699774         * and is_feed() must be true and everything else must be false to pass.
    700775         *
     776         * @since 2.5.0
     777         * @since 3.8.0 Moved from `Tests_Query_Conditionals` to `WP_UnitTestCase`.
     778         *
    701779         * @param string $prop,... Any number of WP_Query properties that are expected to be true for the current request.
    702780         */
    703781        function assertQueryTrue(/* ... */) {
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    760838                }
    761839        }
    762840
     841        /**
     842         * Helper to selectively delete a file.
     843         *
     844         * Does not delete a file if its path is set in the `$ignore_files` property.
     845         *
     846         * @param string $file File path.
     847         */
    763848        function unlink( $file ) {
    764849                $exists = is_file( $file );
    765850                if ( $exists && ! in_array( $file, self::$ignore_files ) ) {
    766                         //error_log( $file );
    767851                        unlink( $file );
    768852                } elseif ( ! $exists ) {
    769853                        $this->fail( "Trying to delete a file that doesn't exist: $file" );
    770854                }
    771855        }
    772856
     857        /**
     858         * Helper to selectively delete files from a directory.
     859         *
     860         * Does not delete files if their paths are set in the `$ignore_files` property.
     861         *
     862         * @param string $path Directory path.
     863         */
    773864        function rmdir( $path ) {
    774865                $files = $this->files_in_dir( $path );
    775866                foreach ( $files as $file ) {
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    779870                }
    780871        }
    781872
     873        /**
     874         * Helper to delete files added to the `uploads` directory during tests.
     875         *
     876         * This method works in tandem with the `setUp()` and `rmdir()` methods:
     877         * - `setUp()` scans the `uploads` directory before every test, and stores its contents inside of the
     878         * `$ignore_files` property.
     879         * - `rmdir()` and its helper methods only delete files that are not listed in the `$ignore_files` property. If
     880         * called during `tearDown()` in tests, this will only delete files added during the previously run test.
     881         */
    782882        function remove_added_uploads() {
    783                 // Remove all uploads.
    784883                $uploads = wp_upload_dir();
    785884                $this->rmdir( $uploads['basedir'] );
    786885        }
    787886
     887        /**
     888         * Return a list of all files contained inside a directory.
     889         *
     890         * @since 4.0.0
     891         *
     892         * @param string $dir Path to the directory to scan.
     893         *
     894         * @return array List of file paths.
     895         */
    788896        function files_in_dir( $dir ) {
    789897                $files = array();
    790898
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    799907                return $files;
    800908        }
    801909
     910        /**
     911         * Return a list of all files contained inside the `uploads` directory.
     912         *
     913         * @since 4.0.0
     914         *
     915         * @return array List of file paths.
     916         */
    802917        function scan_user_uploads() {
    803918                static $files = array();
    804919                if ( ! empty( $files ) ) {
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    810925                return $files;
    811926        }
    812927
     928        /**
     929         * Delete all directories contained inside a directory.
     930         *
     931         * @since 4.1.0
     932         *
     933         * @param string $path Path to the directory to scan.
     934         */
    813935        function delete_folders( $path ) {
    814936                $this->matched_dirs = array();
    815937                if ( ! is_dir( $path ) ) {
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    823945                rmdir( $path );
    824946        }
    825947
     948        /**
     949         * Helper for `delete_folders()` method.
     950         *
     951         * Retrieves all directories contained inside a directory and stores them in the `$matched_dirs` property. Hidden
     952         * directories are ignored.
     953         *
     954         * @since 4.1.0
     955         *
     956         * @param string $dir Path to the directory to scan.
     957         */
    826958        function scandir( $dir ) {
    827959                foreach ( scandir( $dir ) as $path ) {
    828960                        if ( 0 !== strpos( $path, '.' ) && is_dir( $dir . '/' . $path ) ) {
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    833965        }
    834966
    835967        /**
    836          * Helper to Convert a microtime string into a float
     968         * Helper to convert a microtime string into a float.
     969         *
     970         * @since 4.1.0
     971         *
     972         * @param string $microtime Time string generated by `microtime()`.
     973         *
     974         * @return float `microtime()` output as a float.
    837975         */
    838976        protected function _microtime_to_float($microtime ){
    839977                $time_array = explode( ' ', $microtime );
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    844982         * Multisite-agnostic way to delete a user from the database.
    845983         *
    846984         * @since 4.3.0
     985         *
     986         * @param int $user_id User ID.
     987         *
     988         * @return bool True if the user was deleted.
    847989         */
    848990        public static function delete_user( $user_id ) {
    849991                if ( is_multisite() ) {
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    8701012                $wp_rewrite->flush_rules();
    8711013        }
    8721014
     1015        /**
     1016         * Create an attachment post from an uploaded file.
     1017         *
     1018         * @since 4.4.0
     1019         *
     1020         * @param array $upload         Array of information about the uploaded file, provided by wp_upload_bits().
     1021         * @param int   $parent_post_id Optional. Parent post ID.
     1022         *
     1023         * @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure.
     1024         */
    8731025        function _make_attachment($upload, $parent_post_id = 0) {
    8741026                $type = '';
    8751027                if ( !empty($upload['type']) ) {
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    8891041                        'guid' => $upload[ 'url' ],
    8901042                );
    8911043
    892                 // Save the data
    8931044                $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent_post_id );
    8941045                wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
    8951046                return $id;
    8961047        }
    8971048
    8981049        /**
    899          * There's no way to change post_modified through WP functions.
     1050         * Update the modified and modified GMT date of a post in the database.
     1051         *
     1052         * @since 4.8.0
     1053         *
     1054         * @global wpdb $wpdb WordPress database abstraction object.
     1055         *
     1056         * @param int    $post_id Post ID.
     1057         * @param string $date    Post date, in the format YYYY-MM-DD HH:MM:SS.
     1058         *
     1059         * @return int|false 1 on success, or false on error.
    9001060         */
    9011061        protected function update_post_modified( $post_id, $date ) {
    9021062                global $wpdb;