-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathTestCase.php
More file actions
285 lines (239 loc) · 5.45 KB
/
TestCase.php
File metadata and controls
285 lines (239 loc) · 5.45 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
<?php
namespace Plausible\Analytics\Tests;
use Plausible\Analytics\WP\EnhancedMeasurements;
use Yoast\WPTestUtils\BrainMonkey\TestCase as YoastTestCase;
class TestCase extends YoastTestCase {
public function __construct() {
/**
* During local unit testing this constant is required.
*/
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', true );
}
/**
* Required for loading assets.
*/
if ( ! defined( 'PLAUSIBLE_TESTS_ROOT' ) ) {
define( 'PLAUSIBLE_TESTS_ROOT', __DIR__ . '/' );
}
if ( ! defined( 'PLAUSIBLE_CI' ) ) {
define( 'PLAUSIBLE_CI', true );
}
parent::__construct();
}
/**
* Removes any action that (partially) matches the given $callback.
*
* @param $hook
* @param $callback
* @param $priority
*
* @return void
*/
public function removeAction( $hook, $callback, $priority = 10 ) {
global $wp_filter;
if ( ! isset( $wp_filter[ $hook ] ) ) {
return;
}
$callbacks = $wp_filter[ $hook ]->callbacks ?? [];
$callbacks = $callbacks[ $priority ] ?? [];
foreach ( $callbacks as $callback_key => $callback_data ) {
if ( str_contains( $callback_key, $callback ) ) {
unset( $wp_filter[ $hook ]->callbacks[ $priority ][ $callback_key ] );
}
}
}
/**
* Enable View Stats in WordPress.
*
* @param $settings
*
* @return mixed
*/
public function enableAnalyticsDashboard( $settings ) {
$settings['enable_analytics_dashboard'] = 'on';
return $settings;
}
/**
* Enable Enhanced Measurements > Custom Events (Tagged Events)
*
* @param $settings
*
* @return mixed
*/
public function enableRevenue( $settings ) {
$settings['enhanced_measurements'] = [ EnhancedMeasurements::ECOMMERCE_REVENUE ];
return $settings;
}
/**
* Enable form completions by modifying the settings array.
*
* @param array $settings The settings array to be modified.
*
* @return array The modified settings array including form completions.
*/
public function enableFormCompletions( $settings ) {
$settings['enhanced_measurements'] = [ EnhancedMeasurements::FORM_COMPLETIONS ];
return $settings;
}
/**
* Enable Cloaked Affiliate Links by modifying the settings array.
*
* @param $settings
*
* @return void
*/
public function enableCloakedAffiliateLinks( $settings ) {
$settings['enhanced_measurements'] = [ EnhancedMeasurements::CLOAKED_AFFILIATE_LINKS ];
return $settings;
}
/**
* Enable the 404 option by modifying the settings array.
*
* @param $settings
*
* @return mixed
*/
public function enableFourOFour( $settings ) {
$settings['enhanced_measurements'] = [ EnhancedMeasurements::FOUR_O_FOUR ];
return $settings;
}
/**
* Enable the Query Params option by modifying the settings array.
*
* @param $settings
*
* @return mixed
*/
public function enableQueryParams( $settings ) {
$settings['enhanced_measurements'] = [ EnhancedMeasurements::QUERY_PARAMS ];
return $settings;
}
/**
* Enable the Search Queries option by modifying the settings array.
*
* @param $settings
*
* @return mixed
*/
public function enableSearchQueries( $settings ) {
$settings['enhanced_measurements'] = [ EnhancedMeasurements::SEARCH_QUERIES ];
return $settings;
}
/**
* Enable the proxy.
*
* @param $settings
*
* @return mixed
*/
public function enableProxy( $settings ) {
$settings['proxy_enabled'] = 'on';
return $settings;
}
/**
* Dynamically disable the proxy.
*
* @param $settings
*
* @return mixed
*/
public function disableProxy( $settings ) {
$settings['proxy_enabled'] = '';
return $settings;
}
/**
* Set domain_name option.
*
* @param $settings
*
* @return mixed
*/
public function setDomain( $settings ) {
$settings['domain_name'] = 'test.dev';
return $settings;
}
/**
* @param $settings
*
* @return mixed
*/
public function setExcludePageview( $settings ) {
$settings['excluded_pages'] = "/checkout*,utm_\n*.example.com";
return $settings;
}
/**
* @param $settings
*
* @return mixed
*/
public function setExcludePageviewEdgeCaseAsterisk( $settings ) {
$settings['excluded_pages'] = '*';
return $settings;
}
/**
* @param $settings
*
* @return mixed
*/
public function setExcludePageviewEdgeCaseSpace( $settings ) {
$settings['excluded_pages'] = ' ';
return $settings;
}
/**
* Enable Enhanced Measurements > Categories & Authors.
*
* @param $settings
*
* @return mixed
*/
public function enablePageviewProps( $settings ) {
$settings['enhanced_measurements'] = [ 'pageview-props' ];
return $settings;
}
/**
* Set some test query params.
*
* @param $settings
*
* @return mixed
*/
public function setQueryParams( $settings ) {
$settings['query_params'] = [ 'test' ];
$_REQUEST['test'] = 1;
return $settings;
}
public function enableAdministratorTracking( $settings ) {
$settings['tracked_user_roles'][] = 'administrator';
return $settings;
}
/**
* Checks an array for a (partial) match with $string.
*
* @param $string string Needle.
* @param $array array Haystack.
*
* @return bool
*/
public function arrayHasString( $string, $array ) {
foreach ( $array as $element ) {
if ( str_contains( $element, $string ) ) {
return true;
}
}
return false;
}
/**
* Add user capability for testing.
*
* @return void
*/
public function addUserCap( $cap ) {
add_filter(
'user_has_cap',
function ( $caps ) use ( $cap ) {
return array_merge( $caps, [ $cap => true ] );
}
);
}
}