Ticket #43559: 43559.diff
File 43559.diff, 3.6 KB (added by , 3 years ago) |
---|
-
src/wp-admin/includes/ajax-actions.php
diff --git src/wp-admin/includes/ajax-actions.php src/wp-admin/includes/ajax-actions.php index 8764af4..12d91e7 100644
function wp_ajax_add_meta() { 1433 1433 if ( '' == trim( $key ) ) { 1434 1434 wp_die( __( 'Please provide a custom field name.' ) ); 1435 1435 } 1436 if ( '' == trim( $value ) ) {1437 wp_die( __( 'Please provide a custom field value.' ) );1438 }1439 1436 if ( ! $meta = get_metadata_by_mid( 'post', $mid ) ) { 1440 1437 wp_die( 0 ); // if meta doesn't exist 1441 1438 } -
src/wp-admin/includes/post.php
diff --git src/wp-admin/includes/post.php src/wp-admin/includes/post.php index 727269e..222f172 100644
function add_meta( $post_ID ) { 861 861 $metavalue = trim( $metavalue ); 862 862 } 863 863 864 if ( ( '0' === $metavalue || ! empty( $metavalue ) ) && ( ( ( '#NONE#' != $metakeyselect ) && ! empty( $metakeyselect ) ) || ! empty( $metakeyinput )) ) {864 if ( ( ( '#NONE#' != $metakeyselect ) && ! empty( $metakeyselect ) ) || ! empty( $metakeyinput ) ) { 865 865 /* 866 866 * We have a key/value pair. If both the select and the input 867 867 * for the key have data, the input takes precedence. -
tests/phpunit/tests/admin/includesPost.php
diff --git tests/phpunit/tests/admin/includesPost.php tests/phpunit/tests/admin/includesPost.php index 5c080f2..6dcad7b 100644
class Tests_Admin_Includes_Post extends WP_UnitTestCase { 748 748 $this->assertSame( $p, post_exists( $title, $content, $date ) ); 749 749 } 750 750 751 /** 752 * @ticket 43559 753 */ 754 public function test_post_add_meta_empty_is_allowed() { 755 $p = self::factory()->post->create(); 756 757 $_POST = array( 758 'metakeyinput' => 'testkey', 759 'metavalue' => '', 760 ); 761 762 wp_set_current_user( self::$admin_id ); 763 764 $this->assertNotFalse( add_meta( $p ) ); 765 $this->assertEquals( '', get_post_meta( $p, 'testkey', true ) ); 766 } 751 767 } -
new file tests/phpunit/tests/ajax/AddMeta.php
diff --git tests/phpunit/tests/ajax/AddMeta.php tests/phpunit/tests/ajax/AddMeta.php new file mode 100644 index 0000000..a20deed
- + 1 <?php 2 3 /** 4 * Admin ajax functions to be tested 5 */ 6 require_once( ABSPATH . 'wp-admin/includes/ajax-actions.php' ); 7 8 /** 9 * Testing Add Meta AJAX functionality. 10 * 11 * @group ajax 12 */ 13 class Tests_Ajax_AddMeta extends WP_Ajax_UnitTestCase { 14 /** 15 * @ticket 43559 16 */ 17 public function test_post_add_meta_empty_is_allowed_ajax() { 18 $p = self::factory()->post->create(); 19 20 // Become an administrator. 21 $this->_setRole( 'administrator' ); 22 23 $_POST = array( 24 'post_id' => $p, 25 'metakeyinput' => 'testkey', 26 'metavalue' => '', 27 '_ajax_nonce-add-meta' => wp_create_nonce( 'add-meta' ), 28 ); 29 30 // Make the request. 31 try { 32 $this->_handleAjax( 'add-meta' ); 33 } catch ( WPAjaxDieContinueException $e ) { 34 unset( $e ); 35 } 36 37 $this->assertEquals( '', get_post_meta( $p, 'testkey', true ) ); 38 } 39 40 /** 41 * @ticket 43559 42 */ 43 public function test_post_update_meta_empty_is_allowed_ajax() { 44 $p = self::factory()->post->create(); 45 46 $m = add_post_meta( $p, 'testkey', 'hello' ); 47 48 // Become an administrator. 49 $this->_setRole( 'administrator' ); 50 51 $_POST = array( 52 '_ajax_nonce-add-meta' => wp_create_nonce( 'add-meta' ), 53 'post_id' => $p, 54 'meta' => array( 55 $m => array( 56 'key' => 'testkey', 57 'value' => '', 58 ), 59 ), 60 ); 61 62 // Make the request. 63 try { 64 $this->_handleAjax( 'add-meta' ); 65 } catch ( WPAjaxDieContinueException $e ) { 66 unset( $e ); 67 } 68 69 $this->assertEquals( '', get_post_meta( $p, 'testkey', true ) ); 70 } 71 }