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

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 wp-cli/committers as a code owner Mar 26, 2019

Jonathan Bardo
@danielbachhuber

This comment has been minimized.

Copy link
Member

commented Mar 26, 2019

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

This comment has been minimized.

Copy link
Contributor Author

commented Mar 26, 2019

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

@danielbachhuber
Copy link
Member

left a comment

Can you include some functional tests for this change?

Jonathan Bardo
@jonathanbardo

This comment has been minimized.

Copy link
Contributor Author

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

This comment has been minimized.

Copy link
Member

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

This comment has been minimized.

Copy link
Member

commented Apr 14, 2019

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

This comment has been minimized.

Copy link
Contributor Author

commented Apr 15, 2019

@schlessera Here we go, thanks for the feedback!

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

1 check was pending

continuous-integration/travis-ci/pr The Travis CI build is in progress
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants
You can’t perform that action at this time.