Skip to content

Commit 7d8b671

Browse files
committed
Add test cases for WP version resolver
1 parent 801b601 commit 7d8b671

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

tests/test-wp-version-resolver.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
use WP_CLI\Utils;
4+
use WP_CLI\Tests\TestCase;
5+
6+
class WPVersionResolverTest extends TestCase {
7+
8+
private $temp_file;
9+
10+
protected function set_up() {
11+
parent::set_up();
12+
13+
$this->temp_file = Utils\get_temp_dir() . 'wp-cli-tests-wp-versions.json';
14+
15+
$fp = fopen( $this->temp_file, 'w' );
16+
fwrite( $fp, json_encode( $this->wp_versions_data() ) );
17+
fclose( $fp );
18+
}
19+
20+
protected function tear_down() {
21+
if ( $this->temp_file && file_exists( $this->temp_file ) ) {
22+
unlink( $this->temp_file );
23+
}
24+
25+
parent::tear_down();
26+
}
27+
28+
private function wp_versions_data() {
29+
return array(
30+
'5.9' => 'insecure',
31+
'5.9.1' => 'insecure',
32+
'5.9.2' => 'insecure',
33+
'6.0' => 'insecure',
34+
'6.0.1' => 'insecure',
35+
'6.0.2' => 'insecure',
36+
'6.1' => 'insecure',
37+
'6.1.1' => 'insecure',
38+
'6.1.2' => 'insecure',
39+
'6.2' => 'insecure',
40+
'6.2.1' => 'insecure',
41+
'6.2.2' => 'insecure',
42+
'6.5' => 'insecure',
43+
'6.5.2' => 'latest',
44+
);
45+
}
46+
47+
private function data_wp_version_resolver() {
48+
return array(
49+
array( '5.0', '5.0' ), // Does not match any version. So return as it is.
50+
array( '5', '6.5.2' ), // Return the latest major version.
51+
array( '5.9', '5.9.2' ), // Return the latest patch version.
52+
array( '5.9.1', '5.9.1' ), // Return the exact version.
53+
array( '6', '6.5.2' ), // Return the latest minor version.
54+
array( '6.0', '6.0.2' ), // Return the latest patch version.
55+
array( '6.0.0', '6.0' ), // Return the requested version.
56+
array( '', '6.5.2' ), // Return the latest version.
57+
array( 'latest', '6.5.2' ), // Return the latest version.
58+
array( 'some-mismatched-version', 'some-mismatched-version' ), // Does not match any version. So return as it is.
59+
array( '6.5-alpha', '6.5.2' ), // Return the latest version.
60+
array( '6.5-beta', '6.5.2' ), // Return the latest version.
61+
array( '6.5-rc', '6.5.2' ), // Return the latest version.
62+
array( '6.5-nightly', '6.5-nightly' ), // Does not match any version. So return as it is.
63+
array( '6.5.0.0', '6.5' ), // Return the latest version.
64+
array( '6.5.2.0', '6.5.2' ), // Return the latest version.
65+
);
66+
}
67+
68+
/**
69+
* @dataProvider data_wp_version_resolver
70+
*/
71+
public function test_wp_version_resolver( $env, $expected ) {
72+
if ( $env ) {
73+
putenv( "WP_VERSION=$env" );
74+
}
75+
76+
$output = exec( 'php ' . dirname( __DIR__ ) . '/utils/wp-version-resolver.php' );
77+
78+
$this->assertEquals( $expected, $output );
79+
80+
// Reset the environment variable.
81+
putenv( 'WP_VERSION' );
82+
}
83+
}

0 commit comments

Comments
 (0)