|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test Hcard User class. |
| 4 | + * |
| 5 | + * @package Indieweb |
| 6 | + */ |
| 7 | + |
| 8 | +/** |
| 9 | + * Test Hcard User class. |
| 10 | + */ |
| 11 | +class Test_Hcard_User extends WP_UnitTestCase { |
| 12 | + |
| 13 | + /** |
| 14 | + * Test user ID. |
| 15 | + * |
| 16 | + * @var int |
| 17 | + */ |
| 18 | + protected static $user_id; |
| 19 | + |
| 20 | + /** |
| 21 | + * Set up before class. |
| 22 | + * |
| 23 | + * @param WP_UnitTest_Factory $factory Factory instance. |
| 24 | + */ |
| 25 | + public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { |
| 26 | + self::$user_id = $factory->user->create( |
| 27 | + array( |
| 28 | + 'role' => 'administrator', |
| 29 | + 'display_name' => 'Test User', |
| 30 | + 'user_url' => 'https://example.com', |
| 31 | + ) |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Test that silos returns an array. |
| 37 | + */ |
| 38 | + public function test_silos_returns_array() { |
| 39 | + $silos = \Indieweb\Hcard\User::silos(); |
| 40 | + |
| 41 | + $this->assertIsArray( $silos ); |
| 42 | + $this->assertNotEmpty( $silos ); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Test that silos contains expected keys. |
| 47 | + */ |
| 48 | + public function test_silos_contains_expected_keys() { |
| 49 | + $silos = \Indieweb\Hcard\User::silos(); |
| 50 | + |
| 51 | + $this->assertArrayHasKey( 'github', $silos ); |
| 52 | + $this->assertArrayHasKey( 'twitter', $silos ); |
| 53 | + $this->assertArrayHasKey( 'mastodon', $silos ); |
| 54 | + $this->assertArrayHasKey( 'bluesky', $silos ); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Test that silos have required structure. |
| 59 | + */ |
| 60 | + public function test_silos_have_required_structure() { |
| 61 | + $silos = \Indieweb\Hcard\User::silos(); |
| 62 | + |
| 63 | + foreach ( $silos as $silo => $details ) { |
| 64 | + $this->assertArrayHasKey( 'baseurl', $details, "Silo {$silo} should have baseurl" ); |
| 65 | + $this->assertArrayHasKey( 'display', $details, "Silo {$silo} should have display" ); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Test address fields returns an array. |
| 71 | + */ |
| 72 | + public function test_address_fields_returns_array() { |
| 73 | + $fields = \Indieweb\Hcard\User::address_fields(); |
| 74 | + |
| 75 | + $this->assertIsArray( $fields ); |
| 76 | + $this->assertNotEmpty( $fields ); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Test address fields contains expected keys. |
| 81 | + */ |
| 82 | + public function test_address_fields_contains_expected_keys() { |
| 83 | + $fields = \Indieweb\Hcard\User::address_fields(); |
| 84 | + |
| 85 | + $this->assertArrayHasKey( 'street_address', $fields ); |
| 86 | + $this->assertArrayHasKey( 'locality', $fields ); |
| 87 | + $this->assertArrayHasKey( 'region', $fields ); |
| 88 | + $this->assertArrayHasKey( 'postal_code', $fields ); |
| 89 | + $this->assertArrayHasKey( 'country_name', $fields ); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Test extra fields returns an array. |
| 94 | + */ |
| 95 | + public function test_extra_fields_returns_array() { |
| 96 | + $fields = \Indieweb\Hcard\User::extra_fields(); |
| 97 | + |
| 98 | + $this->assertIsArray( $fields ); |
| 99 | + $this->assertNotEmpty( $fields ); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Test extra fields contains expected keys. |
| 104 | + */ |
| 105 | + public function test_extra_fields_contains_expected_keys() { |
| 106 | + $fields = \Indieweb\Hcard\User::extra_fields(); |
| 107 | + |
| 108 | + $this->assertArrayHasKey( 'job_title', $fields ); |
| 109 | + $this->assertArrayHasKey( 'organization', $fields ); |
| 110 | + $this->assertArrayHasKey( 'honorific_prefix', $fields ); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Test clean_url with valid URL. |
| 115 | + */ |
| 116 | + public function test_clean_url_with_valid_url() { |
| 117 | + $url = 'https://example.com/path'; |
| 118 | + $result = \Indieweb\Hcard\User::clean_url( $url ); |
| 119 | + |
| 120 | + $this->assertEquals( $url, $result ); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Test clean_url with invalid URL. |
| 125 | + */ |
| 126 | + public function test_clean_url_with_invalid_url() { |
| 127 | + $result = \Indieweb\Hcard\User::clean_url( 'not-a-url' ); |
| 128 | + |
| 129 | + $this->assertFalse( $result ); |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Test clean_url upgrades http to https for known domains. |
| 134 | + */ |
| 135 | + public function test_clean_url_upgrades_http_to_https() { |
| 136 | + $result = \Indieweb\Hcard\User::clean_url( 'http://github.com/user' ); |
| 137 | + |
| 138 | + $this->assertStringStartsWith( 'https://', $result ); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Test clean_urls filters array of URLs. |
| 143 | + */ |
| 144 | + public function test_clean_urls_filters_array() { |
| 145 | + $urls = array( |
| 146 | + 'https://example.com', |
| 147 | + 'not-a-url', |
| 148 | + 'https://example.org', |
| 149 | + '', |
| 150 | + ); |
| 151 | + $result = \Indieweb\Hcard\User::clean_urls( $urls ); |
| 152 | + |
| 153 | + $this->assertCount( 2, $result ); |
| 154 | + $this->assertContains( 'https://example.com', $result ); |
| 155 | + $this->assertContains( 'https://example.org', $result ); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Test get_hcard_display_defaults returns array. |
| 160 | + */ |
| 161 | + public function test_get_hcard_display_defaults() { |
| 162 | + $defaults = \Indieweb\Hcard\User::get_hcard_display_defaults(); |
| 163 | + |
| 164 | + $this->assertIsArray( $defaults ); |
| 165 | + $this->assertArrayHasKey( 'avatar', $defaults ); |
| 166 | + $this->assertArrayHasKey( 'location', $defaults ); |
| 167 | + $this->assertArrayHasKey( 'notes', $defaults ); |
| 168 | + $this->assertArrayHasKey( 'email', $defaults ); |
| 169 | + $this->assertArrayHasKey( 'me', $defaults ); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Test hcard returns false for invalid user. |
| 174 | + */ |
| 175 | + public function test_hcard_returns_false_for_invalid_user() { |
| 176 | + $result = \Indieweb\Hcard\User::hcard( null ); |
| 177 | + |
| 178 | + $this->assertFalse( $result ); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Test hcard returns string for valid user. |
| 183 | + */ |
| 184 | + public function test_hcard_returns_string_for_valid_user() { |
| 185 | + $result = \Indieweb\Hcard\User::hcard( self::$user_id ); |
| 186 | + |
| 187 | + $this->assertIsString( $result ); |
| 188 | + $this->assertStringContainsString( 'h-card', $result ); |
| 189 | + } |
| 190 | +} |
0 commit comments