Skip to content

Commit 46ce957

Browse files
schlesseraclaude
andcommitted
Reuse plugins and themes downloaded from WordPress.org across scenarios
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b533a56 commit 46ce957

4 files changed

Lines changed: 194 additions & 7 deletions

File tree

‎composer.json‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
"wp-coding-standards/wpcs": "dev-develop",
3333
"yoast/phpunit-polyfills": "^2.0 || ^3.0 || ^4.0"
3434
},
35+
"require-dev": {
36+
"wp-cli/extension-command": "^1 || ^2 || ^3"
37+
},
3538
"config": {
3639
"allow-plugins": {
3740
"dealerdirect/phpcodesniffer-composer-installer": true,

‎features/behat-steps.feature‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,71 @@ 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: Install plugins and themes from WordPress.org
473+
Given a WP installation
474+
And these installed themes:
475+
"""
476+
twentysixteen
477+
"""
478+
And these installed and active plugins:
479+
"""
480+
wordpress-importer --version=0.8
481+
"""
482+
Then the wp-content/themes/twentysixteen/style.css file should exist
483+
484+
When I run `wp plugin get wordpress-importer --fields=status,version --format=csv`
485+
Then STDOUT should contain:
486+
"""
487+
status,active
488+
version,0.8
489+
"""
490+
491+
@require-wp
492+
Scenario: Install cached plugins and themes without requests to WordPress.org
493+
Given a WP installation
494+
And these installed themes:
495+
"""
496+
twentysixteen
497+
"""
498+
And these installed plugins:
499+
"""
500+
wordpress-importer --version=0.8
501+
"""
502+
And a non-existent wp-content/themes/twentysixteen directory
503+
And a non-existent wp-content/plugins/wordpress-importer directory
504+
And a wp-content/mu-plugins/block-http-requests.php file:
505+
"""
506+
<?php
507+
add_filter(
508+
'pre_http_request',
509+
static function () {
510+
return new WP_Error( 'http_request_blocked', 'HTTP requests are blocked.' );
511+
}
512+
);
513+
"""
514+
515+
When these installed and active themes:
516+
"""
517+
twentysixteen
518+
"""
519+
And these installed and active plugins:
520+
"""
521+
wordpress-importer --version=0.8
522+
"""
523+
And I run `wp theme list --status=active --field=name`
524+
Then STDOUT should be:
525+
"""
526+
twentysixteen
527+
"""
528+
529+
When I run `wp plugin get wordpress-importer --fields=status,version --format=csv`
530+
Then STDOUT should contain:
531+
"""
532+
status,active
533+
version,0.8
534+
"""
535+
471536
@require-wp
472537
Scenario: Test custom wp-content directory
473538
Given a WP installation

‎src/Context/FeatureContext.php‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1871,6 +1871,109 @@ public function download_wp( $subdir = '', $version = '' ): void {
18711871
}
18721872
}
18731873

1874+
/**
1875+
* Install plugins or themes, copying the ones from WordPress.org from a local cache when possible.
1876+
*
1877+
* @param string $type Either 'plugin' or 'theme'.
1878+
* @param array<string> $lines Slugs, optionally followed by `--version=<version>` and `--force`, or anything `wp plugin install` accepts.
1879+
* @param bool $activate Whether to activate them.
1880+
*/
1881+
public function install_extensions( $type, array $lines, $activate ): void {
1882+
$target_dir = $this->get_extension_dir( $type );
1883+
$slugs = [];
1884+
$uncached = [];
1885+
$others = [];
1886+
1887+
foreach ( $lines as $line ) {
1888+
if ( ! preg_match( '/^([a-z0-9][a-z0-9._-]*)(?:\s+--version=(\S+))?(\s+--force)?$/i', $line, $matches ) || null === $target_dir ) {
1889+
$others[] = $line;
1890+
continue;
1891+
}
1892+
1893+
$slug = $matches[1];
1894+
$flags = ( isset( $matches[2] ) && '' !== $matches[2] ? ' --version=' . escapeshellarg( $matches[2] ) : '' ) . ( empty( $matches[3] ) ? '' : ' --force' );
1895+
$slugs[] = $slug;
1896+
1897+
$cache_path = self::get_extension_cache_path( $type, $slug, $flags );
1898+
if ( is_dir( $cache_path ) ) {
1899+
self::remove_dir( "$target_dir/$slug" );
1900+
mkdir( "$target_dir/$slug" );
1901+
self::copy_dir( $cache_path, "$target_dir/$slug" );
1902+
} else {
1903+
$uncached[ $flags ][] = $slug;
1904+
}
1905+
}
1906+
1907+
foreach ( $uncached as $flags => $uncached_slugs ) {
1908+
$this->proc( "wp $type install " . implode( ' ', array_map( 'escapeshellarg', $uncached_slugs ) ) . $flags )->run_check();
1909+
1910+
foreach ( $uncached_slugs as $slug ) {
1911+
if ( is_dir( "$target_dir/$slug" ) ) {
1912+
self::store_in_extension_cache( "$target_dir/$slug", self::get_extension_cache_path( $type, $slug, (string) $flags ) );
1913+
}
1914+
}
1915+
}
1916+
1917+
if ( $others ) {
1918+
$this->proc( "wp $type install " . implode( ' ', $others ) . ( $activate ? ' --activate' : '' ) )->run_check();
1919+
}
1920+
1921+
if ( $activate && $slugs ) {
1922+
$this->proc( "wp $type activate " . implode( ' ', array_map( 'escapeshellarg', $slugs ) ) )->run_check();
1923+
}
1924+
}
1925+
1926+
/**
1927+
* Get the directory plugins or themes are installed into, or null if it is not known.
1928+
*
1929+
* @param string $type Either 'plugin' or 'theme'.
1930+
*/
1931+
private function get_extension_dir( $type ): ?string {
1932+
if ( ! isset( $this->variables['RUN_DIR'] ) ) {
1933+
return null;
1934+
}
1935+
1936+
$run_dir = $this->variables['RUN_DIR'];
1937+
$candidates = 'plugin' === $type
1938+
? [ "$run_dir/my-plugins", "$run_dir/wp-content/plugins", "$run_dir/WordPress/wp-content/plugins" ]
1939+
: [ "$run_dir/my-content/themes", "$run_dir/wp-content/themes", "$run_dir/WordPress/wp-content/themes" ];
1940+
1941+
foreach ( $candidates as $candidate ) {
1942+
if ( is_dir( $candidate ) ) {
1943+
return $candidate;
1944+
}
1945+
}
1946+
1947+
return null;
1948+
}
1949+
1950+
/**
1951+
* @param string $type
1952+
* @param string $slug
1953+
* @param string $flags Install flags that change what gets installed.
1954+
*/
1955+
private static function get_extension_cache_path( $type, $slug, $flags ): string {
1956+
$key = implode( ':', [ $type, $slug, $flags, (string) getenv( 'WP_VERSION' ), PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION ] );
1957+
1958+
return sys_get_temp_dir() . '/wp-cli-test-extension-cache/' . $type . '-' . $slug . '-' . substr( md5( $key ), 0, 12 );
1959+
}
1960+
1961+
/**
1962+
* @param string $source_dir
1963+
* @param string $cache_path
1964+
*/
1965+
private static function store_in_extension_cache( $source_dir, $cache_path ): void {
1966+
$temp_path = $cache_path . '-' . uniqid( '', true );
1967+
mkdir( $temp_path, 0777, true );
1968+
1969+
self::copy_dir( $source_dir, $temp_path );
1970+
1971+
// Another process may have filled the cache in the meantime.
1972+
if ( is_dir( $cache_path ) || ! rename( $temp_path, $cache_path ) ) {
1973+
self::remove_dir( $temp_path );
1974+
}
1975+
}
1976+
18741977
/**
18751978
* Create a wp-config.php file.
18761979
*

‎src/Context/GivenStepDefinitions.php‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,12 @@ public function given_a_wp_multisite_installation( $type = 'subdirectory' ): voi
405405
}
406406

407407
/**
408-
* Installs and activates one or more plugins.
408+
* Installs and optionally activates one or more plugins or themes.
409+
*
410+
* Plugins and themes from WordPress.org are downloaded once and copied from a local cache
411+
* afterwards. Each line holds a slug, optionally followed by `--version=<version>` and `--force`. Other
412+
* lines, like a URL or a path to a ZIP file, are installed with `wp plugin install` or
413+
* `wp theme install` every time.
409414
*
410415
* ```
411416
* Scenario: My example scenario
@@ -415,19 +420,30 @@ public function given_a_wp_multisite_installation( $type = 'subdirectory' ): voi
415420
* akismet
416421
* wordpress-importer
417422
* """
423+
*
424+
* Scenario: My other scenario
425+
* Given a WP installation
426+
* And these installed themes:
427+
* """
428+
* twentysixteen
429+
* twentyseventeen --version=1.0
430+
* """
418431
* ```
419432
*
420433
* @access public
421434
*
422-
* @Given these installed and active plugins:
435+
* @Given /^these installed (and active )?(plugins|themes):$/
423436
*
424-
* @param string $stream
437+
* @param string $active
438+
* @param string $type
439+
* @param PyStringNode $stream
425440
*/
426-
public function given_these_installed_and_active_plugins( $stream ): void {
427-
$plugins = implode( ' ', array_map( 'trim', explode( PHP_EOL, (string) $stream ) ) );
428-
$plugins = $this->replace_variables( $plugins );
441+
public function given_these_installed_plugins_or_themes( $active, $type, PyStringNode $stream ): void {
442+
$lines = array_filter( array_map( 'trim', explode( "\n", $this->replace_variables( (string) $stream ) ) ) );
443+
$type = 'plugins' === $type ? 'plugin' : 'theme';
444+
$activate = '' !== trim( $active );
429445

430-
$this->proc( "wp plugin install $plugins --activate" )->run_check();
446+
$this->install_extensions( $type, $lines, $activate );
431447
}
432448

433449
/**

0 commit comments

Comments
 (0)