WordPress.org

Make WordPress Core

Opened 2 weeks ago

Last modified 11 days ago

#49149 new enhancement

Write tests for WP_Query

Reported by: leogermani Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Query Keywords: needs-unit-tests
Focuses: Cc:
PR Number:

Description (last modified by SergeyBiryukov)

WP_Query is one of the most critical classes in WordPress. It holds a lot of features and complicated rules to generate the SQL queries to fetch posts from the databases.

Working on bug fixes and new features for it is very tricky because one is always afraid that any change to it could silently break things and tear the world apart.

One way to make us feel more secure when working with this class is to have better test coverage for it. Looking at the tests we currently have I found very few tests for this class.

I've created this ticket to organize and receive patches to add tests to this class.

Below I organized the WP_Query parameters we should, at first, create tests for. Once we have tests for them individually, we can think of a way to write tests that combine different parameters.

* Author params
 * author
 * author_name
 * author__in
 * author__not_in

* Category params
 * cat
 * category_name
 * category__and
 * category__in
 * category__not_in

* Tag params
 * tag
 * tag_id
 * tag__and
 * tag__in
 * tag__not_in
 * tag_slug__and
 * tag_slug__in

* Tax_query

* Meta_query

* date_query

* post_mime_type

* fields

* name

* page_id

* pagename

* post_parent

* post_parent__in

* post_parent__not_in

* post__in

* post__not_in

* post_name__in

* has_password

* post_password

* post_type

* post_status

* comment_count

* nopaging

* posts_per_page

* offset

* paged

* ignore_sticky_posts

* order and orderby paramaters

PS - what motivated me to open this ticket (and start working on these tests) was #48556 and #44737

Change History (5)

This ticket was mentioned in Slack in #core-test by leogermani. View the logs.


2 weeks ago

#2 @SergeyBiryukov
2 weeks ago

  • Description modified (diff)

Thanks for the ticket!

One way to make us feel more secure when working with this class is to have better test coverage for it. Looking at the tests we currently have I found very few tests for this class.

At a glance, most of the existing tests appear to be in these files, somewhat grouped by component:

#3 follow-up: @desrosj
13 days ago

  • Keywords needs-unit-tests added

Splitting this up into separate tickets will make this far easier to tackle than attempting to test every part of WP_Query in one shot. Let's use this ticket to verify which areas are lacking tests (after comparing the original list to the one @SergeyBiryukov provided above), and then we can evaluate splitting off tickets for the larger chunks.

@leogermani are you interested in working on starting a patch for this?

#4 in reply to: ↑ 3 @leogermani
13 days ago

Replying to desrosj:

Splitting this up into separate tickets will make this far easier to tackle than attempting to test every part of WP_Query in one shot. Let's use this ticket to verify which areas are lacking tests (after comparing the original list to the one @SergeyBiryukov provided above), and then we can evaluate splitting off tickets for the larger chunks.

@leogermani are you interested in working on starting a patch for this?

Yes.

I will investigate it further and report here what we have tested and what tests are missing, then we can open a ticket for each thing.

Yes, I'm willing to work on patches for this.

#5 @leogermani
11 days ago

Here is the result of my search.

I've looked for each parameter to see if there was a specific test to it.

I've marked as "Not found" what I didn't find, but they might exist and I didn't find it :) .

I'll give it a time maybe someone can point some tests that I have missed or complete the list with something else I forgot. THen I will open tickets for each missing test.

(base folder is tests/phpunit/tests/)

  • Author params
    • author
    • author_name
    • authorin
    • authornot_in

tests in query/results.php

  • Category params
    • cat
    • category_name
    • categoryand
    • categoryin
    • categorynot_in

tests in query/results.php. Only catand category_name. Missing category__and, category__in and category__not_in.

  • Tag params
    • tag
    • tag_id
    • tagand
    • tagin
    • tagnot_in
    • tag_slugand
    • tag_slugin

tests in query/results.php Missing tag_slug__and and tag__and.

  • Tax_query

tests in term/taxQuery.php and query/taxQuery.php

  • Meta_query

tests in query/metaQuery.php and meta/query.php

  • date_query

tests in date/query.php, query/date.php and query/dateQuery.php

  • post_mime_type

not found

  • fields

tests in post/query.php (line 599) And also used in many other tests.

  • name

Not found

  • page_id

Not found

  • pagename

Not found

  • post_parent

Nor found

  • post_parentin

tests in query/results.php

  • post_parentnot_in

Not found

  • postin

Not individually tested (but used in other tests)

  • postnot_in

Not found

  • post_namein

tests in post/query.php

  • has_password

tests in query/results.php

  • post_password

Not found

  • post_type

Not individually tested (but used in other tests)

  • post_status

Tests in query/postStatus.php

  • comment_count

tests in query/commentCount.php

  • nopaging

Not found

  • posts_per_page

tests in query/results.php

  • offset

tests in query/results.php

  • paged

tests in query/results.php

  • ignore_sticky_posts

tests in query/stickies.php, tested via query var and not directly making the query

  • order and orderby paramaters
  • ‘none‘ - post/query.php
  • ‘ID‘ - Not individually tested (but used in other tests).
  • ‘author‘ - Not found
  • ‘title‘ - Not found
  • ‘name‘ - Not found
  • ‘type‘ - Not found
  • ‘date‘ - Not individually tested (but used in other tests). Also tests if it is used by default when invalid value is passed to orderby
  • ‘modified‘ - there is an indirect test in rest-api/rest-posts-controller.php
  • ‘parent‘ - Not found
  • ‘rand‘ - post/query.php
  • ‘relevance‘ - Not found
  • ‘meta_value‘ - meta.php
  • ‘meta_value_num‘ - Not found
  • ‘postin‘ - post/query.php
  • ‘post_namein‘ - post/query.php
  • s

tests in query/search.php

Note: See TracTickets for help on using tickets.