forked from Yoast/wordpress-seo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-class-opengraph.php
381 lines (291 loc) · 11.9 KB
/
test-class-opengraph.php
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
class WPSEO_OpenGraph_Test extends WPSEO_UnitTestCase {
/**
* @var WPSEO_OpenGraph
*/
private static $class_instance;
public static function setUpBeforeClass() {
self::$class_instance = new WPSEO_OpenGraph;
}
/**
* Provision tests
*/
public function setUp() {
parent::setUp();
// start each test on the home page
$this->go_to_home();
}
/**
* Test if options were properly fetched upon class instantiation.
*/
public function test_options_not_empty() {
$this->assertNotEmpty( self::$class_instance->options );
}
/**
* @covers WPSEO_OpenGraph::opengraph
*/
public function test_opengraph() {
self::$class_instance->opengraph();
$this->assertEquals( 1, did_action( 'wpseo_opengraph' ) );
ob_clean();
}
/**
* @covers WPSEO_OpenGraph::og_tag
*/
public function test_og_tag() {
// there should be no output when $content is empty
$this->assertFalse( self::$class_instance->og_tag( 'property', '' ) );
$this->expectOutput( '' );
// true when $content is not empty
$this->assertTrue( self::$class_instance->og_tag( 'property', 'content' ) );
$this->expectOutput( '<meta property="property" content="content" />' . "\n" );
// test escaping
$this->assertTrue( self::$class_instance->og_tag( 'property "with quotes"', 'content "with quotes"' ) );
$this->expectOutput( '<meta property="property "with quotes"" content="content "with quotes"" />' . "\n" );
}
/**
* @covers WPSEO_OpenGraph::facebook_filter
*/
public function test_facebook_filter() {
$post_id = $this->factory->post->create();
$this->go_to( get_permalink( $post_id ) );
$c = self::$class_instance;
$result = $c->facebook_filter( array() );
// test if values were filtered
$this->assertArrayHasKey( 'http://ogp.me/ns#type', $result );
$this->assertArrayHasKey( 'http://ogp.me/ns#title', $result );
$this->assertArrayHasKey( 'http://ogp.me/ns#locale', $result );
$this->assertArrayHasKey( 'http://ogp.me/ns#description', $result );
// test filter values
$this->assertEquals( $result['http://ogp.me/ns#type'], $c->type( false ) );
$this->assertEquals( $result['http://ogp.me/ns#title'], $c->og_title( false ) );
$this->assertEquals( $result['http://ogp.me/ns#locale'], $c->locale( false ) );
$this->assertEquals( $result['http://ogp.me/ns#description'], $c->description( false ) );
}
/**
* @covers WPSEO_OpenGraph::add_opengraph_namespace
*/
public function test_add_opengraph_namespace() {
$c = self::$class_instance;
$expected = ' prefix="og: http://ogp.me/ns#' . ( ( $c->options['fbadminapp'] != 0 || ( is_array( $c->options['fb_admins'] ) && $c->options['fb_admins'] !== array() ) ) ? ' fb: http://ogp.me/ns/fb#' : '' ) . '"';
$this->assertEquals( $c->add_opengraph_namespace( '' ), $expected );
}
/**
* @covers WPSEO_OpenGraph::article_author_facebook
*/
public function test_article_author_facebook() {
// test not on singular page
$this->assertFalse( self::$class_instance->article_author_facebook() );
// create post with author
$author_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
$post_id = $this->factory->post->create( array( 'post_author' => $author_id ) );
$this->go_to( get_permalink( $post_id ) );
// on post page but facebook meta not set.
$this->assertFalse( self::$class_instance->article_author_facebook() );
// add facebook meta to post author
$post = get_post( $post_id );
$author = $post->post_author;
add_user_meta( $author, 'facebook', 'facebook_author' );
// test final output
$this->assertTrue( self::$class_instance->article_author_facebook() );
$this->expectOutput( '<meta property="article:author" content="facebook_author" />' . "\n" );
}
/**
* @covers WPSEO_OpenGraph::website_facebook
*/
public function test_website_facebook() {
// option not set
$this->assertFalse( self::$class_instance->website_facebook() );
// set option
self::$class_instance->options['facebook_site'] = 'http://facebook.com/mysite/';
// test output
$this->assertTrue( self::$class_instance->website_facebook() );
$this->expectOutput( '<meta property="article:publisher" content="http://facebook.com/mysite/" />' . "\n" );
}
/**
* @covers WPSEO_OpenGraph::site_owner
*/
public function test_site_owner() {
$this->assertFalse( self::$class_instance->site_owner() );
// @todo
}
/**
* @covers WPSEO_OpenGraph::og_title
*/
public function test_og_title() {
// create and go to post
$post_id = $this->factory->post->create();
$this->go_to( get_permalink( $post_id ) );
$expected_title = self::$class_instance->title( '' );
$expected_html = '<meta property="og:title" content="' . $expected_title . '" />' . "\n";
$this->assertTrue( self::$class_instance->og_title() );
$this->expectOutput( $expected_html );
$this->assertEquals( self::$class_instance->og_title( false ), $expected_title );
}
/**
* @covers WPSEO_OpenGraph::url
*/
public function test_url() {
// create and go to post
$post_id = $this->factory->post->create();
$url = get_permalink( $post_id );
$this->go_to( $url );
$expected_url = $url;
$this->assertTrue( self::$class_instance->url() );
$this->expectOutput( '<meta property="og:url" content="' . $expected_url . '" />' . "\n" );
}
/**
* @covers WPSEO_OpenGraph::locale
*/
public function test_locale() {
global $locale;
$this->assertEquals( 'en_US', self::$class_instance->locale( false ) );
$locale = 'ca';
$this->assertEquals( 'ca_ES', self::$class_instance->locale( false ) );
$locale = 'nl';
$this->assertEquals( 'nl_NL', self::$class_instance->locale( false ) );
$locale = 'nl_NL';
$this->assertEquals( 'nl_NL', self::$class_instance->locale( true ) );
$this->expectOutput( '<meta property="og:locale" content="nl_NL" />' . "\n" );
}
/**
* @covers WPSEO_OpenGraph::type
*/
public function test_type() {
$this->assertEquals( 'website', self::$class_instance->type( false ) );
$category_id = wp_create_category( 'WordPress SEO' );
$this->go_to( get_category_link( $category_id ) );
$this->assertEquals( 'object', self::$class_instance->type( false ) );
// create and go to post
$post_id = $this->factory->post->create();
$this->go_to( get_permalink( $post_id ) );
$this->assertEquals( 'article', self::$class_instance->type( false ) );
}
/**
* @covers WPSEO_OpenGraph::image_output
*/
public function test_image_output() {
$this->assertFalse( self::$class_instance->image_output( '' ) );
$this->assertFalse( self::$class_instance->image_output( 'malformed-relative-url' ) );
$img_url = home_url( 'absolute-image.jpg' );
// test with absolute image
$this->assertTrue( self::$class_instance->image_output( $img_url ) );
$this->expectOutput( '<meta property="og:image" content="' . $img_url . '" />' . "\n" );
// do not output same image twice
$this->assertFalse( self::$class_instance->image_output( $img_url ) );
// test with relative image url
$relative_img_url = '/relative-image.jpg';
$absolute_img_url = home_url( $relative_img_url );
$this->assertTrue( self::$class_instance->image_output( $relative_img_url ) );
$this->expectOutput( '<meta property="og:image" content="' . $absolute_img_url . '" />' . "\n" );
}
/**
* @covers WPSEO_OpenGraph::image
*/
public function test_image() {
}
/**
* @covers WPSEO_OpenGraph::description
*/
public function test_description_frontpage() {
$this->go_to_home();
$expected_frontpage_description = self::$class_instance->description( false );
$this->assertEquals( '', $expected_frontpage_description );
}
/**
* @covers WPSEO_OpenGraph::description
*/
public function test_description_single_post() {
$expected_opengraph_description = 'This is with a opengraph-description';
$expected_meta_description = 'This is with a meta-description';
$expected_excerpt = 'Post excerpt 1';
// Creates the post
$post_id = $this->factory->post->create();
$this->go_to( get_permalink( $post_id ) );
// Checking opengraph-description and after obtaining its value, reset the meta value for it
WPSEO_Meta::set_value( 'opengraph-description', $expected_opengraph_description, $post_id );
$opengraph_description = self::$class_instance->description( false );
WPSEO_Meta::set_value( 'opengraph-description', '', $post_id );
$this->assertEquals( $expected_opengraph_description, $opengraph_description );
// Checking meta-description and after obtaining its value, reset the meta value for it
WPSEO_Meta::set_value( 'metadesc', $expected_meta_description, $post_id );
$meta_description = self::$class_instance->description( false );
WPSEO_Meta::set_value( 'metadesc', '', $post_id );
$this->assertEquals( $expected_meta_description, $meta_description );
// Checking with the excerpt
$excerpt = self::$class_instance->description( false );
$this->assertEquals( $expected_excerpt, $excerpt );
}
/**
* @covers WPSEO_OpenGraph::description
*/
public function test_description_category() {
$expected_meta_description = '';
$category_id = wp_create_category( 'WordPress SEO' );
$this->go_to( get_category_link( $category_id ) );
// Checking meta-description and after obtaining its value, reset the meta value for it
$meta_description = self::$class_instance->description( false );
$this->assertEquals( $expected_meta_description, $meta_description );
}
/**
* @covers WPSEO_OpenGraph::site_name
*/
public function test_site_name() {
}
/**
* @covers WPSEO_OpenGraph::tags
*/
public function test_tags() {
// not singular, return false
$this->assertFalse( self::$class_instance->tags() );
// create post, without tags
$post_id = $this->factory->post->create();
$this->go_to( get_permalink( $post_id ) );
// no tags, should return false
$this->assertFalse( self::$class_instance->tags() );
// add tags to post
wp_set_post_tags( $post_id, 'Tag1, Tag2' );
$expected_tags = '<meta property="article:tag" content="Tag1" />' . "\n" . '<meta property="article:tag" content="Tag2" />' . "\n";
// test again, this time with tags
$this->assertTrue( self::$class_instance->tags() );
$this->expectOutput( $expected_tags );
}
/**
* @covers WPSEO_OpenGraph::category
*/
public function test_category() {
// not singular, should return false
$this->assertFalse( self::$class_instance->category() );
// Create post in category, go to post.
$category_id = wp_create_category( 'Category Name' );
$post_id = $this->factory->post->create( array( 'post_category' => array( $category_id ) ) );
$this->go_to( get_permalink( $post_id ) );
$this->assertTrue( self::$class_instance->category() );
$this->expectOutput( '<meta property="article:section" content="Category Name" />' . "\n" );
}
/**
* @covers WPSEO_OpenGraph::publish_date
*/
public function test_publish_date() {
// not on singular, should return false
$this->assertFalse( self::$class_instance->publish_date() );
// create post, without tags
$post_id = $this->factory->post->create();
$this->go_to( get_permalink( $post_id ) );
// test published_time tags output
$published_time = get_the_date( 'c' );
$published_output = '<meta property="article:published_time" content="' . $published_time . '" />' . "\n";
$this->assertTrue( self::$class_instance->publish_date() );
$this->expectOutput( $published_output );
// modify post time
global $post;
$post = get_post( $post_id );
$post->post_modified = gmdate( 'Y-m-d H:i:s', time() + 1 );
$post->post_modified_gmt = gmdate( 'Y-m-d H:i:s', ( time() + 1 + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ) );
// test modified tags output
$modified_time = get_the_modified_date( 'c' );
$modified_output = '<meta property="article:modified_time" content="' . $modified_time . '" />' . "\n" . '<meta property="og:updated_time" content="' . $modified_time . '" />' . "\n";
$this->assertTrue( self::$class_instance->publish_date() );
$this->expectOutput( $published_output . $modified_output );
}
}