Make WordPress Core

Changeset 34802


Ignore:
Timestamp:
10/03/2015 05:14:12 PM (11 years ago)
Author:
DrewAPicture
Message:

Tests: Introduce WP_UnitTestCase::reset_permalinks(), an attempt to DRY up logic for resetting and restoring default permalinks on setUp() and tearDown().

See #33968.

Location:
trunk/tests/phpunit
Files:
17 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/testcase-canonical.php

    r32918 r34802  
    1717
    1818        public function setUp() {
    19                 global $wp_rewrite;
    20 
    2119                parent::setUp();
    2220
     
    2523                update_option( 'posts_per_page', 5 );
    2624
     25                global $wp_rewrite;
    2726                $wp_rewrite->init();
    2827                $wp_rewrite->set_permalink_structure( $this->structure );
     28
    2929                create_initial_taxonomies();
     30
    3031                $wp_rewrite->flush_rules();
    3132        }
  • trunk/tests/phpunit/includes/testcase.php

    r34720 r34802  
    1616
    1717        protected $db_version;
     18
     19        public static $default_permalink_structure;
    1820
    1921        /**
     
    7173                        add_filter( 'pre_option_db_version', array( $this, 'db_version' ) );
    7274                }
     75
     76                self::$default_permalink_structure = get_option( 'permalink_structure' );
     77
     78                $this->reset_permalinks();
    7379        }
    7480
     
    100106                $this->_restore_hooks();
    101107                wp_set_current_user( 0 );
     108
     109                $this->reset_permalinks( $restore = true );
    102110        }
    103111
     
    646654                return $this->db_version;
    647655        }
     656
     657        /**
     658         * Utility method that resets permalinks and flushes rewrites.
     659         *
     660         * Collects the current permalink structure and stores it in a class property for use
     661         * by sub-classes.
     662         *
     663         * @since 4.4.0
     664         *
     665         * @global WP_Rewrite $wp_rewrite
     666         *
     667         * @param bool $restore_default Optional. Whether to restore the default permalink structure.
     668         *                              Default false.
     669         */
     670        public function reset_permalinks( $restore_default = false ) {
     671                global $wp_rewrite;
     672
     673                if ( ! $restore_default ) {
     674                        $wp_rewrite->init();
     675                        $wp_rewrite->set_permalink_structure( '' );
     676                } else {
     677                        $wp_rewrite->set_permalink_structure( self::$default_permalink_structure );
     678                }
     679
     680                $wp_rewrite->flush_rules();
     681        }
    648682}
  • trunk/tests/phpunit/tests/admin/includesPost.php

    r34680 r34802  
    254254                global $wp_rewrite;
    255255
    256                 $old_permalink_structure = get_option( 'permalink_structure' );
    257256                $permalink_structure = '%postname%';
    258257                $wp_rewrite->set_permalink_structure( "/$permalink_structure/" );
     
    266265
    267266                $this->assertSame( $expected, $found[0] );
    268 
    269                 $wp_rewrite->set_permalink_structure( $old_permalink_structure );
    270                 flush_rewrite_rules();
    271267        }
    272268
     
    276272         */
    277273        public function test_get_sample_permalink_html_should_use_default_permalink_for_view_post_link_when_pretty_permalinks_are_disabled() {
    278                 global $wp_rewrite;
    279                 $old_permalink_structure = get_option( 'permalink_structure' );
    280                 $wp_rewrite->set_permalink_structure( '' );
    281                 flush_rewrite_rules();
    282 
    283274                wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
    284275
     
    288279                $found = get_sample_permalink_html( $p );
    289280                $this->assertContains( 'href="' . get_option( 'home' ) . '/?p=' . $p . '"', $found );
    290 
    291                 $wp_rewrite->set_permalink_structure( $old_permalink_structure );
    292                 flush_rewrite_rules();
    293281        }
    294282
     
    299287        public function test_get_sample_permalink_html_should_use_pretty_permalink_for_view_post_link_when_pretty_permalinks_are_enabled() {
    300288                global $wp_rewrite;
    301                 $old_permalink_structure = get_option( 'permalink_structure' );
    302289                $permalink_structure = '%postname%';
    303290                $wp_rewrite->set_permalink_structure( "/$permalink_structure/" );
     
    312299                $post = get_post( $p );
    313300                $this->assertContains( 'href="' . get_option( 'home' ) . "/" . $post->post_name . '/"', $found );
    314 
    315                 $wp_rewrite->set_permalink_structure( $old_permalink_structure );
    316                 flush_rewrite_rules();
    317301        }
    318302
     
    323307        public function test_get_sample_permalink_html_should_use_correct_permalink_for_view_post_link_when_changing_slug() {
    324308                global $wp_rewrite;
    325                 $old_permalink_structure = get_option( 'permalink_structure' );
    326309                $permalink_structure = '%postname%';
    327310                $wp_rewrite->set_permalink_structure( "/$permalink_structure/" );
     
    358341
    359342                $this->assertContains( 'href="' . esc_url( $preview_link ) . '"', $found, $message );
    360 
    361                 $wp_rewrite->set_permalink_structure( $old_permalink_structure );
    362                 flush_rewrite_rules();
    363343        }
    364344
     
    378358                $found = get_sample_permalink( $p );
    379359                $this->assertEquals( '2015-2', $found[1] );
    380 
    381                 $wp_rewrite->set_permalink_structure( '' );
    382                 flush_rewrite_rules();
    383360        }
    384361
     
    398375                $found = get_sample_permalink( $p );
    399376                $this->assertEquals( '2015', $found[1] );
    400 
    401                 $wp_rewrite->set_permalink_structure( '' );
    402                 flush_rewrite_rules();
    403377        }
    404378
     
    418392                $found = get_sample_permalink( $p );
    419393                $this->assertEquals( '11-2', $found[1] );
    420 
    421                 $wp_rewrite->set_permalink_structure( '' );
    422                 flush_rewrite_rules();
    423394        }
    424395
     
    438409                $found = get_sample_permalink( $p );
    439410                $this->assertEquals( '13', $found[1] );
    440 
    441                 $wp_rewrite->set_permalink_structure( '' );
    442                 flush_rewrite_rules();
    443411        }
    444412
     
    458426                $found = get_sample_permalink( $p );
    459427                $this->assertEquals( '30-2', $found[1] );
    460 
    461                 $wp_rewrite->set_permalink_structure( '' );
    462                 flush_rewrite_rules();
    463428        }
    464429
     
    482447                $found = get_sample_permalink( $p );
    483448                $this->assertEquals( '30-3', $found[1] );
    484 
    485                 $wp_rewrite->set_permalink_structure( '' );
    486                 flush_rewrite_rules();
    487449        }
    488450
     
    502464                $found = get_sample_permalink( $p );
    503465                $this->assertEquals( '32', $found[1] );
    504 
    505                 $wp_rewrite->set_permalink_structure( '' );
    506                 flush_rewrite_rules();
    507466        }
    508467
     
    522481                $found = get_sample_permalink( $p );
    523482                $this->assertEquals( '30', $found[1] );
    524 
    525                 $wp_rewrite->set_permalink_structure( '' );
    526                 flush_rewrite_rules();
    527483        }
    528484
  • trunk/tests/phpunit/tests/comment/commentsTemplate.php

    r34741 r34802  
    77 */
    88class Tests_Comment_CommentsTemplate extends WP_UnitTestCase {
    9         public function setUp() {
    10                 parent::setUp();
    11 
    12                 global $wp_rewrite;
    13                 $wp_rewrite->set_permalink_structure( '' );
    14                 flush_rewrite_rules();
    15         }
    169
    1710        /**
  • trunk/tests/phpunit/tests/feed/rss2.php

    r34092 r34802  
    1010 */
    1111class Tests_Feed_RSS2 extends WP_UnitTestCase {
    12         private $permalink_structure = '';
    13 
    1412        static $user;
    1513        static $posts;
     
    4139
    4240        public function setUp() {
    43                 global $wp_rewrite;
    44                 $this->permalink_structure = get_option( 'permalink_structure' );
    45                 $wp_rewrite->set_permalink_structure( '' );
    46                 $wp_rewrite->flush_rules();
    47 
    4841                parent::setUp();
    4942
     
    5245                // this seems to break something
    5346                update_option('use_smilies', false);
    54         }
    55 
    56         public function tearDown() {
    57                 global $wp_rewrite;
    58                 $wp_rewrite->set_permalink_structure( $this->permalink_structure );
    59                 $wp_rewrite->flush_rules();
    6047        }
    6148
  • trunk/tests/phpunit/tests/general/paginateLinks.php

    r32359 r34802  
    44
    55        private $i18n_count = 0;
    6         private $permalink_structure = '';
    76
    87        function setUp() {
    98                parent::setUp();
    10                 global $wp_rewrite;
    119
    1210                $this->go_to( home_url( '/' ) );
    13 
    14                 $this->permalink_structure = $wp_rewrite->permalink_structure;
    15                 $wp_rewrite->set_permalink_structure( get_option( 'permalink_structure' ) );
    16         }
    17 
    18         function tearDown() {
    19                 global $wp_rewrite;
    20                 $wp_rewrite->set_permalink_structure( $this->permalink_structure );
    2111        }
    2212
  • trunk/tests/phpunit/tests/link.php

    r34690 r34802  
    4040                $post_id = $this->factory->post->create();
    4141                $post_id2 = $this->factory->post->create();
    42 
    43                 $wp_rewrite->init();
    44                 $wp_rewrite->set_permalink_structure( '' );
    45                 $wp_rewrite->flush_rules();
    4642
    4743                // Basic case
  • trunk/tests/phpunit/tests/link/getNextCommentsLink.php

    r34561 r34802  
    77 */
    88class Tests_Link_GetNextCommentsLink extends WP_UnitTestCase {
    9         public function setUp() {
    10                 global $wp_rewrite;
    11 
    12                 parent::setUp();
    13 
    14                 $wp_rewrite->set_permalink_structure( '' );
    15                 $wp_rewrite->flush_rules();
    16         }
    179
    1810        public function test_page_should_respect_value_of_cpage_query_var() {
  • trunk/tests/phpunit/tests/link/getPostCommentsFeedLink.php

    r34479 r34802  
    44 */
    55class Tests_Link_GetPostCommentsFeedLink extends WP_UnitTestCase {
    6         private static $rewrite;
    7         private static $permalink_structure;
    8 
    9         static function setUpBeforeClass() {
    10                 global $wp_rewrite;
    11 
    12                 parent::setUpBeforeClass();
    13 
    14                 self::$rewrite = $wp_rewrite;
    15                 self::$permalink_structure = get_option( 'permalink_structure' );
    16         }
    17 
    18         public static function tearDownAfterClass() {
    19                 parent::tearDownAfterClass();
    20 
    21                 self::$rewrite->init();
    22                 self::$rewrite->set_permalink_structure( self::$permalink_structure );
    23                 self::$rewrite->flush_rules();
    24         }
    25 
    26         public function setUp() {
    27                 parent::setUp();
    28                 self::$rewrite->init();
    29         }
    30 
    31         private function set_permalink_structure( $structure ) {
    32                 self::$rewrite->set_permalink_structure( $structure );
    33                 self::$rewrite->flush_rules();
    34         }
    356
    367        public function test_post_link() {
    37                 $this->set_permalink_structure( '' );
    38 
    398                $post_id = $this->factory->post->create();
    409
     
    4918
    5019        public function test_post_pretty_link() {
    51                 $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
     20                global $wp_rewrite;
     21                $wp_rewrite->init();
     22                $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
     23                $wp_rewrite->flush_rules();
    5224
    5325                $post_id = $this->factory->post->create();
     
    6032
    6133        public function test_attachment_link() {
    62                 $this->set_permalink_structure( '' );
    63 
    6434                $post_id = $this->factory->post->create();
    6535                $attachment_id = $this->factory->attachment->create_object( 'image.jpg', $post_id, array(
     
    7848
    7949        public function test_attachment_pretty_link() {
    80                 $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
     50                global $wp_rewrite;
     51                $wp_rewrite->init();
     52                $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
     53                $wp_rewrite->flush_rules();
    8154
    8255                $post_id = $this->factory->post->create( array(
     
    9871
    9972        public function test_attachment_no_name_pretty_link() {
    100                 $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
     73                global $wp_rewrite;
     74                $wp_rewrite->init();
     75                $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
     76                $wp_rewrite->flush_rules();
    10177
    10278                $post_id = $this->factory->post->create();
     
    11389
    11490        public function test_unattached_link() {
    115                 $this->set_permalink_structure( '' );
    116 
    11791                $attachment_id = $this->factory->attachment->create_object( 'image.jpg', 0, array(
    11892                        'post_mime_type' => 'image/jpeg',
     
    130104
    131105        public function test_unattached_pretty_link() {
    132                 $this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
     106                global $wp_rewrite;
     107                $wp_rewrite->init();
     108                $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
     109                $wp_rewrite->flush_rules();
    133110
    134111                $attachment_id = $this->factory->attachment->create_object( 'image.jpg', 0, array(
  • trunk/tests/phpunit/tests/link/getPreviousCommentsLink.php

    r34561 r34802  
    77 */
    88class Tests_Link_GetPreviousCommentsLink extends WP_UnitTestCase {
    9         public function setUp() {
    10                 global $wp_rewrite;
    11 
    12                 parent::setUp();
    13 
    14                 $wp_rewrite->set_permalink_structure( '' );
    15                 $wp_rewrite->flush_rules();
    16         }
    179
    1810        public function test_page_should_respect_value_of_cpage_query_var() {
  • trunk/tests/phpunit/tests/post.php

    r34762 r34802  
    430430                $post = get_post( $p );
    431431
    432                 $wp_rewrite->set_permalink_structure( '' );
     432                $this->reset_permalinks();
    433433
    434434                $this->assertEquals( "$p-2", $post->post_name );
     
    526526                // permalink should include the post ID at the end
    527527                $this->assertEquals(get_option('siteurl').'/2007/10/31/'.$id.'/', $plink);
    528 
    529                 $wp_rewrite->set_permalink_structure('');
    530528        }
    531529
  • trunk/tests/phpunit/tests/post/wpUniquePostSlug.php

    r33262 r34802  
    182182                $found = wp_unique_post_slug( '2015', $p, 'publish', 'post', 0 );
    183183                $this->assertEquals( '2015-2', $found );
    184 
    185                 $wp_rewrite->set_permalink_structure( '' );
    186                 flush_rewrite_rules();
    187184        }
    188185
     
    204201                $found = wp_unique_post_slug( '2015', $p, 'publish', 'post', 0 );
    205202                $this->assertEquals( '2015-2', $found );
    206 
    207                 $wp_rewrite->set_permalink_structure( '' );
    208                 flush_rewrite_rules();
    209203        }
    210204
     
    225219                $found = wp_unique_post_slug( '2015', $p, 'publish', 'post', 0 );
    226220                $this->assertEquals( '2015', $found );
    227 
    228                 $wp_rewrite->set_permalink_structure( '' );
    229                 flush_rewrite_rules();
    230221        }
    231222
     
    246237                $found = wp_unique_post_slug( '11', $p, 'publish', 'post', 0 );
    247238                $this->assertEquals( '11-2', $found );
    248 
    249                 $wp_rewrite->set_permalink_structure( '' );
    250                 flush_rewrite_rules();
    251239        }
    252240
     
    267255                $found = wp_unique_post_slug( '11', $p, 'publish', 'post', 0 );
    268256                $this->assertEquals( '11', $found );
    269 
    270                 $wp_rewrite->set_permalink_structure( '' );
    271                 flush_rewrite_rules();
    272257        }
    273258
     
    288273                $found = wp_unique_post_slug( '13', $p, 'publish', 'post', 0 );
    289274                $this->assertEquals( '13', $found );
    290 
    291                 $wp_rewrite->set_permalink_structure( '' );
    292                 flush_rewrite_rules();
    293275        }
    294276
     
    309291                $found = wp_unique_post_slug( '30', $p, 'publish', 'post', 0 );
    310292                $this->assertEquals( '30-2', $found );
    311 
    312                 $wp_rewrite->set_permalink_structure( '' );
    313                 flush_rewrite_rules();
    314293        }
    315294
     
    330309                $found = wp_unique_post_slug( '30', $p, 'publish', 'post', 0 );
    331310                $this->assertEquals( '30', $found );
    332 
    333                 $wp_rewrite->set_permalink_structure( '' );
    334                 flush_rewrite_rules();
    335311        }
    336312
     
    351327                $found = wp_unique_post_slug( '32', $p, 'publish', 'post', 0 );
    352328                $this->assertEquals( '32', $found );
    353 
    354                 $wp_rewrite->set_permalink_structure( '' );
    355                 flush_rewrite_rules();
    356329        }
    357330}
  • trunk/tests/phpunit/tests/query/results.php

    r33706 r34802  
    684684         */
    685685        function test_child_post_in_hierarchical_post_type_with_default_permalinks() {
    686                 global $wp_rewrite;
    687 
    688                 $old_permastruct = get_option( 'permalink_structure' );
    689                 $wp_rewrite->set_permalink_structure( '' );
    690                 $wp_rewrite->flush_rules();
    691 
    692686                register_post_type( 'handbook', array( 'hierarchical' => true ) );
    693687
     
    699693                $result = $this->q->query( array( 'handbook' => 'contributing-to-the-wordpress-codex/getting-started', 'post_type' => 'handbook' ) );
    700694                $this->assertCount( 1, $result );
    701 
    702                 $wp_rewrite->set_permalink_structure( $old_permastruct );
    703                 $wp_rewrite->flush_rules();
    704695        }
    705696
  • trunk/tests/phpunit/tests/rewrite/numericSlugs.php

    r32648 r34802  
    4848
    4949                $this->assertQueryTrue( 'is_single', 'is_singular' );
    50 
    51                 $wp_rewrite->set_permalink_structure('');
    5250        }
    5351
     
    7876
    7977                $this->assertEquals( '2015', url_to_postid( get_permalink( '2015' ) ) );
    80 
    81                 $wp_rewrite->set_permalink_structure('');
    8278        }
    8379
     
    9995
    10096                $this->assertQueryTrue( 'is_single', 'is_singular' );
    101 
    102                 $wp_rewrite->set_permalink_structure('');
    10397        }
    10498
     
    118112
    119113                $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
    120 
    121                 $wp_rewrite->set_permalink_structure('');
    122114        }
    123115
     
    140132
    141133                $this->assertQueryTrue( 'is_single', 'is_singular' );
    142 
    143                 $wp_rewrite->set_permalink_structure('');
    144134        }
    145135
     
    160150
    161151                $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
    162 
    163                 $wp_rewrite->set_permalink_structure('');
    164152        }
    165153
     
    182170
    183171                $this->assertQueryTrue( 'is_single', 'is_singular' );
    184 
    185                 $wp_rewrite->set_permalink_structure('');
    186172        }
    187173
     
    202188
    203189                $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
    204 
    205                 $wp_rewrite->set_permalink_structure('');
    206190        }
    207191
     
    223207
    224208                $this->assertQueryTrue( 'is_single', 'is_singular' );
    225 
    226                 $wp_rewrite->set_permalink_structure('');
    227209        }
    228210
     
    242224
    243225                $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
    244 
    245                 $wp_rewrite->set_permalink_structure('');
    246226        }
    247227
     
    263243
    264244                $this->assertQueryTrue( 'is_single', 'is_singular' );
    265 
    266                 $wp_rewrite->set_permalink_structure('');
    267245        }
    268246
     
    282260
    283261                $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
    284 
    285                 $wp_rewrite->set_permalink_structure('');
    286262        }
    287263
     
    304280
    305281                $this->assertQueryTrue( 'is_single', 'is_singular' );
    306 
    307                 $wp_rewrite->set_permalink_structure('');
    308282        }
    309283
     
    324298
    325299                $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
    326 
    327                 $wp_rewrite->set_permalink_structure('');
    328300        }
    329301
     
    345317
    346318                $this->assertQueryTrue( 'is_single', 'is_singular' );
    347 
    348                 $wp_rewrite->set_permalink_structure('');
    349319        }
    350320
     
    364334
    365335                $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
    366 
    367                 $wp_rewrite->set_permalink_structure('');
    368336        }
    369337
     
    390358                $this->assertTrue( $q->is_day );
    391359                $this->assertFalse( $q->is_single );
    392 
    393                 $wp_rewrite->set_permalink_structure('');
    394360        }
    395361
  • trunk/tests/phpunit/tests/rewrite/oldSlugRedirect.php

    r34685 r34802  
    3535
    3636                remove_filter( 'old_slug_redirect_url', array( $this, 'filter_old_slug_redirect_url' ), 10 );
    37 
    38                 global $wp_rewrite;
    39 
    40                 $wp_rewrite->set_permalink_structure( '' );
    41                 $wp_rewrite->init();
    4237        }
    4338
  • trunk/tests/phpunit/tests/term/getTermLink.php

    r33022 r34802  
    55 */
    66class Tests_Term_GetTermLink extends WP_UnitTestCase {
    7         private $permalink_structure;
    87
    98        public function setUp() {
    109                parent::setUp();
    1110
    12                 // Assume no pretty permalinks.
    13                 global $wp_rewrite;
    14                 $this->permalink_structure = get_option( 'permalink_structure' );
    15                 $wp_rewrite->set_permalink_structure( '' );
    16                 $wp_rewrite->flush_rules();
    17 
    1811                register_taxonomy( 'wptests_tax', 'post' );
    19         }
    20 
    21         public function tearDown() {
    22                 global $wp_rewrite;
    23                 $wp_rewrite->set_permalink_structure( $this->permalink_structure );
    24                 $wp_rewrite->flush_rules();
    25 
    26                 parent::tearDown();
    2712        }
    2813
     
    10287        public function test_taxonomy_permastruct_with_hierarchical_rewrite_should_put_term_ancestors_in_link() {
    10388                global $wp_rewrite;
    104                 $permalink_structure = get_option( 'permalink_structure' );
     89                $wp_rewrite->init();
    10590                $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
    10691                $wp_rewrite->flush_rules();
     
    126111
    127112                $actual = get_term_link( $t2, 'wptests_tax2' );
    128 
    129                 $wp_rewrite->set_permalink_structure( $permalink_structure );
    130                 $wp_rewrite->flush_rules();
    131113
    132114                $this->assertContains( '/foo/term1/term2/', $actual );
     
    160142                $actual = get_term_link( $t2, 'wptests_tax2' );
    161143
    162                 $wp_rewrite->set_permalink_structure( $permalink_structure );
    163                 $wp_rewrite->flush_rules();
    164 
    165144                $this->assertContains( '/foo/term2/', $actual );
    166145        }
  • trunk/tests/phpunit/tests/user/author.php

    r34687 r34802  
    1515        function setUp() {
    1616                parent::setUp();
    17 
    18                 global $wp_rewrite;
    19                 $this->permalink_structure = get_option( 'permalink_structure' );
    20                 $wp_rewrite->set_permalink_structure( '' );
    21                 $wp_rewrite->flush_rules();
    2217
    2318                $this->author_id = $this->factory->user->create( array(
     
    4338
    4439        function tearDown() {
    45                 global $wp_rewrite;
    46                 $wp_rewrite->set_permalink_structure( $this->permalink_structure );
    47                 $wp_rewrite->flush_rules();
    48 
    4940                wp_reset_postdata();
    5041                parent::tearDown();
Note: See TracChangeset for help on using the changeset viewer.