Skip to content

Commit bd367f1

Browse files
authored
Merge pull request #404 from wp-cli/cached-extension-installs
Reuse plugins and themes downloaded from WordPress.org across scenarios
2 parents 05007cb + e013cfa commit bd367f1

4 files changed

Lines changed: 230 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: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,80 @@ 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+
oceanly-green
498+
"""
499+
And these installed plugins:
500+
"""
501+
wordpress-importer --version=0.8
502+
"""
503+
And a non-existent wp-content/themes/twentysixteen directory
504+
And a non-existent wp-content/themes/oceanly-green directory
505+
And a non-existent wp-content/themes/oceanly directory
506+
And a non-existent wp-content/plugins/wordpress-importer directory
507+
And a wp-content/mu-plugins/block-http-requests.php file:
508+
"""
509+
<?php
510+
add_filter(
511+
'pre_http_request',
512+
static function () {
513+
return new WP_Error( 'http_request_blocked', 'HTTP requests are blocked.' );
514+
}
515+
);
516+
"""
517+
518+
When these installed themes:
519+
"""
520+
oceanly-green
521+
"""
522+
And these installed and active themes:
523+
"""
524+
twentysixteen
525+
"""
526+
And these installed and active plugins:
527+
"""
528+
wordpress-importer --version=0.8
529+
"""
530+
And I run `wp theme list --status=active --field=name`
531+
Then STDOUT should be:
532+
"""
533+
twentysixteen
534+
"""
535+
536+
When I run `wp plugin get wordpress-importer --fields=status,version --format=csv`
537+
Then STDOUT should contain:
538+
"""
539+
status,active
540+
version,0.8
541+
"""
542+
And the wp-content/themes/oceanly-green/style.css file should exist
543+
And the wp-content/themes/oceanly/style.css file should exist
544+
471545
@require-wp
472546
Scenario: Test custom wp-content directory
473547
Given a WP installation

‎src/Context/FeatureContext.php‎

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

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

‎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)