Make WordPress Core

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_class filter, which has no test, and the arguments that the list_cats, category_description and category_list_link_attributes filters get.
  • The title attribute 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 separator and with a line break.
  • start_lvl(), end_lvl() and end_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/.

  1. The element. start_el(): the filters and their arguments, the title attribute, the feed link, the post count, the styles that are not a list, and a category with an empty name.
  2. The levels and the end of an element. start_lvl(), end_lvl() and end_el() for the list style and for the other styles, and the classes and the aria-current attribute 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_spacing argument, 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

Adds unit tests for Walker_Category::start_el() in src/wp-includes/class-walker-category.php, which is only tested with empty link attributes and through wp_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:

  • The link goes to the category, with its name as the content, and the name is escaped.
  • The list_cats filter 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.
  • The description is the title of the link only when use_desc_for_title is set, without tags, and it goes through the category_description filter with the category.
  • The category_list_link_attributes filter gets the attributes, the category, the depth, the arguments and the current ID, and the attributes are escaped (the address as a URL).
  • The category_css_class filter, which had no test: its arguments, the classes that it returns, that they are escaped, and that there is no class attribute when there are none.
  • The post count (show_count) with a number format, only when it is asked for.
  • The link to the feed with a text (in parentheses) or with an image, the alternative text, the feed type, and no link unless it is asked for.
  • The output of a style that is not a list, with a separator and with a line break.

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 category
  • vendor/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.

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() and end_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 in start_el()
    • The current category has the current-cat class and aria-current="page" on its link.
    • Its parent has the current-cat-parent class, and its ancestors have the current-cat-ancestor class, 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 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 category
  • vendor/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.

Note: See TracTickets for help on using tickets.