Skip to content

Commit

Permalink
Tests: Use the factory method instead of the property.
Browse files Browse the repository at this point in the history
This replaces all references to the `WP_UnitTestCase_Base::$factory` property with static function calls to the `WP_UnitTestCase_Base::factory()` method.

This is a consistency improvement for the test suite.

Follow up to [35225], [35242], [49603], [54087], [54088].

Props jrf.
See #55652.

git-svn-id: https://develop.svn.wordpress.org/trunk@54090 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Sep 6, 2022
1 parent 563e24c commit ff56396
Show file tree
Hide file tree
Showing 52 changed files with 598 additions and 598 deletions.
2 changes: 1 addition & 1 deletion tests/phpunit/tests/admin/includesScreen.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ public function setup_block_editor_test( $hook = 'post.php' ) {
$GLOBALS['hook_suffix'] = $hook;

if ( 'post.php' === $hook ) {
$post_id = $this->factory->post->create(
$post_id = self::factory()->post->create(
array(
'post_type' => 'type_shows_in_rest',
)
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/admin/includesTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public function test_get_post_templates_child_theme() {
* @ticket 28121
*/
public function test_get_theme_featured_list_api() {
wp_set_current_user( $this->factory->user->create( array( 'role' => 'administrator' ) ) );
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
$featured_list_api = get_theme_feature_list( true );
$this->assertNonEmptyMultidimensionalArray( $featured_list_api );
}
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ public function test_log_in_using_email() {
'user_email' => 'mail@example.com',
'user_pass' => 'password',
);
$this->factory->user->create( $user_args );
self::factory()->user->create( $user_args );

$this->assertInstanceOf( 'WP_User', wp_authenticate( $user_args['user_email'], $user_args['user_pass'] ) );
$this->assertInstanceOf( 'WP_User', wp_authenticate( $user_args['user_login'], $user_args['user_pass'] ) );
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/category/walkerCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function set_up() {
*/
public function test_start_el_with_empty_attributes( $value, $expected ) {
$output = '';
$category = $this->factory->category->create_and_get();
$category = self::factory()->category->create_and_get();
$link = get_term_link( $category );

$args = array(
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -720,13 +720,13 @@ public function setup_notify_comment() {
/**
* Set up a comment for testing.
*/
$post = $this->factory->post->create(
$post = self::factory()->post->create(
array(
'post_author' => self::$user_id,
)
);

$comment = $this->factory->comment->create(
$comment = self::factory()->comment->create(
array(
'comment_post_ID' => $post,
)
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/comment/checkComment.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function test_should_return_true_when_comment_previously_approved_is_enab
);

// Make sure comment author has an approved comment.
$this->factory->comment->create(
self::factory()->comment->create(
array(
'user_id' => $subscriber_id,
'comment_approved' => '1',
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/comment/commentsOpen.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public function test_post_does_not_exist() {
* @ticket 54159
*/
public function test_post_exist_status_open() {
$post = $this->factory->post->create_and_get();
$post = self::factory()->post->create_and_get();
$this->assertTrue( comments_open( $post ) );
}

/**
* @ticket 54159
*/
public function test_post_exist_status_closed() {
$post = $this->factory->post->create_and_get();
$post = self::factory()->post->create_and_get();
$post->comment_status = 'closed';

$this->assertFalse( comments_open( $post ) );
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/comment/pingsOpen.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ public function test_post_does_not_exist() {
* @ticket 54159
*/
public function test_post_exist_status_open() {
$post = $this->factory->post->create_and_get();
$post = self::factory()->post->create_and_get();
$this->assertTrue( pings_open( $post ) );
}

/**
* @ticket 54159
*/
public function test_post_exist_status_closed() {
$post = $this->factory->post->create_and_get();
$post = self::factory()->post->create_and_get();
$post->ping_status = 'closed';

$this->assertFalse( pings_open( $post ) );
Expand Down
8 changes: 4 additions & 4 deletions tests/phpunit/tests/comment/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function test_get_comments_number_without_arg() {
*/
public function test_get_comments_number_text_with_post_id() {
$post_id = self::$post_id;
$this->factory->comment->create_post_comments( $post_id, 6 );
self::factory()->comment->create_post_comments( $post_id, 6 );

$comments_number_text = get_comments_number_text( false, false, false, $post_id );

Expand All @@ -84,12 +84,12 @@ public function test_get_comments_number_text_declension_with_default_args() {

$this->assertSame( __( 'No Comments' ), get_comments_number_text() );

$this->factory->comment->create_post_comments( $post_id, 1 );
self::factory()->comment->create_post_comments( $post_id, 1 );
$this->go_to( $permalink );

$this->assertSame( __( '1 Comment' ), get_comments_number_text() );

$this->factory->comment->create_post_comments( $post_id, 1 );
self::factory()->comment->create_post_comments( $post_id, 1 );
$this->go_to( $permalink );

$this->assertSame( sprintf( _n( '%s Comment', '%s Comments', 2 ), '2' ), get_comments_number_text() );
Expand All @@ -106,7 +106,7 @@ public function test_get_comments_number_text_declension_with_custom_args( $numb
$post_id = self::$post_id;
$permalink = get_permalink( $post_id );

$this->factory->comment->create_post_comments( $post_id, $number );
self::factory()->comment->create_post_comments( $post_id, $number );
$this->go_to( $permalink );

add_filter( 'gettext_with_context', array( $this, 'enable_comment_number_declension' ), 10, 4 );
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/customize/nav-menus.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ public function test_search_available_items_query_should_return_unassigned_term_
)
);

$term_id = $this->factory->term->create(
$term_id = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax',
'name' => 'foobar',
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/date/getFeedBuildDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function test_should_fall_back_to_last_post_modified() {

$this->assertFalse( get_feed_build_date( DATE_RFC3339 ), 'False when unable to determine valid time' );

$this->factory->post->create(
self::factory()->post->create(
array(
'post_date' => $datetime->format( 'Y-m-d H:i:s' ),
)
Expand All @@ -68,7 +68,7 @@ public function test_should_fall_back_to_last_post_modified() {
'Fall back to time of last post modified with no posts'
);

$post_id_broken = $this->factory->post->create();
$post_id_broken = self::factory()->post->create();
$post_broken = get_post( $post_id_broken );

$post_broken->post_modified_gmt = 0;
Expand Down
8 changes: 4 additions & 4 deletions tests/phpunit/tests/date/getTheModifiedDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function test_get_the_modified_date_with_post_id() {
'post_date' => '2016-01-21 15:34:36',
'post_date_gmt' => '2016-01-21 15:34:36',
);
$post_id = $this->factory->post->create( $details );
$post_id = self::factory()->post->create( $details );
$format = 'Y-m-d';
$expected = '2016-01-21';
$actual = get_the_modified_date( $format, $post_id );
Expand All @@ -39,7 +39,7 @@ public function test_get_the_modified_date_default() {
'post_date' => '2016-01-21 15:34:36',
'post_date_gmt' => '2016-01-21 15:34:36',
);
$post_id = $this->factory->post->create( $details );
$post_id = self::factory()->post->create( $details );
$post = get_post( $post_id );

$GLOBALS['post'] = $post;
Expand Down Expand Up @@ -111,7 +111,7 @@ public function test_get_the_modified_time_with_post_id() {
'post_date' => '2016-01-21 15:34:36',
'post_date_gmt' => '2016-01-21 15:34:36',
);
$post_id = $this->factory->post->create( $details );
$post_id = self::factory()->post->create( $details );
$format = 'G';
$expected = 1453390476;
$actual = get_the_modified_time( $format, $post_id );
Expand All @@ -130,7 +130,7 @@ public function test_get_the_modified_time_default() {
'post_date' => '2016-01-21 15:34:36',
'post_date_gmt' => '2016-01-21 15:34:36',
);
$post_id = $this->factory->post->create( $details );
$post_id = self::factory()->post->create( $details );
$post = get_post( $post_id );

$GLOBALS['post'] = $post;
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/date/xmlrpc.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public function test_date_edit_comment() {
$datetimeutc = $datetime->setTimezone( new DateTimeZone( 'UTC' ) );

$this->make_user_by_role( 'administrator' );
$post_id = $this->factory->post->create();
$post_id = self::factory()->post->create();

$comment_data = array(
'comment_post_ID' => $post_id,
Expand Down
16 changes: 8 additions & 8 deletions tests/phpunit/tests/general/template.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function test_has_site_icon() {
* @covers ::has_site_icon
*/
public function test_has_site_icon_returns_true_when_called_for_other_site_with_site_icon_set() {
$blog_id = $this->factory->blog->create();
$blog_id = self::factory()->blog->create();
switch_to_blog( $blog_id );
$this->set_site_icon();
restore_current_blog();
Expand All @@ -119,7 +119,7 @@ public function test_has_site_icon_returns_true_when_called_for_other_site_with_
* @covers ::has_site_icon
*/
public function test_has_site_icon_returns_false_when_called_for_other_site_without_site_icon_set() {
$blog_id = $this->factory->blog->create();
$blog_id = self::factory()->blog->create();

$this->assertFalse( has_site_icon( $blog_id ) );
}
Expand Down Expand Up @@ -298,7 +298,7 @@ public function test_has_custom_logo() {
* @covers ::has_custom_logo
*/
public function test_has_custom_logo_returns_true_when_called_for_other_site_with_custom_logo_set() {
$blog_id = $this->factory->blog->create();
$blog_id = self::factory()->blog->create();
switch_to_blog( $blog_id );
$this->set_custom_logo();
restore_current_blog();
Expand All @@ -313,7 +313,7 @@ public function test_has_custom_logo_returns_true_when_called_for_other_site_wit
* @covers ::has_custom_logo
*/
public function test_has_custom_logo_returns_false_when_called_for_other_site_without_custom_logo_set() {
$blog_id = $this->factory->blog->create();
$blog_id = self::factory()->blog->create();

$this->assertFalse( has_custom_logo( $blog_id ) );
}
Expand Down Expand Up @@ -343,7 +343,7 @@ public function test_get_custom_logo() {
* @covers ::get_custom_logo
*/
public function test_get_custom_logo_returns_logo_when_called_for_other_site_with_custom_logo_set() {
$blog_id = $this->factory->blog->create();
$blog_id = self::factory()->blog->create();
switch_to_blog( $blog_id );

$this->set_custom_logo();
Expand Down Expand Up @@ -466,7 +466,7 @@ private function insert_custom_logo() {
* @covers ::get_site_icon_url
*/
public function test_get_site_icon_url_preserves_switched_state() {
$blog_id = $this->factory->blog->create();
$blog_id = self::factory()->blog->create();
switch_to_blog( $blog_id );

$expected = $GLOBALS['_wp_switched_stack'];
Expand All @@ -486,7 +486,7 @@ public function test_get_site_icon_url_preserves_switched_state() {
* @covers ::has_custom_logo
*/
public function test_has_custom_logo_preserves_switched_state() {
$blog_id = $this->factory->blog->create();
$blog_id = self::factory()->blog->create();
switch_to_blog( $blog_id );

$expected = $GLOBALS['_wp_switched_stack'];
Expand All @@ -506,7 +506,7 @@ public function test_has_custom_logo_preserves_switched_state() {
* @covers ::get_custom_logo
*/
public function test_get_custom_logo_preserves_switched_state() {
$blog_id = $this->factory->blog->create();
$blog_id = self::factory()->blog->create();
switch_to_blog( $blog_id );

$expected = $GLOBALS['_wp_switched_stack'];
Expand Down
6 changes: 3 additions & 3 deletions tests/phpunit/tests/general/wpGetDocumentTitle.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function test_front_page_title() {
update_option( 'show_on_front', 'page' );
update_option(
'page_on_front',
$this->factory->post->create(
self::factory()->post->create(
array(
'post_title' => 'front-page',
'post_type' => 'page',
Expand All @@ -128,7 +128,7 @@ public function front_page_title_parts( $parts ) {
}

public function test_home_title() {
$blog_page_id = $this->factory->post->create(
$blog_page_id = self::factory()->post->create(
array(
'post_title' => 'blog-page',
'post_type' => 'page',
Expand Down Expand Up @@ -205,7 +205,7 @@ public function test_post_type_archive_title() {
)
);

$this->factory->post->create(
self::factory()->post->create(
array(
'post_type' => 'cpt',
)
Expand Down
8 changes: 4 additions & 4 deletions tests/phpunit/tests/image/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ public function test_wp_generate_attachment_metadata_pdf() {
$this->markTestSkipped( $editor->get_error_message() );
}

$attachment_id = $this->factory->attachment->create_object(
$attachment_id = self::factory()->attachment->create_object(
$test_file,
0,
array(
Expand Down Expand Up @@ -735,7 +735,7 @@ public function test_crop_setting_for_pdf() {
$this->markTestSkipped( $editor->get_error_message() );
}

$attachment_id = $this->factory->attachment->create_object(
$attachment_id = self::factory()->attachment->create_object(
$test_file,
0,
array(
Expand Down Expand Up @@ -808,7 +808,7 @@ public function test_fallback_intermediate_image_sizes() {
$this->markTestSkipped( $editor->get_error_message() );
}

$attachment_id = $this->factory->attachment->create_object(
$attachment_id = self::factory()->attachment->create_object(
$test_file,
0,
array(
Expand Down Expand Up @@ -882,7 +882,7 @@ public function test_pdf_preview_doesnt_overwrite_existing_jpeg() {
$this->markTestSkipped( $editor->get_error_message() );
}

$attachment_id = $this->factory->attachment->create_object(
$attachment_id = self::factory()->attachment->create_object(
$pdf_path,
0,
array(
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/link/getPostTypeArchiveLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function test_get_post_archive_link_with_post_archive_on_front_page() {
* @ticket 19902
*/
public function test_get_post_archive_link_with_post_archive_on_a_blog_page() {
$page_for_posts = $this->factory->post->create(
$page_for_posts = self::factory()->post->create(
array(
'post_title' => 'blog-page',
'post_type' => 'page',
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/link/getThePrivacyPolicyLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public function test_get_the_privacy_policy_link_should_not_prepend_and_append_s
* @ticket 44192
*/
public function test_function_should_return_empty_string_when_privacy_page_title_empty() {
$nameless_page_id = $this->factory->post->create(
$nameless_page_id = self::factory()->post->create(
array(
'post_type' => 'page',
'post_title' => '',
Expand Down
Loading

0 comments on commit ff56396

Please sign in to comment.