-
Notifications
You must be signed in to change notification settings - Fork 379
Expand file tree
/
Copy pathOptionsMenuTest.php
More file actions
304 lines (261 loc) · 8.99 KB
/
Copy pathOptionsMenuTest.php
File metadata and controls
304 lines (261 loc) · 8.99 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
<?php
/**
* Tests for OptionsMenu.
*
* @package AmpProject\AmpWP\Tests
*/
namespace AmpProject\AmpWP\Tests\Admin;
use AmpProject\AmpWP\Admin\GoogleFonts;
use AmpProject\AmpWP\Admin\OptionsMenu;
use AmpProject\AmpWP\Admin\ReaderThemes;
use AmpProject\AmpWP\Admin\RESTPreloader;
use AmpProject\AmpWP\Admin\SiteHealth;
use AmpProject\AmpWP\DependencySupport;
use AmpProject\AmpWP\Infrastructure\Conditional;
use AmpProject\AmpWP\Infrastructure\Registerable;
use AmpProject\AmpWP\Infrastructure\Service;
use AmpProject\AmpWP\LoadingError;
use AmpProject\AmpWP\Tests\DependencyInjectedTestCase;
use AmpProject\AmpWP\Tests\Helpers\HomeUrlLoopbackRequestMocking;
use AmpProject\AmpWP\Tests\Helpers\PrivateAccess;
use AmpProject\AmpWP\Tests\Helpers\ThemesApiRequestMocking;
use AMP_Options_Manager;
use AMP_Validation_Manager;
/**
* Tests for OptionsMenu.
*
* @group options-menu
* @coversDefaultClass \AmpProject\AmpWP\Admin\OptionsMenu
*/
class OptionsMenuTest extends DependencyInjectedTestCase {
use HomeUrlLoopbackRequestMocking;
use ThemesApiRequestMocking;
use PrivateAccess;
/**
* Instance of OptionsMenu
*
* @var OptionsMenu
*/
public $instance;
/**
* Set up.
*
* @inheritdoc
*/
public function set_up() {
parent::set_up();
$site_health = $this->injector->make( SiteHealth::class );
$this->instance = new OptionsMenu( new GoogleFonts(), new ReaderThemes(), new RESTPreloader(), new DependencySupport(), new LoadingError(), $site_health );
$this->add_reader_themes_request_filter();
$this->add_home_url_loopback_request_mocking();
}
/**
* Tear down.
*
* @inheritdoc
*/
public function tear_down() {
$GLOBALS['wp_scripts'] = null;
$GLOBALS['wp_styles'] = null;
parent::tear_down();
}
/** @covers ::is_needed() */
public function test_is_needed() {
$this->assertFalse( is_admin() );
set_current_screen( 'index.php' );
$this->assertTrue( OptionsMenu::is_needed() );
add_filter( 'amp_options_menu_is_enabled', '__return_false' );
$this->assertFalse( OptionsMenu::is_needed() );
add_filter( 'amp_options_menu_is_enabled', '__return_true', 20 );
$this->assertTrue( OptionsMenu::is_needed() );
}
/** @covers ::__construct() */
public function test__construct() {
$this->assertInstanceOf( OptionsMenu::class, $this->instance );
$this->assertInstanceOf( Service::class, $this->instance );
$this->assertInstanceOf( Registerable::class, $this->instance );
$this->assertInstanceOf( Conditional::class, $this->instance );
}
/**
* Test constants.
*
* @see OptionsMenu::ICON_BASE64_SVG
*/
public function test_constants() {
$this->assertStringStartsWith( 'data:image/svg+xml;base64,', OptionsMenu::ICON_BASE64_SVG );
}
/**
* Test add_hooks.
*
* @see OptionsMenu::add_hooks()
* @cogers ::register()
*/
public function test_register() {
$this->instance->register();
$this->assertEquals( 9, has_action( 'admin_menu', [ $this->instance, 'add_menu_items' ] ) );
$this->assertEquals( 10, has_filter( 'plugin_action_links_amp/amp.php', [ $this->instance, 'add_plugin_action_links' ] ) );
$this->assertEquals( 10, has_action( 'admin_enqueue_scripts', [ $this->instance, 'enqueue_assets' ] ) );
}
/** @covers ::add_plugin_action_links() */
public function test_add_plugin_action_links() {
$links = [
'example' => '<a href="https://example.com">example!</a>',
];
$filtered_links = $this->instance->add_plugin_action_links( $links );
$this->assertArrayHasKey( 'example', $filtered_links );
$this->assertArrayHasKey( 'settings', $filtered_links );
}
/** @covers ::get_menu_slug() */
public function test_get_menu_slug() {
$this->assertSame(
AMP_Options_Manager::OPTION_NAME,
$this->instance->get_menu_slug()
);
}
/**
* Test admin_menu.
*
* @covers ::add_menu_items()
*/
public function test_add_menu_items() {
global $_parent_pages, $submenu;
$original_submenu = $submenu;
$original_parent_pages = $_parent_pages;
wp_set_current_user(
self::factory()->user->create(
[
'role' => 'administrator',
]
)
);
$this->instance->add_menu_items();
$this->assertArrayHasKey( 'amp-options', $_parent_pages );
$this->assertEquals( 'amp-options', $_parent_pages['amp-options'] );
$this->assertArrayHasKey( 'amp-options', $submenu );
$this->assertCount( 1, $submenu['amp-options'] );
$this->assertEquals( 'amp-options', $submenu['amp-options'][0][2] );
$submenu = $original_submenu;
$_parent_pages = $original_parent_pages;
}
/** @covers ::screen_handle() */
public function test_screen_handle() {
$this->assertSame(
'toplevel_page_' . $this->instance->get_menu_slug(),
$this->instance->screen_handle()
);
}
/** @covers ::get_analytics_vendors() */
public function test_get_analytics_vendors() {
$vendors = $this->instance->get_analytics_vendors();
$this->assertIsArray( $vendors );
$this->assertNotEmpty( $vendors );
$pairs = [];
foreach ( $vendors as $vendor ) {
$this->assertIsArray( $vendor );
$this->assertArrayHasKey( 'value', $vendor );
$this->assertArrayHasKey( 'label', $vendor );
$pairs[] = $vendor['value'] . ':' . $vendor['label'];
}
$this->assertIndexedArrayContains(
[
'adobeanalytics:Adobe Analytics',
'googleanalytics:Google Analytics (Legacy)',
'gtag:gtag',
],
$pairs
);
}
/** @covers ::enqueue_assets() */
public function test_enqueue_assets_wrong_hook_suffix() {
$this->instance->enqueue_assets( 'nope' );
$this->assertEquals( 0, did_action( 'amp_register_polyfills' ) );
$this->assertFalse( wp_script_is( OptionsMenu::ASSET_HANDLE, 'enqueued' ) );
$this->assertFalse( wp_style_is( OptionsMenu::ASSET_HANDLE, 'enqueued' ) );
}
/** @return array */
public function get_can_validate_data() {
return [
'can_validate' => [ true ],
'cannot_validate' => [ false ],
];
}
/**
* @dataProvider get_can_validate_data
* @covers ::enqueue_assets()
* @covers ::add_preload_rest_paths()
*
* @param bool $can_validate
*/
public function test_enqueue_assets_right_hook_suffix( $can_validate ) {
wp_set_current_user( self::factory()->user->create( [ 'role' => 'administrator' ] ) );
set_current_screen( $this->instance->screen_handle() );
if ( ! $can_validate ) {
add_filter(
'map_meta_cap',
function ( $caps, $cap ) {
if ( AMP_Validation_Manager::VALIDATE_CAPABILITY === $cap ) {
$caps[] = 'do_not_allow';
}
return $caps;
},
10,
3
);
}
$this->assertEquals( $can_validate, AMP_Validation_Manager::has_cap() );
$rest_preloader = $this->get_private_property( $this->instance, 'rest_preloader' );
$this->assertCount( 0, $this->get_private_property( $rest_preloader, 'paths' ) );
$this->assertFalse( wp_script_is( OptionsMenu::ASSET_HANDLE, 'enqueued' ) );
$this->assertFalse( wp_style_is( OptionsMenu::ASSET_HANDLE, 'enqueued' ) );
add_action( 'admin_enqueue_scripts', [ $this->instance, 'enqueue_assets' ] );
do_action( 'admin_enqueue_scripts', $this->instance->screen_handle() );
$this->assertEquals( 1, did_action( 'amp_register_polyfills' ) );
$this->assertTrue( wp_script_is( OptionsMenu::ASSET_HANDLE, 'enqueued' ) );
$this->assertTrue( wp_style_is( OptionsMenu::ASSET_HANDLE, 'enqueued' ) );
$script_before = implode( "\n", wp_scripts()->get_data( OptionsMenu::ASSET_HANDLE, 'before' ) );
$this->assertStringContainsString( 'var ampSettings', $script_before );
$this->assertStringContainsString( 'USER_FIELD_DEVELOPER_TOOLS_ENABLED', $script_before );
$this->assertStringContainsString( 'USERS_RESOURCE_REST_PATH', $script_before );
if ( $can_validate ) {
$this->assertStringContainsString( AMP_Validation_Manager::get_amp_validate_nonce(), $script_before );
} else {
$this->assertStringNotContainsString( AMP_Validation_Manager::get_amp_validate_nonce(), $script_before );
}
if ( function_exists( 'rest_preload_api_request' ) ) {
$this->assertEqualSets(
[
'/amp/v1/options',
'/amp/v1/reader-themes',
'/amp/v1/scannable-urls?_fields%5B0%5D=url&_fields%5B1%5D=amp_url&_fields%5B2%5D=type&_fields%5B3%5D=label&_fields%5B4%5D=validation_errors&_fields%5B5%5D=stale',
'/wp/v2/plugins?_fields%5B0%5D=author&_fields%5B1%5D=name&_fields%5B2%5D=plugin&_fields%5B3%5D=status&_fields%5B4%5D=version',
'/wp/v2/settings',
'/wp/v2/themes?_fields%5B0%5D=author&_fields%5B1%5D=name&_fields%5B2%5D=status&_fields%5B3%5D=stylesheet&_fields%5B4%5D=template&_fields%5B5%5D=version',
'/wp/v2/users/me',
],
$this->get_private_property( $rest_preloader, 'paths' )
);
}
}
/**
* Test render_screen for admin users.
*
* @covers ::render_screen()
*/
public function test_render_screen_for_admin_user() {
wp_set_current_user(
self::factory()->user->create(
[
'role' => 'administrator',
]
)
);
// Set current screen to be the options menu.
set_current_screen( $this->instance->screen_handle() );
// Set title to be used in the screen.
global $title;
$title = 'Test Title';
ob_start();
$this->instance->render_screen();
$this->assertStringContainsString( '<div class="wrap">', ob_get_clean() );
}
}