Fix all PHPUnit tests errors and compatiblity issues (#3871)

This commit is contained in:
Léo Colombaro 2025-03-21 14:01:34 +01:00 committed by GitHub
parent 9dccaa9531
commit 55f5423039
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
17 changed files with 65 additions and 53 deletions

View File

@ -71,6 +71,6 @@ jobs:
cp tests/data/config/yourls-tests-config-ci.php user/config.php
- name: Test
run: phpunit --configuration phpunit.xml.dist --testdox ${{ matrix.flags }}
run: phpunit --configuration phpunit.xml.dist --testdox --display-deprecations --display-notices --display-warnings --display-errors ${{ matrix.flags }}
env:
DB_PORT: ${{ job.services.mysql.ports['3306'] }}

View File

@ -1313,9 +1313,11 @@ function yourls_tell_if_new_version() {
function yourls_include_file_sandbox($file) {
try {
if (is_readable( $file )) {
include_once $file;
require_once $file;
yourls_debug_log("loaded $file");
return true;
} else {
throw new \Exception('File not readable');
}
} catch ( \Throwable $e ) {
yourls_debug_log("could not load $file");

View File

@ -3,7 +3,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
backupGlobals="false"
stopOnFailure="false"
stopOnFailure="true"
cacheDirectory=".phpunit.cache"
cacheResult="true"
bootstrap="tests/bootstrap.php"
@ -19,6 +19,8 @@
<testsuites>
<testsuite name="YOURLS Test Suite">
<directory suffix=".php">./tests/tests</directory>
<exclude>./tests/tests/auth/AbstractLoginTestCase.php</exclude>
<exclude>./tests/tests/auth/LoginAssertionTrait.php</exclude>
</testsuite>
</testsuites>
<groups>

View File

@ -62,4 +62,5 @@ yourls_add_action( 'pre_yourls_die', function($params) {
echo "YOURLS installed, starting PHPUnit\n\n";
require_once __DIR__ . "/tests/auth/LoginBaseTestCase.php";
require_once __DIR__ . "/tests/auth/AbstractLoginTestCase.php";
require_once __DIR__ . "/tests/auth/LoginAssertionTrait.php";

View File

@ -5,7 +5,7 @@
*/
// No direct call.
if( !defined( 'YOURLS_UNINSTALL_PLUGIN' ) ) die();
if( !defined( 'YOURLS_UNINSTALL_PLUGIN' ) ) return;
// The uninstallation process itself

View File

@ -5,7 +5,7 @@
*/
// No direct call.
if( !defined( 'YOURLS_UNINSTALL_PLUGIN' ) ) die();
if( !defined( 'YOURLS_UNINSTALL_PLUGIN' ) ) return;
// The uninstallation process itself

View File

@ -0,0 +1,21 @@
<?php
/**
* This abstract class isn't supposed to be run as tests
* See login_*.php files
*/
abstract class AbstractLoginTestCase extends PHPUnit\Framework\TestCase {
protected $backup_request;
protected function setUp(): void {
$this->backup_request = $_REQUEST;
$_REQUEST['nonce'] = yourls_create_nonce('admin_login');
}
protected function tearDown(): void {
$_REQUEST = $this->backup_request;
yourls_remove_all_actions('pre_yourls_die');
}
}

View File

@ -7,7 +7,9 @@
*/
#[\PHPUnit\Framework\Attributes\Group('auth')]
#[\PHPUnit\Framework\Attributes\Group('login')]
class LoginAPISecureTest extends LoginBaseTestCase {
class LoginAPISecureTest extends AbstractLoginTestCase {
use LoginAssertionTrait;
public static function setUpBeforeClass(): void {
yourls_add_filter( 'is_API', 'yourls_return_true' );

View File

@ -7,7 +7,9 @@
*/
#[\PHPUnit\Framework\Attributes\Group('auth')]
#[\PHPUnit\Framework\Attributes\Group('login')]
class LoginAPISecureTimeTokenTest extends LoginBaseTestCase {
class LoginAPISecureTimeTokenTest extends AbstractLoginTestCase {
use LoginAssertionTrait;
public static function setUpBeforeClass(): void {
yourls_add_filter( 'is_API', 'yourls_return_true' );

View File

@ -7,7 +7,9 @@
*/
#[\PHPUnit\Framework\Attributes\Group('auth')]
#[\PHPUnit\Framework\Attributes\Group('login')]
class LoginAPITest extends LoginBaseTestCase {
class LoginAPITest extends AbstractLoginTestCase {
use LoginAssertionTrait;
public static function setUpBeforeClass(): void {
yourls_add_filter( 'is_API', 'yourls_return_true' );

View File

@ -1,23 +1,7 @@
<?php
/**
* This abstract class isn't supposed to be run as tests
* See login_*.php files
*/
abstract class LoginBaseTestCase extends PHPUnit\Framework\TestCase {
protected $backup_request;
protected function setUp(): void {
$this->backup_request = $_REQUEST;
$_REQUEST['nonce'] = yourls_create_nonce('admin_login');
}
protected function tearDown(): void {
$_REQUEST = $this->backup_request;
yourls_remove_all_actions('pre_yourls_die');
}
<?php declare(strict_types=1);
trait LoginAssertionTrait
{
/**
* Check that user, as submitted by REQUEST (see phpunit XML config file), is valid
*
@ -98,5 +82,4 @@ abstract class LoginBaseTestCase extends PHPUnit\Framework\TestCase {
$this->assertNotTrue( yourls_is_valid_user() );
}
}

View File

@ -7,6 +7,8 @@
*/
#[\PHPUnit\Framework\Attributes\Group('auth')]
#[\PHPUnit\Framework\Attributes\Group('login')]
class LoginNormalTest extends LoginBaseTestCase {
class LoginNormalTest extends AbstractLoginTestCase {
use LoginAssertionTrait;
}

View File

@ -57,8 +57,8 @@ class GeoIPTest extends PHPUnit\Framework\TestCase {
public static function ipv6_samples(): \Iterator
{
yield array( '::80.24.24.24', 'ES' );
yield array( '2001:4860:0:1001::68', 'US' );
yield array( '2001:0240:2000:0000:0000:0000:0000:0000', 'JP' );
// yield array( '2606:4700:4700::1111', 'US' );
yield array( '2001:0240:2000::', 'JP' );
yield array( '::1', 'none' );
yield array( 'mynameisozh', 'none' );
}

View File

@ -11,7 +11,7 @@
class HTTPRequestsTest extends PHPUnit\Framework\TestCase {
private function url( $what = '' ) {
return 'https://httpbin.org/' . $what;
return 'https://httpbin.io/' . $what;
}
/**

View File

@ -29,10 +29,12 @@ class DomainTest extends PHPUnit\Framework\TestCase {
* @since 0.1
*/
public function test_custom_fake_domain() {
$this->expectError();
$this->expectExceptionMessageMatches('/Cannot read file [0-9a-z]+\/[0-9a-z]+-fr_FR\.mo\. Make sure there is a language file installed. More info: http:\/\/yourls\.org\/translations/');
$this->markTestSkipped(
'Notice are not checked by PHPUnit anymore. https://github.com/sebastianbergmann/phpunit/issues/5222',
);
// $this->expectExceptionMessageMatches('/Cannot read file [0-9a-z]+\/[0-9a-z]+-fr_FR\.mo\. Make sure there is a language file installed. More info: http:\/\/yourls\.org\/translations/');
yourls_load_custom_textdomain( rand_str(), rand_str() );
// yourls_load_custom_textdomain( rand_str(), rand_str() );
}
/**

View File

@ -102,9 +102,7 @@ class FilesTest extends PHPUnit\Framework\TestCase {
*/
#[\PHPUnit\Framework\Attributes\Depends('test_plugin_activate')]
public function test_plugin_activate_twice( $plugin ) {
$this->assertTrue( yourls_activate_plugin( $plugin ) );
// Note: we assertNotSame() with true because the function either returns true or a string
return $plugin;
$this->assertSame( yourls__( 'Plugin already activated' ), yourls_activate_plugin( $plugin ) );
}
/**
@ -166,7 +164,7 @@ class FilesTest extends PHPUnit\Framework\TestCase {
public function test_invalid_plugin_activate() {
$plugin = rand_str();
$this->assertTrue( yourls_activate_plugin( $plugin ) );
$this->assertSame( yourls__( 'Not a valid plugin file' ), yourls_activate_plugin( $plugin ) );
$this->assertFalse( yourls_is_active_plugin( $plugin ) );
}
@ -177,7 +175,7 @@ class FilesTest extends PHPUnit\Framework\TestCase {
$plugin = rand_str();
$this->assertFalse( yourls_is_active_plugin( $plugin ) );
$this->assertTrue( yourls_deactivate_plugin( $plugin ) );
$this->assertSame( yourls__( 'Plugin not active' ), yourls_deactivate_plugin( $plugin ) );
}
/**

View File

@ -5,9 +5,7 @@
*
* @group themes
*/
if( function_exists( 'yourls_activate_theme' ) ) :
#[\PHPUnit\Framework\Attributes\RequiresFunction('yourls_activate_theme')]
class ThemesTest extends \PHPUnit\Framework\TestCase {
/**
@ -37,7 +35,4 @@ class ThemesTest extends \PHPUnit\Framework\TestCase {
$this->assertEquals( $theme, yourls_get_active_theme() );
return $theme;
}
}
endif;