Make WordPress Core

Ticket #36578: 36578.2.diff

File 36578.2.diff, 4.6 KB (added by swissspidy, 9 years ago)

Patch including ajax tests to ensure the bug is fixed

  • src/wp-admin/includes/ajax-actions.php

    diff --git src/wp-admin/includes/ajax-actions.php src/wp-admin/includes/ajax-actions.php
    index f3d8e83..e3b345c 100644
    function wp_ajax_send_attachment_to_editor() { 
    26022602                $rel = $rel ? ' rel="attachment wp-att-' . $id . '"' : ''; // Hard-coded string, $id is already sanitized
    26032603
    26042604                if ( ! empty( $url ) ) {
    2605                         $html = '<a href="' . esc_url( $url ) . '"' . $rel . '">' . $html . '</a>';
     2605                        $html = '<a href="' . esc_url( $url ) . '"' . $rel . '>' . $html . '</a>';
    26062606                }
    26072607        }
    26082608
  • tests/phpunit/includes/testcase-ajax.php

    diff --git tests/phpunit/includes/testcase-ajax.php tests/phpunit/includes/testcase-ajax.php
    index ea809d5..c4466aa 100644
    abstract class WP_Ajax_UnitTestCase extends WP_UnitTestCase { 
    5050                'menu-locations-save', 'menu-quick-search', 'meta-box-order', 'get-permalink',
    5151                'sample-permalink', 'inline-save', 'inline-save-tax', 'find_posts', 'widgets-order',
    5252                'save-widget', 'set-post-thumbnail', 'date_format', 'time_format', 'wp-fullscreen-save-post',
    53                 'wp-remove-post-lock', 'dismiss-wp-pointer', 'heartbeat', 'nopriv_heartbeat', 'get-revision-diffs',
     53                'wp-remove-post-lock', 'dismiss-wp-pointer', 'send-attachment-to-editor', 'heartbeat', 'nopriv_heartbeat', 'get-revision-diffs',
    5454                'save-user-color-scheme', 'update-widget', 'query-themes', 'parse-embed', 'set-attachment-thumbnail',
    5555                'parse-media-shortcode', 'destroy-sessions', 'install-plugin', 'update-plugin', 'press-this-save-post',
    5656                'press-this-add-category', 'crop-image', 'generate-password',
  • new file tests/phpunit/tests/ajax/Attachments.php

    diff --git tests/phpunit/tests/ajax/Attachments.php tests/phpunit/tests/ajax/Attachments.php
    new file mode 100644
    index 0000000..8ac7439
    - +  
     1<?php
     2/**
     3 * Admin ajax functions to be tested
     4 */
     5require_once( ABSPATH . 'wp-admin/includes/ajax-actions.php' );
     6
     7/**
     8 * Testing ajax attachment handling.
     9 *
     10 * @group ajax
     11 */
     12class 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}