diff options
Diffstat (limited to 'tests/phpunit')
-rw-r--r-- | tests/phpunit/includes/abstract-testcase.php | 15 | ||||
-rw-r--r-- | tests/phpunit/tests/auth.php | 5 | ||||
-rw-r--r-- | tests/phpunit/tests/comment/commentForm.php | 34 | ||||
-rw-r--r-- | tests/phpunit/tests/rest-api/rest-settings-controller.php | 32 | ||||
-rw-r--r-- | tests/phpunit/tests/user/getTheAuthorPosts.php | 4 |
5 files changed, 82 insertions, 8 deletions
diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index 6750df13c7..29ce8d8b53 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -136,6 +136,21 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Adapter_TestCase { $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; } /** diff --git a/tests/phpunit/tests/auth.php b/tests/phpunit/tests/auth.php index 405a8526d0..a490842ebd 100644 --- a/tests/phpunit/tests/auth.php +++ b/tests/phpunit/tests/auth.php @@ -2089,9 +2089,6 @@ class Tests_Auth extends WP_UnitTestCase { } 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; } } diff --git a/tests/phpunit/tests/comment/commentForm.php b/tests/phpunit/tests/comment/commentForm.php index 771cbc1b57..e3dab07e24 100644 --- a/tests/phpunit/tests/comment/commentForm.php +++ b/tests/phpunit/tests/comment/commentForm.php @@ -193,4 +193,38 @@ class Tests_Comment_CommentForm extends WP_UnitTestCase { $post_hidden_field = "<input type='hidden' name='comment_post_ID' value='{$post_id}' id='comment_post_ID' />"; $this->assertStringContainsString( $post_hidden_field, $form ); } + + /** + * Tests novalidate attribute on the comment form. + * + * @ticket 47595 + */ + public function test_comment_form_and_novalidate_attribute() { + $post_id = self::$post_id; + + // By default, the novalidate is not emitted. + $form = get_echo( 'comment_form', array( array(), $post_id ) ); + $p = new WP_HTML_Tag_Processor( $form ); + $this->assertTrue( $p->next_tag( array( 'tag_name' => 'FORM' ) ), 'Expected FORM tag.' ); + $this->assertNull( $p->get_attribute( 'novalidate' ), 'Expected FORM to not have novalidate attribute by default.' ); + + // Opt in to the novalidate attribute by passing an arg to comment_form(). + $form = get_echo( 'comment_form', array( array( 'novalidate' => true ), $post_id ) ); + $p = new WP_HTML_Tag_Processor( $form ); + $this->assertTrue( $p->next_tag( array( 'tag_name' => 'FORM' ) ), 'Expected FORM tag.' ); + $this->assertTrue( $p->get_attribute( 'novalidate' ), 'Expected FORM to have the novalidate attribute.' ); + + // Opt in to the novalidate attribute via the comment_form_defaults filter. + add_filter( + 'comment_form_defaults', + static function ( array $defaults ): array { + $defaults['novalidate'] = true; + return $defaults; + } + ); + $form = get_echo( 'comment_form', array( array(), $post_id ) ); + $p = new WP_HTML_Tag_Processor( $form ); + $this->assertTrue( $p->next_tag( array( 'tag_name' => 'FORM' ) ), 'Expected FORM tag.' ); + $this->assertTrue( $p->get_attribute( 'novalidate' ), 'Expected FORM to have novalidate attribute.' ); + } } diff --git a/tests/phpunit/tests/rest-api/rest-settings-controller.php b/tests/phpunit/tests/rest-api/rest-settings-controller.php index e8f90b53f2..2e5e978655 100644 --- a/tests/phpunit/tests/rest-api/rest-settings-controller.php +++ b/tests/phpunit/tests/rest-api/rest-settings-controller.php @@ -385,14 +385,21 @@ class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase } /** - * @doesNotPerformAssertions + * Settings can't be created */ public function test_create_item() { - // Controller does not implement create_item(). + wp_set_current_user( self::$administrator ); + + $request = new WP_REST_Request( 'POST', '/wp/v2/settings' ); + $request->set_param( 'new_setting', 'New value' ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertSame( 400, $response->get_status() ); } public function test_update_item() { wp_set_current_user( self::$administrator ); + $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' ); $request->set_param( 'title', 'The new title!' ); $response = rest_get_server()->dispatch( $request ); @@ -403,6 +410,27 @@ class WP_Test_REST_Settings_Controller extends WP_Test_REST_Controller_Testcase $this->assertSame( get_option( 'blogname' ), $data['title'] ); } + public function test_update_nonexistent_item() { + wp_set_current_user( self::$administrator ); + + $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' ); + $request->set_param( 'i_do_no_exist', 'New value' ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertSame( 400, $response->get_status() ); + } + + public function test_update_partially_valid_items() { + wp_set_current_user( self::$administrator ); + + $request = new WP_REST_Request( 'PUT', '/wp/v2/settings' ); + $request->set_param( 'title', 'The new title!' ); + $request->set_param( 'i_do_no_exist', 'New value' ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertSame( 400, $response->get_status() ); + } + public function update_setting_custom_callback( $result, $name, $value, $args ) { if ( 'title' === $name && 'The new title!' === $value ) { // Do not allow changing the title in this case. diff --git a/tests/phpunit/tests/user/getTheAuthorPosts.php b/tests/phpunit/tests/user/getTheAuthorPosts.php index 5cd87bc79b..3a0abac5de 100644 --- a/tests/phpunit/tests/user/getTheAuthorPosts.php +++ b/tests/phpunit/tests/user/getTheAuthorPosts.php @@ -42,7 +42,7 @@ class Tests_User_GetTheAuthorPosts extends WP_UnitTestCase { // Test with no global post, result should be 0 because no author is found. $this->assertSame( 0, get_the_author_posts() ); $GLOBALS['post'] = self::$post_id; - $this->assertEquals( 1, get_the_author_posts() ); + $this->assertSame( 1, get_the_author_posts() ); } /** @@ -60,7 +60,7 @@ class Tests_User_GetTheAuthorPosts extends WP_UnitTestCase { ); $GLOBALS['post'] = $cpt_ids[0]; - $this->assertEquals( 2, get_the_author_posts() ); + $this->assertSame( 2, get_the_author_posts() ); _unregister_post_type( 'wptests_pt' ); } |