forked from Yoast/wordpress-seo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-class-twitter.php
238 lines (186 loc) · 5.9 KB
/
test-class-twitter.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
<?php
class WPSEO_Twitter_Test extends WPSEO_UnitTestCase {
/**
* @var WPSEO_Twitter
*/
private static $class_instance;
public static function setUpBeforeClass() {
ob_start();
// create instance of WPSEO_Twitter class
self::$class_instance = new WPSEO_Twitter;
// clean output which was outputted by WPSEO_Twitter constructor
ob_end_clean();
}
/**
* @covers WPSEO_Twitter::twitter
*/
public function test_twitter() {
// TODO
}
/**
* @covers WPSEO_Twitter::type
*/
public function test_type() {
// test invalid option, should default to summary
self::$class_instance->options['twitter_card_type'] = 'something_invalid';
$expected = $this->metatag( 'card', 'summary' );
self::$class_instance->type();
$this->expectOutput( $expected );
// test valid option
self::$class_instance->options['twitter_card_type'] = 'photo';
$expected = $this->metatag( 'card', 'photo' );
self::$class_instance->type();
$this->expectOutput( $expected );
}
/**
* @covers WPSEO_Twitter::site_twitter
*/
public function test_site_twitter() {
// test valid option
self::$class_instance->options['twitter_site'] = 'yoast';
$expected = $this->metatag( 'site', '@yoast' );
self::$class_instance->site_twitter();
$this->expectOutput( $expected );
}
/**
* @covers WPSEO_Twitter::site_domain
*/
public function test_site_domain() {
// test valid option
$expected = $this->metatag( 'domain', get_bloginfo( 'name' ) );
self::$class_instance->site_domain();
$this->expectOutput( $expected );
}
/**
* @covers WPSEO_Twitter::site_domain
*/
public function test_author_twitter() {
$name = 'yoast';
$expected = $this->metatag( 'creator', '@' . $name );
// test option
self::$class_instance->options['twitter_site'] = $name;
self::$class_instance->author_twitter();
$this->expectOutput( $expected );
// reset option to make sure next result is from author meta
self::$class_instance->options['twitter_site'] = '';
/*
TODO fix this part
// create post, attach user as author
$user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
$post_id = $this->factory->post->create(
array(
'post_title' => 'Sample Post',
'post_type' => 'post',
'post_status' => 'publish',
)
);
// go to post we just created
$this->go_to( get_permalink( $post_id ) );
// test user meta
update_user_meta( $this->user_id, 'twitter', '@' . $name );
self::$class_instance->author_twitter();
$this->expectOutput( $expected );
*/
}
/**
* @covers WPSEO_Twitter::twitter_title
*/
public function test_twitter_title() {
// create and go to post
$post_id = $this->factory->post->create();
$this->go_to( get_permalink( $post_id ) );
$expected = $this->metatag( 'title', self::$class_instance->title( '' ) );
self::$class_instance->twitter_title();
$this->expectOutput( $expected );
}
/**
* @covers WPSEO_Twitter::twitter_description
*/
public function test_twitter_description() {
// create and go to post
$post_id = $this->factory->post->create();
$this->go_to( get_permalink( $post_id ) );
// test excerpt
$expected = $this->metatag( 'description', get_the_excerpt() );
self::$class_instance->twitter_description();
$this->expectOutput( $expected );
// test wpseo meta
WPSEO_Meta::set_value( 'metadesc', 'Meta description', $post_id );
$expected = $this->metatag( 'description', self::$class_instance->metadesc( false ) );
self::$class_instance->twitter_description();
$this->expectOutput( $expected );
}
/**
* @covers WPSEO_Twitter::twitter_url
*/
public function test_twitter_url() {
// create and go to post
$post_id = $this->factory->post->create();
$this->go_to( get_permalink( $post_id ) );
$expected = $this->metatag( 'url', esc_url( self::$class_instance->canonical( false ) ) );
self::$class_instance->twitter_url();
$this->expectOutput( $expected );
}
/**
* @covers WPSEO_Twitter::image_output
*/
public function test_image_output() {
$image_url = 'http://url.jpg';
// test image url
$expected = $this->metatag( 'image:src', $image_url );
$result = self::$class_instance->image_output( $image_url );
$this->assertTrue( $result );
$this->expectOutput( $expected );
// same image url shouldn't be shown twice
$result = self::$class_instance->image_output( $image_url );
$this->assertFalse( $result );
}
/**
* @covers WPSEO_Twitter::site_domain
*/
public function test_image() {
// create and go to post
$post_id = $this->factory->post->create();
$this->go_to( get_permalink( $post_id ) );
// test default image
$image_url = 'http://url-default-image.jpg';
self::$class_instance->options['og_default_image'] = $image_url;
$expected = $this->get_expected_image_output( $image_url );
self::$class_instance->image();
$this->expectOutput( $expected );
// reset default_image option
self::$class_instance->options['og_default_image'] = '';
// TODO test og_frontpage_image
// test wpseo meta value
$image_url = 'http://url-singular-meta-image.jpg';
WPSEO_Meta::set_value( 'twitter-image', $image_url, $post_id );
$expected = $this->get_expected_image_output( $image_url );
self::$class_instance->image();
$this->expectOutput( $expected );
// TODO test post thumbnail
// TODO test post content image
}
/**
* @param $url
*
* @return string
*/
private function get_expected_image_output( $url ) {
// get expected output
self::$class_instance->image_output( $url );
$expected = ob_get_contents();
ob_clean();
// reset shown_images array
self::$class_instance->shown_images = array();
return $expected;
}
/**
* @param $name
* @param $value
*
* @return string
*/
private function metatag( $name, $value ) {
return '<meta name="twitter:' . $name . '" content="' . $value . '"/>' . "\n";
}
}