Skip to content

Commit a316060

Browse files
schlesseraclaude
andcommitted
Add a step to set several options at once
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 9298472 commit a316060

2 files changed

Lines changed: 99 additions & 0 deletions

File tree

‎features/behat-steps.feature‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,44 @@ Feature: Test that WP-CLI Behat steps work as expected
468468
When I launch in the background `wp eval 'sleep(2); echo "done";'`
469469
# Background process should not block, continuing immediately
470470

471+
@require-wp
472+
Scenario: Set several options at once
473+
Given a WP installation
474+
And these options:
475+
| name | value | autoload | format |
476+
| uploads_use_yearmonth_folders | 0 | | |
477+
| blogdescription | Run in {RUN_DIR} | | |
478+
| wp_cli_test_array | {"a":1,"b":["c"]} | no | json |
479+
480+
When I run `wp eval 'echo get_option( "uploads_use_yearmonth_folders" ) . "|" . get_option( "blogdescription" );'`
481+
Then STDOUT should be:
482+
"""
483+
0|Run in {RUN_DIR}
484+
"""
485+
486+
When I run `wp eval 'echo json_encode( get_option( "wp_cli_test_array" ) );'`
487+
Then STDOUT should be:
488+
"""
489+
{"a":1,"b":["c"]}
490+
"""
491+
492+
When I run `wp eval 'global $wpdb; echo $wpdb->get_var( "SELECT autoload FROM $wpdb->options WHERE option_name = \"wp_cli_test_array\"" );'`
493+
Then STDOUT should match /^(no|off)$/
494+
495+
@require-wp
496+
Scenario: Set several network options at once
497+
Given a WP multisite installation
498+
And these site options:
499+
| name | value |
500+
| wp_cli_test_option | first |
501+
| registration | user |
502+
503+
When I run `wp eval 'echo get_site_option( "wp_cli_test_option" ) . "|" . get_site_option( "registration" );'`
504+
Then STDOUT should be:
505+
"""
506+
first|user
507+
"""
508+
471509
@require-wp
472510
Scenario: Test custom wp-content directory
473511
Given a WP installation

‎src/Context/GivenStepDefinitions.php‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,67 @@ public function given_these_installed_and_active_plugins( $stream ): void {
430430
$this->proc( "wp plugin install $plugins --activate" )->run_check();
431431
}
432432

433+
/**
434+
* Sets one or more options, or network options, in a single process.
435+
*
436+
* Columns are `name` and `value`, plus optionally `autoload` (`yes` or `no`) and `format` (`json` to decode the value).
437+
*
438+
* ```
439+
* Scenario: My example scenario
440+
* Given a WP installation
441+
* And these options:
442+
* | name | value |
443+
* | uploads_use_yearmonth_folders | 0 |
444+
* | blog_public | 1 |
445+
*
446+
* Scenario: My multisite scenario
447+
* Given a WP multisite installation
448+
* And these site options:
449+
* | name | value | format |
450+
* | illegal_names | ["www","admin","files"] | json |
451+
* ```
452+
*
453+
* @access public
454+
*
455+
* @Given /^these (options|site options):$/
456+
*
457+
* @param string $type Either 'options' or 'site options'.
458+
* @param TableNode $table
459+
*/
460+
public function given_these_options( $type, TableNode $table ): void {
461+
$options = [];
462+
foreach ( $table->getHash() as $row ) {
463+
if ( ! isset( $row['name'], $row['value'] ) ) {
464+
throw new RuntimeException( 'The options table needs "name" and "value" columns.' );
465+
}
466+
467+
$value = $this->replace_variables( $row['value'] );
468+
if ( 'json' === ( $row['format'] ?? '' ) ) {
469+
$value = json_decode( $value, true );
470+
if ( JSON_ERROR_NONE !== json_last_error() ) {
471+
throw new RuntimeException( "Invalid JSON value for option '{$row['name']}'." );
472+
}
473+
}
474+
475+
$autoload = null;
476+
if ( isset( $row['autoload'] ) && '' !== $row['autoload'] ) {
477+
$autoload = in_array( strtolower( $row['autoload'] ), [ 'yes', 'on', 'true', '1' ], true );
478+
}
479+
480+
$options[] = [ $this->replace_variables( $row['name'] ), $value, $autoload ];
481+
}
482+
483+
$function = 'site options' === $type ? 'update_site_option( $option[0], $option[1] )' : 'update_option( $option[0], $option[1], $option[2] )';
484+
$file = $this->variables['RUN_DIR'] . '/' . uniqid( 'wp-cli-test-options-', true ) . '.php';
485+
file_put_contents( $file, '<?php foreach ( ' . var_export( $options, true ) . " as \$option ) { $function; }\n" );
486+
487+
try {
488+
$this->proc( Utils\esc_cmd( 'wp eval-file %s', $file ) )->run_check();
489+
} finally {
490+
unlink( $file );
491+
}
492+
}
493+
433494
/**
434495
* Configure a custom `wp-content` directory.
435496
*

0 commit comments

Comments
 (0)