forked from Yoast/wordpress-seo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-class-wpseo-metabox.php
166 lines (127 loc) · 4.46 KB
/
test-class-wpseo-metabox.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
<?php
class WPSEO_Metabox_Test extends WPSEO_UnitTestCase {
/**
* @var WPSEO_Metabox
*/
private static $class_instance;
public static function setUpBeforeClass() {
self::$class_instance = new WPSEO_Metabox;
}
/**
* @covers WPSEO_Metabox::get_post_date()
*/
public function test_get_post_date() {
// create and go to post
$post_id = $this->factory->post->create();
$this->go_to( get_permalink( $post_id ) );
$post = get_post( $post_id );
$expected_date = date( 'j M Y', strtotime( $post->post_date ) );
$this->assertEquals( $expected_date, self::$class_instance->get_post_date( $post ) );
}
/**
* @covers WPSEO_Metabox::enqueue()
*/
public function test_enqueue_not_firing_on_options_page() {
global $pagenow;
$pagenow = 'options.php';
// call enqueue function
self::$class_instance->enqueue();
$enqueued = wp_script_is( 'wp-seo-metabox', 'enqueued' );
$this->assertFalse( $enqueued );
}
/**
* @covers WPSEO_Metabox::enqueue()
*/
public function test_enqueue_firing_on_new_post_page() {
global $pagenow;
$pagenow = 'post-new.php';
// call enqueue function
self::$class_instance->enqueue();
$enqueued = wp_script_is( 'wp-seo-metabox', 'enqueued' );
$this->assertTrue( $enqueued );
}
public function test_column_heading_is_hooked() {
self::$class_instance->setup_page_analysis();
// @todo -> is this double ! correct ?
$hooked = !! has_filter( 'manage_post_posts_columns', array( self::$class_instance, 'column_heading' ) );
$this->assertTrue( $hooked );
}
/**
* @covers WPSEO_Metabox::column_heading()
*/
public function test_column_heading_has_score( ) {
$columns = self::$class_instance->column_heading( array() );
$this->assertArrayHasKey( 'wpseo-score', $columns );
}
/**
* @covers WPSEO_Metabox::column_heading()
*/
public function test_column_heading_has_focuskw( ) {
$columns = self::$class_instance->column_heading( array() );
$this->assertArrayHasKey( 'wpseo-focuskw', $columns );
}
/**
* @covers WPSEO_Metabox::column_heading()
*/
public function test_column_heading_has_metadesc( ) {
$columns = self::$class_instance->column_heading( array() );
$this->assertArrayHasKey( 'wpseo-metadesc', $columns );
}
/**
* @covers WPSEO_Metabox::strtolower_utf8()
*/
public function test_strtolower_utf8() {
$input = 'ABCDEFGHIJKLMNOPQRSTUVWXYZÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЪЬЭЮЯĄĆĘŁŃÓŚŹŻ';
$expected_output = 'abcdefghijklmnopqrstuvwxyzàáâãäåæçèéêëìíîïðñòóôõöøùúûüýабвгдеёжзийклмнопрстуфхцчшщъъьэюяąćęłńóśźż';
$this->assertEquals( $expected_output, self::$class_instance->strtolower_utf8( $input ) );
}
public function test_add_metabox() {
global $wp_meta_boxes;
self::$class_instance->add_meta_box();
$post_types = get_post_types( array( 'public' => true ) );
// test if all post types have the wpseo_meta metabox
foreach ( $post_types as $post_type ) {
$this->assertArrayHasKey( 'wpseo_meta', $wp_meta_boxes[$post_type]['normal']['high'] );
}
}
public function test_save_postdata() {
// create and go to post
$post_id = $this->factory->post->create();
$this->go_to( get_permalink( $post_id ) );
$post = get_post( $post_id );
// setup
$GLOBALS['wpseo_admin'] = new WPSEO_Admin;
// vars
$meta_fields = apply_filters( 'wpseo_save_metaboxes', array() );
$meta_fields = array_merge(
$meta_fields,
self::$class_instance->get_meta_field_defs( 'general', $post->post_type ),
self::$class_instance->get_meta_field_defs( 'advanced' )
);
// set $_POST data to be saved
foreach ( $meta_fields as $key => $field ) {
// set text fields
if ( $field['type'] === 'text' ) {
$_POST[ WPSEO_Metabox::$form_prefix . $key ] = 'text';
} elseif ( $field['type'] === 'checkbox' ) {
$_POST[ WPSEO_Metabox::$form_prefix . $key ] = 'on';
}
}
// call method that saves the $_POST data
self::$class_instance->save_postdata( $post->ID );
// check if output matches
$custom = get_post_custom( $post->ID );
foreach ( $meta_fields as $key => $field ) {
if ( ! isset( $custom[ WPSEO_Metabox::$meta_prefix . $key ][0] ) ) {
continue;
}
$value = $custom[ WPSEO_Metabox::$meta_prefix . $key ][0];
// set text fields
if ( $field['type'] === 'text' ) {
$this->assertNotEmpty( $value );
} elseif ( $field['type'] === 'checkbox' ) {
$this->assertEquals( $value, 'on' );
}
}
}
}