diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 3cc29bb..691376e 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,20 +1,21 @@ - - ./tests/ + + tests/phpunit/tests/ - + ./includes - + diff --git a/tests/bootstrap.php b/tests/phpunit/bootstrap.php similarity index 60% rename from tests/bootstrap.php rename to tests/phpunit/bootstrap.php index 984dcb3..434daf8 100644 --- a/tests/bootstrap.php +++ b/tests/phpunit/bootstrap.php @@ -1,37 +1,44 @@ assertTrue( defined( 'INDIEWEB_VERSION' ) ); + $this->assertNotEmpty( INDIEWEB_VERSION ); + } + + /** + * Test that plugin constants are defined. + */ + public function test_plugin_constants_defined() { + $this->assertTrue( defined( 'INDIEWEB_PLUGIN_DIR' ) ); + $this->assertTrue( defined( 'INDIEWEB_PLUGIN_BASENAME' ) ); + $this->assertTrue( defined( 'INDIEWEB_PLUGIN_FILE' ) ); + $this->assertTrue( defined( 'INDIEWEB_PLUGIN_URL' ) ); + } + + /** + * Test that the singleton returns an instance. + */ + public function test_get_instance() { + $instance = \Indieweb\Indieweb::get_instance(); + + $this->assertInstanceOf( \Indieweb\Indieweb::class, $instance ); + } + + /** + * Test that get_instance returns the same instance. + */ + public function test_singleton_returns_same_instance() { + $instance1 = \Indieweb\Indieweb::get_instance(); + $instance2 = \Indieweb\Indieweb::get_instance(); + + $this->assertSame( $instance1, $instance2 ); + } + + /** + * Test that get_version returns the version constant. + */ + public function test_get_version() { + $instance = \Indieweb\Indieweb::get_instance(); + + $this->assertEquals( INDIEWEB_VERSION, $instance->get_version() ); + } + + /** + * Test that the version function exists and returns version. + */ + public function test_version_function() { + $this->assertEquals( INDIEWEB_VERSION, \Indieweb\version() ); + } + + /** + * Test that hooks are registered after init. + */ + public function test_hooks_registered() { + $instance = \Indieweb\Indieweb::get_instance(); + $instance->init(); + + $this->assertNotFalse( has_action( 'wp_enqueue_scripts', array( $instance, 'enqueue_style' ) ) ); + $this->assertNotFalse( has_action( 'admin_enqueue_scripts', array( $instance, 'enqueue_admin_style' ) ) ); + $this->assertNotFalse( has_action( 'admin_menu', array( $instance, 'add_menu_item' ) ) ); + $this->assertNotFalse( has_action( 'admin_init', array( $instance, 'privacy_declaration' ) ) ); + } + + /** + * Test that the indieweb_loaded action is fired. + */ + public function test_indieweb_loaded_action_fired() { + $this->assertNotFalse( did_action( 'indieweb_loaded' ) ); + } +} diff --git a/tests/phpunit/tests/includes/hcard/class-test-user.php b/tests/phpunit/tests/includes/hcard/class-test-user.php new file mode 100644 index 0000000..67aafb9 --- /dev/null +++ b/tests/phpunit/tests/includes/hcard/class-test-user.php @@ -0,0 +1,190 @@ +user->create( + array( + 'role' => 'administrator', + 'display_name' => 'Test User', + 'user_url' => 'https://example.com', + ) + ); + } + + /** + * Test that silos returns an array. + */ + public function test_silos_returns_array() { + $silos = \Indieweb\Hcard\User::silos(); + + $this->assertIsArray( $silos ); + $this->assertNotEmpty( $silos ); + } + + /** + * Test that silos contains expected keys. + */ + public function test_silos_contains_expected_keys() { + $silos = \Indieweb\Hcard\User::silos(); + + $this->assertArrayHasKey( 'github', $silos ); + $this->assertArrayHasKey( 'twitter', $silos ); + $this->assertArrayHasKey( 'mastodon', $silos ); + $this->assertArrayHasKey( 'bluesky', $silos ); + } + + /** + * Test that silos have required structure. + */ + public function test_silos_have_required_structure() { + $silos = \Indieweb\Hcard\User::silos(); + + foreach ( $silos as $silo => $details ) { + $this->assertArrayHasKey( 'baseurl', $details, "Silo {$silo} should have baseurl" ); + $this->assertArrayHasKey( 'display', $details, "Silo {$silo} should have display" ); + } + } + + /** + * Test address fields returns an array. + */ + public function test_address_fields_returns_array() { + $fields = \Indieweb\Hcard\User::address_fields(); + + $this->assertIsArray( $fields ); + $this->assertNotEmpty( $fields ); + } + + /** + * Test address fields contains expected keys. + */ + public function test_address_fields_contains_expected_keys() { + $fields = \Indieweb\Hcard\User::address_fields(); + + $this->assertArrayHasKey( 'street_address', $fields ); + $this->assertArrayHasKey( 'locality', $fields ); + $this->assertArrayHasKey( 'region', $fields ); + $this->assertArrayHasKey( 'postal_code', $fields ); + $this->assertArrayHasKey( 'country_name', $fields ); + } + + /** + * Test extra fields returns an array. + */ + public function test_extra_fields_returns_array() { + $fields = \Indieweb\Hcard\User::extra_fields(); + + $this->assertIsArray( $fields ); + $this->assertNotEmpty( $fields ); + } + + /** + * Test extra fields contains expected keys. + */ + public function test_extra_fields_contains_expected_keys() { + $fields = \Indieweb\Hcard\User::extra_fields(); + + $this->assertArrayHasKey( 'job_title', $fields ); + $this->assertArrayHasKey( 'organization', $fields ); + $this->assertArrayHasKey( 'honorific_prefix', $fields ); + } + + /** + * Test clean_url with valid URL. + */ + public function test_clean_url_with_valid_url() { + $url = 'https://example.com/path'; + $result = \Indieweb\Hcard\User::clean_url( $url ); + + $this->assertEquals( $url, $result ); + } + + /** + * Test clean_url with invalid URL. + */ + public function test_clean_url_with_invalid_url() { + $result = \Indieweb\Hcard\User::clean_url( 'not-a-url' ); + + $this->assertFalse( $result ); + } + + /** + * Test clean_url upgrades http to https for known domains. + */ + public function test_clean_url_upgrades_http_to_https() { + $result = \Indieweb\Hcard\User::clean_url( 'http://github.com/user' ); + + $this->assertStringStartsWith( 'https://', $result ); + } + + /** + * Test clean_urls filters array of URLs. + */ + public function test_clean_urls_filters_array() { + $urls = array( + 'https://example.com', + 'not-a-url', + 'https://example.org', + '', + ); + $result = \Indieweb\Hcard\User::clean_urls( $urls ); + + $this->assertCount( 2, $result ); + $this->assertContains( 'https://example.com', $result ); + $this->assertContains( 'https://example.org', $result ); + } + + /** + * Test get_hcard_display_defaults returns array. + */ + public function test_get_hcard_display_defaults() { + $defaults = \Indieweb\Hcard\User::get_hcard_display_defaults(); + + $this->assertIsArray( $defaults ); + $this->assertArrayHasKey( 'avatar', $defaults ); + $this->assertArrayHasKey( 'location', $defaults ); + $this->assertArrayHasKey( 'notes', $defaults ); + $this->assertArrayHasKey( 'email', $defaults ); + $this->assertArrayHasKey( 'me', $defaults ); + } + + /** + * Test hcard returns false for invalid user. + */ + public function test_hcard_returns_false_for_invalid_user() { + $result = \Indieweb\Hcard\User::hcard( null ); + + $this->assertFalse( $result ); + } + + /** + * Test hcard returns string for valid user. + */ + public function test_hcard_returns_string_for_valid_user() { + $result = \Indieweb\Hcard\User::hcard( self::$user_id ); + + $this->assertIsString( $result ); + $this->assertStringContainsString( 'h-card', $result ); + } +} diff --git a/tests/phpunit/tests/includes/relme/class-test-domain-icon-map.php b/tests/phpunit/tests/includes/relme/class-test-domain-icon-map.php new file mode 100644 index 0000000..1b94005 --- /dev/null +++ b/tests/phpunit/tests/includes/relme/class-test-domain-icon-map.php @@ -0,0 +1,150 @@ +assertEquals( 'example', $result ); + } + + /** + * Test split_domain with three-part domain. + */ + public function test_split_domain_three_parts() { + $result = \Indieweb\Relme\Domain_Icon_Map::split_domain( 'www.example.com' ); + + $this->assertEquals( 'example', $result ); + } + + /** + * Test split_domain with single part. + */ + public function test_split_domain_single_part() { + $result = \Indieweb\Relme\Domain_Icon_Map::split_domain( 'localhost' ); + + $this->assertEquals( 'localhost', $result ); + } + + /** + * Test url_to_name with known domain. + */ + public function test_url_to_name_known_domain() { + $result = \Indieweb\Relme\Domain_Icon_Map::url_to_name( 'https://github.com/user' ); + + $this->assertEquals( 'github', $result ); + } + + /** + * Test url_to_name with Twitter. + */ + public function test_url_to_name_twitter() { + $result = \Indieweb\Relme\Domain_Icon_Map::url_to_name( 'https://twitter.com/username' ); + + $this->assertEquals( 'twitter', $result ); + } + + /** + * Test url_to_name with Bluesky. + */ + public function test_url_to_name_bluesky() { + $result = \Indieweb\Relme\Domain_Icon_Map::url_to_name( 'https://bsky.app/profile/user.bsky.social' ); + + $this->assertEquals( 'bluesky', $result ); + } + + /** + * Test url_to_name with mailto scheme. + */ + public function test_url_to_name_mailto() { + $result = \Indieweb\Relme\Domain_Icon_Map::url_to_name( 'mailto:test@example.com' ); + + $this->assertEquals( 'mail', $result ); + } + + /** + * Test url_to_name with sms scheme. + */ + public function test_url_to_name_sms() { + $result = \Indieweb\Relme\Domain_Icon_Map::url_to_name( 'sms:+1234567890' ); + + $this->assertEquals( 'phone', $result ); + } + + /** + * Test url_to_name with unknown domain returns website. + */ + public function test_url_to_name_unknown_domain() { + $result = \Indieweb\Relme\Domain_Icon_Map::url_to_name( 'https://unknowndomain12345.com' ); + + $this->assertEquals( 'website', $result ); + } + + /** + * Test url_to_name with non-http scheme returns notice. + */ + public function test_url_to_name_non_http() { + $result = \Indieweb\Relme\Domain_Icon_Map::url_to_name( 'ftp://example.com' ); + + $this->assertEquals( 'notice', $result ); + } + + /** + * Test get_title returns name when not found. + */ + public function test_get_title_returns_name_when_not_found() { + $result = \Indieweb\Relme\Domain_Icon_Map::get_title( 'unknownicon12345' ); + + $this->assertEquals( 'unknownicon12345', $result ); + } + + /** + * Test get_icon_filename returns null for non-existent icon. + */ + public function test_get_icon_filename_returns_null_for_nonexistent() { + $result = \Indieweb\Relme\Domain_Icon_Map::get_icon_filename( 'nonexistenticon12345' ); + + $this->assertNull( $result ); + } + + /** + * Test get_icon returns name for non-existent icon. + */ + public function test_get_icon_returns_name_for_nonexistent() { + $result = \Indieweb\Relme\Domain_Icon_Map::get_icon( 'nonexistenticon12345' ); + + $this->assertEquals( 'nonexistenticon12345', $result ); + } + + /** + * Test url_to_name filter is applied. + */ + public function test_url_to_name_filter() { + add_filter( + 'indieweb_links_url_to_name', + function ( $name, $url ) { + if ( strpos( $url, 'custom.example.com' ) !== false ) { + return 'custom'; + } + return $name; + }, + 10, + 2 + ); + + $result = \Indieweb\Relme\Domain_Icon_Map::url_to_name( 'https://custom.example.com' ); + + $this->assertEquals( 'custom', $result ); + } +} diff --git a/tests/test-microformats.php b/tests/test-microformats.php deleted file mode 100644 index 86c49b3..0000000 --- a/tests/test-microformats.php +++ /dev/null @@ -1,18 +0,0 @@ -assertTrue( true ); - } -}