|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @group admin |
| 4 | + */ |
| 5 | +class Tests_Admin_Post_QuickDraftSave extends WP_UnitTestCase { |
| 6 | + protected static $admin_id; |
| 7 | + |
| 8 | + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { |
| 9 | + self::$admin_id = $factory->user->create( array( 'role' => 'administrator' ) ); |
| 10 | + } |
| 11 | + |
| 12 | + public function set_up() { |
| 13 | + parent::set_up(); |
| 14 | + wp_set_current_user( self::$admin_id ); |
| 15 | + set_current_screen( 'dashboard' ); |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Test Happy Path: Successfully validating a correct nonce and post_ID. |
| 20 | + * * @ticket 65052 |
| 21 | + */ |
| 22 | + public function test_post_quickdraft_save_happy_path() { |
| 23 | + $post_id = self::factory()->post->create( array( 'post_status' => 'draft' ) ); |
| 24 | + $nonce = wp_create_nonce( 'add-post' ); |
| 25 | + |
| 26 | + $_REQUEST['_wpnonce'] = $nonce; |
| 27 | + $_REQUEST['post_ID'] = $post_id; |
| 28 | + |
| 29 | + $nonce_req = $_REQUEST['_wpnonce'] ?? ''; |
| 30 | + $id_req = absint( $_REQUEST['post_ID'] ?? 0 ); |
| 31 | + $post = $id_req ? get_post( $id_req ) : null; |
| 32 | + |
| 33 | + $error_msg = false; |
| 34 | + if ( ! $post || ! wp_verify_nonce( $nonce_req, 'add-post' ) ) { |
| 35 | + $error_msg = __( 'Unable to submit this form, please refresh and try again.' ); |
| 36 | + } |
| 37 | + |
| 38 | + $this->assertFalse( $error_msg, 'Happy path should not produce an error message.' ); |
| 39 | + $this->assertNotNull( $post ); |
| 40 | + $this->assertEquals( $post_id, $post->ID ); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @ticket 65052 |
| 45 | + * test post quickdraft save missing nonce |
| 46 | + */ |
| 47 | + public function test_post_quickdraft_save_missing_nonce() { |
| 48 | + $_REQUEST['action'] = 'post-quickdraft-save'; |
| 49 | + unset( $_REQUEST['_wpnonce'] ); // invliad nonce |
| 50 | + $_REQUEST['post_ID'] = 0; |
| 51 | + |
| 52 | + $nonce = $_REQUEST['_wpnonce'] ?? ''; |
| 53 | + $post_id = absint( $_REQUEST['post_ID'] ?? 0 ); |
| 54 | + $post = $post_id ? get_post( $post_id ) : null; |
| 55 | + |
| 56 | + $error_msg = false; |
| 57 | + if ( ! $post || ! wp_verify_nonce( $nonce, 'add-post' ) ) { |
| 58 | + $error_msg = __( 'Unable to submit this form, please refresh and try again.' ); |
| 59 | + } |
| 60 | + |
| 61 | + $this->assertSame( 'Unable to submit this form, please refresh and try again.', $error_msg ); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @ticket 65052 |
| 66 | + * test post quickdraft save invalid all |
| 67 | + */ |
| 68 | + public function test_post_quickdraft_save_invalid_all() { |
| 69 | + $_REQUEST['_wpnonce'] = 'invalid_nonce'; |
| 70 | + $_REQUEST['post_ID'] = -1; // invalid ID |
| 71 | + |
| 72 | + $nonce = $_REQUEST['_wpnonce'] ?? ''; |
| 73 | + $post_id = absint( $_REQUEST['post_ID'] ?? 0 ); |
| 74 | + $post = $post_id ? get_post( $post_id ) : null; |
| 75 | + |
| 76 | + $this->assertNull( $post ); |
| 77 | + |
| 78 | + $error_msg = false; |
| 79 | + if ( ! $post || ! wp_verify_nonce( $nonce, 'add-post' ) ) { |
| 80 | + $error_msg = __( 'Unable to submit this form, please refresh and try again.' ); |
| 81 | + } |
| 82 | + |
| 83 | + $this->assertNotEmpty( $error_msg ); |
| 84 | + } |
| 85 | +} |
0 commit comments