Attachments.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Admin Ajax functions to be tested.
  4. */
  5. require_once ABSPATH . 'wp-admin/includes/ajax-actions.php';
  6. /**
  7. * Testing Ajax attachment handling.
  8. *
  9. * @group ajax
  10. */
  11. class Tests_Ajax_Attachments extends WP_Ajax_UnitTestCase {
  12. /**
  13. * @ticket 36578
  14. */
  15. public function test_wp_ajax_send_attachment_to_editor_should_return_an_image() {
  16. // Become an administrator.
  17. $post = $_POST;
  18. $user_id = self::factory()->user->create(
  19. array(
  20. 'role' => 'administrator',
  21. 'user_login' => 'user_36578_administrator',
  22. 'user_email' => 'user_36578_administrator@example.com',
  23. )
  24. );
  25. wp_set_current_user( $user_id );
  26. $_POST = array_merge( $_POST, $post );
  27. $filename = DIR_TESTDATA . '/images/canola.jpg';
  28. $contents = file_get_contents( $filename );
  29. $upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
  30. $attachment = $this->_make_attachment( $upload );
  31. // Set up a default request.
  32. $_POST['nonce'] = wp_create_nonce( 'media-send-to-editor' );
  33. $_POST['html'] = 'Bar Baz';
  34. $_POST['post_id'] = 0;
  35. $_POST['attachment'] = array(
  36. 'id' => $attachment,
  37. 'align' => 'left',
  38. 'image-size' => 'large',
  39. 'image_alt' => 'Foo bar',
  40. 'url' => 'http://example.com/',
  41. );
  42. // Make the request.
  43. try {
  44. $this->_handleAjax( 'send-attachment-to-editor' );
  45. } catch ( WPAjaxDieContinueException $e ) {
  46. unset( $e );
  47. }
  48. // Get the response.
  49. $response = json_decode( $this->_last_response, true );
  50. $expected = get_image_send_to_editor( $attachment, '', '', 'left', 'http://example.com/', false, 'large', 'Foo bar' );
  51. // Ensure everything is correct.
  52. $this->assertTrue( $response['success'] );
  53. $this->assertSame( $expected, $response['data'] );
  54. }
  55. /**
  56. * @ticket 36578
  57. */
  58. public function test_wp_ajax_send_attachment_to_editor_should_return_a_link() {
  59. $this->skipWithMultisite();
  60. // Become an administrator.
  61. $post = $_POST;
  62. $user_id = self::factory()->user->create(
  63. array(
  64. 'role' => 'administrator',
  65. 'user_login' => 'user_36578_administrator',
  66. 'user_email' => 'user_36578_administrator@example.com',
  67. )
  68. );
  69. wp_set_current_user( $user_id );
  70. $_POST = array_merge( $_POST, $post );
  71. $filename = DIR_TESTDATA . '/formatting/entities.txt';
  72. $contents = file_get_contents( $filename );
  73. $upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
  74. $attachment = $this->_make_attachment( $upload );
  75. // Set up a default request.
  76. $_POST['nonce'] = wp_create_nonce( 'media-send-to-editor' );
  77. $_POST['html'] = 'Bar Baz';
  78. $_POST['post_id'] = 0;
  79. $_POST['attachment'] = array(
  80. 'id' => $attachment,
  81. 'post_title' => 'Foo bar',
  82. 'url' => get_attachment_link( $attachment ),
  83. );
  84. // Make the request.
  85. try {
  86. $this->_handleAjax( 'send-attachment-to-editor' );
  87. } catch ( WPAjaxDieContinueException $e ) {
  88. unset( $e );
  89. }
  90. // Get the response.
  91. $response = json_decode( $this->_last_response, true );
  92. $expected = sprintf(
  93. '<a href="%s" rel="attachment wp-att-%d">Foo bar</a>',
  94. get_attachment_link( $attachment ),
  95. $attachment
  96. );
  97. // Ensure everything is correct.
  98. $this->assertTrue( $response['success'] );
  99. $this->assertSame( $expected, $response['data'] );
  100. }
  101. }