| 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 | * @group ajax |
| 10 | */ |
| 11 | class Tests_Ajax_SendAttachmentToEditor extends WP_Ajax_UnitTestCase { |
| 12 | private $_ids; |
| 13 | |
| 14 | /** |
| 15 | * Set up the test fixture |
| 16 | */ |
| 17 | public function setUp() { |
| 18 | parent::setUp(); |
| 19 | $_SERVER['REMOTE_ADDR'] = ''; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Tear down the test fixture. |
| 24 | */ |
| 25 | public function tearDown() { |
| 26 | // Cleanup |
| 27 | foreach ( $this->_ids as $id ) { |
| 28 | wp_delete_attachment( $id, true ); |
| 29 | } |
| 30 | |
| 31 | parent::tearDown(); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @ticket 32112 |
| 36 | */ |
| 37 | public function test_src_should_be_http_when_homeurl_is_http_even_when_is_ssl() { |
| 38 | $_https = isset( $_SERVER['HTTPS'] ) ? $_SERVER['HTTPS'] : null; |
| 39 | $_post_backup = $_POST; |
| 40 | |
| 41 | $post_id = $this->factory->post->create(); |
| 42 | |
| 43 | $filename = DIR_TESTDATA . '/images/canola.jpg'; |
| 44 | $contents = file_get_contents( $filename ); |
| 45 | |
| 46 | $upload = wp_upload_bits( basename( $filename ), null, $contents ); |
| 47 | $attachment_id = $this->_make_attachment( $upload, $post_id ); |
| 48 | |
| 49 | $attachment_url = wp_get_attachment_url( $attachment_id ); |
| 50 | $this->assertSame( $attachment_url, set_url_scheme( $attachment_url, 'http' ) ); |
| 51 | |
| 52 | wp_set_current_user( $this->factory->user->create( array( 'role' => 'editor' ) ) ); |
| 53 | |
| 54 | // Fake SSL. |
| 55 | $_SERVER['HTTPS'] = 'on'; |
| 56 | |
| 57 | // Set up a default request. |
| 58 | $_POST['nonce'] = wp_create_nonce( 'media-send-to-editor' ); |
| 59 | $_POST['attachment'] = array( |
| 60 | 'id' => $attachment_id, |
| 61 | ); |
| 62 | $_POST['post_id'] = $post_id; |
| 63 | |
| 64 | // Make the request |
| 65 | try { |
| 66 | $this->_handleAjax( 'send-attachment-to-editor' ); |
| 67 | } catch ( WPAjaxDieContinueException $e ) { |
| 68 | unset( $e ); |
| 69 | } |
| 70 | |
| 71 | // Clean up before assertion. |
| 72 | if ( is_null( $_https ) ) { |
| 73 | unset( $_SERVER['HTTPS'] ); |
| 74 | } else { |
| 75 | $_SERVER['HTTPS'] = $_https; |
| 76 | } |
| 77 | |
| 78 | $_POST = $_post_backup; |
| 79 | |
| 80 | $response = json_decode( $this->_last_response ); |
| 81 | |
| 82 | // We don't test against the full attachment URL because of resizing, etc. |
| 83 | $attachment_base = substr( $attachment_url, 0, strrpos( $attachment_url, '/' ) ); |
| 84 | $this->assertContains( 'src="' . $attachment_base, $response->data ); |
| 85 | } |
| 86 | |
| 87 | private function _make_attachment( $upload, $parent_post_id = 0 ) { |
| 88 | $type = ''; |
| 89 | if ( !empty($upload['type']) ) { |
| 90 | $type = $upload['type']; |
| 91 | } else { |
| 92 | $mime = wp_check_filetype( $upload['file'] ); |
| 93 | if ($mime) |
| 94 | $type = $mime['type']; |
| 95 | } |
| 96 | |
| 97 | $attachment = array( |
| 98 | 'post_title' => basename( $upload['file'] ), |
| 99 | 'post_content' => '', |
| 100 | 'post_type' => 'attachment', |
| 101 | 'post_parent' => $parent_post_id, |
| 102 | 'post_mime_type' => $type, |
| 103 | 'guid' => $upload[ 'url' ], |
| 104 | ); |
| 105 | |
| 106 | // Save the data |
| 107 | $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent_post_id ); |
| 108 | wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) ); |
| 109 | return $this->_ids[] = $id; |
| 110 | } |
| 111 | } |