forked from Yoast/wordpress-seo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-class-wpseo-meta.php
129 lines (99 loc) · 3.87 KB
/
test-class-wpseo-meta.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
<?php
class WPSEO_Meta_Test extends WPSEO_UnitTestCase {
/**
* @covers WPSEO_Meta::set_value()
*/
public function test_set_value() {
// create and go to post
$post_id = $this->factory->post->create();
$this->go_to( get_permalink( $post_id ) );
WPSEO_Meta::set_value( 'test_set_value_key', 'test_set_value_value', $post_id );
$this->assertEquals( 'test_set_value_value', get_post_meta( $post_id, WPSEO_Meta::$meta_prefix . 'test_set_value_key', true ) );
}
/**
* @covers WPSEO_Meta::get_value()
*/
public function test_get_value() {
// create and go to post
$post_id = $this->factory->post->create();
$this->go_to( get_permalink( $post_id ) );
update_post_meta( $post_id, WPSEO_Meta::$meta_prefix . 'test_get_value_key', 'test_get_value_value' );
$this->assertEquals( 'test_get_value_value', WPSEO_Meta::get_value( 'test_get_value_key' ) );
// TODO test for defaults
// TODO test if non-existing keys return an empty string
}
/**
* Test if default meta values are removed when updating post_meta
* @covers WPSEO_Meta::remove_meta_if_default
*/
public function test_remove_meta_if_default() {
// create and go to post
$post_id = $this->factory->post->create();
// generate key
$key = WPSEO_Meta::$meta_prefix . 'meta-robots-noindex';
// set post meta to default value
$default_value = WPSEO_Meta::$defaults[$key];
update_post_meta( $post_id, $key, $default_value );
// default post meta should not be saved
$meta_value = get_post_meta( $post_id, $key, true );
$this->assertEquals( '', $meta_value );
}
/**
* Test if default meta values aren't saved when updating post_meta
* @covers WPSEO_Meta::dont_save_meta_if_default
*/
public function test_dont_save_meta_if_default() {
// create and go to post
$post_id = $this->factory->post->create();
$this->go_to( get_permalink( $post_id ) );
// generate key
$key = WPSEO_Meta::$meta_prefix . 'meta-robots-noindex';
// add default value to post_meta
$default_value = WPSEO_Meta::$defaults[$key];
add_post_meta( $post_id, $key, $default_value );
// default post meta should not be saved
$meta_value = get_post_meta( $post_id, $key );
$this->assertEquals( array(), $meta_value );
}
/**
* @covers WPSEO_Meta::meta_value_is_default
*/
public function test_meta_value_is_default() {
$meta_key = WPSEO_Meta::$meta_prefix . 'meta-robots-noindex';
$meta_value = WPSEO_Meta::$defaults[ $meta_key ];
$this->assertTrue( WPSEO_Meta::meta_value_is_default( $meta_key, $meta_value ) );
}
/**
* Test if two arrays are recursively merged, the latter overwriting the first.
*
* @covers WPSEO_Meta::array_merge_recursive_distinct
*/
public function test_array_merge_recursive_distinct() {
$inputArray1 = array(
'one' => array(
'one-one' => array(),
),
);
$inputArray2 = array(
'one' => array(
'one-one' => 'string',
),
);
$output = WPSEO_Meta::array_merge_recursive_distinct( $inputArray1, $inputArray2 );
$this->assertEquals( $output['one']['one-one'], 'string' );
}
/**
* @covers WPSEO_Meta::validate_meta_robots_adv
*/
public function test_validate_meta_robots_adv() {
// none should take precedence
$this->assertEquals( 'none', WPSEO_Meta::validate_meta_robots_adv( 'none, something-invalid, noarchive' ) );
$this->assertEquals( 'none', WPSEO_Meta::validate_meta_robots_adv( array( 'none', 'something-invalid', 'noarchive' ) ) );
// - should take precedence
$this->assertEquals( '-', WPSEO_Meta::validate_meta_robots_adv( '-, something-invalid, noarchive' ) );
$this->assertEquals( '-', WPSEO_Meta::validate_meta_robots_adv( array( '-', 'something-invalid', 'noarchive' ) ) );
// string should be cleaned
$this->assertEquals( 'noarchive,nosnippet', WPSEO_Meta::validate_meta_robots_adv( 'noarchive, nosnippet' ) );
$this->assertEquals( 'noarchive,nosnippet', WPSEO_Meta::validate_meta_robots_adv( array( 'noarchive', 'nosnippet' ) ) );
}
}