Skip to content

Commit 54a1e7f

Browse files
committed
Build/Test Tools: Increase shared user fixtures following switch to bcrypt hashing.
User creation is somewhat more expensive since switching the default password hashing algorithm to bcrypt in 6.8. This speeds up the tests by making more use of shared user fixtures, thus reducing the number of users that are created during tests. Props peterwilsoncc. See #63026 git-svn-id: https://develop.svn.wordpress.org/trunk@60253 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 2295d8a commit 54a1e7f

28 files changed

+575
-284
lines changed

tests/phpunit/tests/admin/includesPlugin.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@
44
* @group admin
55
*/
66
class Tests_Admin_IncludesPlugin extends WP_UnitTestCase {
7+
8+
/**
9+
* Admin user ID.
10+
*
11+
* @var int $admin_id
12+
*/
13+
public static $admin_id;
14+
715
public static function wpSetUpBeforeClass( $factory ) {
16+
self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );
817
self::_back_up_mu_plugins();
918
}
1019

@@ -37,7 +46,7 @@ public function test_get_plugin_data() {
3746

3847
public function test_menu_page_url() {
3948
$current_user = get_current_user_id();
40-
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
49+
wp_set_current_user( self::$admin_id );
4150
update_option( 'siteurl', 'http://example.com' );
4251

4352
// Add some pages.
@@ -81,7 +90,7 @@ public function test_submenu_position( $position, $expected_position ) {
8190
global $submenu;
8291
global $menu;
8392
$current_user = get_current_user_id();
84-
$admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) );
93+
$admin_user = self::$admin_id;
8594
wp_set_current_user( $admin_user );
8695
set_current_screen( 'dashboard' );
8796

@@ -134,7 +143,7 @@ public function test_submenu_helpers_position( $position, $expected_position ) {
134143
$menu = array();
135144

136145
$current_user = get_current_user_id();
137-
$admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) );
146+
$admin_user = self::$admin_id;
138147
wp_set_current_user( $admin_user );
139148
set_current_screen( 'dashboard' );
140149

@@ -283,7 +292,7 @@ public function test_position_when_parent_slug_child_slug_are_the_same() {
283292
$submenu = array();
284293
$menu = array();
285294
$current_user = get_current_user_id();
286-
$admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) );
295+
$admin_user = self::$admin_id;
287296
wp_set_current_user( $admin_user );
288297
set_current_screen( 'dashboard' );
289298

@@ -316,7 +325,7 @@ public function test_passing_string_as_position_fires_doing_it_wrong_submenu() {
316325
$submenu = array();
317326
$menu = array();
318327
$current_user = get_current_user_id();
319-
$admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) );
328+
$admin_user = self::$admin_id;
320329
wp_set_current_user( $admin_user );
321330
set_current_screen( 'dashboard' );
322331

@@ -344,7 +353,7 @@ public function test_passing_float_as_position_does_not_override_int() {
344353
$submenu = array();
345354
$menu = array();
346355
$current_user = get_current_user_id();
347-
$admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) );
356+
$admin_user = self::$admin_id;
348357
wp_set_current_user( $admin_user );
349358
set_current_screen( 'dashboard' );
350359

tests/phpunit/tests/admin/includesTemplate.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@
33
* @group admin
44
*/
55
class Tests_Admin_IncludesTemplate extends WP_UnitTestCase {
6+
/**
7+
* Editor user ID.
8+
*
9+
* @var int $editor_id
10+
*/
11+
public static $editor_id;
12+
13+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
14+
self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
15+
}
616

717
/**
818
* @ticket 51137
@@ -64,7 +74,7 @@ public function test_get_inline_data_contains_term_if_show_ui_is_false_but_show_
6474
wp_set_object_terms( $post->ID, $term['term_id'], 'wptests_tax_1' );
6575

6676
// Test that get_inline_data() has `post_category` div containing the assigned term.
67-
wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) );
77+
wp_set_current_user( self::$editor_id );
6878
get_inline_data( $post );
6979
$this->expectOutputRegex( '/<div class="post_category" id="wptests_tax_1_' . $post->ID . '">' . $term['term_id'] . '<\/div>/' );
7080
}
@@ -90,7 +100,7 @@ public function test_get_inline_data_contains_term_if_show_ui_is_false_but_show_
90100
wp_set_object_terms( $post->ID, $term['term_id'], 'wptests_tax_1' );
91101

92102
// Test that get_inline_data() has `tags_input` div containing the assigned term.
93-
wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) );
103+
wp_set_current_user( self::$editor_id );
94104
get_inline_data( $post );
95105
$this->expectOutputRegex( '/<div class="tags_input" id="wptests_tax_1_' . $post->ID . '">Test<\/div>/' );
96106
}

tests/phpunit/tests/ajax/wpAjaxSendAttachmentToEditor.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,28 @@
1313
*/
1414
class Tests_Ajax_wpAjaxSendAttachmentToEditor extends WP_Ajax_UnitTestCase {
1515

16+
/**
17+
* Shared user ID for the tests.
18+
*
19+
* @var int
20+
*/
21+
public static $user_id = 0;
22+
23+
/**
24+
* Set up shared fixtures.
25+
*
26+
* @param WP_UnitTest_Factory $factory
27+
*/
28+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
29+
self::$user_id = $factory->user->create(
30+
array(
31+
'role' => 'administrator',
32+
'user_login' => 'user_36578_administrator',
33+
'user_email' => '[email protected]',
34+
)
35+
);
36+
}
37+
1638
/**
1739
* @ticket 36578
1840
*
@@ -105,13 +127,7 @@ public function test_wp_ajax_send_attachment_to_editor_should_return_a_link() {
105127
public function test_wp_ajax_set_attachment_thumbnail_success() {
106128
// Become an administrator.
107129
$post = $_POST;
108-
$user_id = self::factory()->user->create(
109-
array(
110-
'role' => 'administrator',
111-
'user_login' => 'user_36578_administrator',
112-
'user_email' => '[email protected]',
113-
)
114-
);
130+
$user_id = self::$user_id;
115131
wp_set_current_user( $user_id );
116132
$_POST = array_merge( $_POST, $post );
117133

@@ -151,13 +167,7 @@ public function test_wp_ajax_set_attachment_thumbnail_success() {
151167
public function test_wp_ajax_set_attachment_thumbnail_missing_nonce() {
152168
// Become an administrator.
153169
$post = $_POST;
154-
$user_id = self::factory()->user->create(
155-
array(
156-
'role' => 'administrator',
157-
'user_login' => 'user_36578_administrator',
158-
'user_email' => '[email protected]',
159-
)
160-
);
170+
$user_id = self::$user_id;
161171
wp_set_current_user( $user_id );
162172
$_POST = array_merge( $_POST, $post );
163173

tests/phpunit/tests/ajax/wpCustomizeNavMenus.php

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,40 @@ class Tests_Ajax_wpCustomizeNavMenus extends WP_Ajax_UnitTestCase {
3838
*/
3939
public static $terms;
4040

41+
42+
/**
43+
* Admin user ID.
44+
*
45+
* @var int
46+
*/
47+
public static $admin_user_id = 0;
48+
49+
/**
50+
* User IDs keyed by role.
51+
*
52+
* @var int[]
53+
*/
54+
public static $user_ids = array();
55+
56+
/**
57+
* Set up shared fixtures.
58+
*
59+
* @param WP_UnitTest_Factory $factory The factory.
60+
*/
4161
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
4262
// Make some post objects.
4363
self::$posts = $factory->post->create_many( 5 );
4464
self::$pages = $factory->post->create_many( 5, array( 'post_type' => 'page' ) );
4565

4666
// Some terms too.
4767
self::$terms = $factory->term->create_many( 5 );
68+
69+
// Create an admin user.
70+
self::$admin_user_id = $factory->user->create( array( 'role' => 'administrator' ) );
71+
72+
foreach ( array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ) as $role ) {
73+
self::$user_ids[ $role ] = $factory->user->create( array( 'role' => $role ) );
74+
}
4875
}
4976

5077
/**
@@ -53,7 +80,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
5380
public function set_up() {
5481
parent::set_up();
5582
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
56-
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
83+
wp_set_current_user( self::$admin_user_id );
5784
global $wp_customize;
5885
$this->wp_customize = new WP_Customize_Manager();
5986
$wp_customize = $this->wp_customize;
@@ -91,7 +118,7 @@ public function test_ajax_load_available_items_cap_check( $role, $expected_resul
91118
$this->expectExceptionMessage( '-1' );
92119
}
93120

94-
wp_set_current_user( self::factory()->user->create( array( 'role' => $role ) ) );
121+
wp_set_current_user( self::$user_ids[ $role ] );
95122

96123
$_POST = array(
97124
'action' => 'load-available-menu-items-customizer',
@@ -485,7 +512,7 @@ public function test_ajax_search_available_items_caps_check( $role, $expected_re
485512
$this->expectExceptionMessage( '-1' );
486513
}
487514

488-
wp_set_current_user( self::factory()->user->create( array( 'role' => $role ) ) );
515+
wp_set_current_user( self::$user_ids[ $role ] );
489516

490517
$_POST = array(
491518
'action' => 'search-available-menu-items-customizer',
@@ -705,7 +732,7 @@ public function test_ajax_insert_auto_draft_failures() {
705732
$this->assertSame( 'bad_nonce', $response['data'] );
706733

707734
// Bad nonce.
708-
wp_set_current_user( self::factory()->user->create( array( 'role' => 'subscriber' ) ) );
735+
wp_set_current_user( self::$user_ids['subscriber'] );
709736
$_POST = wp_slash(
710737
array(
711738
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
@@ -718,7 +745,7 @@ public function test_ajax_insert_auto_draft_failures() {
718745
$this->assertSame( 'customize_not_allowed', $response['data'] );
719746

720747
// Missing params.
721-
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
748+
wp_set_current_user( self::$user_ids['administrator'] );
722749
$_POST = wp_slash(
723750
array(
724751
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),

tests/phpunit/tests/auth.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1579,12 +1579,7 @@ public function test_wp_signon_does_not_throw_fatal_errors_with_array_parameters
15791579
* @covers ::wp_validate_application_password
15801580
*/
15811581
public function test_application_password_authentication() {
1582-
$user_id = self::factory()->user->create(
1583-
array(
1584-
'user_login' => 'http_auth_login',
1585-
'user_pass' => 'http_auth_pass', // Shouldn't be allowed for API login.
1586-
)
1587-
);
1582+
$user_id = self::$_user->ID;
15881583

15891584
// Create a new app-only password.
15901585
list( $user_app_password, $item ) = WP_Application_Passwords::create_new_application_password( $user_id, array( 'name' => 'phpunit' ) );
@@ -1594,8 +1589,8 @@ public function test_application_password_authentication() {
15941589
add_filter( 'wp_is_application_passwords_available', '__return_true' );
15951590

15961591
// Fake an HTTP Auth request with the regular account password first.
1597-
$_SERVER['PHP_AUTH_USER'] = 'http_auth_login';
1598-
$_SERVER['PHP_AUTH_PW'] = 'http_auth_pass';
1592+
$_SERVER['PHP_AUTH_USER'] = self::USER_LOGIN;
1593+
$_SERVER['PHP_AUTH_PW'] = self::USER_PASS;
15991594

16001595
$this->assertNull(
16011596
wp_validate_application_password( null ),

tests/phpunit/tests/comment/wpHandleCommentSubmission.php

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Tests_Comment_wpHandleCommentSubmission extends WP_UnitTestCase {
99

1010
protected static $post;
1111
protected static $author_id;
12+
protected static $author_id2;
1213
protected static $editor_id;
1314

1415
protected $preprocess_comment_data = array();
@@ -22,6 +23,13 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
2223
)
2324
);
2425

26+
self::$author_id2 = $factory->user->create(
27+
array(
28+
'role' => 'author',
29+
'user_url' => 'http://user.example.org',
30+
)
31+
);
32+
2533
self::$editor_id = $factory->user->create(
2634
array(
2735
'role' => 'editor',
@@ -223,11 +231,7 @@ public function test_submitting_comment_to_password_protected_post_succeeds() {
223231

224232
public function test_submitting_valid_comment_as_logged_in_user_succeeds() {
225233

226-
$user = self::factory()->user->create_and_get(
227-
array(
228-
'user_url' => 'http://user.example.org',
229-
)
230-
);
234+
$user = get_user_by( 'id', self::$author_id2 );
231235

232236
wp_set_current_user( $user->ID );
233237

@@ -314,11 +318,7 @@ public function test_submitting_comment_as_logged_in_user_to_inaccessible_privat
314318

315319
$error = 'comment_id_not_found';
316320

317-
$user = self::factory()->user->create_and_get(
318-
array(
319-
'role' => 'author',
320-
)
321-
);
321+
$user = get_user_by( 'id', self::$author_id2 );
322322

323323
wp_set_current_user( $user->ID );
324324

@@ -343,11 +343,7 @@ public function test_submitting_comment_to_private_post_with_closed_comments_ret
343343

344344
$error = 'comment_id_not_found';
345345

346-
$user = self::factory()->user->create_and_get(
347-
array(
348-
'role' => 'author',
349-
)
350-
);
346+
$user = get_user_by( 'id', self::$author_id2 );
351347

352348
wp_set_current_user( $user->ID );
353349

@@ -834,12 +830,8 @@ public function test_comments_flood() {
834830
/**
835831
* @ticket 36901
836832
*/
837-
public function test_comments_flood_user_is_admin() {
838-
$user = self::factory()->user->create_and_get(
839-
array(
840-
'role' => 'administrator',
841-
)
842-
);
833+
public function test_comments_flood_user_can_moderate_comments() {
834+
$user = get_user_by( 'id', self::$editor_id );
843835
wp_set_current_user( $user->ID );
844836

845837
$data = array(
@@ -853,8 +845,9 @@ public function test_comments_flood_user_is_admin() {
853845
$data['comment'] = 'Wow! I am quick!';
854846
$second_comment = wp_handle_comment_submission( $data );
855847

856-
$this->assertNotWPError( $second_comment );
857-
$this->assertSame( (string) self::$post->ID, $second_comment->comment_post_ID );
848+
$this->assertTrue( current_user_can( 'moderate_comments' ), 'Test user should have the moderate_comments capability' );
849+
$this->assertNotWPError( $second_comment, 'Second comment should not trigger comment flooding error.' );
850+
$this->assertSame( (string) self::$post->ID, $second_comment->comment_post_ID, 'Second comment should be made against initial post.' );
858851
}
859852

860853
/**

tests/phpunit/tests/customize/control.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,28 @@ class Test_WP_Customize_Control extends WP_UnitTestCase {
1717
*/
1818
public $wp_customize;
1919

20+
/**
21+
* Shared user ID for the tests.
22+
23+
* @var int
24+
*/
25+
public static $user_id = 0;
26+
27+
/**
28+
* Set up shared fixtures.
29+
*
30+
* @param WP_UnitTest_Factory $factory Factory.
31+
*/
32+
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
33+
self::$user_id = $factory->user->create( array( 'role' => 'administrator' ) );
34+
}
35+
2036
/**
2137
* Set up.
2238
*/
2339
public function set_up() {
2440
parent::set_up();
25-
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
41+
wp_set_current_user( self::$user_id );
2642
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
2743
$GLOBALS['wp_customize'] = new WP_Customize_Manager();
2844
$this->wp_customize = $GLOBALS['wp_customize'];

0 commit comments

Comments
 (0)