| 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 | $this->_setRole( 'administrator' ); |
| 19 | |
| 20 | $filename = DIR_TESTDATA . '/images/canola.jpg'; |
| 21 | $contents = file_get_contents( $filename ); |
| 22 | |
| 23 | $upload = wp_upload_bits( basename( $filename ), null, $contents ); |
| 24 | $attachment = $this->_make_attachment( $upload ); |
| 25 | |
| 26 | // Set up a default request |
| 27 | $_POST['nonce'] = wp_create_nonce( 'media-send-to-editor' ); |
| 28 | $_POST['html'] = 'Bar Baz'; |
| 29 | $_POST['post_id'] = 0; |
| 30 | $_POST['attachment'] = array( |
| 31 | 'id' => $attachment, |
| 32 | 'align' => 'left', |
| 33 | 'image-size' => 'large', |
| 34 | 'image_alt' => 'Foo bar', |
| 35 | 'url' => 'http://example.com/', |
| 36 | ); |
| 37 | |
| 38 | // Make the request |
| 39 | try { |
| 40 | $this->_handleAjax( 'send-attachment-to-editor' ); |
| 41 | } catch ( WPAjaxDieContinueException $e ) { |
| 42 | unset( $e ); |
| 43 | } |
| 44 | |
| 45 | // Get the response. |
| 46 | $response = json_decode( $this->_last_response, true ); |
| 47 | |
| 48 | $expected = get_image_send_to_editor( $attachment, '', '', 'left', 'http://example.com/', false, 'large', 'Foo bar' ); |
| 49 | |
| 50 | // Ensure everything is correct |
| 51 | $this->assertTrue( $response['success'] ); |
| 52 | $this->assertEquals( $expected, $response['data'] ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @ticket 36578 |
| 57 | */ |
| 58 | public function test_wp_ajax_send_attachment_to_editor_should_return_a_link() { |
| 59 | // Become an administrator |
| 60 | $this->_setRole( 'administrator' ); |
| 61 | |
| 62 | $filename = DIR_TESTDATA . '/formatting/entities.txt'; |
| 63 | $contents = file_get_contents( $filename ); |
| 64 | |
| 65 | $upload = wp_upload_bits( basename( $filename ), null, $contents ); |
| 66 | $attachment = $this->_make_attachment( $upload ); |
| 67 | |
| 68 | // Set up a default request |
| 69 | $_POST['nonce'] = wp_create_nonce( 'media-send-to-editor' ); |
| 70 | $_POST['html'] = 'Bar Baz'; |
| 71 | $_POST['post_id'] = 0; |
| 72 | $_POST['attachment'] = array( |
| 73 | 'id' => $attachment, |
| 74 | 'post_title' => 'Foo bar', |
| 75 | 'url' => get_attachment_link( $attachment ), |
| 76 | ); |
| 77 | |
| 78 | // Make the request |
| 79 | try { |
| 80 | $this->_handleAjax( 'send-attachment-to-editor' ); |
| 81 | } catch ( WPAjaxDieContinueException $e ) { |
| 82 | unset( $e ); |
| 83 | } |
| 84 | |
| 85 | // Get the response. |
| 86 | $response = json_decode( $this->_last_response, true ); |
| 87 | |
| 88 | $expected = sprintf( |
| 89 | '<a href="%s" rel="attachment wp-att-%d">Foo bar</a>', |
| 90 | get_attachment_link( $attachment ), |
| 91 | $attachment |
| 92 | ); |
| 93 | |
| 94 | // Ensure everything is correct |
| 95 | $this->assertTrue( $response['success'] ); |
| 96 | $this->assertEquals( $expected, $response['data'] ); |
| 97 | } |
| 98 | } |