Opened 6 days ago
Last modified 6 days ago
#66497 new enhancement
Tests: Add unit coverage for Walker_Category
| Reported by: | hmbashar | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Taxonomy | Version: | |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: | tests |
Description
Walker_Category in src/wp-includes/class-walker-category.php builds the output of wp_list_categories(). The existing tests cover it through wp_list_categories() (tests/phpunit/tests/category/wpListCategories.php), and test start_el() with empty link attributes (tests/phpunit/tests/category/walkerCategory.php), but a search of tests/phpunit/tests finds no direct tests for these behaviors:
- The
category_css_classfilter, which has no test, and the arguments that thelist_cats,category_descriptionandcategory_list_link_attributesfilters get. - The
titleattribute that is built from the description of the category (use_desc_for_title), with the tags removed. - The link to the feed of the category: the text, the image, the alternative text and the feed type.
- The post count (
show_count). - The output of the styles that are not a list: with a
separatorand with a line break. start_lvl(),end_lvl()andend_el()for the list style and for the other styles.
They are exercised indirectly, so a change in the markup of the list is hard to spot and to attribute. Direct tests describe the current behavior of each method.
Proposed scope
This ticket is meant to be handled with two small patches, so each one is easy to review. The tests go in tests/phpunit/tests/category/.
- The element.
start_el(): the filters and their arguments, thetitleattribute, the feed link, the post count, the styles that are not a list, and a category with an empty name. - The levels and the end of an element.
start_lvl(),end_lvl()andend_el()for the list style and for the other styles, and the classes and thearia-currentattribute of the current category, the parent and the ancestors, called directly on the walker.
The tests check the markup that matters (the elements, their classes and attributes), and not the whitespace between them.
Out of scope
This ticket only adds tests and does not change behavior. The following are left out on purpose because other tickets are working on them:
- New filters for the class names of the level in
start_lvl(), see #40761. - The
item_spacingargument, see #48498. Walker_CategoryDropdown, see #41882.
Related: #65819.
Change History (2)
This ticket was mentioned in PR #15538 on WordPress/wordpress-develop by @hmbashar.
6 days ago
#1
- Keywords has-patch has-unit-tests added; needs-patch removed
This ticket was mentioned in PR #15539 on WordPress/wordpress-develop by @hmbashar.
6 days ago
#2
Adds unit tests for the levels, the end of an element and the current category of Walker_Category in src/wp-includes/class-walker-category.php. This is the second of the two patches described in the ticket, and is independent from the first one. No behavior is changed.
What is covered, in tests/phpunit/tests/category/:
walkerCategoryLevels.php:start_lvl(),end_lvl()andend_el()- The list of the children is opened with its class and closed, and the item is closed, for the list style.
- Each level is indented with one tab for each depth.
- Nothing is added for the styles that are not a list.
- The output that is already there is kept, and the methods build a nested list when they are used by
wp_list_categories().
walkerCategoryCurrentCategory.php: the current category instart_el()- The current category has the
current-catclass andaria-current="page"on its link. - Its parent has the
current-cat-parentclass, and its ancestors have thecurrent-cat-ancestorclass, and neither is the current page. - Other categories, and a child of the current one, are not marked, every category of a list of current categories is marked, and nothing is marked without a current category.
- The current category has the
The tests check the markup (the elements, their classes and attributes) and not the whitespace between them, apart from the tab for each level of depth.
Testing:
vendor/bin/phpunit --filter 'Tests_Category_(WalkerCategoryLevels|WalkerCategoryCurrentCategory|Walker_Category)'vendor/bin/phpunit -c tests/phpunit/multisite.xml --filter 'Tests_Category_(WalkerCategoryLevels|WalkerCategoryCurrentCategory|Walker_Category)'vendor/bin/phpunit --group categoryvendor/bin/phpcs tests/phpunit/tests/category/
I also temporarily broke the related lines in src/wp-includes/class-walker-category.php (the check of the style, the class, the tag and the indentation of each of the three methods, and each of the classes and the attribute of the current category, its parent and its ancestors) and confirmed that a new test fails for each one.
Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5.5
Used for: Written with AI and manually reviewed by me.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Adds unit tests for
Walker_Category::start_el()insrc/wp-includes/class-walker-category.php, which is only tested with empty link attributes and throughwp_list_categories(). This is the first of the two patches described in the ticket. No behavior is changed.What is covered, in
tests/phpunit/tests/category/walkerCategoryStartEl.php:list_catsfilter gets the escaped name and the category, and its result is the content of the link. Nothing is output when the name is empty, for the list style and for the other style.titleof the link only whenuse_desc_for_titleis set, without tags, and it goes through thecategory_descriptionfilter with the category.category_list_link_attributesfilter gets the attributes, the category, the depth, the arguments and the current ID, and the attributes are escaped (the address as a URL).category_css_classfilter, which had no test: its arguments, the classes that it returns, that they are escaped, and that there is noclassattribute when there are none.show_count) with a number format, only when it is asked for.The tests check the markup (the elements, their classes and attributes) and not the whitespace between them.
Testing:
vendor/bin/phpunit --filter 'Tests_Category_(WalkerCategoryStartEl|Walker_Category)'vendor/bin/phpunit -c tests/phpunit/multisite.xml --filter 'Tests_Category_(WalkerCategoryStartEl|Walker_Category)'vendor/bin/phpunit --group categoryvendor/bin/phpcs tests/phpunit/tests/category/I also temporarily broke the related lines in
src/wp-includes/class-walker-category.php(the escaping of the name, the address and the attributes, each filter, the check for an empty name, the title, the post count, each part of the feed link, the style, the separator and the line break) and confirmed that a new test fails for each one.Use of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Sonnet 5.5
Used for: Written with AI and manually reviewed by me.