Make WordPress Core

Ticket #36578: 36578.3.diff

File 36578.3.diff, 5.1 KB (added by boonebgorges, 9 years ago)
  • 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..3ba9ba7
    - +  
     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                $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}