Opened 5 years ago
Last modified 5 years ago
#49149 assigned 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: |
Description (last modified by )
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 (10)
This ticket was mentioned in Slack in #core-test by leogermani. View the logs.
5 years ago
#3
follow-up:
↓ 4
@
5 years 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
@
5 years 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
@
5 years 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 cat
and 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
This ticket was mentioned in PR #157 on WordPress/wordpress-develop by TimothyBJacobs.
5 years ago
#6
This ticket was mentioned in PR #158 on WordPress/wordpress-develop by TimothyBJacobs.
5 years ago
#7
#8
@
5 years ago
- Owner set to TimothyBlynJacobs
- Resolution set to fixed
- Status changed from new to closed
In 47326:
Thanks for the ticket!
At a glance, most of the existing tests appear to be in these files, somewhat grouped by component: