Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit Tests: Run under PHP 8.1, same as production #1457

Open
wants to merge 28 commits into
base: production
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e5b60e2
Unit Tests: Run under PHP 8.1, same as production
dd32 Feb 12, 2025
ce42be7
Unit Tests: Re-run tests on changes to the workflow itself in a PR.
dd32 Feb 12, 2025
64cb7fa
Feature Flags: Fix flags when running in tests
dd32 Feb 12, 2025
3129cf5
DEBUG: Where are composer things installed to?
dd32 Feb 12, 2025
9ba6a0f
DEBUG: why is tmp existing..
dd32 Feb 12, 2025
427322f
Unit Tests: Always install composer dependencies fresh, not locked.
dd32 Feb 12, 2025
60398e0
Composer: Make the pub-sync folder if needed.
dd32 Feb 12, 2025
c575700
Unit Tests: Run with Composer v2
dd32 Feb 12, 2025
e674acc
Unit Test bootstrap: The wporg-mu-plugins are now available here.
dd32 Feb 12, 2025
7b064ba
DEBUG: remove it.
dd32 Feb 12, 2025
68dbc57
Unit Tests: Composer: Try specifying the Github token for composer use.
dd32 Feb 12, 2025
13d820f
Composer: Specify the token for the correct step
dd32 Feb 12, 2025
295fcdc
Composer: This was correct afterall.
dd32 Feb 12, 2025
56b56ad
Actions: Update action versions.
dd32 Feb 12, 2025
ddbf764
Actions: Update action versions.
dd32 Feb 12, 2025
b658f36
Tests: Don't need a mysqli custom db anymore
dd32 Feb 12, 2025
211834f
Tests: Extend the Database_Testcase.
dd32 Feb 12, 2025
a24586f
Composer: Temporarily run an older Jetpack version that's supported b…
dd32 Feb 13, 2025
6aa924f
Linter: Ignore composer.lock.
dd32 Feb 13, 2025
6f165d2
Linter: Run with PHP 8.1 w/ Composer set to the correct token.
dd32 Feb 13, 2025
9689402
None of this should run in the CLI mode..
dd32 Feb 18, 2025
b943f19
Test Bootstrap: Allow for Database_TestCase to create a specific netw…
dd32 Feb 18, 2025
4edeb69
Add extra domain to the expected test.
dd32 Feb 18, 2025
2f385f3
Organizer reminder tests: Use the production-like database setup.
dd32 Feb 18, 2025
f373c01
lint
dd32 Feb 18, 2025
55d154b
Move blog_id munging to the base class.
dd32 Feb 18, 2025
a815b2c
lint
dd32 Feb 18, 2025
ed33630
Try moving everything to DB testcases, not great for performance, but…
dd32 Feb 18, 2025
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
2 changes: 0 additions & 2 deletions .docker/bin/install-wp-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ install_wp() {
download https://wordpress.org/${ARCHIVE_NAME}.tar.gz $TMPDIR/wordpress.tar.gz
tar --strip-components=1 -zxmf $TMPDIR/wordpress.tar.gz -C $WP_CORE_DIR
fi

download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php $WP_CORE_DIR/wp-content/db.php
}

install_test_suite() {
Expand Down
14 changes: 12 additions & 2 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/cache@v2
- uses: actions/cache@v4
with:
path: '**/node_modules'
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
Expand All @@ -35,8 +35,18 @@ jobs:
run: |
sudo apt-get update && sudo apt-get install -y subversion

- name: Set PHP version
uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
coverage: none
tools: composer:v2
env:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: composer install
run: |
rm composer.lock || true
composer install

- name: yarn install and build
Expand Down
17 changes: 11 additions & 6 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
pull_request:
paths:
- public_html/**
- .github/**
push:
branches: [production]
paths:
Expand All @@ -17,9 +18,9 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- uses: actions/cache@v2
- uses: actions/cache@v4
with:
path: '**/node_modules'
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
Expand All @@ -45,7 +46,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php-version: ['7.4'] # 8.0 -- Add back to matrix when 8.0 is supported.
php-version: ['8.1']

services:
mysql:
Expand All @@ -59,7 +60,7 @@ jobs:
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Install SVN
run: sudo apt-get update && sudo apt-get install -y subversion
Expand All @@ -69,13 +70,17 @@ jobs:
with:
php-version: ${{ matrix.php-version }}
coverage: none
tools: composer:v1
tools: composer:v2
env:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Start mysql service
run: sudo /etc/init.d/mysql start

- name: Install dependencies
run: composer install
run: |
rm composer.lock || true
composer install

- name: Install WordPress
run: |
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
"wpackagist-plugin/email-post-changes": "dev-trunk",
"wpackagist-plugin/gutenberg": "*",
"wpackagist-plugin/hyperdb": "dev-trunk",
"wpackagist-plugin/jetpack": "*",
"wpackagist-plugin/jetpack": "13.7.1",
"wpackagist-plugin/liveblog": "*",
"wpackagist-plugin/public-post-preview": "*",
"wpackagist-plugin/pwa": "*",
Expand Down Expand Up @@ -174,6 +174,7 @@
"tw": "@test:watch",
"twg": "@test:watch:group",
"post-update-cmd": [
"[ $COMPOSER_DEV_MODE -eq 0 ] || mkdir -p public_html/wp-content/mu-plugins-private/wporg-mu-plugins/pub-sync",
"[ $COMPOSER_DEV_MODE -eq 0 ] || rm -rf public_html/wp-content/mu-plugins-private/wporg-mu-plugins/pub-sync/*",
"[ $COMPOSER_DEV_MODE -eq 0 ] || mv public_html/wp-content/mu-plugins-private/wporg-mu-plugins/tmp/mu-plugins/* public_html/wp-content/mu-plugins-private/wporg-mu-plugins/pub-sync",
"[ $COMPOSER_DEV_MODE -eq 0 ] || rm -rf public_html/wp-content/mu-plugins-private/wporg-mu-plugins/tmp/"
Expand Down
45 changes: 45 additions & 0 deletions phpunit-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,48 @@
* This has to be done after Core's bootstrapper finished, so that PHPUnit classes will be available.
*/
require_once( __DIR__ . '/phpunit-database-testcase.php' );


/**
* If a site creation attempts to occur with a specific blog_id, force it.
*
* WordCamp operates on a number of assumptions that require specific blog_ids to be used.
* This is a hacky hack to ensure that the blog_ids are always what we expect them to be.
*
* @see WordCamp\Tests\Database_TestCase::wpSetUpBeforeClass()
*/
function normalize_site_data( $data ) {
if (
// Nothing specified.
! isset( $data['blog_id'] ) ||
// Site exists, don't mess with it.. This will likely cause test failures.
get_site( $data['blog_id'] )
) {
return $data;
}

// Filter the WPDB::update() call to include the `blog_id` field..
add_filter(
'query',
$callback = static function ( $query ) use ( $data, &$callback ) {
global $wpdb;

if ( str_starts_with( $query, "INSERT INTO `{$wpdb->blogs}`" ) ) {
$blog_id = intval( $data['blog_id'] );
$query = preg_replace(
"/(INSERT INTO `{$wpdb->blogs}`)\s*\((.+)\) VALUES \(/",
'$1 (`blog_id`, $2 ) VALUES ( ' . $blog_id . ', ',
$query
);

// Unhook, we've done our job.
remove_filter( 'query', $callback );
}

return $query;
}
);

return $data;
}
tests_add_filter( 'wp_normalize_site_data', __NAMESPACE__ . '\normalize_site_data', 10, 2 );
44 changes: 41 additions & 3 deletions phpunit-database-testcase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace WordCamp\Tests;
use WP_UnitTestCase, WP_UnitTest_Factory;


/**
* Provides a mock WordCamp.org network of sites to test against.
*
Expand All @@ -15,6 +14,8 @@
*/
class Database_TestCase extends WP_UnitTestCase {
protected static $central_site_id;
protected static $events_root_site_id;
protected static $wordcamp_root_site_id;
protected static $year_dot_2018_site_id;
protected static $year_dot_2019_site_id;
protected static $slash_year_2016_site_id;
Expand All @@ -29,6 +30,39 @@ class Database_TestCase extends WP_UnitTestCase {
* @param WP_UnitTest_Factory $factory
*/
public static function wpSetUpBeforeClass( $factory ) {
ms_upload_constants();

global $wpdb;
// Reset the sites table, so the IDs are predictable. WordPress doesn't respect network_id=1.
$wpdb->query( "TRUNCATE TABLE $wpdb->site" );
$wpdb->query( "TRUNCATE TABLE $wpdb->sitemeta" );

$factory->network->create( array(
'domain' => 'wordcamp.test',
'path' => '/',
'subdomain_install' => true,
'network_id' => WORDCAMP_NETWORK_ID,
) );
$factory->network->create( array(
'domain' => 'events.wordpress.test',
'path' => '/',
'network_id' => EVENTS_NETWORK_ID,
) );

self::$wordcamp_root_site_id = $factory->blog->create( array(
'domain' => 'wordcamp.test',
'path' => '/',
'blog_id' => WORDCAMP_ROOT_BLOG_ID,
'network_id' => WORDCAMP_NETWORK_ID,
) );

self::$events_root_site_id = $factory->blog->create( array(
'domain' => 'events.wordpress.test',
'path' => '/',
'blog_id' => EVENTS_ROOT_BLOG_ID,
'network_id' => EVENTS_NETWORK_ID,
) );

self::$central_site_id = $factory->blog->create( array(
'domain' => 'central.wordcamp.test',
'path' => '/',
Expand Down Expand Up @@ -93,13 +127,17 @@ public static function wpTearDownAfterClass() {
global $wpdb;

wp_delete_site( self::$central_site_id );
wp_delete_site( self::$wordcamp_root_site_id );
wp_delete_site( self::$events_root_site_id );
wp_delete_site( self::$year_dot_2018_site_id );
wp_delete_site( self::$year_dot_2019_site_id );
wp_delete_site( self::$slash_year_2016_site_id );
wp_delete_site( self::$slash_year_2018_dev_site_id );
wp_delete_site( self::$slash_year_2020_site_id );

$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", WORDCAMP_NETWORK_ID ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id = %d", WORDCAMP_NETWORK_ID ) );
foreach ( [ WORDCAMP_NETWORK_ID, EVENTS_NETWORK_ID ] as $network_id ) {
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $network_id ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id = %d", $network_id ) );
}
}
}
3 changes: 3 additions & 0 deletions public_html/wp-content/mu-plugins/3-helpers-misc.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ function wcorg_skip_feature( $flag, $blog_id = null ) {
}

$flags = get_site_meta( $blog_id, 'wordcamp_skip_feature' );
if ( ! $flags ) {
$flags = [];
}

return in_array( $flag, $flags, true );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*
* @package WordCamp\Tests
*/
class Test_Jetpack_CSS_Sanitization extends WP_UnitTestCase {
class Test_Jetpack_CSS_Sanitization extends Database_TestCase {

/**
* Test that no selector characters are encoded.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static function wpSetUpBeforeClass( $factory ) {
$database_sites = array(
'example.org', // The default site in WP's test suite.
'central.wordcamp.test',
'wordcamp.test',

// It should contain a city root site entry for the year.city domains that exist in the db.
'seattle.wordcamp.test',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@


namespace WordCamp\Sunrise\Events;
use WordCamp\Tests\Database_TestCase;
use WP_UnitTestCase;

defined( 'WPINC' ) || die();
Expand All @@ -19,7 +20,7 @@
* @group sunrise
* @group mu-plugins
*/
class Test_Sunrise_Events extends WP_UnitTestCase {
class Test_Sunrise_Events extends Database_TestCase {
/**
* @covers WordCamp\Sunrise\get_redirect_url
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace WordCamp\Trusted_Deputy_Capabilities\Tests;

use WordCamp\Tests\Database_TestCase;
use WP_UnitTestCase, WP_UnitTest_Factory;
use function WordCamp\Trusted_Deputy_Capabilities\{ is_deputy };

Expand All @@ -11,7 +12,7 @@
* @group mu-plugins
* @group trusted-deputies
*/
class Test_Trusted_Deputy_Capabilities extends WP_UnitTestCase {
class Test_Trusted_Deputy_Capabilities extends Database_TestCase {
/**
* Note: `wporg_remove_super_caps()` denies `import` to non-Super Admins if the domain isn't wordcamp.org,
* which results in a false-negative on sandboxes with alternate domain names.
Expand Down Expand Up @@ -54,9 +55,11 @@ class Test_Trusted_Deputy_Capabilities extends WP_UnitTestCase {
/**
* Setup shared fixtures before any tests are run.
*/
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) : void {
public static function wpSetUpBeforeClass( $factory ) : void {
global $trusted_deputies;

parent::wpSetUpBeforeClass( $factory );

self::$subscriber_id = self::factory()->user->create( array(
'role' => 'subscriber',
) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use WP_UnitTestCase;
use WordCamp\SubRoles;
use WordCamp\Tests\Database_TestCase;

defined( 'WPINC' ) || die();

Expand All @@ -15,7 +16,7 @@
*
* @package WordCamp\Tests
*/
class Test_SubRoles extends WP_UnitTestCase {
class Test_SubRoles extends Database_TestCase {
/**
* Reset global state between tests, for isolation.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

namespace WordCamp\Organizer_Reminders\Tests;
use WP_UnitTestCase, WP_UnitTest_Factory;
use WordCamp\Tests\Database_TestCase;
use WP_UnitTest_Factory;
use WCOR_Reminder, WCOR_Mailer;

defined( 'WPINC' ) || die();
Expand All @@ -13,7 +14,7 @@
*
* @group organizer-reminders
*/
class Test_WCOR_Mailer extends WP_UnitTestCase {
class Test_WCOR_Mailer extends Database_TestCase {
/**
* @var int $triggered_reminder_post_id The ID of an Organizer Reminder post which is configured to be sent on a trigger.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
* Load the plugins that we'll need to be active for the tests.
*/
function manually_load_plugin() {
// @todo switch to `require_once` once it's accessible in all local environments.
// @link https://github.com/WordPress/wordcamp.org/issues/769
include_once SUT_WP_CONTENT_DIR . '/mu-plugins-private/wporg-mu-plugins/pub-sync/utilities/class-export-csv.php';
require_once SUT_WP_CONTENT_DIR . '/mu-plugins-private/wporg-mu-plugins/pub-sync/utilities/class-export-csv.php';

require_once WP_PLUGIN_DIR . '/wordcamp-payments/includes/wordcamp-budgets.php';
require_once WP_PLUGIN_DIR . '/wordcamp-payments/includes/payment-request.php';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ function fetch_unsafe_remote_css( $remote_css_url ) {
*/
function sanitize_unsafe_css( $unsafe_css ) {
if ( ! class_exists( 'Jetpack_Custom_CSS_Enhancements' ) ) {
if ( version_compare( JETPACK__VERSION, '11.6', '<' ) ) {
if ( version_compare( JETPACK__VERSION, '13.7.1', '>' ) ) {
return ''; // Custom CSS not included in Jetpack anymore.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is obviously a big TODO; to replace the Jetpack CSS sanitization dependency with our own bundled here.
This is needed, and was one of the reasons for this PR in the first places.

This change should be reverted from the PR really.

} elseif ( version_compare( JETPACK__VERSION, '11.6', '<' ) ) {
require_once JETPACK__PLUGIN_DIR . '/modules/custom-css/custom-css-4.7.php';
} else {
require_once JETPACK__PLUGIN_DIR . '/modules/custom-css/custom-css.php';
Expand Down
Loading
Loading