Make WordPress Core

Changeset 64616


Ignore:
Timestamp:
10/20/2026 05:52:55 PM (5 weeks ago)
Author:
lancewillett
Message:

Tests: Cover media date filtering in attachment queries.

Exercise wp_ajax_query_attachments() with attachments in different months and years. Check the exact returned IDs for a selected month and year, a month without matches, and all dates.

Cover the server-side date query without requiring browser tests or image files.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/7947

Props jayson31, jorbin, lucatume.
See #65252, #52895.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/admin/includes/ajax-actions/queryAttachments.php

    r64011 r64616  
    128128
    129129        /**
     130         * Tests that the media date filter returns only attachments in the selected month and year.
     131         *
     132         * @ticket 52895
     133         * @dataProvider data_query_attachments_date_filter
     134         *
     135         * @param array $query            Date query arguments sent by the media grid.
     136         * @param int[] $expected_indexes Indexes of the attachments expected in the response.
     137         */
     138        public function test_query_attachments_date_filter( array $query, array $expected_indexes ): void {
     139                wp_set_current_user( self::$admin_id );
     140
     141                $attachment_ids = array();
     142                foreach ( array( '2020-07-15 12:00:00', '2020-08-15 12:00:00', '2021-07-15 12:00:00' ) as $date ) {
     143                        $attachment_ids[] = self::factory()->attachment->create_object(
     144                                array(
     145                                        'file'           => 'date-filter.jpg',
     146                                        'post_mime_type' => 'image/jpeg',
     147                                        'post_date'      => $date,
     148                                        'post_date_gmt'  => $date,
     149                                )
     150                        );
     151                }
     152
     153                $_POST['query'] = array_merge(
     154                        $query,
     155                        array( 'post__in' => $attachment_ids )
     156                );
     157
     158                try {
     159                        $this->_handleAjax( 'query-attachments' );
     160                } catch ( WPAjaxDieContinueException $e ) {
     161                }
     162
     163                $response = json_decode( $this->_last_response, true );
     164                $this->assertTrue( $response['success'], 'The attachment query should succeed.' );
     165
     166                $expected_ids = array();
     167                foreach ( $expected_indexes as $index ) {
     168                        $expected_ids[] = $attachment_ids[ $index ];
     169                }
     170
     171                $this->assertSameSets(
     172                        $expected_ids,
     173                        wp_list_pluck( $response['data'], 'id' ),
     174                        'The response should contain exactly the attachments matching the date filter.'
     175                );
     176        }
     177
     178        /**
     179         * Data provider for test_query_attachments_date_filter().
     180         *
     181         * @return array[]
     182         */
     183        public static function data_query_attachments_date_filter(): array {
     184                return array(
     185                        'only the selected month and year'     => array(
     186                                array(
     187                                        'year'     => '2020',
     188                                        'monthnum' => '7',
     189                                ),
     190                                array( 0 ),
     191                        ),
     192                        'no attachments in the selected month' => array(
     193                                array(
     194                                        'year'     => '2020',
     195                                        'monthnum' => '9',
     196                                ),
     197                                array(),
     198                        ),
     199                        'all dates'                            => array(
     200                                array(
     201                                        'year'     => 'false',
     202                                        'monthnum' => 'false',
     203                                ),
     204                                array( 0, 1, 2 ),
     205                        ),
     206                );
     207        }
     208
     209        /**
    130210         * Tests failure with insufficient permissions.
    131211         *
Note: See TracChangeset for help on using the changeset viewer.