AddMeta.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * Admin Ajax functions to be tested.
  4. */
  5. require_once ABSPATH . 'wp-admin/includes/ajax-actions.php';
  6. /**
  7. * Testing Add Meta AJAX functionality.
  8. *
  9. * @group ajax
  10. */
  11. class Tests_Ajax_AddMeta extends WP_Ajax_UnitTestCase {
  12. /**
  13. * @ticket 43559
  14. */
  15. public function test_post_add_meta_empty_is_allowed_ajax() {
  16. $p = self::factory()->post->create();
  17. // Become an administrator.
  18. $this->_setRole( 'administrator' );
  19. $_POST = array(
  20. 'post_id' => $p,
  21. 'metakeyinput' => 'testkey',
  22. 'metavalue' => '',
  23. '_ajax_nonce-add-meta' => wp_create_nonce( 'add-meta' ),
  24. );
  25. // Make the request.
  26. try {
  27. $this->_handleAjax( 'add-meta' );
  28. } catch ( WPAjaxDieContinueException $e ) {
  29. unset( $e );
  30. }
  31. $this->assertSame( '', get_post_meta( $p, 'testkey', true ) );
  32. }
  33. /**
  34. * @ticket 43559
  35. */
  36. public function test_post_update_meta_empty_is_allowed_ajax() {
  37. $p = self::factory()->post->create();
  38. $m = add_post_meta( $p, 'testkey', 'hello' );
  39. // Become an administrator.
  40. $this->_setRole( 'administrator' );
  41. $_POST = array(
  42. '_ajax_nonce-add-meta' => wp_create_nonce( 'add-meta' ),
  43. 'post_id' => $p,
  44. 'meta' => array(
  45. $m => array(
  46. 'key' => 'testkey',
  47. 'value' => '',
  48. ),
  49. ),
  50. );
  51. // Make the request.
  52. try {
  53. $this->_handleAjax( 'add-meta' );
  54. } catch ( WPAjaxDieContinueException $e ) {
  55. unset( $e );
  56. }
  57. $this->assertSame( '', get_post_meta( $p, 'testkey', true ) );
  58. }
  59. }