Make WordPress Core

Ticket #56203: Reduce_duplication_in_attachments_AJAX_tests.patch

File Reduce_duplication_in_attachments_AJAX_tests.patch, 2.6 KB (added by martin.krcho, 15 months ago)

Patch reducing the duplication in the AJAX attachments handler tests

  • tests/phpunit/tests/ajax/Attachments.php

    IDEA additional info:
    Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
    <+>UTF-8
    diff --git a/tests/phpunit/tests/ajax/Attachments.php b/tests/phpunit/tests/ajax/Attachments.php
    a b  
    1010 * @group ajax
    1111 */
    1212class Tests_Ajax_Attachments extends WP_Ajax_UnitTestCase {
     13
     14        protected function _uploadFileAndMakeAttachment( $filename ) {
     15                $contents = file_get_contents( $filename );
     16                $upload   = wp_upload_bits( wp_basename( $filename ), null, $contents );
     17
     18                return $this->_make_attachment( $upload );
     19        }
     20
    1321        /**
    1422         * @ticket 36578
    1523         *
     
    1826         */
    1927        public function test_wp_ajax_send_attachment_to_editor_should_return_an_image() {
    2028                // Become an administrator.
    21                 $post    = $_POST;
    22                 $user_id = self::factory()->user->create(
    23                         array(
    24                                 'role'       => 'administrator',
    25                                 'user_login' => 'user_36578_administrator',
    26                                 'user_email' => 'user_36578_administrator@example.com',
    27                         )
    28                 );
    29                 wp_set_current_user( $user_id );
    30                 $_POST = array_merge( $_POST, $post );
     29                $this->_setRole( 'administrator' );
    3130
    32                 $filename = DIR_TESTDATA . '/images/canola.jpg';
    33                 $contents = file_get_contents( $filename );
    34 
    35                 $upload     = wp_upload_bits( wp_basename( $filename ), null, $contents );
    36                 $attachment = $this->_make_attachment( $upload );
     31                $attachment = $this->_uploadFileAndMakeAttachment( DIR_TESTDATA . '/images/canola.jpg' );
    3732
    3833                // Set up a default request.
    3934                $_POST['nonce']      = wp_create_nonce( 'media-send-to-editor' );
     
    7469                $this->skipWithMultisite();
    7570
    7671                // Become an administrator.
    77                 $post    = $_POST;
    78                 $user_id = self::factory()->user->create(
    79                         array(
    80                                 'role'       => 'administrator',
    81                                 'user_login' => 'user_36578_administrator',
    82                                 'user_email' => 'user_36578_administrator@example.com',
    83                         )
    84                 );
    85                 wp_set_current_user( $user_id );
    86                 $_POST = array_merge( $_POST, $post );
     72                $this->_setRole( 'administrator' );
    8773
    88                 $filename = DIR_TESTDATA . '/formatting/entities.txt';
    89                 $contents = file_get_contents( $filename );
    90 
    91                 $upload     = wp_upload_bits( wp_basename( $filename ), null, $contents );
    92                 $attachment = $this->_make_attachment( $upload );
     74                $attachment = $this->_uploadFileAndMakeAttachment( DIR_TESTDATA . '/formatting/entities.txt' );
    9375
    9476                // Set up a default request.
    9577                $_POST['nonce']      = wp_create_nonce( 'media-send-to-editor' );