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