Make WordPress Core


Ignore:
Timestamp:
10/17/2022 11:17:38 AM (3 years ago)
Author:
audrasjb
Message:

Media: Refactor search by filename within the admin.

Props vortfu, xknown, peterwilsoncc, paulkevan.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/query/search.php

    r54090 r54524  
    455455
    456456        add_post_meta( $attachment, '_wp_attached_file', 'some-image1.png', true );
    457         add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
     457        add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
    458458
    459459        // Pass post_type a string value.
     
    485485
    486486        add_post_meta( $attachment, '_wp_attached_file', 'some-image2.png', true );
    487         add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
     487        add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
    488488
    489489        // Pass post_type an array value.
     
    544544        add_post_meta( $attachment, '_wp_attached_file', 'some-image4.png', true );
    545545        add_post_meta( $attachment, '_test_meta_key', 'value', true );
    546         add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
     546        add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
    547547
    548548        // Pass post_type a string value.
     
    584584
    585585        add_post_meta( $attachment, '_wp_attached_file', 'some-image5.png', true );
    586         add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
     586        add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
    587587
    588588        // Pass post_type a string value.
     
    609609     * @ticket 22744
    610610     */
    611     public function test_filter_query_attachment_filenames_unhooks_itself() {
    612         add_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
    613 
    614         apply_filters(
    615             'posts_clauses',
    616             array(
    617                 'where'    => '',
    618                 'groupby'  => '',
    619                 'join'     => '',
    620                 'orderby'  => '',
    621                 'distinct' => '',
    622                 'fields'   => '',
    623                 'limit'    => '',
    624             )
    625         );
    626 
    627         $result = has_filter( 'posts_clauses', '_filter_query_attachment_filenames' );
    628 
    629         $this->assertFalse( $result );
     611    public function test_wp_query_removes_filter_wp_allow_query_attachment_by_filename() {
     612        $attachment = self::factory()->post->create(
     613            array(
     614                'post_type'    => 'attachment',
     615                'post_status'  => 'publish',
     616                'post_title'   => 'bar foo',
     617                'post_content' => 'foo bar',
     618                'post_excerpt' => 'This post has foo',
     619            )
     620        );
     621
     622        add_post_meta( $attachment, '_wp_attached_file', 'some-image1.png', true );
     623        add_filter( 'wp_allow_query_attachment_by_filename', '__return_true' );
     624
     625        $q = new WP_Query(
     626            array(
     627                's'           => 'image1',
     628                'fields'      => 'ids',
     629                'post_type'   => 'attachment',
     630                'post_status' => 'inherit',
     631            )
     632        );
     633
     634        $this->assertSame( array( $attachment ), $q->posts );
     635
     636        /*
     637         * WP_Query should have removed the wp_allow_query_attachment_by_filename filter
     638         * and thus not match the attachment created above.
     639         */
     640        $q->get_posts();
     641        $this->assertEmpty( $q->posts );
    630642    }
    631643
Note: See TracChangeset for help on using the changeset viewer.