Make WordPress Core

Ticket #42731: 42731-1.2.patch

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

    diff --git tests/phpunit/includes/testcase.php tests/phpunit/includes/testcase.php
    index 4b47ddca0a..71117dc003 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 backported 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 ) ) {
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    770855                }
    771856        }
    772857
     858        /**
     859         * Helper to selectively delete files from a directory.
     860         *
     861         * Does not delete files if their paths are set in the `$ignore_files` property.
     862         *
     863         * @param string $path Directory path.
     864         */
    773865        function rmdir( $path ) {
    774866                $files = $this->files_in_dir( $path );
    775867                foreach ( $files as $file ) {
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    779871                }
    780872        }
    781873
     874        /**
     875         * Helper to delete files added to the `uploads` directory during tests.
     876         *
     877         * This method works in tandem with the `setUp()` and `rmdir()` methods:
     878         * - `setUp()` scans the `uploads` directory before every test, and stores its contents inside of the
     879         * `$ignore_files` property.
     880         * - `rmdir()` and its helper methods only delete files that are not listed in the `$ignore_files` property. If
     881         * called during `tearDown()` in tests, this will only delete files added during the previously run test.
     882         */
    782883        function remove_added_uploads() {
    783                 // Remove all uploads.
    784884                $uploads = wp_upload_dir();
    785885                $this->rmdir( $uploads['basedir'] );
    786886        }
    787887
     888        /**
     889         * Return a list of all files contained inside a directory.
     890         *
     891         * @since 4.0.0
     892         *
     893         * @param string $dir Path to the directory to scan.
     894         *
     895         * @return array List of file paths.
     896         */
    788897        function files_in_dir( $dir ) {
    789898                $files = array();
    790899
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    799908                return $files;
    800909        }
    801910
     911        /**
     912         * Return a list of all files contained inside the `uploads` directory.
     913         *
     914         * @since 4.0.0
     915         *
     916         * @return array List of file paths.
     917         */
    802918        function scan_user_uploads() {
    803919                static $files = array();
    804920                if ( ! empty( $files ) ) {
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    810926                return $files;
    811927        }
    812928
     929        /**
     930         * Delete all directories contained inside a directory.
     931         *
     932         * @since 4.1.0
     933         *
     934         * @param string $path Path to the directory to scan.
     935         */
    813936        function delete_folders( $path ) {
    814937                $this->matched_dirs = array();
    815938                if ( ! is_dir( $path ) ) {
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    823946                rmdir( $path );
    824947        }
    825948
     949        /**
     950         * Helper for `delete_folders()` method.
     951         *
     952         * Retrieves all directories contained inside a directory and stores them in the `$matched_dirs` property. Hidden
     953         * directories are ignored.
     954         *
     955         * @since 4.1.0
     956         *
     957         * @param string $dir Path to the directory to scan.
     958         */
    826959        function scandir( $dir ) {
    827960                foreach ( scandir( $dir ) as $path ) {
    828961                        if ( 0 !== strpos( $path, '.' ) && is_dir( $dir . '/' . $path ) ) {
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    833966        }
    834967
    835968        /**
    836          * Helper to Convert a microtime string into a float
     969         * Helper to convert a microtime string into a float.
     970         *
     971         * @since 4.1.0
     972         *
     973         * @param string $microtime Time string generated by `microtime()`.
     974         *
     975         * @return float `microtime()` output as a float.
    837976         */
    838977        protected function _microtime_to_float($microtime ){
    839978                $time_array = explode( ' ', $microtime );
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    844983         * Multisite-agnostic way to delete a user from the database.
    845984         *
    846985         * @since 4.3.0
     986         *
     987         * @param int $user_id User ID.
     988         *
     989         * @return bool True if the user was deleted.
    847990         */
    848991        public static function delete_user( $user_id ) {
    849992                if ( is_multisite() ) {
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    8701013                $wp_rewrite->flush_rules();
    8711014        }
    8721015
     1016        /**
     1017         * Create an attachment post from an uploaded file.
     1018         *
     1019         * @since 4.4.0
     1020         *
     1021         * @param array $upload         Array of information about the uploaded file, provided by wp_upload_bits().
     1022         * @param int   $parent_post_id Optional. Parent post ID.
     1023         *
     1024         * @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure.
     1025         */
    8731026        function _make_attachment($upload, $parent_post_id = 0) {
    8741027                $type = '';
    8751028                if ( !empty($upload['type']) ) {
    class WP_UnitTestCase extends PHPUnit_Framework_TestCase { 
    8891042                        'guid' => $upload[ 'url' ],
    8901043                );
    8911044
    892                 // Save the data
    8931045                $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent_post_id );
    8941046                wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
    8951047                return $id;
    8961048        }
    8971049
    8981050        /**
    899          * There's no way to change post_modified through WP functions.
     1051         * Update the modified and modified GMT date of a post in the database.
     1052         *
     1053         * @since 4.8.0
     1054         *
     1055         * @global wpdb $wpdb WordPress database abstraction object.
     1056         *
     1057         * @param int    $post_id Post ID.
     1058         * @param string $date    Post date, in the format YYYY-MM-DD HH:MM:SS.
     1059         *
     1060         * @return int|false 1 on success, or false on error.
    9001061         */
    9011062        protected function update_post_modified( $post_id, $date ) {
    9021063                global $wpdb;