diff --git tests/phpunit/includes/testcase.php tests/phpunit/includes/testcase.php
index 4b47ddca0a..71117dc003 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 backported 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 ) ) { |
… |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
770 | 855 | } |
771 | 856 | } |
772 | 857 | |
| 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 | */ |
773 | 865 | function rmdir( $path ) { |
774 | 866 | $files = $this->files_in_dir( $path ); |
775 | 867 | foreach ( $files as $file ) { |
… |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
779 | 871 | } |
780 | 872 | } |
781 | 873 | |
| 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 | */ |
782 | 883 | function remove_added_uploads() { |
783 | | // Remove all uploads. |
784 | 884 | $uploads = wp_upload_dir(); |
785 | 885 | $this->rmdir( $uploads['basedir'] ); |
786 | 886 | } |
787 | 887 | |
| 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 | */ |
788 | 897 | function files_in_dir( $dir ) { |
789 | 898 | $files = array(); |
790 | 899 | |
… |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
799 | 908 | return $files; |
800 | 909 | } |
801 | 910 | |
| 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 | */ |
802 | 918 | function scan_user_uploads() { |
803 | 919 | static $files = array(); |
804 | 920 | if ( ! empty( $files ) ) { |
… |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
810 | 926 | return $files; |
811 | 927 | } |
812 | 928 | |
| 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 | */ |
813 | 936 | function delete_folders( $path ) { |
814 | 937 | $this->matched_dirs = array(); |
815 | 938 | if ( ! is_dir( $path ) ) { |
… |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
823 | 946 | rmdir( $path ); |
824 | 947 | } |
825 | 948 | |
| 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 | */ |
826 | 959 | function scandir( $dir ) { |
827 | 960 | foreach ( scandir( $dir ) as $path ) { |
828 | 961 | if ( 0 !== strpos( $path, '.' ) && is_dir( $dir . '/' . $path ) ) { |
… |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
833 | 966 | } |
834 | 967 | |
835 | 968 | /** |
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. |
837 | 976 | */ |
838 | 977 | protected function _microtime_to_float($microtime ){ |
839 | 978 | $time_array = explode( ' ', $microtime ); |
… |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
844 | 983 | * Multisite-agnostic way to delete a user from the database. |
845 | 984 | * |
846 | 985 | * @since 4.3.0 |
| 986 | * |
| 987 | * @param int $user_id User ID. |
| 988 | * |
| 989 | * @return bool True if the user was deleted. |
847 | 990 | */ |
848 | 991 | public static function delete_user( $user_id ) { |
849 | 992 | if ( is_multisite() ) { |
… |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
870 | 1013 | $wp_rewrite->flush_rules(); |
871 | 1014 | } |
872 | 1015 | |
| 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 | */ |
873 | 1026 | function _make_attachment($upload, $parent_post_id = 0) { |
874 | 1027 | $type = ''; |
875 | 1028 | if ( !empty($upload['type']) ) { |
… |
… |
class WP_UnitTestCase extends PHPUnit_Framework_TestCase { |
889 | 1042 | 'guid' => $upload[ 'url' ], |
890 | 1043 | ); |
891 | 1044 | |
892 | | // Save the data |
893 | 1045 | $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent_post_id ); |
894 | 1046 | wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) ); |
895 | 1047 | return $id; |
896 | 1048 | } |
897 | 1049 | |
898 | 1050 | /** |
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. |
900 | 1061 | */ |
901 | 1062 | protected function update_post_modified( $post_id, $date ) { |
902 | 1063 | global $wpdb; |