Make WordPress Core


Ignore:
Timestamp:
11/03/2018 02:13:16 PM (8 years ago)
Author:
danielbachhuber
Message:

REST API: Prevent duplicate firing of rest(_after)?_insert_attachment actions.

Props danielbachhuber, peterwilsoncc.
Fixes #45269.
See #42864.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r43732 r43862  
    1717        protected static $contributor_id;
    1818        protected static $uploader_id;
     19        protected static $rest_after_insert_attachment_count;
     20        protected static $rest_insert_attachment_count;
    1921
    2022        public static function wpSetUpBeforeClass( $factory ) {
     
    13071309                }
    13081310
     1311                remove_action( 'rest_insert_attachment', array( $this, 'filter_rest_insert_attachment' ) );
     1312                remove_action( 'rest_after_insert_attachment', array( $this, 'filter_rest_after_insert_attachment' ) );
     1313
    13091314                $this->remove_added_uploads();
    13101315        }
     
    14381443        }
    14391444
     1445        /**
     1446         * Ensure the `rest_after_insert_attachment` and `rest_insert_attachment` hooks only fire
     1447         * once when attachments are created.
     1448         *
     1449         * @ticket 45269
     1450         */
     1451        public function test_rest_insert_attachment_hooks_fire_once_on_create() {
     1452                self::$rest_insert_attachment_count = 0;
     1453                self::$rest_after_insert_attachment_count = 0;
     1454                add_action( 'rest_insert_attachment', array( $this, 'filter_rest_insert_attachment' ) );
     1455                add_action( 'rest_after_insert_attachment', array( $this, 'filter_rest_after_insert_attachment' ) );
     1456
     1457                wp_set_current_user( self::$editor_id );
     1458                $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
     1459                $request->set_header( 'Content-Type', 'image/jpeg' );
     1460                $request->set_header( 'Content-Disposition', 'attachment; filename=canola.jpg' );
     1461                $request->set_param( 'title', 'My title is very cool' );
     1462                $request->set_param( 'caption', 'This is a better caption.' );
     1463                $request->set_param( 'description', 'Without a description, my attachment is descriptionless.' );
     1464                $request->set_param( 'alt_text', 'Alt text is stored outside post schema.' );
     1465
     1466                $request->set_body( file_get_contents( $this->test_file ) );
     1467                $response = $this->server->dispatch( $request );
     1468                $data = $response->get_data();
     1469                $this->assertEquals( 201, $response->get_status() );
     1470
     1471                $this->assertSame( 1, self::$rest_insert_attachment_count );
     1472                $this->assertSame( 1, self::$rest_after_insert_attachment_count );
     1473        }
     1474
     1475        /**
     1476         * Ensure the `rest_after_insert_attachment` and `rest_insert_attachment` hooks only fire
     1477         * once when attachments are updated.
     1478         *
     1479         * @ticket 45269
     1480         */
     1481        public function test_rest_insert_attachment_hooks_fire_once_on_update() {
     1482                self::$rest_insert_attachment_count = 0;
     1483                self::$rest_after_insert_attachment_count = 0;
     1484                add_action( 'rest_insert_attachment', array( $this, 'filter_rest_insert_attachment' ) );
     1485                add_action( 'rest_after_insert_attachment', array( $this, 'filter_rest_after_insert_attachment' ) );
     1486
     1487                wp_set_current_user( self::$editor_id );
     1488                $attachment_id = $this->factory->attachment->create_object( $this->test_file, 0, array(
     1489                        'post_mime_type' => 'image/jpeg',
     1490                        'post_excerpt'   => 'A sample caption',
     1491                        'post_author'    => self::$editor_id,
     1492                ) );
     1493                $request = new WP_REST_Request( 'POST', '/wp/v2/media/' . $attachment_id );
     1494                $request->set_param( 'title', 'My title is very cool' );
     1495                $response = $this->server->dispatch( $request );
     1496
     1497                $this->assertSame( 1, self::$rest_insert_attachment_count );
     1498                $this->assertSame( 1, self::$rest_after_insert_attachment_count );
     1499        }
     1500
     1501        public function filter_rest_insert_attachment( $attachment ) {
     1502                self::$rest_insert_attachment_count++;
     1503        }
     1504
     1505        public function filter_rest_after_insert_attachment( $attachment ) {
     1506                self::$rest_after_insert_attachment_count++;
     1507        }
    14401508}
Note: See TracChangeset for help on using the changeset viewer.