forked from wp-cli/wp-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.feature
More file actions
195 lines (166 loc) · 5.67 KB
/
context.feature
File metadata and controls
195 lines (166 loc) · 5.67 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
Feature: Context handling via --context global flag
Scenario: CLI context can be selected, but is same as default
Given a WP install
When I run `wp eval 'var_export( is_admin() );'`
Then the return code should be 0
And STDOUT should be:
"""
false
"""
When I run `wp --context=cli eval 'var_export( is_admin() );'`
Then the return code should be 0
And STDOUT should be:
"""
false
"""
When I run `wp eval 'var_export( function_exists( "media_handle_upload" ) );'`
Then the return code should be 0
And STDOUT should be:
"""
true
"""
When I run `wp --context=cli eval 'var_export( function_exists( "media_handle_upload" ) );'`
Then the return code should be 0
And STDOUT should be:
"""
true
"""
When I run `wp eval 'add_action( "admin_init", static function () { WP_CLI::warning( "admin_init was triggered." ); } );'`
Then the return code should be 0
And STDERR should not contain:
"""
admin_init was triggered.
"""
When I run `wp --context=cli eval 'add_action( "admin_init", static function () { WP_CLI::warning( "admin_init was triggered." ); } );'`
Then the return code should be 0
And STDERR should not contain:
"""
admin_init was triggered.
"""
Scenario: Admin context can be selected
Given a WP install
When I run `wp --context=admin eval 'var_export( is_admin() );'`
Then the return code should be 0
And STDOUT should be:
"""
true
"""
When I run `wp --context=admin eval 'var_export( function_exists( "media_handle_upload" ) );'`
Then the return code should be 0
And STDOUT should be:
"""
true
"""
When I run `wp eval --context=admin 'add_action( "admin_init", static function () { WP_CLI::warning( "admin_init was triggered." ); } );'`
Then the return code should be 0
And STDERR should not contain:
"""
admin_init was triggered.
"""
Scenario: Frontend context can be selected (and does nothing yet...)
Given a WP install
When I run `wp --context=frontend eval 'var_export( is_admin() );'`
Then the return code should be 0
And STDOUT should be:
"""
false
"""
When I run `wp --context=frontend eval 'var_export( function_exists( "media_handle_upload" ) );'`
Then the return code should be 0
And STDOUT should be:
"""
true
"""
When I run `wp --context=frontend eval 'add_action( "admin_init", static function () { WP_CLI::warning( "admin_init was triggered." ); } );'`
Then the return code should be 0
And STDERR should not contain:
"""
admin_init was triggered.
"""
Scenario: Auto context can be selected and changes environment based on command
Given a WP install
And a context-logger.php file:
"""
<?php
WP_CLI::add_hook( 'before_run_command', static function () {
$context = WP_CLI::get_runner()->context_manager->get_context();
WP_CLI::log( "Current context: {$context}" );
} );
"""
When I run `wp --require=context-logger.php --context=auto post list`
Then the return code should be 0
And STDOUT should contain:
"""
Current context: cli
"""
When I run `wp --require=context-logger.php --context=auto plugin list`
Then the return code should be 0
And STDOUT should contain:
"""
Current context: admin
"""
Scenario: Unknown contexts throw an exception
Given a WP install
When I try `wp --context=nonsense post list`
Then the return code should be 1
And STDOUT should be empty
And STDERR should contain:
"""
Error: Unknown context 'nonsense'
"""
Scenario: Bundled contexts can be filtered
Given a WP install
And a custom-contexts.php file:
"""
<?php
final class OverriddenAdminContext implements \WP_CLI\Context {
public function process( $config ) {
\WP_CLI::log( 'admin context was overridden' );
}
}
final class CustomContext implements \WP_CLI\Context {
public function process( $config ) {
\WP_CLI::log( 'custom context was added' );
}
}
WP_CLI::add_hook( 'before_registering_contexts', static function ( $contexts ) {
unset( $contexts['frontend'] );
$contexts['admin'] = new OverriddenAdminContext();
$contexts['custom_context'] = new CustomContext();
return $contexts;
} );
"""
When I try `wp --require=custom-contexts.php --context=frontend post list`
Then the return code should be 1
And STDOUT should be empty
And STDERR should contain:
"""
Error: Unknown context 'frontend'
"""
When I run `wp --require=custom-contexts.php --context=admin post list`
Then the return code should be 0
And STDOUT should contain:
"""
admin context was overridden
"""
When I run `wp --require=custom-contexts.php --context=custom_context post list`
Then the return code should be 0
And STDOUT should contain:
"""
custom context was added
"""
Scenario: Core wp-admin/admin.php with CRLF lines does not fail.
Given a WP install
And a modify-wp-admin.php file:
"""
<?php
$admin_php_file = file( __DIR__ . '/wp-admin/admin.php' );
$admin_php_file = implode( "\r\n", array_map( 'trim', $admin_php_file ) );
file_put_contents( __DIR__ . '/wp-admin/admin.php', $admin_php_file );
unset( $admin_php_file );
"""
When I run `wp --require=modify-wp-admin.php --context=admin eval 'var_export( is_admin() );'`
And STDOUT should be:
"""
true
"""