@@ -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