Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (7 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/multisite/updateBlogDetails.php

    r38935 r42343  
    33if ( is_multisite() ) :
    44
    5 /**
    6  * @group ms-site
    7  * @group multisite
    8  */
    9 class Tests_Multisite_Update_Blog_Details extends WP_UnitTestCase {
    105    /**
    11      * If `update_blog_details()` is called with any kind of empty arguments, it
    12      * should return false.
     6     * @group ms-site
     7     * @group multisite
    138     */
    14     function test_update_blog_details_with_empty_args() {
    15         $result = update_blog_details( 1, array() );
    16         $this->assertFalse( $result );
    17     }
    18 
    19     /**
    20      * If the ID passed is not that of a current site, we should expect false.
    21      */
    22     function test_update_blog_details_invalid_blog_id() {
    23         $result = update_blog_details( 999, array( 'domain' => 'example.com' ) );
    24         $this->assertFalse( $result );
    25     }
    26 
    27     function test_update_blog_details() {
    28         $blog_id = self::factory()->blog->create();
    29 
    30         $result = update_blog_details( $blog_id, array( 'domain' => 'example.com', 'path' => 'my_path/' ) );
    31 
    32         $this->assertTrue( $result );
    33 
    34         $blog = get_site( $blog_id );
    35 
    36         $this->assertEquals( 'example.com', $blog->domain );
    37         $this->assertEquals( '/my_path/', $blog->path );
    38         $this->assertEquals( '0', $blog->spam );
    39     }
    40 
    41     /**
    42      * Test each of the actions that should fire in update_blog_details() depending on
    43      * the flag and flag value being set. Each action should fire once and should not
    44      * fire if a flag is already set for the given flag value.
    45      *
    46      * @param string $flag       The name of the flag being set or unset on a site.
    47      * @param string $flag_value '0' or '1'. The value of the flag being set.
    48      * @param string $action     The hook expected to fire for the flag name and flag combination.
    49      *
    50      * @dataProvider data_flag_hooks
    51      */
    52     public function test_update_blog_details_flag_action( $flag, $flag_value, $hook ) {
    53         global $test_action_counter;
    54         $test_action_counter = 0;
    55 
    56         $blog_id = self::factory()->blog->create();
    57 
    58         // Set an initial value of '1' for the flag when '0' is the flag value being tested.
    59         if ( '0' === $flag_value ) {
    60             update_blog_details( $blog_id, array( $flag => '1' ) );
     9    class Tests_Multisite_Update_Blog_Details extends WP_UnitTestCase {
     10        /**
     11         * If `update_blog_details()` is called with any kind of empty arguments, it
     12         * should return false.
     13         */
     14        function test_update_blog_details_with_empty_args() {
     15            $result = update_blog_details( 1, array() );
     16            $this->assertFalse( $result );
    6117        }
    6218
    63         add_action( $hook, array( $this, '_action_counter_cb' ), 10 );
     19        /**
     20         * If the ID passed is not that of a current site, we should expect false.
     21         */
     22        function test_update_blog_details_invalid_blog_id() {
     23            $result = update_blog_details( 999, array( 'domain' => 'example.com' ) );
     24            $this->assertFalse( $result );
     25        }
    6426
    65         update_blog_details( $blog_id, array( $flag => $flag_value ) );
    66         $blog = get_site( $blog_id );
     27        function test_update_blog_details() {
     28            $blog_id = self::factory()->blog->create();
    6729
    68         $this->assertEquals( $flag_value, $blog->{$flag} );
     30            $result = update_blog_details(
     31                $blog_id, array(
     32                    'domain' => 'example.com',
     33                    'path'   => 'my_path/',
     34                )
     35            );
    6936
    70         // The hook attached to this flag should have fired once during update_blog_details().
    71         $this->assertEquals( 1, $test_action_counter );
     37            $this->assertTrue( $result );
    7238
    73         // Update the site to the exact same flag value for this flag.
    74         update_blog_details( $blog_id, array( $flag => $flag_value ) );
     39            $blog = get_site( $blog_id );
    7540
    76         // The hook attached to this flag should not have fired again.
    77         $this->assertEquals( 1, $test_action_counter );
     41            $this->assertEquals( 'example.com', $blog->domain );
     42            $this->assertEquals( '/my_path/', $blog->path );
     43            $this->assertEquals( '0', $blog->spam );
     44        }
    7845
    79         remove_action( $hook, array( $this, '_action_counter_cb' ), 10 );
     46        /**
     47         * Test each of the actions that should fire in update_blog_details() depending on
     48         * the flag and flag value being set. Each action should fire once and should not
     49         * fire if a flag is already set for the given flag value.
     50         *
     51         * @param string $flag       The name of the flag being set or unset on a site.
     52         * @param string $flag_value '0' or '1'. The value of the flag being set.
     53         * @param string $action     The hook expected to fire for the flag name and flag combination.
     54         *
     55         * @dataProvider data_flag_hooks
     56         */
     57        public function test_update_blog_details_flag_action( $flag, $flag_value, $hook ) {
     58            global $test_action_counter;
     59            $test_action_counter = 0;
     60
     61            $blog_id = self::factory()->blog->create();
     62
     63            // Set an initial value of '1' for the flag when '0' is the flag value being tested.
     64            if ( '0' === $flag_value ) {
     65                update_blog_details( $blog_id, array( $flag => '1' ) );
     66            }
     67
     68            add_action( $hook, array( $this, '_action_counter_cb' ), 10 );
     69
     70            update_blog_details( $blog_id, array( $flag => $flag_value ) );
     71            $blog = get_site( $blog_id );
     72
     73            $this->assertEquals( $flag_value, $blog->{$flag} );
     74
     75            // The hook attached to this flag should have fired once during update_blog_details().
     76            $this->assertEquals( 1, $test_action_counter );
     77
     78            // Update the site to the exact same flag value for this flag.
     79            update_blog_details( $blog_id, array( $flag => $flag_value ) );
     80
     81            // The hook attached to this flag should not have fired again.
     82            $this->assertEquals( 1, $test_action_counter );
     83
     84            remove_action( $hook, array( $this, '_action_counter_cb' ), 10 );
     85        }
     86
     87        public function data_flag_hooks() {
     88            return array(
     89                array( 'spam', '0', 'make_ham_blog' ),
     90                array( 'spam', '1', 'make_spam_blog' ),
     91                array( 'archived', '1', 'archive_blog' ),
     92                array( 'archived', '0', 'unarchive_blog' ),
     93                array( 'deleted', '1', 'make_delete_blog' ),
     94                array( 'deleted', '0', 'make_undelete_blog' ),
     95                array( 'mature', '1', 'mature_blog' ),
     96                array( 'mature', '0', 'unmature_blog' ),
     97            );
     98        }
     99
     100        /**
     101         * Provide a counter to determine that hooks are firing when intended.
     102         */
     103        function _action_counter_cb() {
     104            global $test_action_counter;
     105            $test_action_counter++;
     106        }
     107
     108        /**
     109         * When the path for a site is updated with update_blog_details(), the final path
     110         * should have a leading and trailing slash.
     111         *
     112         * @dataProvider data_single_directory_path
     113         */
     114        public function test_update_blog_details_single_directory_path( $path, $expected ) {
     115            update_blog_details( 1, array( 'path' => $path ) );
     116            $site = get_site( 1 );
     117
     118            $this->assertEquals( $expected, $site->path );
     119        }
     120
     121        public function data_single_directory_path() {
     122            return array(
     123                array( 'my_path', '/my_path/' ),
     124                array( 'my_path//', '/my_path/' ),
     125                array( '//my_path', '/my_path/' ),
     126                array( 'my_path/', '/my_path/' ),
     127                array( '/my_path', '/my_path/' ),
     128                array( '/my_path/', '/my_path/' ),
     129
     130                array( 'multiple/dirs', '/multiple/dirs/' ),
     131                array( '/multiple/dirs', '/multiple/dirs/' ),
     132                array( 'multiple/dirs/', '/multiple/dirs/' ),
     133                array( '/multiple/dirs/', '/multiple/dirs/' ),
     134
     135                // update_blog_details() does not resolve multiple slashes in the middle of a path string.
     136                array( 'multiple///dirs', '/multiple///dirs/' ),
     137            );
     138        }
     139
    80140    }
    81 
    82     public function data_flag_hooks() {
    83         return array(
    84             array( 'spam',     '0', 'make_ham_blog' ),
    85             array( 'spam',     '1', 'make_spam_blog' ),
    86             array( 'archived', '1', 'archive_blog' ),
    87             array( 'archived', '0', 'unarchive_blog' ),
    88             array( 'deleted',  '1', 'make_delete_blog' ),
    89             array( 'deleted',  '0', 'make_undelete_blog' ),
    90             array( 'mature',   '1', 'mature_blog' ),
    91             array( 'mature',   '0', 'unmature_blog' ),
    92         );
    93     }
    94 
    95     /**
    96      * Provide a counter to determine that hooks are firing when intended.
    97      */
    98     function _action_counter_cb() {
    99         global $test_action_counter;
    100         $test_action_counter++;
    101     }
    102 
    103     /**
    104      * When the path for a site is updated with update_blog_details(), the final path
    105      * should have a leading and trailing slash.
    106      *
    107      * @dataProvider data_single_directory_path
    108      */
    109     public function test_update_blog_details_single_directory_path( $path, $expected ) {
    110         update_blog_details( 1, array( 'path' => $path ) );
    111         $site = get_site( 1 );
    112 
    113         $this->assertEquals( $expected, $site->path );
    114     }
    115 
    116     public function data_single_directory_path() {
    117         return array(
    118             array( 'my_path',   '/my_path/' ),
    119             array( 'my_path//', '/my_path/' ),
    120             array( '//my_path', '/my_path/' ),
    121             array( 'my_path/',  '/my_path/' ),
    122             array( '/my_path',  '/my_path/' ),
    123             array( '/my_path/', '/my_path/' ),
    124 
    125             array( 'multiple/dirs',   '/multiple/dirs/' ),
    126             array( '/multiple/dirs',  '/multiple/dirs/' ),
    127             array( 'multiple/dirs/',  '/multiple/dirs/' ),
    128             array( '/multiple/dirs/', '/multiple/dirs/' ),
    129 
    130             // update_blog_details() does not resolve multiple slashes in the middle of a path string.
    131             array( 'multiple///dirs', '/multiple///dirs/' ),
    132         );
    133     }
    134 
    135 }
    136141endif;
Note: See TracChangeset for help on using the changeset viewer.