diff --git tests/phpunit/includes/testcase.php tests/phpunit/includes/testcase.php
index 4b47ddca0a..914826a42f 100644
|
|
|
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
| 513 | 513 | $this->assertEquals( preg_replace( '/\s*/', '', $expected ), preg_replace( '/\s*/', '', $actual ) ); |
| 514 | 514 | } |
| 515 | 515 | |
| | 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 | */ |
| 516 | 524 | function assertEqualSets( $expected, $actual ) { |
| 517 | 525 | sort( $expected ); |
| 518 | 526 | sort( $actual ); |
| 519 | 527 | $this->assertEquals( $expected, $actual ); |
| 520 | 528 | } |
| 521 | 529 | |
| | 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 | */ |
| 522 | 538 | function assertEqualSetsWithIndex( $expected, $actual ) { |
| 523 | 539 | ksort( $expected ); |
| 524 | 540 | ksort( $actual ); |
| … |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
| 526 | 542 | } |
| 527 | 543 | |
| 528 | 544 | /** |
| 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. |
| 530 | 546 | * |
| 531 | | * @param array $array |
| | 547 | * @since 4.8.0 |
| | 548 | * |
| | 549 | * @param array $array Array to check. |
| 532 | 550 | */ |
| 533 | 551 | function assertNonEmptyMultidimensionalArray( $array ) { |
| 534 | 552 | $this->assertTrue( is_array( $array ) ); |
| … |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
| 541 | 559 | } |
| 542 | 560 | |
| 543 | 561 | /** |
| 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. |
| 545 | 566 | * |
| 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. |
| 548 | 571 | * |
| 549 | 572 | * @throws PHPUnit_Framework_AssertionFailedError |
| 550 | 573 | */ |
| … |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
| 553 | 576 | } |
| 554 | 577 | |
| 555 | 578 | /** |
| 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 |
| 557 | 588 | * |
| 558 | 589 | * @param string $url The URL for the request. |
| 559 | 590 | */ |
| … |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
| 600 | 631 | $GLOBALS['wp']->main($parts['query']); |
| 601 | 632 | } |
| 602 | 633 | |
| | 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 | */ |
| 603 | 644 | protected function checkRequirements() { |
| 604 | 645 | parent::checkRequirements(); |
| 605 | 646 | |
| … |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
| 634 | 675 | } |
| 635 | 676 | |
| 636 | 677 | /** |
| 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. |
| 638 | 683 | */ |
| 639 | 684 | function knownWPBug( $ticket_id ) { |
| 640 | 685 | if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( $ticket_id, self::$forced_tickets ) ) |
| … |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
| 644 | 689 | } |
| 645 | 690 | |
| 646 | 691 | /** |
| 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. |
| 648 | 699 | */ |
| 649 | 700 | function knownUTBug( $ticket_id ) { |
| 650 | 701 | return; |
| 651 | 702 | } |
| 652 | 703 | |
| 653 | 704 | /** |
| 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. |
| 655 | 710 | */ |
| 656 | 711 | function knownPluginBug( $ticket_id ) { |
| 657 | 712 | if ( WP_TESTS_FORCE_KNOWN_BUGS || in_array( 'Plugin' . $ticket_id, self::$forced_tickets ) ) |
| … |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
| 660 | 715 | $this->markTestSkipped( sprintf( 'WordPress Plugin Ticket #%d is not fixed', $ticket_id ) ); |
| 661 | 716 | } |
| 662 | 717 | |
| | 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 | */ |
| 663 | 725 | public static function forceTicket( $ticket ) { |
| 664 | 726 | self::$forced_tickets[] = $ticket; |
| 665 | 727 | } |
| 666 | 728 | |
| 667 | 729 | /** |
| 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 |
| 669 | 738 | */ |
| 670 | 739 | function prepareTemplate( Text_Template $template ) { |
| 671 | 740 | $template->setVar( array( 'constants' => '' ) ); |
| … |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
| 674 | 743 | } |
| 675 | 744 | |
| 676 | 745 | /** |
| 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. |
| 678 | 753 | */ |
| 679 | 754 | function temp_filename() { |
| 680 | 755 | $tmp_dir = ''; |
| … |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
| 698 | 773 | * expected to be false. For example, assertQueryTrue('is_single', 'is_feed') means is_single() |
| 699 | 774 | * and is_feed() must be true and everything else must be false to pass. |
| 700 | 775 | * |
| | 776 | * @since 2.5.0 |
| | 777 | * @since 3.8.0 Moved from `Tests_Query_Conditionals` to `WP_UnitTestCase`. |
| | 778 | * |
| 701 | 779 | * @param string $prop,... Any number of WP_Query properties that are expected to be true for the current request. |
| 702 | 780 | */ |
| 703 | 781 | function assertQueryTrue(/* ... */) { |
| … |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
| 760 | 838 | } |
| 761 | 839 | } |
| 762 | 840 | |
| | 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 | */ |
| 763 | 848 | function unlink( $file ) { |
| 764 | 849 | $exists = is_file( $file ); |
| 765 | 850 | if ( $exists && ! in_array( $file, self::$ignore_files ) ) { |
| 766 | | //error_log( $file ); |
| 767 | 851 | unlink( $file ); |
| 768 | 852 | } elseif ( ! $exists ) { |
| 769 | 853 | $this->fail( "Trying to delete a file that doesn't exist: $file" ); |
| 770 | 854 | } |
| 771 | 855 | } |
| 772 | 856 | |
| | 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 | */ |
| 773 | 864 | function rmdir( $path ) { |
| 774 | 865 | $files = $this->files_in_dir( $path ); |
| 775 | 866 | foreach ( $files as $file ) { |
| … |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
| 779 | 870 | } |
| 780 | 871 | } |
| 781 | 872 | |
| | 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 | */ |
| 782 | 882 | function remove_added_uploads() { |
| 783 | | // Remove all uploads. |
| 784 | 883 | $uploads = wp_upload_dir(); |
| 785 | 884 | $this->rmdir( $uploads['basedir'] ); |
| 786 | 885 | } |
| 787 | 886 | |
| | 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 | */ |
| 788 | 896 | function files_in_dir( $dir ) { |
| 789 | 897 | $files = array(); |
| 790 | 898 | |
| … |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
| 799 | 907 | return $files; |
| 800 | 908 | } |
| 801 | 909 | |
| | 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 | */ |
| 802 | 917 | function scan_user_uploads() { |
| 803 | 918 | static $files = array(); |
| 804 | 919 | if ( ! empty( $files ) ) { |
| … |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
| 810 | 925 | return $files; |
| 811 | 926 | } |
| 812 | 927 | |
| | 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 | */ |
| 813 | 935 | function delete_folders( $path ) { |
| 814 | 936 | $this->matched_dirs = array(); |
| 815 | 937 | if ( ! is_dir( $path ) ) { |
| … |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
| 823 | 945 | rmdir( $path ); |
| 824 | 946 | } |
| 825 | 947 | |
| | 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 | */ |
| 826 | 958 | function scandir( $dir ) { |
| 827 | 959 | foreach ( scandir( $dir ) as $path ) { |
| 828 | 960 | if ( 0 !== strpos( $path, '.' ) && is_dir( $dir . '/' . $path ) ) { |
| … |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
| 833 | 965 | } |
| 834 | 966 | |
| 835 | 967 | /** |
| 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. |
| 837 | 975 | */ |
| 838 | 976 | protected function _microtime_to_float($microtime ){ |
| 839 | 977 | $time_array = explode( ' ', $microtime ); |
| … |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
| 844 | 982 | * Multisite-agnostic way to delete a user from the database. |
| 845 | 983 | * |
| 846 | 984 | * @since 4.3.0 |
| | 985 | * |
| | 986 | * @param int $user_id User ID. |
| | 987 | * |
| | 988 | * @return bool True if the user was deleted. |
| 847 | 989 | */ |
| 848 | 990 | public static function delete_user( $user_id ) { |
| 849 | 991 | if ( is_multisite() ) { |
| … |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
| 870 | 1012 | $wp_rewrite->flush_rules(); |
| 871 | 1013 | } |
| 872 | 1014 | |
| | 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 | */ |
| 873 | 1025 | function _make_attachment($upload, $parent_post_id = 0) { |
| 874 | 1026 | $type = ''; |
| 875 | 1027 | if ( !empty($upload['type']) ) { |
| … |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
| 889 | 1041 | 'guid' => $upload[ 'url' ], |
| 890 | 1042 | ); |
| 891 | 1043 | |
| 892 | | // Save the data |
| 893 | 1044 | $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent_post_id ); |
| 894 | 1045 | wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) ); |
| 895 | 1046 | return $id; |
| 896 | 1047 | } |
| 897 | 1048 | |
| 898 | 1049 | /** |
| 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. |
| 900 | 1060 | */ |
| 901 | 1061 | protected function update_post_modified( $post_id, $date ) { |
| 902 | 1062 | global $wpdb; |