Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions tests/phpunit/includes/abstract-testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,21 @@ public function set_up() {
$this->start_transaction();
$this->expectDeprecated();
add_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) );
add_filter( 'wp_hash_password_options', array( $this, 'wp_hash_password_options' ), 1, 2 );
}

/**
* Sets the bcrypt cost option for password hashing during tests.
*
* @param array $options The options for password hashing.
* @param string|int $algorithm The algorithm to use for hashing. This is a string in PHP 7.4+ and an integer in PHP 7.3 and earlier.
*/
public function wp_hash_password_options( array $options, $algorithm ): array {
if ( PASSWORD_BCRYPT === $algorithm ) {
$options['cost'] = 5;
}

return $options;
}

/**
Expand Down
21 changes: 15 additions & 6 deletions tests/phpunit/tests/admin/includesPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@
* @group admin
*/
class Tests_Admin_IncludesPlugin extends WP_UnitTestCase {

/**
* Admin user ID.
*
* @var int $admin_id
*/
public static $admin_id;

public static function wpSetUpBeforeClass( $factory ) {
self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) );
self::_back_up_mu_plugins();
}

Expand Down Expand Up @@ -37,7 +46,7 @@ public function test_get_plugin_data() {

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

// Add some pages.
Expand Down Expand Up @@ -81,7 +90,7 @@ public function test_submenu_position( $position, $expected_position ) {
global $submenu;
global $menu;
$current_user = get_current_user_id();
$admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) );
$admin_user = self::$admin_id;
wp_set_current_user( $admin_user );
set_current_screen( 'dashboard' );

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

$current_user = get_current_user_id();
$admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) );
$admin_user = self::$admin_id;
wp_set_current_user( $admin_user );
set_current_screen( 'dashboard' );

Expand Down Expand Up @@ -283,7 +292,7 @@ public function test_position_when_parent_slug_child_slug_are_the_same() {
$submenu = array();
$menu = array();
$current_user = get_current_user_id();
$admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) );
$admin_user = self::$admin_id;
wp_set_current_user( $admin_user );
set_current_screen( 'dashboard' );

Expand Down Expand Up @@ -316,7 +325,7 @@ public function test_passing_string_as_position_fires_doing_it_wrong_submenu() {
$submenu = array();
$menu = array();
$current_user = get_current_user_id();
$admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) );
$admin_user = self::$admin_id;
wp_set_current_user( $admin_user );
set_current_screen( 'dashboard' );

Expand Down Expand Up @@ -344,7 +353,7 @@ public function test_passing_float_as_position_does_not_override_int() {
$submenu = array();
$menu = array();
$current_user = get_current_user_id();
$admin_user = self::factory()->user->create( array( 'role' => 'administrator' ) );
$admin_user = self::$admin_id;
wp_set_current_user( $admin_user );
set_current_screen( 'dashboard' );

Expand Down
14 changes: 12 additions & 2 deletions tests/phpunit/tests/admin/includesTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
* @group admin
*/
class Tests_Admin_IncludesTemplate extends WP_UnitTestCase {
/**
* Editor user ID.
*
* @var int $editor_id
*/
public static $editor_id;

public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
}

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

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

// Test that get_inline_data() has `tags_input` div containing the assigned term.
wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) );
wp_set_current_user( self::$editor_id );
get_inline_data( $post );
$this->expectOutputRegex( '/<div class="tags_input" id="wptests_tax_1_' . $post->ID . '">Test<\/div>/' );
}
Expand Down
38 changes: 24 additions & 14 deletions tests/phpunit/tests/ajax/wpAjaxSendAttachmentToEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@
*/
class Tests_Ajax_wpAjaxSendAttachmentToEditor extends WP_Ajax_UnitTestCase {

/**
* Shared user ID for the tests.
*
* @var int
*/
public static $user_id = 0;

/**
* Set up shared fixtures.
*
* @param WP_UnitTest_Factory $factory
*/
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$user_id = $factory->user->create(
array(
'role' => 'administrator',
'user_login' => 'user_36578_administrator',
'user_email' => 'user_36578_administrator@example.com',
)
);
}

/**
* @ticket 36578
*
Expand Down Expand Up @@ -105,13 +127,7 @@ public function test_wp_ajax_send_attachment_to_editor_should_return_a_link() {
public function test_wp_ajax_set_attachment_thumbnail_success() {
// Become an administrator.
$post = $_POST;
$user_id = self::factory()->user->create(
array(
'role' => 'administrator',
'user_login' => 'user_36578_administrator',
'user_email' => 'user_36578_administrator@example.com',
)
);
$user_id = self::$user_id;
wp_set_current_user( $user_id );
$_POST = array_merge( $_POST, $post );

Expand Down Expand Up @@ -151,13 +167,7 @@ public function test_wp_ajax_set_attachment_thumbnail_success() {
public function test_wp_ajax_set_attachment_thumbnail_missing_nonce() {
// Become an administrator.
$post = $_POST;
$user_id = self::factory()->user->create(
array(
'role' => 'administrator',
'user_login' => 'user_36578_administrator',
'user_email' => 'user_36578_administrator@example.com',
)
);
$user_id = self::$user_id;
wp_set_current_user( $user_id );
$_POST = array_merge( $_POST, $post );

Expand Down
37 changes: 32 additions & 5 deletions tests/phpunit/tests/ajax/wpCustomizeNavMenus.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,40 @@ class Tests_Ajax_wpCustomizeNavMenus extends WP_Ajax_UnitTestCase {
*/
public static $terms;


/**
* Admin user ID.
*
* @var int
*/
public static $admin_user_id = 0;

/**
* User IDs keyed by role.
*
* @var int[]
*/
public static $user_ids = array();

/**
* Set up shared fixtures.
*
* @param WP_UnitTest_Factory $factory The factory.
*/
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
// Make some post objects.
self::$posts = $factory->post->create_many( 5 );
self::$pages = $factory->post->create_many( 5, array( 'post_type' => 'page' ) );

// Some terms too.
self::$terms = $factory->term->create_many( 5 );

// Create an admin user.
self::$admin_user_id = $factory->user->create( array( 'role' => 'administrator' ) );

foreach ( array( 'administrator', 'editor', 'author', 'contributor', 'subscriber' ) as $role ) {
self::$user_ids[ $role ] = $factory->user->create( array( 'role' => $role ) );
}
}

/**
Expand All @@ -53,7 +80,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
public function set_up() {
parent::set_up();
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
wp_set_current_user( self::$admin_user_id );
global $wp_customize;
$this->wp_customize = new WP_Customize_Manager();
$wp_customize = $this->wp_customize;
Expand Down Expand Up @@ -91,7 +118,7 @@ public function test_ajax_load_available_items_cap_check( $role, $expected_resul
$this->expectExceptionMessage( '-1' );
}

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

$_POST = array(
'action' => 'load-available-menu-items-customizer',
Expand Down Expand Up @@ -485,7 +512,7 @@ public function test_ajax_search_available_items_caps_check( $role, $expected_re
$this->expectExceptionMessage( '-1' );
}

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

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

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

// Missing params.
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
wp_set_current_user( self::$user_ids['administrator'] );
$_POST = wp_slash(
array(
'customize-menus-nonce' => wp_create_nonce( 'customize-menus' ),
Expand Down
16 changes: 4 additions & 12 deletions tests/phpunit/tests/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -1579,12 +1579,7 @@ public function test_wp_signon_does_not_throw_fatal_errors_with_array_parameters
* @covers ::wp_validate_application_password
*/
public function test_application_password_authentication() {
$user_id = self::factory()->user->create(
array(
'user_login' => 'http_auth_login',
'user_pass' => 'http_auth_pass', // Shouldn't be allowed for API login.
)
);
$user_id = self::$_user->ID;

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

// Fake an HTTP Auth request with the regular account password first.
$_SERVER['PHP_AUTH_USER'] = 'http_auth_login';
$_SERVER['PHP_AUTH_PW'] = 'http_auth_pass';
$_SERVER['PHP_AUTH_USER'] = self::USER_LOGIN;
$_SERVER['PHP_AUTH_PW'] = self::USER_PASS;

$this->assertNull(
wp_validate_application_password( null ),
Expand Down Expand Up @@ -2094,9 +2089,6 @@ private static function set_application_password( string $hash, int $user_id ) {
}

private static function get_default_bcrypt_cost(): int {
$hash = password_hash( 'password', PASSWORD_BCRYPT );
$info = password_get_info( $hash );

return $info['options']['cost'];
return 5;
}
}
39 changes: 16 additions & 23 deletions tests/phpunit/tests/comment/wpHandleCommentSubmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Tests_Comment_wpHandleCommentSubmission extends WP_UnitTestCase {

protected static $post;
protected static $author_id;
protected static $author_id2;
protected static $editor_id;

protected $preprocess_comment_data = array();
Expand All @@ -22,6 +23,13 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
)
);

self::$author_id2 = $factory->user->create(
array(
'role' => 'author',
'user_url' => 'http://user.example.org',
)
);

self::$editor_id = $factory->user->create(
array(
'role' => 'editor',
Expand Down Expand Up @@ -223,11 +231,7 @@ public function test_submitting_comment_to_password_protected_post_succeeds() {

public function test_submitting_valid_comment_as_logged_in_user_succeeds() {

$user = self::factory()->user->create_and_get(
array(
'user_url' => 'http://user.example.org',
)
);
$user = get_user_by( 'id', self::$author_id2 );

wp_set_current_user( $user->ID );

Expand Down Expand Up @@ -314,11 +318,7 @@ public function test_submitting_comment_as_logged_in_user_to_inaccessible_privat

$error = 'comment_id_not_found';

$user = self::factory()->user->create_and_get(
array(
'role' => 'author',
)
);
$user = get_user_by( 'id', self::$author_id2 );

wp_set_current_user( $user->ID );

Expand All @@ -343,11 +343,7 @@ public function test_submitting_comment_to_private_post_with_closed_comments_ret

$error = 'comment_id_not_found';

$user = self::factory()->user->create_and_get(
array(
'role' => 'author',
)
);
$user = get_user_by( 'id', self::$author_id2 );

wp_set_current_user( $user->ID );

Expand Down Expand Up @@ -834,12 +830,8 @@ public function test_comments_flood() {
/**
* @ticket 36901
*/
public function test_comments_flood_user_is_admin() {
$user = self::factory()->user->create_and_get(
array(
'role' => 'administrator',
)
);
public function test_comments_flood_user_can_moderate_comments() {
$user = get_user_by( 'id', self::$editor_id );
wp_set_current_user( $user->ID );

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

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

/**
Expand Down
Loading
Loading