2
0
Fork 0
mirror of https://github.com/discourse/wp-discourse.git synced 2025-08-17 18:11:19 +08:00

Update to PHPUnit 9 (#493)

* Update phpunit

* phpunit dependency should only be require-dev

* Exclude tests from CI php version syntax check

* We only need to run sniff and jshint on the latest supported version

* Run tests on php 8.2
This commit is contained in:
Angus McLeod 2023-11-15 01:55:36 +01:00 committed by GitHub
parent d5d84d9db5
commit 8b1a7d68d8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 1166 additions and 744 deletions

View file

@ -14,13 +14,11 @@ jobs:
matrix:
include:
- php: '5.6'
sniff: true
- php: '7.0'
sniff: true
- php: '7.4'
- php: '8.0'
sniff: true
jshint: true
- php: '8.0'
steps:
- uses: actions/checkout@v3
@ -32,7 +30,7 @@ jobs:
- name: Syntax Check
run: |
find -L . -name '*.php' -not -path "./vendor/*" -print0 | xargs -0 -n 1 -P 4 php -l
find -L . -name '*.php' -not -path "./vendor/*" -not -path "./tests/*" -print0 | xargs -0 -n 1 -P 4 php -l
- name: Install Composer
if: 'matrix.sniff'
@ -44,7 +42,7 @@ jobs:
- name: WordPress Coding Standards
if: 'matrix.sniff'
run: |
vendor/bin/phpcs -p -s -v -n .
vendor/bin/phpcs -p -s -v -n . -d error_reporting=E_ALL^E_DEPRECATED
- name: JSHint
if: 'matrix.jshint'

View file

@ -12,8 +12,8 @@ jobs:
name: Tests on PHP ${{ matrix.php }}
strategy:
matrix:
php: ['7.4']
wordpress: ['5.1.2', 'latest']
php: ['8.2']
wordpress: ['latest']
steps:
- uses: actions/checkout@v3

1
.phpunit.result.cache Normal file

File diff suppressed because one or more lines are too long

View file

@ -27,14 +27,15 @@
},
"require-dev": {
"squizlabs/php_codesniffer": "3.*",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.1",
"dealerdirect/phpcodesniffer-composer-installer": "^1.0.0",
"10up/wp_mock": "0.4.2",
"phpunit/phpunit": "7.5.20",
"phpunit/php-code-coverage": "^6.1.4",
"phpunit/phpunit": "9.6",
"phpunit/php-code-coverage": "^9.2.13",
"phpcompatibility/php-compatibility": "^9.3.5",
"wp-coding-standards/wpcs": "^2.3",
"monolog/monolog": "^1.25",
"yoast/phpunit-polyfills": "^1.0.1"
"yoast/phpunit-polyfills": "^1.0.1",
"dms/phpunit-arraysubset-asserts": "^0.5.0"
},
"minimum-stability": "dev",
"prefer-stable": true,

1432
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -9,6 +9,8 @@ These formatters will be applied on each pull request in Github Actions (via ``.
#### PHPCS
> Note: Use -d error_reporting=E_ALL^E_DEPRECATED until https://github.com/PHPCompatibility/PHPCompatibility/issues/1651 is resolved
The ``phpcs`` configuration is handled in the ``.phpcs.xml`` file, a type of [Annotated Ruleset](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-Ruleset). Install the development composer packages by running ``composer install`` prior to using ``phpcs``, and run it using ``vendor/bin/phpcs``, for example
```

View file

@ -113,6 +113,10 @@ class FileManager {
*/
protected function create_files( $files ) {
foreach ( $files as $file ) {
if ( ! $file['base'] ) {
continue;
}
$file_path = trailingslashit( $file['base'] ) . $file['file'];
$dir_exists = wp_mkdir_p( $file['base'] );
$dir_writable = is_writable( $file['base'] );
@ -140,6 +144,10 @@ class FileManager {
*/
protected function files_are_ready( $files ) {
foreach ( $files as $file ) {
if ( ! $file['base'] ) {
return false;
}
$directory_path = trailingslashit( $file['base'] );
$file_path = $directory_path . $file['file'];

View file

@ -29,6 +29,24 @@ class Nonce {
*/
private static $instance = null;
/**
* One can override the default nonce life.
*
* The default is set to 10 minutes, which is plenty for most of the cases
*
* @access private
* @var int
*/
private $nonce_life;
/**
* Wpdb
*
* @access private
* @var mixed
*/
private $wpdb;
/**
* Constructor
*
@ -36,17 +54,8 @@ class Nonce {
*/
private function __construct() {
global $wpdb;
$this->wpdb = $wpdb;
/**
* One can override the default nonce life.
*
* The default is set to 10 minutes, which is plenty for most of the cases
*
* @var int
*/
$this->wpdb = $wpdb;
$this->nonce_life = intval( apply_filters( 'wpdc_nonce_life', 600 ) );
$this->maybe_create_db();
}

View file

@ -331,7 +331,10 @@ trait TemplateFunctions {
$use_internal_errors = libxml_use_internal_errors( true );
$disable_entity_loader = $this->libxml_disable_entity_loader( true );
$doc = new \DOMDocument( '1.0', 'utf-8' );
$doc->loadHTML( mb_convert_encoding( $content, 'HTML-ENTITIES', 'UTF-8' ) );
$html = htmlentities( $content, ENT_COMPAT, 'utf-8', false );
$html = mb_convert_encoding( $html, 'UTF-8', mb_detect_encoding( $html ) );
$html = htmlspecialchars_decode( $html );
$doc->loadHTML( $html );
$finder = new \DOMXPath( $doc );
$avatars_in_quotes = $finder->query( "//aside[contains(concat(' ', normalize-space(@class), ' '), ' quote ')]//img[contains(concat(' ', normalize-space(@class), ' '), ' avatar ')]" );

View file

@ -1,25 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="tests/phpunit/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
convertDeprecationsToExceptions="false"
>
<testsuites>
<testsuite name="unit">
<directory prefix="test-" suffix=".php">tests/phpunit/</directory>
<exclude>tests/phpunit/multisite/</exclude>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">lib</directory>
</whitelist>
</filter>
<php>
<server name="REQUEST_URI" value="https://wordpress.discourse.org"/>
</php>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="tests/phpunit/bootstrap.php" backupGlobals="false" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" convertDeprecationsToExceptions="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">lib</directory>
</include>
</coverage>
<testsuites>
<testsuite name="unit">
<directory prefix="test-" suffix=".php">tests/phpunit/</directory>
<exclude>tests/phpunit/multisite/</exclude>
</testsuite>
</testsuites>
<php>
<server name="REQUEST_URI" value="https://wordpress.discourse.org"/>
</php>
</phpunit>

25
phpunit.xml.bak Normal file
View file

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="tests/phpunit/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
convertDeprecationsToExceptions="false"
>
<testsuites>
<testsuite name="unit">
<directory prefix="test-" suffix=".php">tests/phpunit/</directory>
<exclude>tests/phpunit/multisite/</exclude>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">lib</directory>
</whitelist>
</filter>
<php>
<server name="REQUEST_URI" value="https://wordpress.discourse.org"/>
</php>
</phpunit>

View file

@ -0,0 +1 @@
{"version":1,"defects":[],"times":{"DiscoursePublishMultisiteTest::test_sync_to_discourse_when_creating_in_multisite":0.036,"DiscoursePublishMultisiteTest::test_sync_to_discourse_when_creating":0.014,"DiscoursePublishMultisiteTest::test_sync_to_discourse_when_creating_with_embed_error":0.017,"DiscoursePublishMultisiteTest::test_sync_to_discourse_when_creating_with_category_error":0.015,"DiscoursePublishMultisiteTest::test_sync_to_discourse_when_creating_with_response_body_error":0.019,"DiscoursePublishMultisiteTest::test_sync_to_discourse_when_creating_with_enqueued_post":0.016,"DiscoursePublishMultisiteTest::test_sync_to_discourse_when_creating_with_direct_db_publication_flags":0.014,"DiscoursePublishMultisiteTest::test_sync_to_discourse_pin_topic":0.015,"DiscoursePublishMultisiteTest::test_sync_to_discourse_discourse_username":0.022,"DiscoursePublishMultisiteTest::test_sync_to_discourse_discourse_username_with_single_user_api_key":0.022,"DiscoursePublishMultisiteTest::test_sync_to_discourse_when_updating":0.013,"DiscoursePublishMultisiteTest::test_sync_to_discourse_when_updating_with_deleted_topic":0.017,"DiscoursePublishMultisiteTest::test_sync_to_discourse_when_updating_with_featured_link":0.015,"DiscoursePublishMultisiteTest::test_sync_to_discourse_when_updating_with_direct_db_publication_flags":0.017,"DiscoursePublishMultisiteTest::test_exclude_tags_with_exclusionary_tag":0.016,"DiscoursePublishMultisiteTest::test_exclude_tags_with_non_exclusionary_tag":0.017,"DiscoursePublishMultisiteTest::test_wpdc_publish_options":0.016,"DiscoursePublishMultisiteTest::test_force_publish_allowed_property":0.015,"DiscoursePublishMultisiteTest::test_force_publish_option":0.015,"DiscoursePublishMultisiteTest::test_force_publish_max_age_prevents_older_posts_from_being_published":0.015,"DiscoursePublishMultisiteTest::test_wp_discourse_before_xmlrpc_publish_filter":0.016,"DiscoursePublishMultisiteTest::test_xmlrpc_publish_failure_notification":0.017,"DiscoursePublishMultisiteTest::test_remote_post_success":0.003,"DiscoursePublishMultisiteTest::test_remote_post_forbidden":0.007,"DiscoursePublishMultisiteTest::test_remote_post_unprocessable":0.007,"DiscoursePublishMultisiteTest::test_remote_post_failed_to_connect":0.006,"WPDiscourse\\Test\\DiscoursePublishTest::test_sync_to_discourse_when_creating":0.012,"WPDiscourse\\Test\\DiscoursePublishTest::test_sync_to_discourse_when_creating_with_embed_error":0.013,"WPDiscourse\\Test\\DiscoursePublishTest::test_sync_to_discourse_when_creating_with_category_error":0.014,"WPDiscourse\\Test\\DiscoursePublishTest::test_sync_to_discourse_when_creating_with_response_body_error":0.014,"WPDiscourse\\Test\\DiscoursePublishTest::test_sync_to_discourse_when_creating_with_enqueued_post":0.013,"WPDiscourse\\Test\\DiscoursePublishTest::test_sync_to_discourse_when_creating_with_direct_db_publication_flags":0.012,"WPDiscourse\\Test\\DiscoursePublishTest::test_sync_to_discourse_pin_topic":0.013,"WPDiscourse\\Test\\DiscoursePublishTest::test_sync_to_discourse_discourse_username":0.019,"WPDiscourse\\Test\\DiscoursePublishTest::test_sync_to_discourse_discourse_username_with_single_user_api_key":0.02,"WPDiscourse\\Test\\DiscoursePublishTest::test_sync_to_discourse_when_updating":0.012,"WPDiscourse\\Test\\DiscoursePublishTest::test_sync_to_discourse_when_updating_with_deleted_topic":0.015,"WPDiscourse\\Test\\DiscoursePublishTest::test_sync_to_discourse_when_updating_with_featured_link":0.012,"WPDiscourse\\Test\\DiscoursePublishTest::test_sync_to_discourse_when_updating_with_direct_db_publication_flags":0.012,"WPDiscourse\\Test\\DiscoursePublishTest::test_exclude_tags_with_exclusionary_tag":0.013,"WPDiscourse\\Test\\DiscoursePublishTest::test_exclude_tags_with_non_exclusionary_tag":0.016,"WPDiscourse\\Test\\DiscoursePublishTest::test_wpdc_publish_options":0.014,"WPDiscourse\\Test\\DiscoursePublishTest::test_force_publish_allowed_property":0.014,"WPDiscourse\\Test\\DiscoursePublishTest::test_force_publish_option":0.019,"WPDiscourse\\Test\\DiscoursePublishTest::test_force_publish_max_age_prevents_older_posts_from_being_published":0.014,"WPDiscourse\\Test\\DiscoursePublishTest::test_wp_discourse_before_xmlrpc_publish_filter":0.014,"WPDiscourse\\Test\\DiscoursePublishTest::test_xmlrpc_publish_failure_notification":0.012,"WPDiscourse\\Test\\DiscoursePublishTest::test_remote_post_success":0,"WPDiscourse\\Test\\DiscoursePublishTest::test_remote_post_forbidden":0.004,"WPDiscourse\\Test\\DiscoursePublishTest::test_remote_post_unprocessable":0.004,"WPDiscourse\\Test\\DiscoursePublishTest::test_remote_post_failed_to_connect":0.004,"SyncDiscourseTopicMultisiteTest::test_update_topic_content":0.016,"SyncDiscourseTopicMultisiteTest::test_update_topic_content_invalid_signature":0.022,"WPDiscourse\\Test\\SyncDiscourseTopicTest::test_update_topic_content":0.01,"WPDiscourse\\Test\\SyncDiscourseTopicTest::test_update_topic_content_invalid_signature":0.01}}

View file

@ -15,7 +15,7 @@ trait Multisite {
/**
* Setup multisite tests
*/
public function setUp() {
public function setUp(): void {
parent::setUp();
$this->create_topic_blog_table();
}
@ -23,7 +23,7 @@ trait Multisite {
/**
* Teardown multisite tests
*/
public function tearDown() {
public function tearDown(): void {
parent::tearDown();
$this->clear_topic_blog_table();
}

View file

@ -23,10 +23,42 @@ class DiscourseCommentFormatterTest extends UnitTest {
*/
protected $comment_formatter;
/**
* Discourse post
*
* @access protected
* @var array
*/
protected $discourse_post;
/**
* Wordpress post id
*
* @access protected
* @var int
*/
protected $post_id;
/**
* Discourse topic id
*
* @access protected
* @var int
*/
protected $discourse_topic_id;
/**
* Discourse permalink
*
* @access protected
* @var string
*/
protected $discourse_permalink;
/**
* Setup each test.
*/
public function setUp() {
public function setUp(): void {
parent::setUp();
$this->comment_formatter = new DiscourseCommentFormatter();
@ -49,7 +81,7 @@ class DiscourseCommentFormatterTest extends UnitTest {
update_post_meta( $this->post_id, 'discourse_topic_id', $this->discourse_topic_id );
}
public function tearDown() {
public function tearDown(): void {
parent::tearDown();
// Cleanup.
@ -120,8 +152,8 @@ class DiscourseCommentFormatterTest extends UnitTest {
// TO FIX. Ensure we've made the right logs.
// $log = $this->get_last_log();
// $this->assertRegExp( '/comment_formatter.ERROR: format.missing_post_data/', $log );
// $this->assertRegExp( '/"keys":"' . $deleted_required_meta_key . '"/', $log );
// $this->assertMatchesRegularExpression( '/comment_formatter.ERROR: format.missing_post_data/', $log );
// $this->assertMatchesRegularExpression( '/"keys":"' . $deleted_required_meta_key . '"/', $log );
}
protected function sanitize_html( $buffer ) {

View file

@ -27,7 +27,7 @@ class DiscourseCommentTest extends UnitTest {
/**
* Setup each test.
*/
public function setUp() {
public function setUp(): void {
parent::setUp();
$comment_formatter = new DiscourseCommentFormatter();
@ -110,11 +110,11 @@ class DiscourseCommentTest extends UnitTest {
// Ensure we've made the right logs
$log = $this->get_last_log();
$this->assertRegExp( '/comment.ERROR: sync_comments.response_error/', $log );
$this->assertRegExp( '/"message":"Not found"/', $log );
$this->assertRegExp( '/"discourse_topic_id":"' . $discourse_topic_id . '"/', $log );
$this->assertRegExp( '/"wp_post_id":' . $post_id . '/', $log );
$this->assertRegExp( '/"http_code":404/', $log );
$this->assertMatchesRegularExpression( '/comment.ERROR: sync_comments.response_error/', $log );
$this->assertMatchesRegularExpression( '/"message":"Not found"/', $log );
$this->assertMatchesRegularExpression( '/"discourse_topic_id":"' . $discourse_topic_id . '"/', $log );
$this->assertMatchesRegularExpression( '/"wp_post_id":' . $post_id . '/', $log );
$this->assertMatchesRegularExpression( '/"http_code":404/', $log );
// Cleanup
wp_delete_post( $post_id );
@ -230,8 +230,8 @@ class DiscourseCommentTest extends UnitTest {
// Ensure we've made the right logs
$log = $this->get_last_log();
$this->assertRegExp( "/comment.ERROR: $context.get_discourse_category/", $log );
$this->assertRegExp( '/"message":"An invalid response was returned from Discourse"/', $log );
$this->assertMatchesRegularExpression( "/comment.ERROR: $context.get_discourse_category/", $log );
$this->assertMatchesRegularExpression( '/"message":"An invalid response was returned from Discourse"/', $log );
// Cleanup.
wp_delete_post( $public_post_id );

View file

@ -26,7 +26,7 @@ class DiscourseConnectionTest extends UnitTest {
/**
* Setup each test.
*/
public function setUp() {
public function setUp(): void {
parent::setUp();
$this->form_helper = FormHelper::get_instance();
self::$plugin_options['connection-logs'] = 1;
@ -78,7 +78,7 @@ class DiscourseConnectionTest extends UnitTest {
$this->assertFalse( $result );
$log = $this->get_last_log();
$this->assertRegExp( '/connection.INFO: check_connection_status.failed_to_connect/', $log );
$this->assertMatchesRegularExpression( '/connection.INFO: check_connection_status.failed_to_connect/', $log );
}
/**
@ -101,7 +101,7 @@ class DiscourseConnectionTest extends UnitTest {
$this->assertFalse( $result );
$log = $this->get_last_log();
$this->assertRegExp( '/connection.INFO: check_connection_status.failed_to_connect/', $log );
$this->assertMatchesRegularExpression( '/connection.INFO: check_connection_status.failed_to_connect/', $log );
}
/**
@ -130,7 +130,7 @@ class DiscourseConnectionTest extends UnitTest {
$this->assertFalse( $result );
$log = $this->get_last_log();
$this->assertRegExp( '/connection.INFO: check_connection_status.invalid_scopes/', $log );
$this->assertMatchesRegularExpression( '/connection.INFO: check_connection_status.invalid_scopes/', $log );
self::$plugin_options['enable-discourse-comments'] = 0;
$this->form_helper->setup_options( self::$plugin_options );
@ -139,6 +139,6 @@ class DiscourseConnectionTest extends UnitTest {
$this->assertTrue( $result );
$log = $this->get_last_log();
$this->assertRegExp( '/connection.INFO: check_connection_status.valid_scopes/', $log );
$this->assertMatchesRegularExpression( '/connection.INFO: check_connection_status.valid_scopes/', $log );
}
}

View file

@ -37,7 +37,7 @@ class DiscoursePublishTest extends UnitTest {
/**
* Setup test class
*/
public static function setUpBeforeClass() {
public static function setUpBeforeClass(): void {
parent::setUpBeforeClass();
self::initialize_variables();
@ -46,7 +46,7 @@ class DiscoursePublishTest extends UnitTest {
/**
* Setup each test.
*/
public function setUp() {
public function setUp(): void {
parent::setUp();
$register_actions = false;
$this->email_notifier = \Mockery::mock( EmailNotification::class )->makePartial();
@ -118,9 +118,9 @@ class DiscoursePublishTest extends UnitTest {
// Ensure the right log is created.
$log = $this->get_last_log();
$this->assertRegExp( '/publish.ERROR: create_post.post_error/', $log );
$this->assertRegExp( '/"http_code":' . $raw_response['response']['code'] . '/', $log );
$this->assertRegExp( '/"response_message":"' . $error_message . '"/', $log );
$this->assertMatchesRegularExpression( '/publish.ERROR: create_post.post_error/', $log );
$this->assertMatchesRegularExpression( '/"http_code":' . $raw_response['response']['code'] . '/', $log );
$this->assertMatchesRegularExpression( '/"response_message":"' . $error_message . '"/', $log );
// Cleanup.
wp_delete_post( $post_id );
@ -159,9 +159,9 @@ class DiscoursePublishTest extends UnitTest {
// Ensure the right log is created.
$log = $this->get_last_log();
$this->assertRegExp( '/publish.ERROR: create_post.post_error/', $log );
$this->assertRegExp( '/"http_code":' . $raw_response['response']['code'] . '/', $log );
$this->assertRegExp( '/"response_message":"' . $error_message . '"/', $log );
$this->assertMatchesRegularExpression( '/publish.ERROR: create_post.post_error/', $log );
$this->assertMatchesRegularExpression( '/"http_code":' . $raw_response['response']['code'] . '/', $log );
$this->assertMatchesRegularExpression( '/"response_message":"' . $error_message . '"/', $log );
// cleanup.
wp_delete_post( $post_id );
@ -201,7 +201,7 @@ class DiscoursePublishTest extends UnitTest {
// Ensure the right log is created.
$log = $this->get_last_log();
$this->assertRegExp( '/publish.ERROR: create_post.body_validation_error/', $log );
$this->assertMatchesRegularExpression( '/publish.ERROR: create_post.body_validation_error/', $log );
// cleanup.
wp_delete_post( $post_id );
@ -242,7 +242,7 @@ class DiscoursePublishTest extends UnitTest {
// Ensure the right log is created.
$log = $this->get_last_log();
$this->assertRegExp( '/publish.WARNING: create_post.queued_topic_notice/', $log );
$this->assertMatchesRegularExpression( '/publish.WARNING: create_post.queued_topic_notice/', $log );
// cleanup.
wp_delete_post( $post_id );
@ -501,7 +501,7 @@ class DiscoursePublishTest extends UnitTest {
// Ensure the right log is created.
$log = $this->get_last_log();
$this->assertRegExp( '/publish.WARNING: update_post.deleted_topic_notice/', $log );
$this->assertMatchesRegularExpression( '/publish.WARNING: update_post.deleted_topic_notice/', $log );
// cleanup.
wp_delete_post( $post_id );
@ -933,8 +933,8 @@ class DiscoursePublishTest extends UnitTest {
$this->assertEquals( $response, $this->build_post_error() );
$log = $this->get_last_log();
$this->assertRegExp( '/publish.ERROR: create_post.post_error/', $log );
$this->assertRegExp( '/"http_code":' . $raw_response['response']['code'] . '/', $log );
$this->assertMatchesRegularExpression( '/publish.ERROR: create_post.post_error/', $log );
$this->assertMatchesRegularExpression( '/"http_code":' . $raw_response['response']['code'] . '/', $log );
}
/**
@ -952,8 +952,8 @@ class DiscoursePublishTest extends UnitTest {
$this->assertEquals( $response, $this->build_post_error() );
$log = $this->get_last_log();
$this->assertRegExp( '/publish.ERROR: create_post.post_error/', $log );
$this->assertRegExp( '/"http_code":' . $raw_response['response']['code'] . '/', $log );
$this->assertMatchesRegularExpression( '/publish.ERROR: create_post.post_error/', $log );
$this->assertMatchesRegularExpression( '/"http_code":' . $raw_response['response']['code'] . '/', $log );
}
/**
@ -973,7 +973,7 @@ class DiscoursePublishTest extends UnitTest {
$this->assertEquals( $response, $this->build_post_error() );
$log = $this->get_last_log();
$this->assertRegExp( '/publish.ERROR: create_post.post_error/', $log );
$this->assertMatchesRegularExpression( '/publish.ERROR: create_post.post_error/', $log );
}
/**

View file

@ -15,7 +15,63 @@ use \WPDiscourse\Test\UnitTest;
*/
class DiscourseSSOTest extends UnitTest {
public function setUp() {
/**
* User id
*
* @access protected
* @var int
*/
protected $user_id;
/**
* Secret
*
* @access protected
* @var string
*/
protected $secret;
/**
* Nonce
*
* @access protected
* @var string
*/
protected $nonce;
/**
* Query vars
*
* @access protected
* @var array
*/
protected $query_vars;
/**
* Client
*
* @access protected
* @var \WPDiscourse\DiscourseSSO\DiscourseSSO
*/
protected $discourse_sso;
/**
* Signaure
*
* @access protected
* @var string
*/
protected $signature;
/**
* Payload
*
* @access protected
* @var string
*/
protected $payload;
public function setUp(): void {
$this->secret = 'secret';
$this->nonce = 'abcd';
$this->payload = base64_encode( "nonce={$this->nonce}" );
@ -35,7 +91,7 @@ class DiscourseSSOTest extends UnitTest {
$this->user_id = self::factory()->user->create();
}
public function tearDown() {
public function tearDown(): void {
parent::tearDown();
$_GET['request'] = null;
@ -104,7 +160,7 @@ class DiscourseSSOTest extends UnitTest {
$this->assertEquals( $parse_result->get_error_message(), 'SSO error' );
$log = $this->get_last_log();
$this->assertRegExp( '/sso_provider.ERROR: parse_request.invalid_sso/', $log );
$this->assertMatchesRegularExpression( '/sso_provider.ERROR: parse_request.invalid_sso/', $log );
}
/**
@ -126,8 +182,8 @@ class DiscourseSSOTest extends UnitTest {
$this->assertEquals( $parse_result->get_error_message(), $error_message );
$log = $this->get_last_log();
$this->assertRegExp( '/sso_provider.ERROR: parse_request.invalid_sso/', $log );
$this->assertRegExp( '/"message":"' . $error_message . '"/', $log );
$this->assertMatchesRegularExpression( '/sso_provider.ERROR: parse_request.invalid_sso/', $log );
$this->assertMatchesRegularExpression( '/"message":"' . $error_message . '"/', $log );
}
/**
@ -171,8 +227,8 @@ class DiscourseSSOTest extends UnitTest {
$this->assertEquals( $logout_result->get_error_message(), $error_message );
$log = $this->get_last_log();
$this->assertRegExp( '/sso_provider.ERROR: logout.discourse_user/', $log );
$this->assertRegExp( '/"message":"' . $error_message . '"/', $log );
$this->assertMatchesRegularExpression( '/sso_provider.ERROR: logout.discourse_user/', $log );
$this->assertMatchesRegularExpression( '/"message":"' . $error_message . '"/', $log );
}
/**
@ -196,7 +252,7 @@ class DiscourseSSOTest extends UnitTest {
$this->assertEquals( $logout_result->get_error_message(), $error_message );
$log = $this->get_last_log();
$this->assertRegExp( '/sso_provider.ERROR: logout.response_error/', $log );
$this->assertRegExp( '/"message":"' . $error_message . '"/', $log );
$this->assertMatchesRegularExpression( '/sso_provider.ERROR: logout.response_error/', $log );
$this->assertMatchesRegularExpression( '/"message":"' . $error_message . '"/', $log );
}
}

View file

@ -73,7 +73,7 @@ class FileHandlerTest extends UnitTest {
$log_file = $log_files[0];
$last_entry = shell_exec( "tail -n 1 $log_file" );
$this->assertRegExp( '/New Log/', $last_entry );
$this->assertMatchesRegularExpression( '/New Log/', $last_entry );
}
/**
@ -136,8 +136,8 @@ class FileHandlerTest extends UnitTest {
$todays_date = $todays_datetime->format( FileHandler::DATE_FORMAT );
$files = $file_handler->list_files();
$this->assertRegExp( '/' . $tomorrows_date . '/', $files[0] );
$this->assertRegExp( '/' . $todays_date . '/', $files[1] );
$this->assertMatchesRegularExpression( '/' . $tomorrows_date . '/', $files[0] );
$this->assertMatchesRegularExpression( '/' . $todays_date . '/', $files[1] );
}
/**

View file

@ -17,7 +17,7 @@ class FileManagerTest extends UnitTest {
/**
* Setup test class.
*/
public function setUp() {
public function setUp(): void {
parent::setUp();
static::reset_permissions();
}
@ -25,7 +25,7 @@ class FileManagerTest extends UnitTest {
/**
* Teardown test class.
*/
public function tearDown() {
public function tearDown(): void {
parent::tearDown();
static::reset_permissions();
}
@ -37,7 +37,7 @@ class FileManagerTest extends UnitTest {
$file_manager = new FileManager();
$this->recursive_rmdir( $file_manager->upload_dir );
$this->assertDirectoryNotExists( $file_manager->upload_dir );
$this->assertDirectoryDoesNotExist( $file_manager->upload_dir );
$file_manager->validate();
@ -52,7 +52,7 @@ class FileManagerTest extends UnitTest {
$file_manager = new FileManager();
$this->recursive_rmdir( $file_manager->logs_dir );
$this->assertDirectoryNotExists( $file_manager->logs_dir );
$this->assertDirectoryDoesNotExist( $file_manager->logs_dir );
$file_manager->validate();

View file

@ -7,6 +7,7 @@
namespace WPDiscourse\Test;
use DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts;
use \WPDiscourse\Logs\FileManager;
use \WPDiscourse\Logs\FileHandler;
use \WPDiscourse\Logs\Logger;
@ -18,6 +19,7 @@ use \WPDiscourse\Test\UnitTest;
* Logger test case.
*/
class LogViewerTest extends UnitTest {
use ArraySubsetAsserts;
/**
* Instance of LogViewer.
@ -30,7 +32,7 @@ class LogViewerTest extends UnitTest {
/**
* Setup each test.
*/
public function setUp() {
public function setUp(): void {
parent::setUp();
$this->viewer = new LogViewer( FormHelper::get_instance() );
@ -40,7 +42,7 @@ class LogViewerTest extends UnitTest {
/**
* Teardown each test.
*/
public function tearDown() {
public function tearDown(): void {
parent::tearDown();
self::$plugin_options['logs-enabled'] = 1;

View file

@ -21,7 +21,7 @@ class LoggerTest extends UnitTest {
/**
* Teardown each test.
*/
public function tearDown() {
public function tearDown(): void {
parent::tearDown();
self::$plugin_options['logs-enabled'] = 1;

View file

@ -16,7 +16,71 @@ use \WPDiscourse\Test\UnitTest;
*/
class SSOClientTest extends UnitTest {
public static function setUpBeforeClass() {
/**
* Discourse user id
*
* @access protected
* @var int
*/
protected $discourse_user_id;
/**
* User id
*
* @access protected
* @var int
*/
protected $user_id;
/**
* Secret
*
* @access protected
* @var string
*/
protected $secret;
/**
* Nonce
*
* @access protected
* @var string
*/
protected $nonce;
/**
* Query args
*
* @access protected
* @var array
*/
protected $query_args;
/**
* Client
*
* @access protected
* @var \WPDiscourse\SSOClient\Client
*/
protected $sso_client;
/**
* Signaure
*
* @access protected
* @var string
*/
protected $signature;
/**
* Payload
*
* @access protected
* @var array
*/
protected $payload;
public static function setUpBeforeClass(): void {
parent::initialize_shared_variables();
wp_logout();
@ -26,7 +90,7 @@ class SSOClientTest extends UnitTest {
}
}
public function setUp() {
public function setUp(): void {
parent::setUp();
$this->discourse_user_id = 5;
@ -60,7 +124,7 @@ class SSOClientTest extends UnitTest {
$_GET['sig'] = rawurlencode( $this->signature );
}
public function tearDown() {
public function tearDown(): void {
parent::tearDown();
$_GET['sso'] = null;
@ -101,7 +165,7 @@ class SSOClientTest extends UnitTest {
$this->assertNotEquals( $user->ID, $this->user_id );
$log = $this->get_last_log();
$this->assertRegExp( '/sso_client.ERROR: parse_request.invalid_signature/', $log );
$this->assertMatchesRegularExpression( '/sso_client.ERROR: parse_request.invalid_signature/', $log );
}
/**
@ -120,7 +184,7 @@ class SSOClientTest extends UnitTest {
$this->assertNotEquals( $user->ID, $this->user_id );
$log = $this->get_last_log();
$this->assertRegExp( '/sso_client.ERROR: parse_request.get_user_id/', $log );
$this->assertMatchesRegularExpression( '/sso_client.ERROR: parse_request.get_user_id/', $log );
}
/**
@ -135,7 +199,7 @@ class SSOClientTest extends UnitTest {
$this->assertNotEquals( $user->ID, $this->user_id );
$log = $this->get_last_log();
$this->assertRegExp( '/sso_client.ERROR: parse_request.update_user/', $log );
$this->assertMatchesRegularExpression( '/sso_client.ERROR: parse_request.update_user/', $log );
remove_filter( 'wpdc_sso_client_updated_user', array( $this, 'invalid_update_user_filter' ), 10 );
}

View file

@ -23,10 +23,34 @@ class SyncDiscourseTopicTest extends UnitTest {
*/
protected $sync_topic;
/**
* Request
*
* @access protected
* @var WP_REST_Request
*/
protected $request;
/**
* Signaure
*
* @access protected
* @var string
*/
protected $signature;
/**
* Payload
*
* @access protected
* @var array
*/
protected $payload;
/**
* Setup each test.
*/
public function setUp() {
public function setUp(): void {
parent::setUp();
self::$plugin_options['webhook-secret'] = '1234567891011';

View file

@ -23,10 +23,34 @@ class SyncDiscourseUserTest extends UnitTest {
*/
protected $sync_user;
/**
* Request
*
* @access protected
* @var WP_REST_Request
*/
protected $request;
/**
* Signaure
*
* @access protected
* @var string
*/
protected $signature;
/**
* Payload
*
* @access protected
* @var array
*/
protected $payload;
/**
* Setup each test.
*/
public function setUp() {
public function setUp(): void {
parent::setUp();
self::$plugin_options['webhook-secret'] = '1234567891011';
@ -48,7 +72,7 @@ class SyncDiscourseUserTest extends UnitTest {
$this->request->set_body( $this->payload );
}
public function tearDown() {
public function tearDown(): void {
parent::tearDown();
$payload = json_decode( $this->payload );
@ -151,7 +175,7 @@ class SyncDiscourseUserTest extends UnitTest {
// Ensure the right log is created.
$log = $this->get_last_log();
$this->assertRegExp( '/webhook_user.WARNING: update_user.user_not_found/', $log );
$this->assertMatchesRegularExpression( '/webhook_user.WARNING: update_user.user_not_found/', $log );
}
}

View file

@ -14,7 +14,15 @@ use WPDiscourse\Utilities\Utilities;
*/
class UtilitiesTest extends UnitTest {
public function setUp() {
/**
* User id
*
* @access protected
* @var int
*/
protected $user_id;
public function setUp(): void {
$connection_options = get_option( 'discourse_connect' );
$connection_options = array_merge( $connection_options, self::$connection_options );
update_option( 'discourse_connect', $connection_options );

View file

@ -76,20 +76,20 @@ class UnitTest extends \WP_UnitTestCase {
/**
* Setup test class
*/
public static function setUpBeforeClass() {
public static function setUpBeforeClass(): void {
self::initialize_shared_variables();
}
/**
* Setup each test.
*/
public function setUp() {
public function setUp(): void {
}
/**
* Teardown each test.
*/
public function tearDown() {
public function tearDown(): void {
$this->clear_logs();
remove_all_filters( 'pre_http_request' );
\Mockery::close();

View file

@ -271,7 +271,7 @@ class Logger implements \WPDiscourse\Psr\Log\LoggerInterface, \WPDiscourse\Monol
if ($this->microsecondTimestamps && \PHP_VERSION_ID < 70100) {
$ts = \DateTime::createFromFormat('U.u', \sprintf('%.6F', \microtime(\true)), static::$timezone);
} else {
$ts = new \DateTime(null, static::$timezone);
$ts = new \DateTime('now', static::$timezone);
}
$ts->setTimezone(static::$timezone);
$record = array('message' => (string) $message, 'context' => $context, 'level' => $level, 'level_name' => $levelName, 'channel' => $this->name, 'datetime' => $ts, 'extra' => array());