Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
<?xml version="1.0"?>
<phpunit
bootstrap="tests/bootstrap.php"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
bootstrap="tests/phpunit/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutOutputDuringTests="true"
>
<testsuites>
<testsuite name="IndieWeb">
<directory prefix="test-" suffix=".php">./tests/</directory>
<testsuite name="Indieweb">
<directory prefix="class-test-" suffix=".php">tests/phpunit/tests/</directory>
</testsuite>
</testsuites>
<coverage>
<source>
<include>
<directory suffix=".php">./includes</directory>
</include>
</coverage>
</source>
</phpunit>
31 changes: 19 additions & 12 deletions tests/bootstrap.php → tests/phpunit/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
<?php
/**
* PHPUnit bootstrap file
* PHPUnit bootstrap file.
*
* @package IndieWeb
* @package Indieweb
*/

define( 'INDIEWEB_TESTS_DIR', __DIR__ );

// Forward custom PHPUnit Polyfills configuration to PHPUnit bootstrap file.
$_phpunit_polyfills_path = getenv( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' );
if ( false !== $_phpunit_polyfills_path ) {
define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', $_phpunit_polyfills_path );
}

$_tests_dir = getenv( 'WP_TESTS_DIR' );

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

if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) {
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
exit( 1 );
if ( ! is_dir( $_tests_dir ) ) {
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/classicpress-tests-lib';
}

// Forward custom PHPUnit Polyfills configuration to PHPUnit bootstrap file.
$_phpunit_polyfills_path = getenv( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH' );
if ( false !== $_phpunit_polyfills_path ) {
define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', $_phpunit_polyfills_path );
} elseif ( file_exists( dirname( __DIR__ ) . '/vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php' ) ) {
define( 'WP_TESTS_PHPUNIT_POLYFILLS_PATH', dirname( __DIR__ ) . '/vendor/yoast/phpunit-polyfills' );
if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) {
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
exit( 1 );
}

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

// Load PHPUnit Polyfills.
require_once dirname( __DIR__, 2 ) . '/vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php';

/**
* Manually load the plugin being tested.
*/
function _manually_load_plugin() {
require dirname( __DIR__ ) . '/indieweb.php';
require dirname( __DIR__, 2 ) . '/indieweb.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

Expand Down
85 changes: 85 additions & 0 deletions tests/phpunit/tests/includes/class-test-indieweb.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* Test Indieweb class.
*
* @package Indieweb
*/

/**
* Test Indieweb class.
*/
class Test_Indieweb extends WP_UnitTestCase {

/**
* Test that the plugin version constant is defined.
*/
public function test_version_constant_defined() {
$this->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' ) );
}
}
190 changes: 190 additions & 0 deletions tests/phpunit/tests/includes/hcard/class-test-user.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
<?php
/**
* Test Hcard User class.
*
* @package Indieweb
*/

/**
* Test Hcard User class.
*/
class Test_Hcard_User extends WP_UnitTestCase {

/**
* Test user ID.
*
* @var int
*/
protected static $user_id;

/**
* Set up before class.
*
* @param WP_UnitTest_Factory $factory Factory instance.
*/
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$user_id = $factory->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 );
}
}
Loading