Skip to content

Commit eed4164

Browse files
schlesseraclaude
andcommitted
Add a step to create several sites at once
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 537dfec commit eed4164

3 files changed

Lines changed: 62 additions & 1 deletion

File tree

‎composer.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"yoast/phpunit-polyfills": "^2.0 || ^3.0 || ^4.0"
3434
},
3535
"require-dev": {
36-
"roave/security-advisories": "dev-latest"
36+
"roave/security-advisories": "dev-latest",
37+
"wp-cli/entity-command": "^1 || ^2 || ^3"
3738
},
3839
"config": {
3940
"allow-plugins": {

‎features/behat-steps.feature‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,27 @@ Feature: Test that WP-CLI Behat steps work as expected
495495
'WP_CLI_TEST_CONSTANT', 'a value'
496496
"""
497497

498+
@require-wp
499+
Scenario: Create several sites at once
500+
Given a WP multisite installation
501+
And these sites:
502+
| slug | title | save |
503+
| first | First site | FIRST_ID |
504+
| second | | SECOND_ID |
505+
506+
When I run `wp site list --fields=blog_id,url --format=csv`
507+
Then STDOUT should contain:
508+
"""
509+
{FIRST_ID},https://example.com/first/
510+
{SECOND_ID},https://example.com/second/
511+
"""
512+
513+
When I run `wp eval 'echo get_blog_option( {FIRST_ID}, "blogname" ) . "|" . get_blog_option( {SECOND_ID}, "blogname" );'`
514+
Then STDOUT should be:
515+
"""
516+
First site|Second
517+
"""
518+
498519
@require-wp
499520
Scenario: Test custom wp-content directory
500521
Given a WP installation

‎src/Context/GivenStepDefinitions.php‎

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

433+
/**
434+
* Creates one or more sites on a multisite installation in a single process.
435+
*
436+
* The `slug` column is required. The optional `title` column sets the site title, and the
437+
* optional `save` column names a variable to save the new site's ID into.
438+
*
439+
* ```
440+
* Scenario: My example scenario
441+
* Given a WP multisite installation
442+
* And these sites:
443+
* | slug | title | save |
444+
* | first | First site | FIRST_ID |
445+
* | second | | SECOND_ID |
446+
* ```
447+
*
448+
* @access public
449+
*
450+
* @Given these sites:
451+
*
452+
* @param TableNode $table
453+
*/
454+
public function given_these_sites( TableNode $table ): void {
455+
$commands = [];
456+
foreach ( $table->getHash() as $row ) {
457+
if ( empty( $row['slug'] ) ) {
458+
throw new RuntimeException( 'The sites table needs a "slug" column.' );
459+
}
460+
461+
$command = 'wp site create --slug=' . $row['slug'] . ' --porcelain';
462+
if ( isset( $row['title'] ) && '' !== $row['title'] ) {
463+
$command .= ' --title="' . addcslashes( $row['title'], '"\\' ) . '"';
464+
}
465+
466+
$commands[] = [ $command, trim( $row['save'] ?? '', '{} ' ) ];
467+
}
468+
469+
$this->run_commands_in_single_process( $commands );
470+
}
471+
433472
/**
434473
* Runs several setup commands in a single process.
435474
*

0 commit comments

Comments
 (0)