Skip to content

Commit 23c6dc4

Browse files
authored
Merge pull request #406 from wp-cli/only-these-themes
Add a step for scenarios that need a specific set of themes
2 parents bd367f1 + b2edc3c commit 23c6dc4

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎features/behat-steps.feature‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,38 @@ Feature: Test that WP-CLI Behat steps work as expected
542542
And the wp-content/themes/oceanly-green/style.css file should exist
543543
And the wp-content/themes/oceanly/style.css file should exist
544544

545+
@require-wp
546+
Scenario: Keep only the listed themes
547+
Given a WP installation
548+
And only these installed themes:
549+
"""
550+
twentysixteen
551+
twentyseventeen
552+
"""
553+
554+
When I run `wp theme list --format=count`
555+
Then STDOUT should be:
556+
"""
557+
2
558+
"""
559+
And the wp-content/themes/twentysixteen/style.css file should exist
560+
And the wp-content/themes/twentyseventeen/style.css file should exist
561+
562+
@require-wp
563+
Scenario: Keep only the listed theme and activate it
564+
Given a WP installation
565+
And only these installed and active themes:
566+
"""
567+
twentysixteen
568+
"""
569+
570+
When I run `wp theme list --fields=name,status --format=csv`
571+
Then STDOUT should be:
572+
"""
573+
name,status
574+
twentysixteen,active
575+
"""
576+
545577
@require-wp
546578
Scenario: Test custom wp-content directory
547579
Given a WP installation

‎src/Context/FeatureContext.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,6 +1935,23 @@ public function install_extensions( $type, array $lines, $activate ): void {
19351935
}
19361936
}
19371937

1938+
/**
1939+
* Remove every installed theme, like `wp theme delete --all --force` does.
1940+
*/
1941+
public function remove_all_themes(): void {
1942+
$themes_dir = $this->get_extension_dir( 'theme' );
1943+
if ( null === $themes_dir ) {
1944+
throw new RuntimeException( 'Could not find the themes directory.' );
1945+
}
1946+
1947+
$entries = scandir( $themes_dir );
1948+
foreach ( false === $entries ? [] : $entries as $entry ) {
1949+
if ( '.' !== $entry[0] && is_dir( "$themes_dir/$entry" ) ) {
1950+
self::remove_dir( "$themes_dir/$entry" );
1951+
}
1952+
}
1953+
}
1954+
19381955
/**
19391956
* Get the directory plugins or themes are installed into, or null if it is not known.
19401957
*

‎src/Context/GivenStepDefinitions.php‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,37 @@ public function given_a_wp_multisite_installation( $type = 'subdirectory' ): voi
404404
)->run_check();
405405
}
406406

407+
/**
408+
* Removes all themes and installs, and optionally activates, the listed ones instead.
409+
*
410+
* Takes the same lines as `Given these installed themes:`. This replaces running
411+
* `wp theme delete --all --force` followed by one or more `wp theme install` commands.
412+
*
413+
* ```
414+
* Scenario: My example scenario
415+
* Given a WP installation
416+
* And only these installed themes:
417+
* """
418+
* twentysixteen
419+
* twentyseventeen
420+
* """
421+
* ```
422+
*
423+
* @access public
424+
*
425+
* @Given /^only these installed (themes|and active themes):$/
426+
*
427+
* @param string $kind Either 'themes' or 'and active themes'.
428+
* @param PyStringNode $stream
429+
*/
430+
public function given_only_these_installed_themes( $kind, PyStringNode $stream ): void {
431+
$lines = array_filter( array_map( 'trim', explode( "\n", $this->replace_variables( (string) $stream ) ) ) );
432+
$activate = 'and active themes' === $kind;
433+
434+
$this->remove_all_themes();
435+
$this->install_extensions( 'theme', $lines, $activate );
436+
}
437+
407438
/**
408439
* Installs and optionally activates one or more plugins or themes.
409440
*

0 commit comments

Comments
 (0)