Skip to content

Commit 2a07454

Browse files
committed
Add unit tests
- Add tests for Indieweb main class - Add tests for Hcard\User class - Add tests for Relme\Domain_Icon_Map class - Update phpunit.xml.dist for new test structure - Update bootstrap.php for new structure
1 parent e66d966 commit 2a07454

File tree

6 files changed

+453
-38
lines changed

6 files changed

+453
-38
lines changed

phpunit.xml.dist

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
<?xml version="1.0"?>
22
<phpunit
3-
bootstrap="tests/bootstrap.php"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
5+
bootstrap="tests/phpunit/bootstrap.php"
46
backupGlobals="false"
57
colors="true"
6-
convertErrorsToExceptions="true"
7-
convertNoticesToExceptions="true"
8-
convertWarningsToExceptions="true"
8+
beStrictAboutTestsThatDoNotTestAnything="true"
9+
beStrictAboutOutputDuringTests="true"
910
>
1011
<testsuites>
11-
<testsuite name="IndieWeb">
12-
<directory prefix="test-" suffix=".php">./tests/</directory>
12+
<testsuite name="Indieweb">
13+
<directory prefix="class-test-" suffix=".php">tests/phpunit/tests/</directory>
1314
</testsuite>
1415
</testsuites>
15-
<coverage>
16+
<source>
1617
<include>
1718
<directory suffix=".php">./includes</directory>
1819
</include>
19-
</coverage>
20+
</source>
2021
</phpunit>
Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,44 @@
11
<?php
22
/**
3-
* PHPUnit bootstrap file
3+
* PHPUnit bootstrap file.
44
*
5-
* @package IndieWeb
5+
* @package Indieweb
66
*/
77

8+
define( 'INDIEWEB_TESTS_DIR', __DIR__ );
9+
10+
// Forward custom PHPUnit Polyfills configuration to PHPUnit bootstrap file.
11+
$_phpunit_polyfills_path = getenv( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' );
12+
if ( false !== $_phpunit_polyfills_path ) {
13+
define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', $_phpunit_polyfills_path );
14+
}
15+
816
$_tests_dir = getenv( 'WP_TESTS_DIR' );
917

1018
if ( ! $_tests_dir ) {
1119
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
1220
}
1321

14-
if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) {
15-
echo "Could not find $_tests_dir/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
16-
exit( 1 );
22+
if ( ! is_dir( $_tests_dir ) ) {
23+
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/classicpress-tests-lib';
1724
}
1825

19-
// Forward custom PHPUnit Polyfills configuration to PHPUnit bootstrap file.
20-
$_phpunit_polyfills_path = getenv( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' );
21-
if ( false !== $_phpunit_polyfills_path ) {
22-
define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', $_phpunit_polyfills_path );
23-
} elseif ( file_exists( dirname( __DIR__ ) . '/vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php' ) ) {
24-
define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', dirname( __DIR__ ) . '/vendor/yoast/phpunit-polyfills' );
26+
if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) {
27+
echo "Could not find {$_tests_dir}/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
28+
exit( 1 );
2529
}
2630

2731
// Give access to tests_add_filter() function.
2832
require_once $_tests_dir . '/includes/functions.php';
2933

34+
// Load PHPUnit Polyfills.
35+
require_once dirname( __DIR__, 2 ) . '/vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php';
36+
3037
/**
3138
* Manually load the plugin being tested.
3239
*/
3340
function _manually_load_plugin() {
34-
require dirname( __DIR__ ) . '/indieweb.php';
41+
require dirname( __DIR__, 2 ) . '/indieweb.php';
3542
}
3643
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
3744

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
/**
3+
* Test Indieweb class.
4+
*
5+
* @package Indieweb
6+
*/
7+
8+
/**
9+
* Test Indieweb class.
10+
*/
11+
class Test_Indieweb extends WP_UnitTestCase {
12+
13+
/**
14+
* Test that the plugin version constant is defined.
15+
*/
16+
public function test_version_constant_defined() {
17+
$this->assertTrue( defined( 'INDIEWEB_VERSION' ) );
18+
$this->assertNotEmpty( INDIEWEB_VERSION );
19+
}
20+
21+
/**
22+
* Test that plugin constants are defined.
23+
*/
24+
public function test_plugin_constants_defined() {
25+
$this->assertTrue( defined( 'INDIEWEB_PLUGIN_DIR' ) );
26+
$this->assertTrue( defined( 'INDIEWEB_PLUGIN_BASENAME' ) );
27+
$this->assertTrue( defined( 'INDIEWEB_PLUGIN_FILE' ) );
28+
$this->assertTrue( defined( 'INDIEWEB_PLUGIN_URL' ) );
29+
}
30+
31+
/**
32+
* Test that the singleton returns an instance.
33+
*/
34+
public function test_get_instance() {
35+
$instance = \Indieweb\Indieweb::get_instance();
36+
37+
$this->assertInstanceOf( \Indieweb\Indieweb::class, $instance );
38+
}
39+
40+
/**
41+
* Test that get_instance returns the same instance.
42+
*/
43+
public function test_singleton_returns_same_instance() {
44+
$instance1 = \Indieweb\Indieweb::get_instance();
45+
$instance2 = \Indieweb\Indieweb::get_instance();
46+
47+
$this->assertSame( $instance1, $instance2 );
48+
}
49+
50+
/**
51+
* Test that get_version returns the version constant.
52+
*/
53+
public function test_get_version() {
54+
$instance = \Indieweb\Indieweb::get_instance();
55+
56+
$this->assertEquals( INDIEWEB_VERSION, $instance->get_version() );
57+
}
58+
59+
/**
60+
* Test that the version function exists and returns version.
61+
*/
62+
public function test_version_function() {
63+
$this->assertEquals( INDIEWEB_VERSION, \Indieweb\version() );
64+
}
65+
66+
/**
67+
* Test that hooks are registered after init.
68+
*/
69+
public function test_hooks_registered() {
70+
$instance = \Indieweb\Indieweb::get_instance();
71+
$instance->init();
72+
73+
$this->assertNotFalse( has_action( 'wp_enqueue_scripts', array( $instance, 'enqueue_style' ) ) );
74+
$this->assertNotFalse( has_action( 'admin_enqueue_scripts', array( $instance, 'enqueue_admin_style' ) ) );
75+
$this->assertNotFalse( has_action( 'admin_menu', array( $instance, 'add_menu_item' ) ) );
76+
$this->assertNotFalse( has_action( 'admin_init', array( $instance, 'privacy_declaration' ) ) );
77+
}
78+
79+
/**
80+
* Test that the indieweb_loaded action is fired.
81+
*/
82+
public function test_indieweb_loaded_action_fired() {
83+
$this->assertNotFalse( did_action( 'indieweb_loaded' ) );
84+
}
85+
}
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
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

Comments
 (0)