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

Introduce ability to override wp-config.php path with env var WP_CONFIG_PATH #5119

Merged
merged 5 commits into from
Apr 16, 2019

Conversation

jonathanbardo
Copy link
Contributor

@jonathanbardo jonathanbardo commented Mar 26, 2019

Hi all!

I would like to propose to add the ability to provide our own config override path based on an environment variable (WP_CONFIG_PATH). This would solve a lot of headaches for managed hosts that rely on wp-cli but can't trust the wp-config that the user can play with. (For example, when the WordPress site loads fine on the server but somehow the wp-config.php custom php code prevents wp-cli from loading correctly).

This can also provide a solution for people who want to use different configs for the WP-CLI without having to resort to conditionals inside of the config itself.

Note: I see there are no unit tests for this function already. I can introduce one if we feel like there is a need for it.

Fixes #5120

@jonathanbardo jonathanbardo requested a review from a team as a code owner March 26, 2019 19:15
@danielbachhuber
Copy link
Member

I would like to propose to add the ability to provide our own config override path based on an environment variable (WP_CONFIG_PATH).

I feel like this might have unintended consequences... Can you open an issue to discuss the idea at depth before we proceed with a proposed solution?

@jonathanbardo
Copy link
Contributor Author

@danielbachhuber Sure thing. Created an issue here: #5120

Copy link
Member

@danielbachhuber danielbachhuber left a comment

Choose a reason for hiding this comment

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

Can you include some functional tests for this change?

@jonathanbardo
Copy link
Contributor Author

jonathanbardo commented Apr 8, 2019

@danielbachhuber Not too familiar with Gherkins but let me know if what I came up with works. This does not seem to work locally on mac.

I think I'll have to add something like:

if ( false !== strpos( $str, '{WP_RUN_DIR}' ) ) {
  $str = str_replace( '{WP_RUN_DIR}', preg_replace( '|^/private/var/|', '/var/', $this->variables[ 'RUN_DIR' ] ));
}

to this function and use the new var instead of RUN_DIR https://github.com/wp-cli/wp-cli-tests/blob/master/features/bootstrap/FeatureContext.php#L447

features/wp-config.feature Outdated Show resolved Hide resolved
features/wp-config.feature Outdated Show resolved Hide resolved
features/wp-config.feature Outdated Show resolved Hide resolved
features/wp-config.feature Outdated Show resolved Hide resolved
features/wp-config.feature Outdated Show resolved Hide resolved
@schlessera
Copy link
Member

schlessera commented Apr 14, 2019

@jonathanbardo The general approach for the test is fine. I added some further observations to the change request above.

features/wp-config.feature Outdated Show resolved Hide resolved
@schlessera
Copy link
Member

Here's the Behat test I propose:

Feature: wp-config

  Scenario: Default WordPress install with WP_CONFIG_PATH specified in environment variable
    Given a WP installation
    And a wp-config-override.php file:
      """
      <?php
      define('DB_NAME', 'wp_cli_test');
      define('DB_USER', 'wp_cli_test');
      define('DB_PASSWORD', 'password1');
      define('DB_HOST', '127.0.0.1');
      define('DB_CHARSET', 'utf8');
      define('DB_COLLATE', '');
      $table_prefix = 'wp_';

      // Provide custom define in override only that we can test against
      define('SOME_VARIABLE', 'some_value');

      if ( !defined('ABSPATH') )
        define('ABSPATH', dirname(__FILE__) . '/');
      require_once(ABSPATH . 'wp-settings.php');
      """

    When I try `wp eval "echo 'SOME_VARIABLE => ' . SOME_VARIABLE;"`
    Then STDOUT should contain:
      """
      SOME_VARIABLE => SOME_VARIABLE
      """
    And STDERR should contain:
      """
      Use of undefined constant SOME_VARIABLE
      """

    When I run `WP_CONFIG_PATH=wp-config-override.php wp eval "echo 'SOME_VARIABLE => ' . SOME_VARIABLE;"`
    Then STDERR should be empty
    And STDOUT should contain:
      """
      SOME_VARIABLE => some_value
      """

@jonathanbardo
Copy link
Contributor Author

@schlessera Here we go, thanks for the feedback!

@schlessera schlessera dismissed danielbachhuber’s stale review April 16, 2019 10:46

Functional test was added as requested

@schlessera schlessera added this to the 2.2.0 milestone Apr 16, 2019
@schlessera schlessera merged commit 978ab0a into wp-cli:master Apr 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Proposal: Have the ability to override the wp-config.php path
3 participants