Make WordPress Core


Ignore:
Timestamp:
10/16/2015 09:04:12 PM (11 years ago)
Author:
wonderboymusic
Message:

Unit Tests: one $factory to rule them all, and it shall be static.

Using more than one instance of WP_UnitTest_Factory causes all kinds of craziness, due to out-of-sync internal generator sequences. Since we want to use setUpBeforeClass, we were creating ad hoc instances. To avoid that, we were injecting one static instance via Dependency Injection in wpSetUpBeforeClass. All tests should really use the static instance, so we will remove the instance prop $factory.

Replace $this->factory with self::$factory over 2000 times.
Rewrite all of the tests that were hard-coding dynamic values.

#YOLOFriday

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/admin/includesPost.php

    r34810 r35225  
    1212
    1313        function test__wp_translate_postdata_cap_checks_contributor() {
    14                 $contributor_id = $this->factory->user->create( array( 'role' => 'contributor' ) );
    15                 $editor_id = $this->factory->user->create( array( 'role' => 'editor' ) );
     14                $contributor_id = self::$factory->user->create( array( 'role' => 'contributor' ) );
     15                $editor_id = self::$factory->user->create( array( 'role' => 'editor' ) );
    1616
    1717                wp_set_current_user( $contributor_id );
     
    5252                // Edit Draft Post for another user
    5353                $_post_data = array();
    54                 $_post_data['post_ID'] = $this->factory->post->create( array( 'post_author' => $editor_id ) );
     54                $_post_data['post_ID'] = self::$factory->post->create( array( 'post_author' => $editor_id ) );
    5555                $_post_data['post_author'] = $editor_id;
    5656                $_post_data['post_type'] = 'post';
     
    6565
    6666        function test__wp_translate_postdata_cap_checks_editor() {
    67                 $contributor_id = $this->factory->user->create( array( 'role' => 'contributor' ) );
    68                 $editor_id = $this->factory->user->create( array( 'role' => 'editor' ) );
     67                $contributor_id = self::$factory->user->create( array( 'role' => 'contributor' ) );
     68                $editor_id = self::$factory->user->create( array( 'role' => 'editor' ) );
    6969
    7070                wp_set_current_user( $editor_id );
     
    105105                // Edit Draft Post for another user
    106106                $_post_data = array();
    107                 $_post_data['post_ID'] = $this->factory->post->create( array( 'post_author' => $contributor_id ) );
     107                $_post_data['post_ID'] = self::$factory->post->create( array( 'post_author' => $contributor_id ) );
    108108                $_post_data['post_author'] = $contributor_id;
    109109                $_post_data['post_type'] = 'post';
     
    123123         */
    124124        function test_edit_post_auto_draft() {
    125                 $user_id = $this->factory->user->create( array( 'role' => 'editor' ) );
     125                $user_id = self::$factory->user->create( array( 'role' => 'editor' ) );
    126126                wp_set_current_user( $user_id );
    127                 $post = $this->factory->post->create_and_get( array( 'post_status' => 'auto-draft' ) );
     127                $post = self::$factory->post->create_and_get( array( 'post_status' => 'auto-draft' ) );
    128128                $this->assertEquals( 'auto-draft', $post->post_status );
    129129                $post_data = array(
     
    141141         */
    142142        public function test_edit_post_should_parse_tax_input_by_name_rather_than_slug_for_nonhierarchical_taxonomies() {
    143                 $u = $this->factory->user->create( array( 'role' => 'editor' ) );
     143                $u = self::$factory->user->create( array( 'role' => 'editor' ) );
    144144                wp_set_current_user( $u );
    145145
    146146                register_taxonomy( 'wptests_tax', array( 'post' ) );
    147                 $t1 = $this->factory->term->create( array(
     147                $t1 = self::$factory->term->create( array(
    148148                        'taxonomy' => 'wptests_tax',
    149149                        'name' => 'foo',
    150150                        'slug' => 'bar',
    151151                ) );
    152                 $t2 = $this->factory->term->create( array(
     152                $t2 = self::$factory->term->create( array(
    153153                        'taxonomy' => 'wptests_tax',
    154154                        'name' => 'bar',
     
    156156                ) );
    157157
    158                 $p = $this->factory->post->create();
     158                $p = self::$factory->post->create();
    159159
    160160                $post_data = array(
     
    180180         */
    181181        public function test_edit_post_should_not_create_terms_for_an_empty_tag_input_field() {
    182                 $u = $this->factory->user->create( array( 'role' => 'editor' ) );
     182                $u = self::$factory->user->create( array( 'role' => 'editor' ) );
    183183                wp_set_current_user( $u );
    184184
    185185                register_taxonomy( 'wptests_tax', array( 'post' ) );
    186                 $t1 = $this->factory->term->create( array(
     186                $t1 = self::$factory->term->create( array(
    187187                        'taxonomy' => 'wptests_tax',
    188188                        'name' => 'foo',
     
    190190                ) );
    191191
    192                 $p = $this->factory->post->create();
     192                $p = self::$factory->post->create();
    193193
    194194                $post_data = array(
     
    210210         */
    211211        function test_bulk_edit_posts_stomping() {
    212                 $admin = $this->factory->user->create( array( 'role' => 'administrator' ) );
    213                 $users = $this->factory->user->create_many( 2, array( 'role' => 'author' ) );
     212                $admin = self::$factory->user->create( array( 'role' => 'administrator' ) );
     213                $users = self::$factory->user->create_many( 2, array( 'role' => 'author' ) );
    214214                wp_set_current_user( $admin );
    215215
    216                 $post1 = $this->factory->post->create( array(
     216                $post1 = self::$factory->post->create( array(
    217217                        'post_author'    => $users[0],
    218218                        'comment_status' => 'open',
     
    221221                ) );
    222222
    223                 $post2 = $this->factory->post->create( array(
     223                $post2 = self::$factory->post->create( array(
    224224                        'post_author'    => $users[1],
    225225                        'comment_status' => 'closed',
     
    256256
    257257                $future_date = date( 'Y-m-d H:i:s', time() + 100 );
    258                 $p = $this->factory->post->create( array( 'post_status' => 'future', 'post_name' => 'foo', 'post_date' => $future_date ) );
     258                $p = self::$factory->post->create( array( 'post_status' => 'future', 'post_name' => 'foo', 'post_date' => $future_date ) );
    259259
    260260                $found = get_sample_permalink( $p );
     
    269269         */
    270270        public function test_get_sample_permalink_html_should_use_default_permalink_for_view_post_link_when_pretty_permalinks_are_disabled() {
    271                 wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
     271                wp_set_current_user( self::$factory->user->create( array( 'role' => 'administrator' ) ) );
    272272
    273273                $future_date = date( 'Y-m-d H:i:s', time() + 100 );
    274                 $p = $this->factory->post->create( array( 'post_status' => 'future', 'post_name' => 'foo', 'post_date' => $future_date ) );
     274                $p = self::$factory->post->create( array( 'post_status' => 'future', 'post_name' => 'foo', 'post_date' => $future_date ) );
    275275
    276276                $found = get_sample_permalink_html( $p );
     
    285285                $this->set_permalink_structure( '/%postname%/' );
    286286
    287                 wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
     287                wp_set_current_user( self::$factory->user->create( array( 'role' => 'administrator' ) ) );
    288288
    289289                $future_date = date( 'Y-m-d H:i:s', time() + 100 );
    290                 $p = $this->factory->post->create( array( 'post_status' => 'future', 'post_name' => 'foo', 'post_date' => $future_date ) );
     290                $p = self::$factory->post->create( array( 'post_status' => 'future', 'post_name' => 'foo', 'post_date' => $future_date ) );
    291291
    292292                $found = get_sample_permalink_html( $p );
     
    302302                $this->set_permalink_structure( '/%postname%/' );
    303303
    304                 wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
     304                wp_set_current_user( self::$factory->user->create( array( 'role' => 'administrator' ) ) );
    305305
    306306                // Published posts should use published permalink
    307                 $p = $this->factory->post->create( array( 'post_status' => 'publish', 'post_name' => 'foo' ) );
     307                $p = self::$factory->post->create( array( 'post_status' => 'publish', 'post_name' => 'foo' ) );
    308308
    309309                $found = get_sample_permalink_html( $p, null, 'new_slug' );
     
    314314                // Scheduled posts should use published permalink
    315315                $future_date = date( 'Y-m-d H:i:s', time() + 100 );
    316                 $p = $this->factory->post->create( array( 'post_status' => 'future', 'post_name' => 'bar', 'post_date' => $future_date ) );
     316                $p = self::$factory->post->create( array( 'post_status' => 'future', 'post_name' => 'bar', 'post_date' => $future_date ) );
    317317
    318318                $found = get_sample_permalink_html( $p, null, 'new_slug' );
     
    322322
    323323                // Draft posts should use preview link
    324                 $p = $this->factory->post->create( array( 'post_status' => 'draft', 'post_name' => 'baz' ) );
     324                $p = self::$factory->post->create( array( 'post_status' => 'draft', 'post_name' => 'baz' ) );
    325325
    326326                $found = get_sample_permalink_html( $p, null, 'new_slug' );
     
    340340                $this->set_permalink_structure( '/%postname%/' );
    341341
    342                 $p = $this->factory->post->create( array(
     342                $p = self::$factory->post->create( array(
    343343                        'post_name' => '2015',
    344344                ) );
     
    354354                $this->set_permalink_structure( '/%year%/%postname%/' );
    355355
    356                 $p = $this->factory->post->create( array(
     356                $p = self::$factory->post->create( array(
    357357                        'post_name' => '2015',
    358358                ) );
     
    368368                $this->set_permalink_structure( '/%year%/%postname%/' );
    369369
    370                 $p = $this->factory->post->create( array(
     370                $p = self::$factory->post->create( array(
    371371                        'post_name' => '11',
    372372                ) );
     
    382382                $this->set_permalink_structure( '/%year%/%postname%/' );
    383383
    384                 $p = $this->factory->post->create( array(
     384                $p = self::$factory->post->create( array(
    385385                        'post_name' => '13',
    386386                ) );
     
    396396                $this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
    397397
    398                 $p = $this->factory->post->create( array(
     398                $p = self::$factory->post->create( array(
    399399                        'post_name' => '30',
    400400                ) );
     
    410410                $this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
    411411
    412                 $this->factory->post->create( array(
     412                self::$factory->post->create( array(
    413413                        'post_name' => '30-2',
    414414                ) );
    415415
    416                 $p = $this->factory->post->create( array(
     416                $p = self::$factory->post->create( array(
    417417                        'post_name' => '30',
    418418                ) );
     
    428428                $this->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
    429429
    430                 $p = $this->factory->post->create( array(
     430                $p = self::$factory->post->create( array(
    431431                        'post_name' => '32',
    432432                ) );
     
    442442                $this->set_permalink_structure( '/%year%/%month%/%day%/%postname%/' );
    443443
    444                 $p = $this->factory->post->create( array(
     444                $p = self::$factory->post->create( array(
    445445                        'post_name' => '30',
    446446                ) );
     
    451451
    452452        public function test_post_exists_should_match_title() {
    453                 $p = $this->factory->post->create( array(
     453                $p = self::$factory->post->create( array(
    454454                        'post_title' => 'Foo Bar',
    455455                ) );
     
    459459
    460460        public function test_post_exists_should_not_match_nonexistent_title() {
    461                 $p = $this->factory->post->create( array(
     461                $p = self::$factory->post->create( array(
    462462                        'post_title' => 'Foo Bar',
    463463                ) );
     
    469469                $title = 'Foo Bar';
    470470                $content = 'Foo Bar Baz';
    471                 $p = $this->factory->post->create( array(
     471                $p = self::$factory->post->create( array(
    472472                        'post_title' => $title,
    473473                        'post_content' => $content,
     
    480480                $title = 'Foo Bar';
    481481                $content = 'Foo Bar Baz';
    482                 $p = $this->factory->post->create( array(
     482                $p = self::$factory->post->create( array(
    483483                        'post_title' => $title,
    484484                        'post_content' => $content . ' Quz',
     
    491491                $title = 'Foo Bar';
    492492                $date = '2014-05-08 12:00:00';
    493                 $p = $this->factory->post->create( array(
     493                $p = self::$factory->post->create( array(
    494494                        'post_title' => $title,
    495495                        'post_date' => $date,
     
    502502                $title = 'Foo Bar';
    503503                $date = '2014-05-08 12:00:00';
    504                 $p = $this->factory->post->create( array(
     504                $p = self::$factory->post->create( array(
    505505                        'post_title' => $title,
    506506                        'post_date' => '2015-10-10 00:00:00',
     
    514514                $content = 'Foo Bar Baz';
    515515                $date = '2014-05-08 12:00:00';
    516                 $p = $this->factory->post->create( array(
     516                $p = self::$factory->post->create( array(
    517517                        'post_title' => $title,
    518518                        'post_content' => $content,
Note: See TracChangeset for help on using the changeset viewer.