-
Notifications
You must be signed in to change notification settings - Fork 98
Create Runtime_Environment_Setup and object-cache.php #80
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
85380bb
update Plugin_Request_Utility with runner methods
jjgrainger 1cdc767
create object-cache.php file
jjgrainger 518958a
create Runtime_Environment_Setup class
jjgrainger cc61df6
add Runtime_Environment_Setup tests
jjgrainger 396a47e
update Runtime_Environment_Setup prefix and object-cache.php
jjgrainger d6e7190
add tests with existing object-cache.php file
jjgrainger 1e5a447
set database prefix in runtime preparation
jjgrainger 2745ff9
use $table_prefix global to restore wpdb prefix
jjgrainger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| <?php | ||
| /** | ||
| * Class WordPress\Plugin_Check\Checker\Runtime_Environment_Setup | ||
| * | ||
| * @package plugin-check | ||
| */ | ||
|
|
||
| namespace WordPress\Plugin_Check\Checker; | ||
|
|
||
| /** | ||
| * Class to setup the Runtime Environment for Runtime checks. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| class Runtime_Environment_Setup { | ||
|
|
||
| /** | ||
| * Sets up the WordPress environment for runtime checks | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| public function setup() { | ||
| global $wpdb, $wp_filesystem; | ||
|
|
||
| require_once ABSPATH . '/wp-admin/includes/upgrade.php'; | ||
|
|
||
| // Set the new prefix. | ||
| $old_prefix = $wpdb->set_prefix( 'wppc_' ); | ||
|
|
||
| // Create and populate the test database tables if they do not exist. | ||
| if ( 'wppc_posts' !== $wpdb->get_var( "SHOW TABLES LIKE 'wppc_posts'" ) ) { | ||
| wp_install( | ||
| 'Plugin Check', | ||
| 'plugincheck', | ||
| 'demo@plugincheck.test', | ||
| false | ||
| ); | ||
| } | ||
|
|
||
| // Restore the old prefix. | ||
| $wpdb->set_prefix( $old_prefix ); | ||
|
|
||
| // Return early if the plugin check object cache already exists. | ||
| if ( defined( 'WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION' ) && WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION ) { | ||
| return; | ||
| } | ||
|
|
||
| // Create the object-cache.php file. | ||
| if ( $wp_filesystem || WP_Filesystem() ) { | ||
|
jjgrainger marked this conversation as resolved.
|
||
| // Do not replace the object-cache.php file if it already exists. | ||
| if ( ! $wp_filesystem->exists( WP_CONTENT_DIR . '/object-cache.php' ) ) { | ||
| $wp_filesystem->copy( WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'object-cache.copy.php', WP_CONTENT_DIR . '/object-cache.php' ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Cleans up the runtime environment setup. | ||
| * | ||
| * @since n.e.x.t | ||
| */ | ||
| public function cleanup() { | ||
| global $wpdb, $wp_filesystem; | ||
|
|
||
| require_once ABSPATH . '/wp-admin/includes/upgrade.php'; | ||
|
|
||
| $old_prefix = $wpdb->set_prefix( 'wppc_' ); | ||
| $tables = $wpdb->tables(); | ||
|
|
||
| foreach ( $tables as $table ) { | ||
| $wpdb->query( "DROP TABLE IF EXISTS `$table`" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared | ||
| } | ||
|
|
||
| // Restore the old prefix. | ||
| $wpdb->set_prefix( $old_prefix ); | ||
|
|
||
| // Return early if the plugin check object cache does not exist. | ||
| if ( ! defined( 'WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION' ) || ! WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION ) { | ||
| return; | ||
| } | ||
|
|
||
| // Remove the object-cache.php file. | ||
| if ( $wp_filesystem || WP_Filesystem() ) { | ||
|
jjgrainger marked this conversation as resolved.
|
||
| if ( ! $wp_filesystem->exists( WP_CONTENT_DIR . '/object-cache.php' ) ) { | ||
| return; | ||
| } | ||
|
|
||
| // Check the drop-in file matches the copy. | ||
| $original_content = $wp_filesystem->get_contents( WP_CONTENT_DIR . '/object-cache.php' ); | ||
| $copy_content = $wp_filesystem->get_contents( WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'object-cache.copy.php' ); | ||
|
|
||
| if ( $original_content && $original_content === $copy_content ) { | ||
| $wp_filesystem->delete( WP_CONTENT_DIR . '/object-cache.php' ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
|
|
||
| <?php | ||
| /** | ||
| * Plugin Name: Plugin Check Object Cache Drop-In | ||
| * Plugin URI: https://github.com/WordPress/plugin-check | ||
| * Description: Plugin check drop-in to setup the test environment early. This is not a real object cache drop-in and will not override other actual object cache drop-ins. | ||
| * Version: 1 | ||
| * Author: WordPress Performance Team | ||
| * Author URI: https://make.wordpress.org/performance/ | ||
| * License: GPLv2 or later | ||
| * License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html | ||
| * | ||
| * Object cache drop-in from Plugin Check. | ||
| * | ||
| * This drop-in is used, admittedly as a hack, to be able to setup the | ||
| * WordPress environment as early as possible. Once a plugin is loaded, it is | ||
| * too late to configure the test environment. | ||
| * | ||
| * This file respects any real object cache implementation the site may already | ||
| * be using, and it is implemented in a way that there is no risk for breakage. | ||
| * | ||
| * @package plugin-check | ||
| * @since n.e.x.t | ||
| */ | ||
|
|
||
| // Set constant to be able to later check for whether this file was loaded. | ||
| define( 'WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION', 1 ); | ||
|
|
||
| function plugin_check_initialize_runner() { | ||
| $plugins_dir = defined( 'WP_PLUGIN_DIR' ) ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/plugins'; | ||
| $plugin_dir = $plugins_dir . '/plugin-check/'; | ||
| if ( ! file_exists( $plugin_dir . 'vendor/autoload.php' ) ) { | ||
| return; | ||
| } | ||
|
|
||
| require_once $plugin_dir . 'vendor/autoload.php'; | ||
|
|
||
| if ( class_exists( 'WordPress\Plugin_Check\Utilities\Plugin_Request_Utility' ) ) { | ||
| // Initialize the Check Runner class based on the request. | ||
| WordPress\Plugin_Check\Utilities\Plugin_Request_Utility::initialize_runner(); | ||
| } | ||
| } | ||
| plugin_check_initialize_runner(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| <?php | ||
| /** | ||
| * Tests for the Checks class. | ||
| * | ||
| * @package plugin-check | ||
| */ | ||
|
|
||
| use WordPress\Plugin_Check\Checker\Runtime_Environment_Setup; | ||
|
|
||
| class Runtime_Environment_Setup_Tests extends WP_UnitTestCase { | ||
|
|
||
| public function test_setup() { | ||
| global $wp_filesystem, $wpdb; | ||
|
|
||
| $this->set_up_mock_filesystem(); | ||
|
|
||
| $runtime_setup = new Runtime_Environment_Setup(); | ||
| $runtime_setup->setup(); | ||
|
|
||
| $this->assertTrue( 0 <= strpos( $wpdb->last_query, 'wppc_' ) ); | ||
| $this->assertTrue( $wp_filesystem->exists( WP_CONTENT_DIR . '/object-cache.php' ) ); | ||
| $this->assertSame( file_get_contents( WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'object-cache.copy.php' ), $wp_filesystem->get_contents( WP_CONTENT_DIR . '/object-cache.php' ) ); | ||
| } | ||
|
|
||
| public function test_cleanup() { | ||
| global $wp_filesystem, $wpdb; | ||
|
|
||
| $this->set_up_mock_filesystem(); | ||
|
|
||
| $runtime_setup = new Runtime_Environment_Setup(); | ||
| $runtime_setup->setup(); | ||
|
|
||
| // Simulate file exists by setting constant found in object-cache.php. | ||
| define( 'WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION', 1 ); | ||
|
|
||
| $runtime_setup->cleanup(); | ||
|
|
||
| $this->assertTrue( 0 <= strpos( $wpdb->last_query, 'wppc_' ) ); | ||
| $this->assertFalse( $wp_filesystem->exists( WP_CONTENT_DIR . '/object-cache.php' ) ); | ||
|
jjgrainger marked this conversation as resolved.
|
||
| } | ||
|
|
||
| public function test_setup_with_existing_object_cache() { | ||
| global $wp_filesystem, $wpdb; | ||
|
|
||
| $this->set_up_mock_filesystem(); | ||
|
|
||
| // Simulate a different object-cache.php. | ||
| $dummy_file_content = '<?php /* Empty object-cache.php drop-in file. */'; | ||
| $wp_filesystem->put_contents( WP_CONTENT_DIR . '/object-cache.php', $dummy_file_content ); | ||
|
|
||
| $runtime_setup = new Runtime_Environment_Setup(); | ||
| $runtime_setup->setup(); | ||
|
|
||
| $this->assertTrue( 0 <= strpos( $wpdb->last_query, 'wppc_' ) ); | ||
| $this->assertTrue( $wp_filesystem->exists( WP_CONTENT_DIR . '/object-cache.php' ) ); | ||
| $this->assertSame( $dummy_file_content, $wp_filesystem->get_contents( WP_CONTENT_DIR . '/object-cache.php' ) ); | ||
| } | ||
|
|
||
| public function test_cleanup_with_existing_object_cache() { | ||
| global $wp_filesystem, $wpdb; | ||
|
|
||
| $this->set_up_mock_filesystem(); | ||
|
|
||
| // Simulate a different object-cache.php. | ||
| $dummy_file_content = '<?php /* Empty object-cache.php drop-in file. */'; | ||
| $wp_filesystem->put_contents( WP_CONTENT_DIR . '/object-cache.php', $dummy_file_content ); | ||
|
|
||
| $runtime_setup = new Runtime_Environment_Setup(); | ||
| $runtime_setup->setup(); | ||
| $runtime_setup->cleanup(); | ||
|
|
||
| $this->assertTrue( 0 <= strpos( $wpdb->last_query, 'wppc_' ) ); | ||
| $this->assertTrue( $wp_filesystem->exists( WP_CONTENT_DIR . '/object-cache.php' ) ); | ||
| $this->assertSame( $dummy_file_content, $wp_filesystem->get_contents( WP_CONTENT_DIR . '/object-cache.php' ) ); | ||
| } | ||
|
|
||
| private function set_up_mock_filesystem() { | ||
| global $wp_filesystem; | ||
|
|
||
| add_filter( | ||
| 'filesystem_method_file', | ||
| function() { | ||
| return __DIR__ . '/../testdata/Filesystem/WP_Filesystem_MockFilesystem.php'; | ||
| } | ||
| ); | ||
| add_filter( | ||
| 'filesystem_method', | ||
| function() { | ||
| return 'MockFilesystem'; | ||
| } | ||
| ); | ||
|
|
||
| WP_Filesystem(); | ||
|
|
||
| // Simulate that the original object-cache.copy.php file exists. | ||
| $wp_filesystem->put_contents( WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'object-cache.copy.php', file_get_contents( WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'object-cache.copy.php' ) ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.