-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathMetaboxesTest.php
More file actions
289 lines (227 loc) · 9.5 KB
/
Copy pathMetaboxesTest.php
File metadata and controls
289 lines (227 loc) · 9.5 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
<?php
/**
* WP SEO Tests: Tests for registering metaboxes, the metabox markup, and saving data.
*
* @package wp-seo
*/
namespace Alley\WP\WP_SEO\Tests\Feature;
use Alley\WP\WP_SEO\Tests\TestCase;
use Mantle\Testing\Utils;
use WP_SEO_Settings;
use WP_SEO;
class MetaboxesTest extends TestCase {
function setUp(): void {
parent::setUp();
update_option( WP_SEO_Settings::SLUG, [
'post_types' => [ 'post' ],
'taxonomies' => [ 'category' ],
] );
require_once ABSPATH . 'wp-admin/includes/template.php';
}
function tearDown(): void {
parent::tearDown();
// Clean up after ourselves.
delete_option( WP_SEO_Settings::SLUG );
wp_set_current_user( 1 );
}
/**
* These tests can be reenabled once we have a way to mock being in wp-admin context.
*/
// function test_add_meta_boxes() {
// WP_SEO()->add_meta_boxes( 'post' );
// global $wp_meta_boxes;
// $this->assertTrue( isset( $wp_meta_boxes['post']['normal']['high']['wp_seo'] ) );
// }
// function test_add_meta_boxes_no_post_field() {
// WP_SEO()->add_meta_boxes( 'page' );
// global $wp_meta_boxes;
// $this->assertFalse( isset( $wp_meta_boxes['page']['normal']['high']['wp_seo'] ) );
// }
// function test_add_meta_boxes_filters() {
// add_filter( 'wp_seo_meta_box_context', function() {
// return 'advanced';
// } );
// add_filter( 'wp_seo_meta_box_priority', function() {
// return 'low';
// } );
// WP_SEO()->add_meta_boxes( 'post' );
// global $wp_meta_boxes;
// $this->assertTrue( isset( $wp_meta_boxes['post']['advanced']['low']['wp_seo'] ) );
// }
/**
* Test that markup for post fields has our expected fields and values.
*/
function test_post_meta_fields() {
$post_ID = $this->factory->post->create();
$title = rand_str();
$description = rand_str();
add_post_meta( $post_ID, '_alley_seo_meta_title', $title );
add_post_meta( $post_ID, '_alley_seo_meta_description', $description );
$post = get_post( $post_ID );
// Capture the output of the function.
$html = Utils::get_echo( [ WP_SEO(), 'post_meta_fields' ], [ $post ] );
self::assertStringContainsString( 'name="seo_meta[title]" value="' . $title . '" size="96"', $html );
self::assertMatchesRegularExpression( '/<input[^>]+type="hidden"[^>]+name="wp-seo-nonce"/', $html );
self::assertMatchesRegularExpression( "/<textarea.*?>{$description}<\/textarea>/", $html );
self::assertStringContainsString( sprintf( '<noscript>%d (save changes to update)</noscript>', strlen( $title ) ), $html );
self::assertStringContainsString( sprintf( '<noscript>%d (save changes to update)</noscript>', strlen( $description ) ), $html );
}
/**
* Test most ways saving post fields can fail and the one way they succeed.
*/
function test_save_post_fields() {
wp_set_current_user( 1 );
$post_ID = $this->factory->post->create();
$post = get_post( $post_ID );
// Capture the output of the function.
$html = Utils::get_echo( [ WP_SEO(), 'post_meta_fields' ], [ $post ] );
// No $_POST.
$this->assertNull( WP_SEO()->save_post_fields( $post_ID ) );
// Wrong post type.
$_POST = [
'post_type' => 'page',
];
$this->assertNull( WP_SEO()->save_post_fields( $post_ID ) );
$_POST = [
'post_type' => 'post',
];
// Incapable user.
$user_id = $this->factory->user->create( [ 'role' => 'subscriber' ] );
wp_set_current_user( $user_id );
$this->assertNull( WP_SEO()->save_post_fields( $post_ID ) );
wp_set_current_user( 1 );
// No nonce.
$this->assertNull( WP_SEO()->save_post_fields( $post_ID ) );
$_POST['wp-seo-nonce'] = rand_str();
// Wrong nonce.
$this->assertNull( WP_SEO()->save_post_fields( $post_ID ) );
preg_match( "/name=\"wp-seo-nonce\" value=\"(.*?)\"/m", $html, $nonce_matches );
$_POST['wp-seo-nonce'] = $nonce_matches[1];
// No POST'ed ID.
$this->assertNull( WP_SEO()->save_post_fields( $post_ID ) );
$_POST['post_ID'] = $post_ID;
// No SEO data.
WP_SEO()->save_post_fields( $post_ID );
$this->assertEmpty( get_post_meta( $post_ID, '_alley_seo_meta_title', true ) );
$this->assertEmpty( get_post_meta( $post_ID, '_alley_seo_meta_description', true ) );
$title = rand_str();
$description = rand_str();
// add_magic_quotes() to simulate wp_magic_quotes().
$_POST['seo_meta'] = add_magic_quotes( [
'title' => $title,
'description' => $description . '<script>meta</script>',
] );
// Successful save.
WP_SEO()->save_post_fields( $post_ID );
$this->assertEquals( $title, get_post_meta( $post_ID, '_alley_seo_meta_title', true ) );
$this->assertEquals( $description, get_post_meta( $post_ID, '_alley_seo_meta_description', true ) );
$title = "Is your name O'Reilly?";
$description = 'What is Folder\SubFolder\File.txt?';
// Successfully save data with slashes. add_magic_quotes() to simulate wp_magic_quotes().
$_POST['seo_meta'] = add_magic_quotes( [
'title' => $title,
'description' => $description,
] );
WP_SEO()->save_post_fields( $post_ID );
$this->assertEquals( $title, get_post_meta( $post_ID, '_alley_seo_meta_title', true ) );
$this->assertEquals( $description, get_post_meta( $post_ID, '_alley_seo_meta_description', true ) );
}
/**
* Test that actions are added for term fields to the correct taxonomies.
*/
function test_add_term_boxes() {
WP_SEO()->add_term_boxes();
$this->assertNotFalse( has_action( 'category_add_form_fields', [ WP_SEO(), 'add_term_meta_fields' ] ) );
$this->assertNotFalse( has_action( 'category_edit_form', [ WP_SEO(), 'edit_term_meta_fields' ] ) );
$this->assertFalse( has_action( 'post_tag_add_form_fields', [ WP_SEO(), 'add_term_meta_fields' ] ) );
$this->assertFalse( has_action( 'post_tag_edit_form', [ WP_SEO(), 'edit_term_meta_fields' ] ) );
}
/**
* On a "New Term" form, check that our nonce and field names are present.
*/
function test_add_term_meta_fields() {
// Capture the output of the function.
$html = Utils::get_echo( [ WP_SEO(), 'add_term_meta_fields' ], [ 'category' ] );
self::assertMatchesRegularExpression( '/<input[^>]+type="hidden"[^>]+name="wp-seo-nonce"/', $html );
self::assertStringContainsString( 'name="seo_meta[title]"', $html );
self::assertStringContainsString( 'name="seo_meta[description]"', $html );
}
/**
* Test that markup for term fields has our expected fields and values.
*/
function test_edit_term_meta_fields() {
$category_ID = $this->factory->term->create( [ 'taxonomy' => 'category' ] );
$category = get_term( $category_ID, 'category' );
$title = rand_str();
$description = rand_str();
update_option(
WP_SEO()->get_term_option_name( $category ),
[
'title' => $title,
'description' => $description,
]
);
// Capture the output of the function.
$html = Utils::get_echo( [ WP_SEO(), 'edit_term_meta_fields' ], [ $category, 'category' ] );
self::assertMatchesRegularExpression( '/<input[^>]+type="hidden"[^>]+name="wp-seo-nonce"/', $html );
self::assertMatchesRegularExpression( "/<textarea.*?>{$description}<\/textarea>/", $html );
self::assertStringContainsString( 'name="seo_meta[title]" value="' . $title . '" size="96"', $html );
self::assertStringContainsString( sprintf( '<noscript>%d (save changes to update)</noscript>', strlen( $title ) ), $html );
self::assertStringContainsString( sprintf( '<noscript>%d (save changes to update)</noscript>', strlen( $description ) ), $html );
}
function test_save_term_fields() {
wp_set_current_user( 1 );
$category_ID = $this->factory->term->create( [ 'taxonomy' => 'category' ] );
$category = get_term( $category_ID, 'category' );
// Capture the output of the function.
$html = Utils::get_echo( [ WP_SEO(), 'add_term_meta_fields' ], [ $category, 'category' ] );
// No $_POST.
$this->assertNull( WP_SEO()->save_term_fields( $category_ID, $category->term_taxonomy_id, 'category' ) );
$_POST['taxonomy'] = 'category';
// Wrong taxonomy.
$this->assertNull( WP_SEO()->save_term_fields( $category_ID, $category->term_taxonomy_id, 'post_tag' ) );
// Incapable user.
$user_id = $this->factory->user->create( [ 'role' => 'subscriber' ] );
wp_set_current_user( $user_id );
$this->assertNull( WP_SEO()->save_term_fields( $category_ID, $category->term_taxonomy_id, 'category' ) );
wp_set_current_user( 1 );
// No nonce.
$this->assertNull( WP_SEO()->save_term_fields( $category_ID, $category->term_taxonomy_id, 'category' ) );
$_POST['wp-seo-nonce'] = rand_str();
// Wrong nonce.
$this->assertNull( WP_SEO()->save_term_fields( $category_ID, $category->term_taxonomy_id, 'category' ) );
preg_match( "/name=\"wp-seo-nonce\" value=\"(.*?)\"/m", $html, $nonce_matches );
$_POST['wp-seo-nonce'] = $nonce_matches[1];
// No SEO data? No option.
WP_SEO()->save_term_fields( $category_ID, $category->term_taxonomy_id, 'category' );
$this->assertFalse( get_option( WP_SEO()->get_term_option_name( $category ) ) );
$title = rand_str();
$description = rand_str();
$_POST['seo_meta'] = [
'title' => $title,
'description' => $description . '<script>meta</script>',
];
// Successful add_option().
WP_SEO()->save_term_fields( $category_ID, $category->term_taxonomy_id, 'category' );
$this->assertSame(
[
'title' => $title,
'description' => $description,
],
get_option( WP_SEO()->get_term_option_name( $category ) )
);
$updated_title = rand_str();
$_POST['seo_meta'] = [
'title' => $updated_title,
];
// Successful update_option().
WP_SEO()->save_term_fields( $category_ID, $category->term_taxonomy_id, 'category' );
$this->assertSame(
[
'title' => $updated_title,
'description' => '',
],
get_option( WP_SEO()->get_term_option_name( $category ) )
);
}
}