-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathGivenStepDefinitions.php
More file actions
815 lines (747 loc) · 19.9 KB
/
GivenStepDefinitions.php
File metadata and controls
815 lines (747 loc) · 19.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
<?php
namespace WP_CLI\Tests\Context;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use RuntimeException;
use WP_CLI\Utils;
use WP_CLI\Path;
trait GivenStepDefinitions {
/**
* Creates an empty directory.
*
* ```
* Scenario: My example scenario
* Given an empty directory
* ...
* ```
*
* @access public
*
* @Given an empty directory
*/
public function given_an_empty_directory(): void {
$this->create_run_dir();
}
/**
* Creates or deletes a specific directory.
*
* ```
* Scenario: My example scenario
* Given an empty foo-plugin directory
* And a non-existent bar-plugin directory
* ...
* ```
*
* @access public
*
* @Given /^an? (empty|non-existent) ([^\s]+) directory$/
*
* @param string $empty_or_nonexistent
* @param string $dir
*/
public function given_a_specific_directory( $empty_or_nonexistent, $dir ): void {
$dir = $this->replace_variables( $dir );
if ( ! Path::is_absolute( $dir ) ) {
$dir = $this->variables['RUN_DIR'] . "/$dir";
}
// Mac OS X can prefix the `/var` folder to turn it into `/private/var`.
$dir = preg_replace( '|^/private/var/|', '/var/', $dir );
$temp_dir = sys_get_temp_dir();
// Also check for temp dir prefixed with `/private` for Mac OS X.
if ( 0 !== strpos( $dir, $temp_dir ) && 0 !== strpos( $dir, "/private{$temp_dir}" ) ) {
throw new RuntimeException(
sprintf(
"Attempted to delete directory '%s' that is not in the temp directory '%s'. " . __FILE__ . ':' . __LINE__,
$dir,
$temp_dir
)
);
}
self::remove_dir( $dir );
if ( 'empty' === $empty_or_nonexistent ) {
mkdir( $dir, 0777, true /*recursive*/ );
}
}
/**
* Clears the WP-CLI cache directory.
*
* ```
* Scenario: My example scenario
* Given an empty cache
* ...
* ```
*
* @access public
*
* @Given an empty cache
*/
public function given_an_empty_cache(): void {
$this->variables['SUITE_CACHE_DIR'] = FeatureContext::create_cache_dir();
}
/**
* Creates a file with the given contents.
*
* The file can be created either in the current working directory
* or in the cache directory.
*
* ```
* Scenario: My example scenario
* Given a wp-cli.yml file:
* """
* @foo:
* path: foo
* user: admin
* """
* ```
*
* @access public
*
* @Given /^an? ([^\s]+) (file|cache file):$/
*
* @param string $path
* @param string $type
* @param PyStringNode $content
*/
public function given_a_specific_file( $path, $type, PyStringNode $content ): void {
$path = $this->replace_variables( (string) $path );
$content = $this->replace_variables( (string) $content ) . "\n";
$full_path = 'cache file' === $type
? $this->variables['SUITE_CACHE_DIR'] . "/$path"
: $this->variables['RUN_DIR'] . "/$path";
$dir = dirname( $full_path );
if ( ! file_exists( $dir ) ) {
mkdir( $dir, 0777, true /*recursive*/ );
}
file_put_contents( $full_path, $content );
}
/**
* Search and replace a string in a file using regex.
*
* ```
* Scenario: My example scenario
* Given "Foo" replaced with "Bar" in the readme.html file
* ...
* ```
*
* @access public
*
* @Given /^"([^"]+)" replaced with "([^"]+)" in the ([^\s]+) file$/
*
* @param string $search
* @param string $replace
* @param string $path
*/
public function given_string_replaced_with_string_in_a_specific_file( $search, $replace, $path ): void {
$full_path = $this->variables['RUN_DIR'] . "/$path";
$contents = file_get_contents( $full_path );
$contents = str_replace( $search, $replace, $contents );
file_put_contents( $full_path, $contents );
}
/**
* Mock HTTP requests to a given URL.
*
* ```
* Scenario: My example scenario
* Given that HTTP requests to https://api.github.com/repos/wp-cli/wp-cli/releases?per_page=100 will respond with:
* """
* HTTP/1.1 200
* Content-Type: application/json
*
* { "foo": "bar" }
* """
* ```
*
* @access public
*
* @Given /^that HTTP requests to (.*?) will respond with:$/
*
* @param string $url_or_pattern
* @param PyStringNode $content
*/
public function given_a_request_to_a_url_respond_with_file( $url_or_pattern, PyStringNode $content ): void {
if ( ! isset( $this->variables['RUN_DIR'] ) ) {
$this->create_run_dir();
}
$config_file = $this->variables['RUN_DIR'] . '/wp-cli.yml';
$mock_file = $this->variables['RUN_DIR'] . '/mock-requests.php';
$dir = dirname( $config_file );
if ( ! file_exists( $dir ) ) {
mkdir( $dir, 0777, true /*recursive*/ );
}
$config_file_contents = <<<'FILE'
require:
- mock-requests.php
FILE;
file_put_contents(
$config_file,
$config_file_contents
);
$this->mocked_requests[ $url_or_pattern ] = (string) $content;
$mocked_requests = var_export( $this->mocked_requests, true /* return */ );
$mock_file_contents = <<<FILE
<?php
/**
* HTTP request mocking supporting both Requests v1 and v2.
*/
trait WP_CLI_Tests_Mock_Requests_Trait {
public function request( \$url, \$headers = array(), \$data = array(), \$options = array() ) {
\$mocked_requests = $mocked_requests;
foreach ( \$mocked_requests as \$pattern => \$response ) {
\$pattern = '/' . preg_quote( \$pattern, '/' ) . '/';
if ( 1 === preg_match( \$pattern, \$url ) ) {
\$pos = strpos( \$response, "\\n\\n");
if ( false !== \$pos ) {
\$response = substr( \$response, 0, \$pos ) . "\\r\\n\\r\\n" . substr( \$response, \$pos + 2 );
}
return \$response;
}
}
if ( class_exists( '\WpOrg\Requests\Transport\Curl' ) ) {
return ( new \WpOrg\Requests\Transport\Curl() )->request( \$url, \$headers, \$data, \$options );
}
return ( new \Requests_Transport_cURL() )->request( \$url, \$headers, \$data, \$options );
}
public function request_multiple( \$requests, \$options ) {
throw new Exception( 'Method not implemented: ' . __METHOD__ );
}
public static function test( \$capabilities = array() ) {
return true;
}
}
if ( interface_exists( '\WpOrg\Requests\Transport' ) ) {
class WP_CLI_Tests_Mock_Requests_Transport implements \WpOrg\Requests\Transport {
use WP_CLI_Tests_Mock_Requests_Trait;
}
} else {
class WP_CLI_Tests_Mock_Requests_Transport implements \Requests_Transport {
use WP_CLI_Tests_Mock_Requests_Trait;
}
}
WP_CLI::add_hook(
'http_request_options',
static function( \$options ) {
\$options['transport'] = new WP_CLI_Tests_Mock_Requests_Transport();
return \$options;
}
);
WP_CLI::add_wp_hook(
'pre_http_request',
static function( \$pre, \$parsed_args, \$url ) {
\$mocked_requests = $mocked_requests;
foreach ( \$mocked_requests as \$pattern => \$response ) {
\$pattern = '/' . preg_quote( \$pattern, '/' ) . '/';
if ( 1 === preg_match( \$pattern, \$url ) ) {
\$pos = strpos( \$response, "\n\n");
if ( false !== \$pos ) {
\$response = substr( \$response, 0, \$pos ) . "\r\n\r\n" . substr( \$response, \$pos + 2 );
}
if ( class_exists( '\WpOrg\Requests\Requests' ) ) {
WpOrg\Requests\Requests::parse_multiple(
\$response,
array(
'url' => \$url,
'headers' => array(),
'data' => array(),
'options' => array_merge(
WpOrg\Requests\Requests::OPTION_DEFAULTS,
array(
'hooks' => new WpOrg\Requests\Hooks(),
)
),
)
);
} else {
\Requests::parse_multiple(
\$response,
array(
'url' => \$url,
'headers' => array(),
'data' => array(),
'options' => array(
'blocking' => true,
'filename' => false,
'follow_redirects' => true,
'redirected' => 0,
'redirects' => 10,
'hooks' => new Requests_Hooks(),
),
)
);
}
return array(
'headers' => \$response->headers->getAll(),
'body' => \$response->body,
'response' => array(
'code' => \$response->status_code,
'message' => get_status_header_desc( \$response->status_code ),
),
'cookies' => array(),
'filename' => '',
);
}
}
return \$pre;
},
10,
3
);
FILE;
file_put_contents(
$mock_file,
$mock_file_contents
);
}
/**
* Download WordPress files without installing.
*
* ```
* Scenario: My example scenario
* Given an empty directory
* And WP files
* ```
*
* @access public
*
* @Given WP files
*/
public function given_wp_files(): void {
$this->download_wp();
}
/**
* Create a wp-config.php file using `wp config create`.
*
* ```
* Scenario: My example scenario
* Given an empty directory
* And WP files
* And wp-config.php
* ```
*
* @access public
*
* @Given wp-config.php
*/
public function given_wp_config_php(): void {
$this->create_config();
}
/**
* Creates an empty database.
*
* Has no effect when tests run with SQLite.
*
* ```
* Scenario: My example scenario
* Given a database
* ...
* ```
*
* @access public
*
* @Given a database
*/
public function given_a_database(): void {
$this->create_db();
}
/**
* Installs WordPress.
*
* ```
* Scenario: My example scenario
* Given a WP installation
* ...
*
* Scenario: My other scenario
* Given a WP install
* ...
*
* Scenario: My version-specific scenario
* Given a WP 6.4.2 installation
* ...
* ```
*
* @access public
*
* @Given /^a WP( \d[^\s]+)? install(?:ation)?$/
*
* @param string $version Optional version number (may include leading space)
*/
public function given_a_wp_installation( $version = '' ): void {
$this->install_wp( '', trim( $version ) );
}
/**
* Installs WordPress in a given directory.
*
* ```
* Scenario: My example scenario
* Given a WP installation in 'foo'
* ...
*
* Scenario: My other scenario
* Given a WP install in 'bar'
* ...
*
* Scenario: My version-specific scenario
* Given a WP 6.4.2 installation in 'subdir'
* ...
* ```
*
* @access public
*
* @Given /^a WP( [^\s]+)? install(?:ation)? in ['"]?([^'"]+)['"]?$/
*
* @param string $version Optional version number (may include leading space)
* @param string $subdir
*/
public function given_a_wp_installation_in_a_specific_folder( $version = '', $subdir = '' ): void {
$this->install_wp( $subdir, trim( $version ) );
}
/**
* Installs WordPress with Composer.
*
* ```
* Scenario: My example scenario
* Given a WP installation with Composer
* ...
*
* Scenario: My other scenario
* Given a WP install with Composer
* ...
* ```
*
* @access public
*
* @Given a WP install(ation) with Composer
*/
public function given_a_wp_installation_with_composer(): void {
$this->install_wp_with_composer();
}
/**
* Installs WordPress with Composer and a custom vendor directory.
*
* ```
* Scenario: My example scenario
* Given a WP installation with Composer and a custom vendor directory 'vendor-custom'
* ...
*
* Scenario: My other scenario
* Given a WP install with Composer with Composer and a custom vendor directory 'vendor-custom'
* ...
* ```
*
* @access public
*
* @Given a WP install(ation) with Composer and a custom vendor directory :vendor_directory
*
* @param string $vendor_directory
*/
public function given_a_wp_installation_with_composer_and_a_custom_vendor_folder( $vendor_directory ): void {
$this->install_wp_with_composer( $vendor_directory );
}
/**
* Installs WordPress Multisite.
*
* Supports either subdirectory or subdomain installation.
*
* ```
* Scenario: My example scenario
* Given a WP multisite subdomain installation
* ...
*
* Scenario: My other scenario
* Given a WP subdirectory install
* ...
* ```
*
* @access public
*
* @Given /^a WP multisite (subdirectory|subdomain)?\s?(install|installation)$/
*
* @param string $type Multisite installation type.
*/
public function given_a_wp_multisite_installation( $type = 'subdirectory' ): void {
$this->install_wp();
$subdomains = ! empty( $type ) && 'subdomain' === $type ? 1 : 0;
$this->proc(
'wp core install-network',
array(
'title' => 'WP CLI Network',
'subdomains' => $subdomains,
)
)->run_check();
}
/**
* Installs and activates one or more plugins.
*
* ```
* Scenario: My example scenario
* Given a WP installation
* And these installed and active plugins:
* """
* akismet
* wordpress-importer
* """
* ```
*
* @access public
*
* @Given these installed and active plugins:
*
* @param string $stream
*/
public function given_these_installed_and_active_plugins( $stream ): void {
$plugins = implode( ' ', array_map( 'trim', explode( PHP_EOL, (string) $stream ) ) );
$plugins = $this->replace_variables( $plugins );
$this->proc( "wp plugin install $plugins --activate" )->run_check();
}
/**
* Configure a custom `wp-content` directory.
*
* Defines the `WP_CONTENT_DIR`, `WP_PLUGIN_DIR`, and `WPMU_PLUGIN_DIR` constants.
*
* ```
* Scenario: My example scenario
* Given a WP install
* And a custom wp-content directory
* ```
*
* @access public
*
* @Given a custom wp-content directory
*/
public function given_a_custom_wp_directory(): void {
$wp_config_path = $this->variables['RUN_DIR'] . '/wp-config.php';
$wp_config_code = file_get_contents( $wp_config_path );
$this->move_files( 'wp-content', 'my-content' );
$this->add_line_to_wp_config(
$wp_config_code,
"define( 'WP_CONTENT_DIR', dirname(__FILE__) . '/my-content' );"
);
$this->move_files( 'my-content/plugins', 'my-plugins' );
$this->add_line_to_wp_config(
$wp_config_code,
"define( 'WP_PLUGIN_DIR', __DIR__ . '/my-plugins' );"
);
$this->move_files( 'my-content/mu-plugins', 'my-mu-plugins' );
$this->add_line_to_wp_config(
$wp_config_code,
"define( 'WPMU_PLUGIN_DIR', __DIR__ . '/my-mu-plugins' );"
);
file_put_contents( $wp_config_path, $wp_config_code );
if ( 'sqlite' === self::$db_type ) {
$db_dropin = $this->variables['RUN_DIR'] . '/my-content/db.php';
/* similar to https://github.com/WordPress/sqlite-database-integration/blob/3306576c9b606bc23bbb26c15383fef08e03ab11/activate.php#L95 */
$file_contents = str_replace(
'mu-plugins/',
'../my-mu-plugins/',
file_get_contents( $db_dropin )
);
file_put_contents( $db_dropin, $file_contents );
}
}
/**
* Download multiple files into the given destinations.
*
* ```
* Scenario: My example scenario
* Given download:
* | path | url |
* | {CACHE_DIR}/foo.jpg | https://example.com/foo.jpg |
* | {CACHE_DIR}/bar.png | https://example.com/another-image.png |
* ```
*
* @access public
*
* @Given download:
*/
public function given_a_download( TableNode $table ): void {
foreach ( $table->getHash() as $row ) {
$path = $this->replace_variables( $row['path'] );
if ( file_exists( $path ) ) {
// Assume it's the same file and skip re-download.
continue;
}
$response = Utils\http_request( 'GET', $row['url'], null, [], [ 'filename' => $path ] );
if ( 200 !== $response->status_code ) {
throw new RuntimeException( "Could not download file (HTTP code {$response->status_code})" );
}
}
}
/**
* Store STDOUT or STDERR contents in a variable.
*
* ```
* Scenario: My example scenario
* When I run `wp package path`
* Then save STDOUT as {PACKAGE_PATH}
*
* Scenario: My other scenario
* When I run `wp core download`
* Then save STDOUT 'Downloading WordPress ([\d\.]+)' as {VERSION}
* ```
*
* @access public
*
* @Given /^save (STDOUT|STDERR) ([\'].+[^\'])?\s?as \{(\w+)\}$/
*
* @param string $stream
* @param string $output_filter
* @param string $key
*/
public function given_saved_stdout_stderr( $stream, $output_filter, $key ): void {
$stream = strtolower( $stream );
if ( $output_filter ) {
$output_filter = '/' . trim( str_replace( '%s', '(.+[^\b])', $output_filter ), "' " ) . '/';
if ( false !== preg_match( $output_filter, $this->result->$stream, $matches ) ) {
$output = array_pop( $matches );
} else {
$output = '';
}
} else {
$output = $this->result->$stream;
}
$this->variables[ $key ] = trim( $output, "\n" );
}
/**
* Build a new WP-CLI Phar file with a given version.
*
* ```
* Scenario: My example scenario
* Given an empty directory
* And a new Phar with version "2.11.0"
* ```
*
* @access public
*
* @Given /^a new Phar with (?:the same version|version "([^"]+)")$/
*
* @param string $version
*/
public function given_a_new_phar_with_a_specific_version( $version = 'same' ): void {
$this->build_phar( $version );
}
/**
* Download a specific WP-CLI Phar version from GitHub.
*
* ```
* Scenario: My example scenario
* Given an empty directory
* And a downloaded Phar with version "2.11.0"
*
* Scenario: My other scenario
* Given an empty directory
* And a downloaded Phar with the same version
* ```
*
* @access public
*
* @Given /^a downloaded Phar with (?:the same version|version "([^"]+)")$/
*
* @param string $version
*/
public function given_a_downloaded_phar_with_a_specific_version( $version = 'same' ): void {
$this->download_phar( $version );
}
/**
* Stores the contents of the given file in a variable.
*
* ```
* Scenario: My example scenario
* Given a WP installation with Composer
* And save the {RUN_DIR}/composer.json file as {COMPOSER_JSON}
* ```
*
* @access public
*
* @Given /^save the (.+) file ([\'].+[^\'])?as \{(\w+)\}$/
*
* @param string $filepath
* @param string $output_filter
* @param string $key
*/
public function given_saved_a_specific_file( $filepath, $output_filter, $key ): void {
$full_file = file_get_contents( $this->replace_variables( $filepath ) );
if ( $output_filter ) {
$output_filter = '/' . trim( str_replace( '%s', '(.+[^\b])', $output_filter ), "' " ) . '/';
if ( false !== preg_match( $output_filter, $full_file, $matches ) ) {
$output = array_pop( $matches );
} else {
$output = '';
}
} else {
$output = $full_file;
}
$this->variables[ $key ] = trim( $output, "\n" );
}
/**
* Modify wp-config.php to set `WP_CONTENT_DIR` to an empty string.
*
* ```
* Scenario: My example scenario
* Given a WP install
* And a misconfigured WP_CONTENT_DIR constant directory
* ```
*
* @access public
*
* @Given a misconfigured WP_CONTENT_DIR constant directory
*/
public function given_a_misconfigured_wp_content_dir_constant_directory(): void {
$wp_config_path = $this->variables['RUN_DIR'] . '/wp-config.php';
$wp_config_code = file_get_contents( $wp_config_path );
$this->add_line_to_wp_config(
$wp_config_code,
"define( 'WP_CONTENT_DIR', '' );"
);
file_put_contents( $wp_config_path, $wp_config_code );
}
/**
* Add `wp-cli/wp-cli` as a Composer dependency.
*
* ```
* Scenario: My example scenario
* Given a WP installation with Composer
* And a dependency on current wp-cli
* ```
*
* @access public
*
* @Given a dependency on current wp-cli
*/
public function given_a_dependency_on_wp_cli(): void {
$this->composer_require_current_wp_cli();
}
/**
* Start a PHP built-in web server in the current directory.
*
* ```
* Scenario: My example scenario
* Given a WP installation
* And a PHP built-in web server
* ```
*
* @access public
*
* @Given a PHP built-in web server
*/
public function given_a_php_built_in_web_server(): void {
$this->start_php_server();
}
/**
* Start a PHP built-in web server in the given subdirectory.
*
* ```
* Scenario: My example scenario
* Given a WP installation
* And a PHP built-in web server to serve 'WordPress'
* ```
*
* @access public
*
* @Given a PHP built-in web server to serve :subdir
*
* @param string $subdir
*/
public function given_a_php_built_in_web_server_to_serve_a_specific_folder( $subdir ): void {
$this->start_php_server( $subdir );
}
}