Ticket #43559: 43559.2.diff
File 43559.2.diff, 3.8 KB (added by , 6 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 8fb348c..fcc67eb 100644
function wp_ajax_add_meta() { 1341 1341 $value = wp_unslash( $_POST['meta'][$mid]['value'] ); 1342 1342 if ( '' == trim($key) ) 1343 1343 wp_die( __( 'Please provide a custom field name.' ) ); 1344 if ( '' == trim($value) )1345 wp_die( __( 'Please provide a custom field value.' ) );1346 1344 if ( ! $meta = get_metadata_by_mid( 'post', $mid ) ) 1347 1345 wp_die( 0 ); // if meta doesn't exist 1348 1346 if ( is_protected_meta( $meta->meta_key, 'post' ) || is_protected_meta( $key, 'post' ) || -
src/wp-admin/includes/post.php
diff --git src/wp-admin/includes/post.php src/wp-admin/includes/post.php index 015c7d3..a95ff78 100644
function add_meta( $post_ID ) { 814 814 if ( is_string( $metavalue ) ) 815 815 $metavalue = trim( $metavalue ); 816 816 817 if ( ( '0' === $metavalue || ! empty ( $metavalue ) ) && ( ( ( '#NONE#' != $metakeyselect ) && !empty ( $metakeyselect) ) || !empty ( $metakeyinput )) ) {817 if ( ( ( '#NONE#' != $metakeyselect ) && ! empty( $metakeyselect ) ) || ! empty( $metakeyinput ) ) { 818 818 /* 819 819 * We have a key/value pair. If both the select and the input 820 820 * 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 f57b27f..3d782e2 100644
class Tests_Admin_Includes_Post extends WP_UnitTestCase { 707 707 $this->assertArrayHasKey( $name, $blocks ); 708 708 $this->assertSame( array( 'icon' => 'text' ), $blocks[ $name ] ); 709 709 } 710 711 /** 712 * @ticket 43559 713 */ 714 public function test_post_add_meta_empty_is_allowed() { 715 $p = self::factory()->post->create(); 716 717 $_POST = array( 718 'metakeyinput' => 'testkey', 719 'metavalue' => '', 720 ); 721 722 wp_set_current_user( self::$admin_id ); 723 724 $this->assertNotFalse( add_meta( $p ) ); 725 $this->assertEquals( '', get_post_meta( $p, 'testkey', true ) ); 726 } 710 727 } -
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 }