Skip to content

Commit

Permalink
Normalize paths, reduce network requests to WPORG for stability
Browse files Browse the repository at this point in the history
  • Loading branch information
Luc45 committed Oct 15, 2024
1 parent ed307c8 commit 2456ab9
Show file tree
Hide file tree
Showing 9 changed files with 374 additions and 13 deletions.
14 changes: 14 additions & 0 deletions _tests/custom_tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,24 @@ function qit( array $command, array $qit_env_json = [], int $expected_exit_code
'QIT_HOME' => $GLOBALS['QIT_HOME'],
'QIT_DISABLE_CLEANUP' => '1', // We need to disable it because of parallelization with individualized QIT_HOMEs.
'QIT_SELF_TESTS' => '1',
'QIT_NO_PULL' => '1',
'CI' => '1',
'COLUMNS' => '300', // Set a fixed width so that we can snapshot the output.
];

/*
* Add our helper mu-plugin, if applicable.
* To do this, we check if the command we are running have a "--volume" option.
*/
$volume_check = new Process( [ 'php', $GLOBALS['qit-php'], $command[0], '--help' ] );
$volume_check->setEnv( $env );
$volume_check->run();

if ( strpos( $volume_check->getOutput(), '--volume' ) !== false ) {
$args[] = '--volume';
$args[] = sprintf( '%s:%s', __DIR__ . '/helpers/custom-test-mu-plugin.php', '/var/www/html/wp-content/mu-plugins/custom-test-mu-plugin.php' );
}

$env = array_merge( $env, $extra_env );

$qit = new Process( $args );
Expand Down
345 changes: 345 additions & 0 deletions _tests/custom_tests/helpers/custom-test-mu-plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,345 @@
<?php
/*
* Plugin Name: Custom Test - Mu Plugin
* Description: We disable internet connection on our self-tests, and we also unhook things here to avoid unnecessary warnings as a consequence.
*/

if ( get_option( 'qit_setup_complete' ) !== 'yes' ) {
return;
}

$disable_news_and_events = function() {
remove_meta_box( 'dashboard_primary', get_current_screen(), 'side' );
};

add_action( 'wp_network_dashboard_setup', $disable_news_and_events, 20 );
add_action( 'wp_user_dashboard_setup', $disable_news_and_events, 20 );
add_action( 'wp_dashboard_setup', $disable_news_and_events, 20 );

// If we are in WP CLI context, keep network enabled and allow update checks.
if ( defined( 'WP_CLI' ) && WP_CLI ) {
return;
}

// Block network access except for WP CLI requests.
if ( ! defined( 'WP_HTTP_BLOCK_EXTERNAL' ) ) {
define( 'WP_HTTP_BLOCK_EXTERNAL', true );
}

/**
* Port "Disable WordPress Updates" plugin.
*/
/**
* Define the plugin version
*/
define("OSDWPUVERSION", "1.7.1");


/**
* The OS_Disable_WordPress_Updates class
*
* @package WordPress_Plugins
* @subpackage OS_Disable_WordPress_Updates
* @since 1.3
* @author [email protected]
*/
class OS_Disable_WordPress_Updates {
/**
* The OS_Disable_WordPress_Updates class constructor
* initializing required stuff for the plugin
*
* PHP 5 Constructor
*
* @since 1.3
* @author [email protected]
*/
function __construct() {
add_action( 'admin_init', array(&$this, 'admin_init') );

/*
* Disable Theme Updates
* 2.8 to 3.0
*/
add_filter( 'pre_transient_update_themes', array($this, 'last_checked_atm') );
/*
* 3.0
*/
add_filter( 'pre_site_transient_update_themes', array($this, 'last_checked_atm') );


/*
* Disable Plugin Updates
* 2.8 to 3.0
*/
add_action( 'pre_transient_update_plugins', array($this, 'last_checked_atm') );
/*
* 3.0
*/
add_filter( 'pre_site_transient_update_plugins', array($this, 'last_checked_atm') );


/*
* Disable Core Updates
* 2.8 to 3.0
*/
add_filter( 'pre_transient_update_core', array($this, 'last_checked_atm') );
/*
* 3.0
*/
add_filter( 'pre_site_transient_update_core', array($this, 'last_checked_atm') );


/*
* Filter schedule checks
*
* @link https://wordpress.org/support/topic/possible-performance-improvement/#post-8970451
*/
add_action('schedule_event', array($this, 'filter_cron_events'));

add_action( 'pre_set_site_transient_update_plugins', array($this, 'last_checked_atm'), 21, 1 );
add_action( 'pre_set_site_transient_update_themes', array($this, 'last_checked_atm'), 21, 1 );

/*
* Disable All Automatic Updates
* 3.7+
*
* @author sLa NGjI's @ slangji.wordpress.com
*/
add_filter( 'auto_update_translation', '__return_false' );
add_filter( 'automatic_updater_disabled', '__return_true' );
add_filter( 'allow_minor_auto_core_updates', '__return_false' );
add_filter( 'allow_major_auto_core_updates', '__return_false' );
add_filter( 'allow_dev_auto_core_updates', '__return_false' );
add_filter( 'auto_update_core', '__return_false' );
add_filter( 'wp_auto_update_core', '__return_false' );
add_filter( 'auto_core_update_send_email', '__return_false' );
add_filter( 'send_core_update_notification_email', '__return_false' );
add_filter( 'auto_update_plugin', '__return_false' );
add_filter( 'auto_update_theme', '__return_false' );
add_filter( 'automatic_updates_send_debug_email', '__return_false' );
add_filter( 'automatic_updates_is_vcs_checkout', '__return_true' );

remove_action( 'init', 'wp_schedule_update_checks' );
remove_all_filters( 'plugins_api' );

add_filter( 'automatic_updates_send_debug_email ', '__return_false', 1 );
if( !defined( 'AUTOMATIC_UPDATER_DISABLED' ) ) define( 'AUTOMATIC_UPDATER_DISABLED', true );
if( !defined( 'WP_AUTO_UPDATE_CORE') ) define( 'WP_AUTO_UPDATE_CORE', false );

add_filter( 'pre_http_request', array($this, 'block_request'), 10, 3 );
}


/**
* Initialize and load the plugin stuff
*
* @since 1.3
* @author [email protected]
*/
function admin_init() {
if ( !function_exists("remove_action") ) return;

if ( current_user_can( 'update_core' ) ) {
add_action( 'admin_bar_menu', array($this, 'add_adminbar_items'), 100 );
add_action( 'admin_enqueue_scripts', array($this, 'admin_css_overrides') );
}

/*
* Remove 'update plugins' option from bulk operations select list
*/
global $current_user;
$current_user->allcaps['update_plugins'] = 0;

/*
* Hide maintenance and update nag
*/
add_filter( 'site_status_tests', array($this, 'site_status_tests') );
remove_action( 'admin_notices', 'update_nag', 3 );
remove_action( 'network_admin_notices', 'update_nag', 3 );
remove_action( 'admin_notices', 'maintenance_nag' );
remove_action( 'network_admin_notices', 'maintenance_nag' );


/*
* Disable Theme Updates
* 2.8 to 3.0
*/
remove_action( 'load-themes.php', 'wp_update_themes' );
remove_action( 'load-update.php', 'wp_update_themes' );
remove_action( 'admin_init', '_maybe_update_themes' );
remove_action( 'wp_update_themes', 'wp_update_themes' );
wp_clear_scheduled_hook( 'wp_update_themes' );


/*
* 3.0
*/
remove_action( 'load-update-core.php', 'wp_update_themes' );
wp_clear_scheduled_hook( 'wp_update_themes' );


/*
* Disable Plugin Updates
* 2.8 to 3.0
*/
remove_action( 'load-plugins.php', 'wp_update_plugins' );
remove_action( 'load-update.php', 'wp_update_plugins' );
remove_action( 'admin_init', '_maybe_update_plugins' );
remove_action( 'wp_update_plugins', 'wp_update_plugins' );
wp_clear_scheduled_hook( 'wp_update_plugins' );

/*
* 3.0
*/
remove_action( 'load-update-core.php', 'wp_update_plugins' );
wp_clear_scheduled_hook( 'wp_update_plugins' );


/*
* Disable Core Updates
* 2.8 to 3.0
*/
add_filter( 'pre_option_update_core', '__return_null' );

remove_action( 'wp_version_check', 'wp_version_check' );
remove_action( 'admin_init', '_maybe_update_core' );
wp_clear_scheduled_hook( 'wp_version_check' );


/*
* 3.0
*/
wp_clear_scheduled_hook( 'wp_version_check' );


/*
* 3.7+
*/
remove_action( 'wp_maybe_auto_update', 'wp_maybe_auto_update' );
remove_action( 'admin_init', 'wp_maybe_auto_update' );
remove_action( 'admin_init', 'wp_auto_update_core' );
wp_clear_scheduled_hook( 'wp_maybe_auto_update' );

remove_all_filters( 'plugins_api' );
}



/**
* Hide update checks in the Site Health screen
*
* @since 1.6.8
*/
public function site_status_tests($tests) {
unset( $tests['async']['background_updates'] );
unset( $tests['direct']['plugin_theme_auto_updates'] );
return $tests;
}



/**
* Add notice to admin bar when plugin is active
*
* @since 1.7.0
*/
public function add_adminbar_items($admin_bar) {
$plugin_data = get_plugin_data( __FILE__ );

$admin_bar->add_menu( array(
'id' => 'dwuos-notice',
'title' => '<span class="dashicons dashicons-info" aria-hidden="true"></span>',
'href' => network_admin_url('plugins.php'),
'meta' => array(
'class' => 'wp-admin-bar-dwuos-notice',
'title' => sprintf(
/* translators: %s: Name of the plugin */
__( '"%s" plugin is enabled!', 'disable-wordpress-updates' ),
$plugin_data['Name']
)
),
));
}



/**
* Apply CSS styles to admin bar notice
*
* @since 1.7.0
*/
public function admin_css_overrides() {
wp_add_inline_style( 'admin-bar', '.wp-admin-bar-dwuos-notice { background-color: rgba(190, 0, 0, 0.4) !important; } .wp-admin-bar-dwuos-notice .dashicons { font-family: dashicons !important; }' );
}


/**
* Check the outgoing request
*
* @since 1.4.4
*/
public function block_request($pre, $args, $url) {
/* Empty url */
if( empty( $url ) ) {
return $pre;
}

/* Invalid host */
if( !$host = parse_url($url, PHP_URL_HOST) ) {
return $pre;
}

$url_data = parse_url( $url );

/* block request */
if( false !== stripos( $host, 'api.wordpress.org' ) &&
isset( $url_data['path'] ) &&
(false !== stripos( $url_data['path'], 'update-check' ) ||
false !== stripos( $url_data['path'], 'version-check' ) ||
false !== stripos( $url_data['path'], 'browse-happy' ) ||
false !== stripos( $url_data['path'], 'serve-happy' )) ) {
return true;
}

return $pre;
}


/**
* Filter cron events
*
* @since 1.5.0
*/
public function filter_cron_events($event) {
switch( $event->hook ) {
case 'wp_version_check':
case 'wp_update_plugins':
case 'wp_update_themes':
case 'wp_maybe_auto_update':
$event = false;
break;
}
return $event;
}


/**
* Override version check info
*
* @since 1.6.0
*/
public function last_checked_atm( $t ) {
include( ABSPATH . WPINC . '/version.php' );

$current = new stdClass;
$current->updates = array();
$current->version_checked = $wp_version;
$current->last_checked = time();

return $current;
}
}

if ( class_exists('OS_Disable_WordPress_Updates') ) {
$OS_Disable_WordPress_Updates = new OS_Disable_WordPress_Updates();
}
2 changes: 1 addition & 1 deletion _tests/custom_tests/tests/Traits/ScaffoldHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected function normalize_env_info( array $env_info ): array {

$env_info = str_replace( $id, 'ENV_ID_NORMALIZED', $env_info );
$env_info = str_replace( $dir_to_replace, '/path/normalized/', $env_info );
$env_info = str_replace( '/' . sys_get_temp_dir() . '/', '/tmp-normalized/', $env_info );
$env_info = str_replace( rtrim( sys_get_temp_dir(), '/' ) . '/', '/tmp-normalized/', $env_info );
$env_info = str_replace( '/tmp/', '/tmp-normalized/', $env_info );
$env_info = preg_replace( '/qit_scaffolded_e2e-[a-f0-9]+/', 'qit_scaffolded_e2e-NORMALIZED_ID', $env_info );
$env_info = preg_replace( '/qit_config-qit_custom_tests_[a-f0-9]+/', 'qit_config-qit_custom_tests_NORMALIZED_ID', $env_info );
Expand Down
2 changes: 1 addition & 1 deletion _tests/custom_tests/tests/Traits/SnapshotHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ trait SnapshotHelpers {
use MatchesSnapshots;

public function assertMatchesNormalizedSnapshot( string $actual, ?\Spatie\Snapshots\Driver $driver = null ): void {
$actual = str_replace( '/' . sys_get_temp_dir() . '/', '/tmp-normalized/', $actual );
$actual = str_replace( rtrim( sys_get_temp_dir(), '/' ) . '/', '/tmp-normalized/', $actual );
$actual = str_replace( '/tmp/', '/tmp-normalized/', $actual );
$actual = preg_replace( '/qit-results-[a-z0-9]+/', 'qit-results-normalizedid', $actual );

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name status
custom-test-mu-plugin must-use
qit-individual-log must-use
qit-wp-cli must-use
woocommerce active
Expand Down
Loading

0 comments on commit 2456ab9

Please sign in to comment.