Skip to content

Conversation

obenland
Copy link
Member

@obenland obenland commented Aug 21, 2025

See https://wordpress.org/support/topic/critical-error-on-this-website-on-followerspage-page-1-page-2-works/

Proposed changes:

This PR adds comprehensive unit tests for the Followers and Following table classes in response to a critical error reported on the WordPress support forums where avatar data processing was failing with a TypeError.

Changes made:

  • Added unit tests for the Followers table class (tests/includes/wp-admin/table/class-test-followers.php)
  • Added unit tests for the Following table class (tests/includes/wp-admin/table/class-test-following.php)
  • Fixed icon processing in the Following table to use object_to_uri() consistently like the Followers table
  • Tests specifically cover the column_username method with ActivityPub icon objects
  • Tests use real prepare_items() methods and proper ActivityPub actor creation to verify the complete data flow

Bug context:

The tests were created in response to this WordPress support topic where users experienced a PHP Fatal TypeError with ltrim() when processing avatar data that was an array instead of a string. The tests ensure proper handling of ActivityPub actor icon objects throughout the data processing pipeline.

Other information:

  • Have you written new tests for your changes, if applicable?

Testing instructions:

Running the new tests:

  1. Ensure you have the WordPress test environment set up with npm run env-start
  2. Run the specific new tests:
    npm run env-test -- --filter=test_column_username_with_icon_object
  3. Or run all table tests:
    npm run env-test tests/includes/wp-admin/table/

Verifying the fix:

  1. The tests create real ActivityPub actors with icon objects (containing type and url properties)
  2. They use the actual prepare_items() methods to process this data
  3. The tests verify that icon objects are properly converted to URL strings using object_to_uri()
  4. Tests confirm the final HTML output contains the correct escaped avatar URLs

Manual testing (optional):

  1. Set up followers with ActivityPub actors that have complex icon objects
  2. Navigate to WP Admin → Users → Followers page
  3. Verify that avatar images display correctly without PHP errors
  4. Check that the Following page also processes avatars consistently

Changelog entry

  • Automatically create a changelog entry from the details below.
Changelog Entry Details

Significance

  • Patch
  • Minor
  • Major

Type

  • Added - for new features
  • Changed - for changes in existing functionality
  • Deprecated - for soon-to-be removed features
  • Removed - for now removed features
  • Fixed - for any bug fixes
  • Security - in case of vulnerabilities

Message

Add comprehensive unit tests for Followers and Following table classes with proper ActivityPub icon object handling.

- Add comprehensive test class for WP_Admin\Table\Followers
- Test column_username method with ActivityPub actor icon objects
- Simulate realistic data processing from icon object to URL extraction
- Include proper WordPress screen mocking and setup
- Verify HTML output formatting and escaping
- Use Followers::add_follower() to create actual follower relationships
- Mock remote actor metadata using pre_get_remote_metadata_by_actor filter
- Test complete data flow: add_follower → prepare_items → column_username
- Verify ActivityPub icon object processing through object_to_uri()
- Test confirms icon object {type: 'Image', url: '...'} → URL string conversion
- All tests pass with real integration of components
- Create test class for Following table column_username method
- Use Actors::upsert() and Following::follow() for real data setup
- Mock remote actor metadata using pre_get_remote_metadata_by_actor filter
- Test complete data flow: upsert → follow → prepare_items → column_username
- Verify ActivityPub icon object processing through object_to_uri()
- Test confirms icon object {type: 'Image', url: '...'} → URL string conversion
- Mirrors Followers table test structure for consistency
- Fix Following table to use object_to_uri() for consistent icon processing like Followers table
- Clean up Following test formatting and import organization
- Remove unnecessary test assertions for cleaner test structure
@Copilot Copilot AI review requested due to automatic review settings August 21, 2025 20:16
@obenland obenland self-assigned this Aug 21, 2025
@obenland obenland requested a review from pfefferle August 21, 2025 20:16
Copy link

@Copilot Copilot AI left a comment

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 comprehensive unit tests for the Followers and Following table classes to address a critical TypeError reported on WordPress support forums where avatar data processing was failing when handling ActivityPub icon objects.

  • Adds unit tests for both Followers and Following table classes with proper ActivityPub icon object handling
  • Fixes inconsistent icon processing in the Following table to use object_to_uri() like the Followers table
  • Tests verify complete data flow from ActivityPub actor creation through table rendering to ensure proper icon object conversion

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

File Description
tests/includes/wp-admin/table/class-test-following.php New comprehensive unit tests for Following table class with ActivityPub icon object handling
tests/includes/wp-admin/table/class-test-followers.php New comprehensive unit tests for Followers table class with ActivityPub icon object handling
includes/wp-admin/table/class-following.php Fixed icon processing to use object_to_uri() consistently with Followers table
includes/wp-admin/table/class-followers.php Context showing existing object_to_uri() usage for icon processing

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@obenland obenland changed the title Add comprehensive unit tests for Followers and Following table classes Follow[ers|ing]: Avoid errors when getting avatar url Aug 21, 2025
@Jiwoon-Kim
Copy link

It would be useful to have the option to configure a default image that is displayed when a remote profile has no avatar.

@@ -223,7 +223,7 @@ public function prepare_items() {

$this->items[] = array(
'id' => $follower->ID,
'icon' => $actor->get_icon()['url'] ?? '',
'icon' => object_to_uri( $actor->get_icon() ?? '' ),
Copy link
Member

Choose a reason for hiding this comment

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

oh, nice solution! never thought about using it here!

pfefferle
pfefferle previously approved these changes Aug 24, 2025
@obenland
Copy link
Member Author

For posterity, the icon object for the actor mentioned in the support forum looked like this:

  "icon": {
    "url": [
      "https://queef.in/storage/profile.webp"
    ],
    "type": "Image",
    "mediaType": "image/webp"
  },

Add unit test to verify that actors with icon objects containing
arrays of URLs are handled correctly by the Following table.
Currently this test will fail as the object_to_uri function doesn't
fully process nested arrays in Image objects.
Update object_to_uri function to properly handle Image objects where
the url property is an array of URLs. This fixes the Following table
display when actors have icon objects with multiple URL fallbacks.

The fix recursively processes Image url arrays to extract the first
URL, matching the ActivityStreams vocabulary specification.
@obenland obenland requested a review from pfefferle August 25, 2025 14:13
@@ -730,7 +730,8 @@ function object_to_uri( $data ) {
// Return part of Object that makes most sense.
switch ( $type ) {
case 'Image':
$data = $data['url'];
// See https://www.w3.org/TR/activitystreams-vocabulary/#dfn-image.
$data = object_to_uri( $data['url'] );
Copy link
Member

Choose a reason for hiding this comment

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

just wanted to suggest that :)

nice 👍

@obenland obenland merged commit 4313bc9 into trunk Aug 25, 2025
13 checks passed
@obenland obenland deleted the add-followers-table-tests branch August 25, 2025 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants