WordPress.org

Make WordPress Core

Changeset 38762


Ignore:
Timestamp:
10/09/2016 01:11:14 AM (20 months ago)
Author:
johnbillion
Message:

Build/Test Tools: Begin eliminating unnecessary randomness in tests.

Although unlikely, clashes in randomly generated strings could cause unexpected failures. In addition, most randomness is entirely unnecessary, is bad practice, and increases test time (however small it may be).

See #37371

Location:
trunk/tests/phpunit/tests
Files:
21 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/file.php

    r36882 r38762  
    8585    function test_unique_filename_is_valid() {
    8686        // make sure it produces a valid, writable, unique filename
    87         $filename = wp_unique_filename( $this->dir, rand_str() . '.txt' );
     87        $filename = wp_unique_filename( $this->dir, __FUNCTION__ . '.txt' );
    8888
    8989        $this->assertTrue( $this->is_unique_writable_file($this->dir, $filename) );
     
    9494    function test_unique_filename_is_unique() {
    9595        // make sure it produces two unique filenames
    96         $name = rand_str();
     96        $name = __FUNCTION__;
    9797
    9898        $filename1 = wp_unique_filename( $this->dir, $name . '.txt' );
     
    109109
    110110    function test_unique_filename_is_sanitized() {
    111         $name = rand_str();
     111        $name = __FUNCTION__;
    112112        $filename = wp_unique_filename( $this->dir, $name . $this->badchars .  '.txt' );
    113113
     
    121121
    122122    function test_unique_filename_with_slashes() {
    123         $name = rand_str();
     123        $name = __FUNCTION__;
    124124        // "foo/foo.txt"
    125125        $filename = wp_unique_filename( $this->dir, $name . '/' . $name .  '.txt' );
     
    134134
    135135    function test_unique_filename_multiple_ext() {
    136         $name = rand_str();
     136        $name = __FUNCTION__;
    137137        $filename = wp_unique_filename( $this->dir, $name . '.php.txt' );
    138138
     
    146146
    147147    function test_unique_filename_no_ext() {
    148         $name = rand_str();
     148        $name = __FUNCTION__;
    149149        $filename = wp_unique_filename( $this->dir, $name );
    150150
  • trunk/tests/phpunit/tests/filters.php

    r38571 r38762  
    1010    function test_simple_filter() {
    1111        $a = new MockAction();
    12         $tag = rand_str();
    13         $val = rand_str();
     12        $tag = __FUNCTION__;
     13        $val = __FUNCTION__ . '_val';
    1414
    1515        add_filter($tag, array($a, 'filter'));
     
    2828    function test_remove_filter() {
    2929        $a = new MockAction();
    30         $tag = rand_str();
    31         $val = rand_str();
     30        $tag = __FUNCTION__;
     31        $val = __FUNCTION__ . '_val';
    3232
    3333        add_filter($tag, array($a, 'filter'));
     
    4747
    4848    function test_has_filter() {
    49             $tag = rand_str();
    50             $func = rand_str();
     49            $tag  = __FUNCTION__;
     50            $func = __FUNCTION__ . '_func';
    5151
    5252            $this->assertFalse( has_filter($tag, $func) );
     
    6464        $a1 = new MockAction();
    6565        $a2 = new MockAction();
    66         $tag = rand_str();
    67         $val = rand_str();
     66        $tag = __FUNCTION__;
     67        $val = __FUNCTION__ . '_val';
    6868
    6969        // add both filters to the hook
     
    8080    function test_filter_args_1() {
    8181        $a = new MockAction();
    82         $tag = rand_str();
    83         $val = rand_str();
    84         $arg1 = rand_str();
     82        $tag  = __FUNCTION__;
     83        $val  = __FUNCTION__ . '_val';
     84        $arg1 = __FUNCTION__ . '_arg1';
    8585
    8686        add_filter($tag, array($a, 'filter'), 10, 2);
     
    9696        $a1 = new MockAction();
    9797        $a2 = new MockAction();
    98         $tag = rand_str();
    99         $val = rand_str();
    100         $arg1 = rand_str();
    101         $arg2 = rand_str();
     98        $tag  = __FUNCTION__;
     99        $val  = __FUNCTION__ . '_val';
     100        $arg1 = __FUNCTION__ . '_arg1';
     101        $arg2 = __FUNCTION__ . '_arg2';
    102102
    103103        // a1 accepts two arguments, a2 doesn't
     
    120120    function test_filter_priority() {
    121121        $a = new MockAction();
    122         $tag = rand_str();
    123         $val = rand_str();
     122        $tag = __FUNCTION__;
     123        $val = __FUNCTION__ . '_val';
    124124
    125125        // make two filters with different priorities
     
    151151    function test_all_filter() {
    152152        $a = new MockAction();
    153         $tag1 = rand_str();
    154         $tag2 = rand_str();
    155         $val = rand_str();
     153        $tag1 = __FUNCTION__ . '_1';
     154        $tag2 = __FUNCTION__ . '_2';
     155        $val  = __FUNCTION__ . '_val';
    156156
    157157        // add an 'all' filter
     
    175175    function test_remove_all_filter() {
    176176        $a = new MockAction();
    177         $tag = rand_str();
    178         $val = rand_str();
     177        $tag = __FUNCTION__;
     178        $val = __FUNCTION__ . '_val';
    179179
    180180        add_filter('all', array($a, 'filterall'));
     
    202202    function test_remove_all_filters_should_respect_the_priority_argument() {
    203203        $a = new MockAction();
    204         $tag = rand_str();
    205         $val = rand_str();
     204        $tag = __FUNCTION__;
     205        $val = __FUNCTION__ . '_val';
    206206
    207207        add_filter( $tag, array( $a, 'filter' ), 12 );
     
    222222        $obj = new stdClass();
    223223        $a = new MockAction();
    224         $tag = rand_str();
     224        $tag = __FUNCTION__;
    225225
    226226        add_action($tag, array($a, 'filter'));
     
    242242        $a = new MockAction();
    243243        $b = new MockAction();
    244         $tag = rand_str();
     244        $tag = __FUNCTION__;
    245245
    246246        add_action($tag, array($a, 'filter_append'), 10, 2);
     
    275275    function test_has_filter_after_remove_all_filters() {
    276276        $a = new MockAction();
    277         $tag = rand_str();
    278         $val = rand_str();
     277        $tag = __FUNCTION__;
     278        $val = __FUNCTION__ . '_val';
    279279
    280280        // No priority
  • trunk/tests/phpunit/tests/general/document-title.php

    r36168 r38762  
    2929            'post_author'  => $this->author_id,
    3030            'post_status'  => 'publish',
    31             'post_content' => rand_str(),
    3231            'post_title'   => 'test_title',
    3332            'post_type'    => 'post',
  • trunk/tests/phpunit/tests/hooks/add_filter.php

    r38571 r38762  
    1414        $callback = '__return_null';
    1515        $hook = new WP_Hook();
    16         $tag = rand_str();
     16        $tag = __FUNCTION__;
    1717        $priority = rand( 1, 100 );
    1818        $accepted_args = rand( 1, 100 );
     
    2929        $callback = array( $a, 'action' );
    3030        $hook = new WP_Hook();
    31         $tag = rand_str();
     31        $tag = __FUNCTION__;
    3232        $priority = rand( 1, 100 );
    3333        $accepted_args = rand( 1, 100 );
     
    4343        $callback = array( 'MockAction', 'action' );
    4444        $hook = new WP_Hook();
    45         $tag = rand_str();
     45        $tag = __FUNCTION__;
    4646        $priority = rand( 1, 100 );
    4747        $accepted_args = rand( 1, 100 );
     
    5858        $callback_two = '__return_false';
    5959        $hook = new WP_Hook();
    60         $tag = rand_str();
     60        $tag = __FUNCTION__;
    6161        $priority = rand( 1, 100 );
    6262        $accepted_args = rand( 1, 100 );
     
    7373        $callback_two = '__return_false';
    7474        $hook = new WP_Hook();
    75         $tag = rand_str();
     75        $tag = __FUNCTION__;
    7676        $priority = rand( 1, 100 );
    7777        $accepted_args = rand( 1, 100 );
     
    8888        $callback = '__return_null';
    8989        $hook = new WP_Hook();
    90         $tag = rand_str();
     90        $tag = __FUNCTION__;
    9191        $priority = rand( 1, 100 );
    9292        $accepted_args = rand( 1, 100 );
     
    102102        $callback = '__return_null';
    103103        $hook = new WP_Hook();
    104         $tag = rand_str();
     104        $tag = __FUNCTION__;
    105105        $priority = rand( 1, 100 );
    106106        $accepted_args = rand( 1, 100 );
     
    119119        $c = new MockAction();
    120120        $hook = new WP_Hook();
    121         $tag = rand_str();
     121        $tag = __FUNCTION__;
    122122
    123123        $hook->add_filter( $tag, array( $a, 'action' ), 10, 1 );
  • trunk/tests/phpunit/tests/hooks/apply_filters.php

    r38571 r38762  
    1212        $callback = array( $a, 'filter' );
    1313        $hook = new WP_Hook();
    14         $tag = rand_str();
     14        $tag = __FUNCTION__;
    1515        $priority = rand( 1, 100 );
    1616        $accepted_args = rand( 1, 100 );
    17         $arg = rand_str();
     17        $arg = __FUNCTION__ . '_arg';
    1818
    1919        $hook->add_filter( $tag, $callback, $priority, $accepted_args );
     
    2929        $callback = array( $a, 'filter' );
    3030        $hook = new WP_Hook();
    31         $tag = rand_str();
     31        $tag = __FUNCTION__;
    3232        $priority = rand( 1, 100 );
    3333        $accepted_args = rand( 1, 100 );
    34         $arg = rand_str();
     34        $arg = __FUNCTION__ . '_arg';
    3535
    3636        $hook->add_filter( $tag, $callback, $priority, $accepted_args );
  • trunk/tests/phpunit/tests/hooks/do_action.php

    r38571 r38762  
    2020        $callback = array( $a, 'action' );
    2121        $hook = new WP_Hook();
    22         $tag = rand_str();
     22        $tag = __FUNCTION__;
    2323        $priority = rand( 1, 100 );
    2424        $accepted_args = rand( 1, 100 );
    25         $arg = rand_str();
     25        $arg = __FUNCTION__ . '_arg';
    2626
    2727        $hook->add_filter( $tag, $callback, $priority, $accepted_args );
     
    3535        $callback = array( $a, 'filter' );
    3636        $hook = new WP_Hook();
    37         $tag = rand_str();
     37        $tag = __FUNCTION__;
    3838        $priority = rand( 1, 100 );
    3939        $accepted_args = rand( 1, 100 );
    40         $arg = rand_str();
     40        $arg = __FUNCTION__ . '_arg';
    4141
    4242        $hook->add_filter( $tag, $callback, $priority, $accepted_args );
     
    5353        $callback_two = array( $b, 'filter' );
    5454        $hook = new WP_Hook();
    55         $tag = rand_str();
     55        $tag = __FUNCTION__;
    5656        $priority = rand( 1, 100 );
    5757        $accepted_args = rand( 1, 100 );
    58         $arg = rand_str();
     58        $arg = __FUNCTION__ . '_arg';
    5959
    6060        $hook->add_filter( $tag, $callback_one, $priority, $accepted_args );
     
    7272        $callback_two = array( $b, 'filter' );
    7373        $hook = new WP_Hook();
    74         $tag = rand_str();
     74        $tag = __FUNCTION__;
    7575        $priority = rand( 1, 100 );
    7676        $accepted_args = rand( 1, 100 );
    77         $arg = rand_str();
     77        $arg = __FUNCTION__ . '_arg';
    7878
    7979        $hook->add_filter( $tag, $callback_one, $priority, $accepted_args );
     
    8888        $callback = array( $this, '_action_callback' );
    8989        $hook = new WP_Hook();
    90         $tag = rand_str();
     90        $tag = __FUNCTION__;
    9191        $priority = rand( 1, 100 );
    9292        $accepted_args = 0;
    93         $arg = rand_str();
     93        $arg = __FUNCTION__ . '_arg';
    9494
    9595        $hook->add_filter( $tag, $callback, $priority, $accepted_args );
     
    102102        $callback = array( $this, '_action_callback' );
    103103        $hook = new WP_Hook();
    104         $tag = rand_str();
     104        $tag = __FUNCTION__;
    105105        $priority = rand( 1, 100 );
    106106        $accepted_args = 1;
    107         $arg = rand_str();
     107        $arg = __FUNCTION__ . '_arg';
    108108
    109109        $hook->add_filter( $tag, $callback, $priority, $accepted_args );
     
    116116        $callback = array( $this, '_action_callback' );
    117117        $hook = new WP_Hook();
    118         $tag = rand_str();
     118        $tag = __FUNCTION__;
    119119        $priority = rand( 1, 100 );
    120120        $accepted_args = 1000;
    121         $arg = rand_str();
     121        $arg = __FUNCTION__ . '_arg';
    122122
    123123        $hook->add_filter( $tag, $callback, $priority, $accepted_args );
  • trunk/tests/phpunit/tests/hooks/do_all_hook.php

    r38571 r38762  
    1515        $priority = rand( 1, 100 );
    1616        $accepted_args = rand( 1, 100 );
    17         $arg = rand_str();
     17        $arg = 'all_arg';
    1818
    1919        $hook->add_filter( $tag, $callback, $priority, $accepted_args );
  • trunk/tests/phpunit/tests/hooks/has_filter.php

    r38571 r38762  
    1111        $callback = '__return_null';
    1212        $hook = new WP_Hook();
    13         $tag = rand_str();
     13        $tag = __FUNCTION__;
    1414        $priority = rand( 1, 100 );
    1515        $accepted_args = rand( 1, 100 );
     
    2424        $callback = array( $a, 'action' );
    2525        $hook = new WP_Hook();
    26         $tag = rand_str();
     26        $tag = __FUNCTION__;
    2727        $priority = rand( 1, 100 );
    2828        $accepted_args = rand( 1, 100 );
     
    3636        $callback = array( 'MockAction', 'action' );
    3737        $hook = new WP_Hook();
    38         $tag = rand_str();
     38        $tag = __FUNCTION__;
    3939        $priority = rand( 1, 100 );
    4040        $accepted_args = rand( 1, 100 );
     
    4848        $callback = '__return_null';
    4949        $hook = new WP_Hook();
    50         $tag = rand_str();
     50        $tag = __FUNCTION__;
    5151        $priority = rand( 1, 100 );
    5252        $accepted_args = rand( 1, 100 );
     
    6565        $callback = '__return_null';
    6666        $hook = new WP_Hook();
    67         $tag = rand_str();
     67        $tag = __FUNCTION__;
    6868
    6969        $this->assertFalse( $hook->has_filter( $tag, $callback ) );
     
    7373        $callback = '__return_null';
    7474        $hook = new WP_Hook();
    75         $tag = rand_str();
     75        $tag = __FUNCTION__;
    7676        $priority = rand( 1, 100 );
    7777        $accepted_args = rand( 1, 100 );
  • trunk/tests/phpunit/tests/hooks/has_filters.php

    r38571 r38762  
    1111        $callback = '__return_null';
    1212        $hook = new WP_Hook();
    13         $tag = rand_str();
     13        $tag = __FUNCTION__;
    1414        $priority = rand( 1, 100 );
    1515        $accepted_args = rand( 1, 100 );
     
    2828        $callback = '__return_null';
    2929        $hook = new WP_Hook();
    30         $tag = rand_str();
     30        $tag = __FUNCTION__;
    3131        $priority = rand( 1, 100 );
    3232        $accepted_args = rand( 1, 100 );
     
    4040        $callback = '__return_null';
    4141        $hook = new WP_Hook();
    42         $tag = rand_str();
     42        $tag = __FUNCTION__;
    4343        $priority = rand( 1, 100 );
    4444        $accepted_args = rand( 1, 100 );
  • trunk/tests/phpunit/tests/hooks/iterator.php

    r38571 r38762  
    1212        $callback_two = '__return_false';
    1313        $hook = new WP_Hook();
    14         $tag = rand_str();
     14        $tag = __FUNCTION__;
    1515        $priority = rand( 1, 100 );
    1616        $accepted_args = rand( 1, 100 );
  • trunk/tests/phpunit/tests/hooks/preinit_hooks.php

    r38571 r38762  
    99
    1010    public function test_array_to_hooks() {
    11         $tag1 = rand_str();
     11        $tag1 = __FUNCTION__ . '_1';
    1212        $priority1 = rand( 1, 100 );
    13         $tag2 = rand_str();
     13        $tag2 = __FUNCTION__ . '_2';
    1414        $priority2 = rand( 1, 100 );
    1515        $filters = array(
  • trunk/tests/phpunit/tests/hooks/remove_all_filters.php

    r38571 r38762  
    1111        $callback = '__return_null';
    1212        $hook = new WP_Hook();
    13         $tag = rand_str();
     13        $tag = __FUNCTION__;
    1414        $priority = rand( 1, 100 );
    1515        $accepted_args = rand( 1, 100 );
     
    2626        $callback_two = '__return_false';
    2727        $hook = new WP_Hook();
    28         $tag = rand_str();
     28        $tag = __FUNCTION__;
    2929        $priority = rand( 1, 100 );
    3030        $accepted_args = rand( 1, 100 );
  • trunk/tests/phpunit/tests/hooks/remove_filter.php

    r38571 r38762  
    1111        $callback = '__return_null';
    1212        $hook = new WP_Hook();
    13         $tag = rand_str();
     13        $tag = __FUNCTION__;
    1414        $priority = rand( 1, 100 );
    1515        $accepted_args = rand( 1, 100 );
     
    2525        $callback = array( $a, 'action' );
    2626        $hook = new WP_Hook();
    27         $tag = rand_str();
     27        $tag = __FUNCTION__;
    2828        $priority = rand( 1, 100 );
    2929        $accepted_args = rand( 1, 100 );
     
    3838        $callback = array( 'MockAction', 'action' );
    3939        $hook = new WP_Hook();
    40         $tag = rand_str();
     40        $tag = __FUNCTION__;
    4141        $priority = rand( 1, 100 );
    4242        $accepted_args = rand( 1, 100 );
     
    5252        $callback_two = '__return_false';
    5353        $hook = new WP_Hook();
    54         $tag = rand_str();
     54        $tag = __FUNCTION__;
    5555        $priority = rand( 1, 100 );
    5656        $accepted_args = rand( 1, 100 );
     
    6868        $callback_two = '__return_false';
    6969        $hook = new WP_Hook();
    70         $tag = rand_str();
     70        $tag = __FUNCTION__;
    7171        $priority = rand( 1, 100 );
    7272        $accepted_args = rand( 1, 100 );
  • trunk/tests/phpunit/tests/image/functions.php

    r38761 r38762  
    307307        $file = wp_crop_image( 'https://asdftestblog1.files.wordpress.com/2008/04/canola.jpg',
    308308                              0, 0, 100, 100, 100, 100, false,
    309                               DIR_TESTDATA . '/images/' . rand_str() . '.jpg' );
     309                              DIR_TESTDATA . '/images/' . __FUNCTION__ . '.jpg' );
    310310        $this->assertNotInstanceOf( 'WP_Error', $file );
    311311        $this->assertFileExists( $file );
  • trunk/tests/phpunit/tests/link/getPreviewPostLink.php

    r37566 r38762  
    5353
    5454    public function test_get_preview_post_link_should_return_empty_string_for_non_viewable_post_type() {
    55         $post_type = register_post_type( rand_str(12), array(
     55        $post_type = register_post_type( 'non_viewable_cpt', array(
    5656            'public' => false,
    5757        ) );
  • trunk/tests/phpunit/tests/meta.php

    r38699 r38762  
    162162
    163163    function test_metadata_slashes() {
    164         $key = rand_str();
     164        $key = __FUNCTION__;
    165165        $value = 'Test\\singleslash';
    166166        $expected = 'Testsingleslash';
  • trunk/tests/phpunit/tests/multisite/getSpaceUsed.php

    r35242 r38762  
    3636
    3737        // Upload a file to the new site.
    38         $filename = rand_str().'.jpg';
    39         $contents = rand_str();
     38        $filename = __FUNCTION__ . '.jpg';
     39        $contents = __FUNCTION__ . '_contents';
    4040        $file = wp_upload_bits( $filename, null, $contents );
    4141
     
    7272
    7373        // Upload a file to the new site.
    74         $filename = rand_str().'.jpg';
    75         $contents = rand_str();
     74        $filename = __FUNCTION__ . '.jpg';
     75        $contents = __FUNCTION__ . '_contents';
    7676        wp_upload_bits( $filename, null, $contents );
    7777
  • trunk/tests/phpunit/tests/multisite/ms-files-rewriting.php

    r35242 r38762  
    6262     */
    6363    function test_upload_directories_after_multiple_wpmu_delete_blog_with_ms_files() {
    64         $filename = rand_str().'.jpg';
    65         $contents = rand_str();
     64        $filename = __FUNCTION__ . '.jpg';
     65        $contents = __FUNCTION__ . '_contents';
    6666
    6767        // Upload a file to the main site on the network.
  • trunk/tests/phpunit/tests/multisite/site.php

    r38659 r38762  
    271271     */
    272272    function test_upload_directories_after_multiple_wpmu_delete_blog() {
    273         $filename = rand_str().'.jpg';
    274         $contents = rand_str();
     273        $filename = __FUNCTION__ . '.jpg';
     274        $contents = __FUNCTION__ . '_contents';
    275275
    276276        // Upload a file to the main site on the network.
  • trunk/tests/phpunit/tests/oembed/WpEmbed.php

    r37892 r38762  
    6565
    6666    public function test_wp_embed_register_handler() {
    67         $handle   = rand_str();
     67        $handle   = __FUNCTION__;
    6868        $regex    = '#https?://example\.com/embed/([^/]+)#i';
    6969        $callback = array( $this, '_embed_handler_callback' );
     
    103103
    104104    public function test_autoembed_should_return_modified_content() {
    105         $handle   = rand_str();
     105        $handle   = __FUNCTION__;
    106106        $regex    = '#https?://example\.com/embed/([^/]+)#i';
    107107        $callback = array( $this, '_embed_handler_callback' );
  • trunk/tests/phpunit/tests/option/multisite.php

    r35242 r38762  
    2626
    2727    function test_from_same_site() {
    28         $key = rand_str();
    29         $key2 = rand_str();
    30         $value = rand_str();
    31         $value2 = rand_str();
     28        $key    = __FUNCTION__ . '_1';
     29        $key2   = __FUNCTION__ . '_2';
     30        $value  = __FUNCTION__ . '_val1';
     31        $value2 = __FUNCTION__ . '_val2';
    3232
    3333        $this->assertFalse( get_blog_option( 1, 'doesnotexist' ) );
     
    6363
    6464    function test_from_same_site_with_null_blog_id() {
    65         $key = rand_str();
    66         $key2 = rand_str();
    67         $value = rand_str();
    68         $value2 = rand_str();
     65        $key    = __FUNCTION__ . '_1';
     66        $key2   = __FUNCTION__ . '_2';
     67        $value  = __FUNCTION__ . '_val1';
     68        $value2 = __FUNCTION__ . '_val2';
    6969
    7070        $this->assertFalse( get_blog_option( null, 'doesnotexist' ) );
     
    110110        $this->assertInternalType( 'integer', $blog_id );
    111111
    112         $key = rand_str();
    113         $key2 = rand_str();
    114         $value = rand_str();
    115         $value2 = rand_str();
     112        $key    = __FUNCTION__ . '_key1';
     113        $key2   = __FUNCTION__ . '_key2';
     114        $value  = __FUNCTION__ . '_val1';
     115        $value2 = __FUNCTION__ . '_val2';
    116116
    117117        $this->assertFalse( get_blog_option( $blog_id, 'doesnotexist' ) );
Note: See TracChangeset for help on using the changeset viewer.