|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test file for Following Table. |
| 4 | + * |
| 5 | + * @package Activitypub |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Activitypub\Tests\WP_Admin\Table; |
| 9 | + |
| 10 | +use Activitypub\Collection\Actors; |
| 11 | +use Activitypub\Collection\Following as Following_Collection; |
| 12 | +use Activitypub\WP_Admin\Table\Following; |
| 13 | + |
| 14 | +/** |
| 15 | + * Test class for Following Table. |
| 16 | + * |
| 17 | + * @coversDefaultClass \Activitypub\WP_Admin\Table\Following |
| 18 | + */ |
| 19 | +class Test_Following extends \WP_UnitTestCase { |
| 20 | + |
| 21 | + /** |
| 22 | + * Following table instance. |
| 23 | + * |
| 24 | + * @var Following |
| 25 | + */ |
| 26 | + private $following_table; |
| 27 | + |
| 28 | + /** |
| 29 | + * Set up before each test. |
| 30 | + */ |
| 31 | + public function set_up() { |
| 32 | + parent::set_up(); |
| 33 | + |
| 34 | + // Set up global screen mock. |
| 35 | + set_current_screen( 'users_page_activitypub-following-list' ); |
| 36 | + |
| 37 | + // Set current user. |
| 38 | + wp_set_current_user( 1 ); |
| 39 | + |
| 40 | + // Create following table instance. |
| 41 | + $this->following_table = new Following(); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Test column_username with actor having icon object using real prepare_items(). |
| 46 | + * |
| 47 | + * This test uses Following::follow() to create a real following and uses |
| 48 | + * the actual prepare_items() method to test the complete data flow from |
| 49 | + * ActivityPub actor with icon object to the final column output. |
| 50 | + * |
| 51 | + * @covers ::column_username |
| 52 | + * @covers ::prepare_items |
| 53 | + */ |
| 54 | + public function test_column_username_with_icon_object() { |
| 55 | + // Mock remote metadata for the actor with icon object. |
| 56 | + $actor_url = 'https://example.com/users/testuser'; |
| 57 | + $actor_data = array( |
| 58 | + 'name' => 'Test User', |
| 59 | + 'icon' => array( |
| 60 | + 'type' => 'Image', |
| 61 | + 'url' => 'https://secure.gravatar.com/avatar/example?s=120&d=mm&r=g', |
| 62 | + ), |
| 63 | + 'url' => $actor_url, |
| 64 | + 'id' => 'https://example.com/users/testuser', |
| 65 | + 'preferredUsername' => 'testuser', |
| 66 | + 'inbox' => 'https://example.com/users/testuser/inbox', |
| 67 | + ); |
| 68 | + |
| 69 | + // Mock the remote metadata call using the correct filter. |
| 70 | + add_filter( |
| 71 | + 'pre_get_remote_metadata_by_actor', |
| 72 | + function ( $value, $actor ) use ( $actor_url, $actor_data ) { |
| 73 | + if ( $actor === $actor_url ) { |
| 74 | + return $actor_data; |
| 75 | + } |
| 76 | + return $value; |
| 77 | + }, |
| 78 | + 10, |
| 79 | + 2 |
| 80 | + ); |
| 81 | + |
| 82 | + // Add the actor first, then follow them. |
| 83 | + $actor_post_id = Actors::upsert( $actor_data ); |
| 84 | + |
| 85 | + // Follow the actor using the proper method. |
| 86 | + Following_Collection::follow( $actor_post_id, get_current_user_id() ); |
| 87 | + |
| 88 | + // Use the real prepare_items() method. |
| 89 | + $this->following_table->prepare_items(); |
| 90 | + |
| 91 | + // Verify we have items. |
| 92 | + $this->assertNotEmpty( $this->following_table->items ); |
| 93 | + |
| 94 | + // Get the first item and test column_username. |
| 95 | + $item = $this->following_table->items[0]; |
| 96 | + $result = $this->following_table->column_username( $item ); |
| 97 | + |
| 98 | + // Verify the icon URL was extracted from the object by object_to_uri() and properly rendered. |
| 99 | + $this->assertStringContainsString( 'src="https://secure.gravatar.com/avatar/example?s=120&d=mm&r=g"', $result ); |
| 100 | + |
| 101 | + // Verify that the icon was processed correctly: from object to URL. |
| 102 | + $this->assertEquals( 'https://secure.gravatar.com/avatar/example?s=120&d=mm&r=g', $item['icon'] ); |
| 103 | + |
| 104 | + // Clean up. |
| 105 | + \remove_all_filters( 'pre_get_remote_metadata_by_actor' ); |
| 106 | + wp_delete_post( $actor_post_id, true ); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Test prepare_items with actor having icon array of URLs. |
| 111 | + * |
| 112 | + * This test verifies that when an icon field contains an array of URLs, |
| 113 | + * the object_to_uri() function correctly extracts the first URL from the array. |
| 114 | + * |
| 115 | + * @covers ::prepare_items |
| 116 | + */ |
| 117 | + public function test_prepare_items_with_icon_array_of_urls() { |
| 118 | + // Mock remote metadata for the actor with icon as direct array of URLs. |
| 119 | + $actor_url = 'https://example.com/users/arrayuser'; |
| 120 | + $actor_data = array( |
| 121 | + 'name' => 'Array User', |
| 122 | + 'icon' => array( |
| 123 | + 'url' => array( |
| 124 | + 'https://example.com/storage/profile.webp', |
| 125 | + ), |
| 126 | + 'type' => 'Image', |
| 127 | + 'mediaType' => 'image/webp', |
| 128 | + ), |
| 129 | + 'url' => $actor_url, |
| 130 | + 'id' => 'https://example.com/users/arrayuser', |
| 131 | + 'preferredUsername' => 'arrayuser', |
| 132 | + 'inbox' => 'https://example.com/users/arrayuser/inbox', |
| 133 | + ); |
| 134 | + |
| 135 | + // Mock the remote metadata call using the correct filter. |
| 136 | + add_filter( |
| 137 | + 'pre_get_remote_metadata_by_actor', |
| 138 | + function ( $value, $actor ) use ( $actor_url, $actor_data ) { |
| 139 | + if ( $actor === $actor_url ) { |
| 140 | + return $actor_data; |
| 141 | + } |
| 142 | + return $value; |
| 143 | + }, |
| 144 | + 10, |
| 145 | + 2 |
| 146 | + ); |
| 147 | + |
| 148 | + // Add the actor first, then follow them. |
| 149 | + $actor_post_id = Actors::upsert( $actor_data ); |
| 150 | + |
| 151 | + // Follow the actor using the proper method. |
| 152 | + Following_Collection::follow( $actor_post_id, get_current_user_id() ); |
| 153 | + |
| 154 | + // Use the real prepare_items() method. |
| 155 | + $this->following_table->prepare_items(); |
| 156 | + |
| 157 | + // Verify that the icon array was processed correctly: from array to first URL. |
| 158 | + $this->assertEquals( 'https://example.com/storage/profile.webp', $this->following_table->items[0]['icon'] ); |
| 159 | + |
| 160 | + // Clean up. |
| 161 | + \remove_all_filters( 'pre_get_remote_metadata_by_actor' ); |
| 162 | + wp_delete_post( $actor_post_id, true ); |
| 163 | + } |
| 164 | +} |
0 commit comments