Make WordPress Core


Ignore:
Timestamp:
06/15/2022 10:41:32 AM (2 years ago)
Author:
spacedmonkey
Message:

REST API: Prime caches for post parents in post REST API controller.

Prime caches for all post parents in the post REST API controller using the _prime_post_caches function. Post parent objects are required as part of the check_read_permission method’s permission check in post REST API controller.

Props spacedmonkey, furi3r, peterwilsoncc, mitogh, madpixels.
Fixes #55593.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r53499 r53506  
    17861786
    17871787        $this->assertSame( $formats, $data['schema']['properties']['format']['enum'] );
     1788    }
     1789
     1790    /**
     1791     * @ticket 55593
     1792     */
     1793    public function test_get_items_parent_ids_primed() {
     1794        $parent_id1 = self::$post_ids[0];
     1795        $parent_id2 = self::$post_ids[1];
     1796        $parent_ids = array( $parent_id2, $parent_id1 );
     1797
     1798        $this->factory->attachment->create_object(
     1799            DIR_TESTDATA . '/images/canola.jpg',
     1800            $parent_id1,
     1801            array(
     1802                'post_mime_type' => 'image/jpeg',
     1803                'post_excerpt'   => 'A sample caption 1',
     1804            )
     1805        );
     1806
     1807        $this->factory->attachment->create_object(
     1808            DIR_TESTDATA . '/images/canola.jpg',
     1809            $parent_id2,
     1810            array(
     1811                'post_mime_type' => 'image/jpeg',
     1812                'post_excerpt'   => 'A sample caption 2',
     1813            )
     1814        );
     1815
     1816        // Attachment creation warms parent ids. Needs clean up for test.
     1817        wp_cache_delete_multiple( $parent_ids, 'posts' );
     1818
     1819        $filter = new MockAction();
     1820        add_filter( 'update_post_metadata_cache', array( $filter, 'filter' ), 10, 2 );
     1821
     1822        $request = new WP_REST_Request( 'GET', '/wp/v2/media' );
     1823        rest_get_server()->dispatch( $request );
     1824
     1825        $args = $filter->get_args();
     1826        $last = end( $args );
     1827        $this->assertIsArray( $last, 'The last value is not an array' );
     1828        $this->assertEqualSets( $parent_ids, $last[1] );
    17881829    }
    17891830
Note: See TracChangeset for help on using the changeset viewer.