-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathWPTitleWPHeadTest.php
More file actions
325 lines (280 loc) · 10.4 KB
/
Copy pathWPTitleWPHeadTest.php
File metadata and controls
325 lines (280 loc) · 10.4 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
<?php
/**
* WP SEO Tests: Tests for functions that hook into wp_title() and wp_head.
*
* @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 WPTitleWPHeadTest extends TestCase {
var string $taxonomy = 'demo_taxonomy';
var string $post_type = 'demo_post_type';
var array $options = [];
function setUp(): void {
parent::setUp();
register_taxonomy( $this->taxonomy, 'post' );
register_post_type( $this->post_type, [ 'rewrite' => true, 'has_archive' => true, 'public' => true ] );
WP_SEO_Settings()->set_properties();
$this->_update_option_for_tests();
WP_SEO_Settings()->set_options();
global $wp_rewrite;
$wp_rewrite->init();
$wp_rewrite->flush_rules();
}
function tearDown(): void {
parent::tearDown();
// Leave the place as we found it.
_wp_seo_reset_post_types();
_wp_seo_reset_taxonomies();
delete_option( WP_SEO_Settings::SLUG );
WP_SEO_Settings()->set_properties();
WP_SEO_Settings()->set_options();
}
/**
* Update the plugin option with data for each test.
*
* This option should include all of the expected values used in these
* tests. Not each test uses all values, but setting them all is a little
* cleaner, and the option has to be set one way or another.
*/
function _update_option_for_tests() {
$this->options['post_types'] = [ 'post' ];
$this->options['taxonomies'] = [ 'category' ];
$this->options['arbitrary_tags'] = [
[
'name' => 'demo arbitrary title',
'content' => 'demo arbitrary content',
],
];
$this->options['robots_directives'] = [
[ 'label' => 'NoIndex', 'value' => 'noindex', 'description' => 'Directive description' ],
[ 'label' => 'NoFollow', 'value' => 'nofollow', 'description' => 'Directive description' ],
];
foreach ( [
'home',
'single_post',
"single_{$this->post_type}",
'archive_author',
'archive_category',
"archive_{$this->taxonomy}",
"archive_{$this->post_type}",
'archive_date',
'search',
'404',
'feed',
] as $key ) {
$this->options[ "{$key}_title" ] = "demo_{$key}_title";
$this->options[ "{$key}_description" ] = "demo_{$key}_description";
$this->options[ "{$key}_canonical_url" ] = "demo_{$key}_canonical_url";
$this->options[ "{$key}_robots_noindex" ] = '1';
$this->options[ "{$key}_robots_nofollow" ] = '';
}
update_option( WP_SEO_Settings::SLUG, WP_SEO_Settings()->sanitize_options( $this->options ) );
}
/**
* Test that a value matches wp_title().
*
* @param string $title The expected value.
*/
function _assert_title( $title ) {
$this->assertSame( $title, wp_title( '|', false, 'right' ) );
if ( function_exists( 'wp_get_document_title' ) ) {
$this->assertSame( $title, wp_get_document_title() );
}
}
/**
* Test that WP_SEO::wp_head() echoes all <meta> tags with expected values.
*
* @param string $description The expected meta description content.
* @param string $robots_noindex The expected robots noindex value, '1' or ''.
* @param string $robots_nofollow The expected robots nofollow value, '1' or ''.
*/
function _assert_all_meta( $description, $robots_noindex, $robots_nofollow ) {
$robots = implode( ', ', [
$robots_noindex ? 'noindex' : '',
$robots_nofollow ? 'nofollow' : '',
] );
$expected = <<<EOF
<meta name='description' content='{$description}' /><!-- WP SEO -->
<meta name='demo arbitrary title' content='demo arbitrary content' /><!-- WP SEO -->
<meta name='robots' content='{$robots}' /><!-- WP SEO -->
EOF;
$this->assertSame( strip_ws( $expected ), strip_ws( Utils::get_echo( [ WP_SEO(), 'wp_head' ] ) ) );
}
/**
* Test that WP_SEO::wp_head() echoes only the arbitrary <meta> tags.
*/
function _assert_arbitrary_meta() {
$expected = <<<EOF
<meta name='demo arbitrary title' content='demo arbitrary content' /><!-- WP SEO -->
EOF;
$this->assertSame( strip_ws( $expected ), strip_ws( Utils::get_echo( [ WP_SEO(), 'wp_head' ] ) ) );
}
/**
* Test that WP_SEO::wp_head() echoes <link> canonical tag with expected value.
*
* @param string $canonical_url The expected canonical URL.
*/
function _assert_canonical( $canonical_url ) {
$expected = "<link rel='canonical' href='{$canonical_url}' /><!-- WP SEO -->";
$this->assertSame( strip_ws( $expected ), strip_ws( get_echo( [ WP_SEO(), 'wp_head' ] ) ) );
}
/**
* Wrapper for checking _assert_title(), _assert_all_meta() and
* _assert_canonical() on option values.
*
* @param string $key The option to test. Use a name that prefixes
* '_title' and '_description' in the option, like 'home'.
*/
function _assert_option_filters( $key ) {
$this->_assert_title( $this->options[ "{$key}_title" ] );
$this->_assert_all_meta(
$this->options["{$key}_description"],
$this->options["{$key}_robots_noindex"],
$this->options["{$key}_robots_nofollow"],
);
$this->_assert_canonical( $this->options["{$key}_canonical_url"] );
}
/**
* Tests for the core filters on each supported type of request.
*
* Most requests should be subject to _assert_option_filters(), at least.
*/
function test_single() {
$this->go_to( get_permalink( $this->factory->post->create() ) );
$this->_assert_option_filters( 'single_post' );
}
function test_singular() {
$this->go_to( get_permalink( $this->factory->post->create( [ 'post_type' => $this->post_type ] ) ) );
$this->_assert_option_filters( "single_{$this->post_type}" );
}
// A post with custom values should not use the single_{type}_ values.
function test_single_custom() {
$this->go_to( get_permalink( $post_ID = $this->factory->post->create() ) );
update_post_meta( $post_ID, '_alley_seo_meta_title', '_custom_meta_title' );
update_post_meta( $post_ID, '_alley_seo_meta_description', '_custom_meta_description' );
update_post_meta( $post_ID, '_alley_seo_meta_canonical_url', '_custom_canonical_url' );
update_post_meta( $post_ID, '_alley_seo_meta_robots_noindex', '1' );
update_post_meta( $post_ID, '_alley_seo_meta_robots_nofollow', '' );
$this->_assert_title( '_custom_meta_title' );
$this->_assert_all_meta( '_custom_meta_description', '1', '' );
$this->_assert_canonical( '_custom_canonical_url' );
}
// If there is no format string, return the original post title.
function test_no_format_string() {
add_filter( 'wp_seo_title_tag_format', '__return_false' );
$title = rand_str();
$this->go_to( get_permalink( $this->factory->post->create( [ 'post_title' => $title ] ) ) );
// The site name doesn't appear in all versions we test against; just check for our title.
$this->assertStringContainsString( $title, wp_title( '»', false ) );
// WP_UnitTestCase::_restore_hooks() was introduced in 4.0.
remove_filter( 'wp_seo_title_tag_format', '__return_false' );
}
function test_home() {
$this->go_to( '/' );
$this->_assert_option_filters( 'home' );
}
function test_author_archive() {
$author_ID = $this->factory->user->create( [ 'user_login' => 'user-a' ] );
$this->factory->post->create( [ 'post_author' => $author_ID ] );
$this->go_to( get_author_posts_url( $author_ID ) );
$this->_assert_option_filters( 'archive_author' );
}
function test_category() {
$category_ID = $this->factory->term->create( [ 'name' => 'cat-a', 'taxonomy' => 'category' ] );
$this->go_to( get_term_link( $category_ID, 'category' ) );
$this->_assert_option_filters( 'archive_category' );
}
function test_tax() {
$term_ID = $this->factory->term->create( [ 'name' => 'demo-a', 'taxonomy' => $this->taxonomy ] );
$this->go_to( get_term_link( $term_ID, $this->taxonomy ) );
$this->_assert_option_filters( "archive_{$this->taxonomy}" );
}
// A term with custom values should not use the archive_{taxonomy}_ fields.
function test_category_custom() {
$term_ID = $this->factory->term->create( [ 'name' => 'cat-b', 'taxonomy' => 'category' ] );
update_option(
WP_SEO()->get_term_option_name(
get_term( $term_ID, 'category' )
),
[
'title' => '_custom_title',
'description' => '_custom_description',
'canonical_url' => '_custom_canonical_url',
'robots_noindex' => '1',
'robots_nofollow' => '',
],
);
$this->go_to( get_term_link( $term_ID, 'category' ) );
$this->_assert_title( '_custom_title' );
$this->_assert_all_meta( '_custom_description', '1', '' );
$this->_assert_canonical( '_custom_canonical_url' );
}
function test_post_type_archive() {
$this->go_to( get_post_type_archive_link( $this->post_type ) );
$this->_assert_option_filters( "archive_{$this->post_type}" );
}
function test_date_archive() {
$this->factory->post->create( [ 'post_date' => '2007-09-04 12:34' ] );
$this->go_to( get_day_link( '2007', '09', '04' ) );
$this->_assert_option_filters( 'archive_date' );
}
// No <meta> support.
function test_search() {
$this->go_to( get_search_link( 'wp-seo' ) );
$this->_assert_title( 'demo_search_title' );
$this->_assert_arbitrary_meta();
}
// No <meta> support.
function test_404() {
$this->go_to( get_day_link( '2014', '13', '13' ) );
$this->_assert_title( 'demo_404_title' );
$this->_assert_arbitrary_meta();
}
/**
* Proxy for testing an unsupported page.
*
* This tests both that nothing is output on a feed and that both if somehow
* $key became true for a feed, there would be no setting for it.
*/
function test_feed() {
/**
* Valid feed URLs without posts returned 404 before WordPress 4.0.
*
* @see https://core.trac.wordpress.org/ticket/18505
*/
$this->factory->post->create();
$this->go_to( get_feed_link() );
$this->assertEmpty( wp_title( '|', false ) );
}
/**
* If no option exists, test that the title is the default and that no meta are rendered.
*/
function test_no_option() {
delete_option( WP_SEO_Settings::SLUG );
WP_SEO_Settings()->set_options();
$this->go_to( get_permalink( $this->factory->post->create() ) );
// Uses a random $sep to be sure it couldn't have come from us.
$sep = rand_str();
$this->assertStringContainsString( $sep, wp_title( $sep, false ) );
$this->assertEmpty( Utils::get_echo( [ WP_SEO(), 'wp_head' ] ) );
}
/**
* Test that WP_SEO::meta_field() rejects non-string input.
*/
function test_invalid_meta_field() {
delete_option( WP_SEO_Settings::SLUG );
WP_SEO_Settings()->set_options();
update_option( WP_SEO_Settings::SLUG, [
'arbitrary_tags' => [
'name' => 'foo',
'value' => new \WP_Error(),
],
] );
$this->go_to( '/' );
$this->assertEmpty( Utils::get_echo( [ WP_SEO(), 'wp_head' ] ) );
}
}