Skip to content

Add media order normalization add tests for https://github.com/WordPress/wordpress-develop/pull/10680 - #10682

Closed
adamsilverstein wants to merge 8 commits into
WordPress:trunkfrom
adamsilverstein:fix/64467-media-order-normalization-add-tests
Closed

Add media order normalization add tests for https://github.com/WordPress/wordpress-develop/pull/10680#10682
adamsilverstein wants to merge 8 commits into
WordPress:trunkfrom
adamsilverstein:fix/64467-media-order-normalization-add-tests

Conversation

@adamsilverstein

@adamsilverstein adamsilverstein commented Jan 5, 2026

Copy link
Copy Markdown
Member

Add: tests for #10680


Commit message

Media: Normalize the order property in media models.

The Media Library grid view renders attachments in the reverse of the order the server returned whenever the `order` query var is present but not uppercase, most commonly after sorting in list view and then clicking the grid view toggle, which carries `order=desc` over in the URL. `WP_Query` normalizes and defaults `order` server side, but the media models compare against the literal strings `'ASC'` and `'DESC'`, so a lowercase or invalid value flips the display.

`wp.media.model.Query.get()` already normalized `order`, but `wp.media.model.Attachments.initialize()` did not, so a `Query` and the plain `Attachments` collection mirroring it could disagree about the sort direction. Normalizing at initialization instead gives every attachment collection a consistent `order` regardless of how it was constructed.

Props trivedikavit, sabernhardt, mukesh27, shailu25, soyebsalar01, ozgursar, darshitrajyaguru97.
Fixes #64467.

Trac ticket: https://core.trac.wordpress.org/ticket/64467

TrivediKavit and others added 3 commits January 5, 2026 10:16
Add order normalization in the Attachments model initialize method to ensure
the 'order' property is always 'ASC' or 'DESC' (defaulting to 'DESC' for invalid values).
This provides consistent behavior across all attachment collections.

Part of fixing Media Library Grid view ordering when order query var is not normalized.
See #64467.
Since Query inherits from Attachments, and Attachments now normalizes the order
property in its initialize method, remove the duplicate normalization code from
Query.get() to avoid redundancy and ensure consistent behavior.

Part of fixing Media Library Grid view ordering when order query var is not normalized.
See #64467.
@github-actions

github-actions Bot commented Jan 5, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props trivedikavit, adamsilverstein, wildworks, mukesh27, shailu25.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

github-actions Bot commented Jan 5, 2026

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@mukeshpanchal27 mukeshpanchal27 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. Left some suggestion.

Comment thread src/js/media/models/attachments.js Outdated
this.props.on( 'change:query', this._changeQuery, this );

this.props.set( _.defaults( options.props || {} ) );
options.props = _.defaults( options.props || {} );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_.defaults() only adds properties from the source object if they don't exist in the target. Since options.props || {} creates a fresh object. Direct assignment is cleaner.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: this code was from #10680.

Is your suggestion to leave this line change off?

@adamsilverstein adamsilverstein Aug 3, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @mukeshpanchal27 - switched to the direct assignment.

Comment thread src/js/media/models/attachments.js Outdated
adamsilverstein and others added 2 commits January 12, 2026 13:53
Co-authored-by: Mukesh Panchal <mukeshpanchal27@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds QUnit test coverage for Media Library attachment order normalization and consolidates normalization behavior by moving it into wp.media.model.Attachments (with wp.media.model.Query inheriting it), aligning with the fix described in referenced PR #10680.

Changes:

  • Normalize options.props.order in src/js/media/models/attachments.js and remove duplicate normalization from src/js/media/models/query.js.
  • Add a new QUnit suite to validate normalization behavior across Attachments and Query.
  • Register the new QUnit test file in the QUnit runner HTML.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

File Description
tests/qunit/wp-includes/js/media/test-media-models.js Adds QUnit tests covering order normalization and inheritance behavior.
tests/qunit/index.html Includes the new media model test script in the QUnit test runner.
src/js/media/models/query.js Removes now-redundant order normalization in Query.get() (expects inheritance from Attachments).
src/js/media/models/attachments.js Adds centralized order normalization during Attachments.initialize().
Suppressed comments (7)

tests/qunit/wp-includes/js/media/test-media-models.js:119

  • Like the null case, explicitly passing order: undefined should be treated as invalid and coerced to 'DESC' to avoid truthy/non-string values influencing sort direction unexpectedly.
	QUnit.test( 'Attachments should not process undefined order value', function( assert ) {
		var collection = new wp.media.model.Attachments( [], {
			props: {
				order: undefined
			}

tests/qunit/wp-includes/js/media/test-media-models.js:130

  • Numeric order values should be treated as invalid and defaulted to 'DESC'; otherwise they can be interpreted as ascending due to non-'DESC' truthy values.
	QUnit.test( 'Attachments should not process numeric order value', function( assert ) {
		var collection = new wp.media.model.Attachments( [], {
			props: {
				order: 123
			}

tests/qunit/wp-includes/js/media/test-media-models.js:141

  • Boolean order values should be coerced to 'DESC' (invalid input). Preserving true in particular would be treated as ascending by the comparator, which is not intended.
	QUnit.test( 'Attachments should not process boolean true order value', function( assert ) {
		var collection = new wp.media.model.Attachments( [], {
			props: {
				order: true
			}

tests/qunit/wp-includes/js/media/test-media-models.js:152

  • Boolean order values should be coerced to 'DESC' (invalid input). Keeping false as-is is ambiguous and differs from how invalid values are handled elsewhere (e.g., WP_Query::parse_order()).
	QUnit.test( 'Attachments should not process boolean false order value', function( assert ) {
		var collection = new wp.media.model.Attachments( [], {
			props: {
				order: false
			}

tests/qunit/wp-includes/js/media/test-media-models.js:164

  • Object order values should be treated as invalid and coerced to 'DESC'; otherwise any truthy object value will be interpreted as ascending by the comparator.
	QUnit.test( 'Attachments should not process object order value', function( assert ) {
		var orderObj = { value: 'ASC' };
		var collection = new wp.media.model.Attachments( [], {
			props: {
				order: orderObj
			}

tests/qunit/wp-includes/js/media/test-media-models.js:176

  • Array order values should be treated as invalid and coerced to 'DESC'; preserving an array would be truthy and therefore interpreted as ascending by the comparator.
	QUnit.test( 'Attachments should not process array order value', function( assert ) {
		var orderArray = ['ASC', 'DESC'];
		var collection = new wp.media.model.Attachments( [], {
			props: {
				order: orderArray
			}

tests/qunit/wp-includes/js/media/test-media-models.js:216

  • Same as the previous Query test: avoid props.query = true (it triggers Attachments mirroring), and pass an args object to better reflect how Query instances are constructed in production.
	QUnit.test( 'Query should default invalid order to "DESC"', function( assert ) {
		var query = new wp.media.model.Query( [], {
			props: {
				order: 'random',
				query: true
			}
		});

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/qunit/wp-includes/js/media/test-media-models.js Outdated
Comment thread tests/qunit/wp-includes/js/media/test-media-models.js
Comment thread src/js/media/models/attachments.js Outdated
The `typeof` guard let truthy non-string order values through
unnormalized. `Attachments.comparator()` and Query's order filter both
test for the literal 'ASC'/'DESC', so `order: true` or `order: 123`
sorted ascending instead of falling back to DESC - the same class of bug
this change set out to fix. Coerce with `String()` and normalize
whenever order is set, leaving null and undefined alone so the existing
comparator fallback still applies.

Also drops the `_.defaults()` call, which returns its argument unchanged
when given no sources, and restores `var` since nothing else in
src/js/media uses block scoping.

The Query tests set `props.query`, which fires `_requery()` and a real
server request; they now pass `args` to match what `Query.get()` builds.
@adamsilverstein

adamsilverstein commented Aug 3, 2026

Copy link
Copy Markdown
Member Author

Pushed updates for the feedback here, thanks @mukeshpanchal27.

The one that mattered was the Copilot catch on non-string values. The typeof guard meant order: true or order: 123 skipped normalization completely, and Attachments.comparator() treats anything that isn't the literal 'DESC' as ascending - so those wound up sorting the wrong way, which is the same bug this is trying to fix. Normalization now runs for any value that is actually set, coercing with String() first. null and undefined are still left alone so the existing || 'DESC' fallback in the comparator applies.

Also in this push:

  • const back to var. There is no const or let anywhere else in src/js/media/, so that would have been the first one.
  • Dropped the _.defaults() wrapper per @mukeshpanchal27 - with a single argument it returns the object unchanged.
  • The two Query tests were setting props.query = true, which fires _changeQuery() -> _requery() and kicks off a server request. They pass args now instead, matching what Query.get() actually constructs.
  • Updated the non-string tests, which were asserting the old pass-through behavior as if it were correct.

@shail-mehta

Copy link
Copy Markdown
Member

The order parameter is now correctly respected in the Media Library grid view.✅

Before After
before-patch after-patch

@adamsilverstein

Copy link
Copy Markdown
Member Author

Thanks for testing @shail-mehta! I also verified this works correctly locally.

@github-actions

github-actions Bot commented Aug 5, 2026

Copy link
Copy Markdown

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 63017
GitHub commit: 3a20e59

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions Bot closed this Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants