forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoption.php
More file actions
143 lines (122 loc) · 4.42 KB
/
option.php
File metadata and controls
143 lines (122 loc) · 4.42 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
<?php
/**
* @group option
*/
class Tests_Option_Option extends WP_UnitTestCase {
function __return_foo() {
return 'foo';
}
function test_the_basics() {
$key = 'key1';
$key2 = 'key2';
$value = 'value1';
$value2 = 'value2';
$this->assertFalse( get_option( 'doesnotexist' ) );
$this->assertTrue( add_option( $key, $value ) );
$this->assertEquals( $value, get_option( $key ) );
$this->assertFalse( add_option( $key, $value ) ); // Already exists.
$this->assertFalse( update_option( $key, $value ) ); // Value is the same.
$this->assertTrue( update_option( $key, $value2 ) );
$this->assertEquals( $value2, get_option( $key ) );
$this->assertFalse( add_option( $key, $value ) );
$this->assertEquals( $value2, get_option( $key ) );
$this->assertTrue( delete_option( $key ) );
$this->assertFalse( get_option( $key ) );
$this->assertFalse( delete_option( $key ) );
$this->assertTrue( update_option( $key2, $value2 ) );
$this->assertEquals( $value2, get_option( $key2 ) );
$this->assertTrue( delete_option( $key2 ) );
$this->assertFalse( get_option( $key2 ) );
}
function test_default_filter() {
$value = 'value';
$this->assertFalse( get_option( 'doesnotexist' ) );
// Default filter overrides $default arg.
add_filter( 'default_option_doesnotexist', array( $this, '__return_foo' ) );
$this->assertEquals( 'foo', get_option( 'doesnotexist', 'bar' ) );
// Remove the filter and the $default arg is honored.
remove_filter( 'default_option_doesnotexist', array( $this, '__return_foo' ) );
$this->assertEquals( 'bar', get_option( 'doesnotexist', 'bar' ) );
// Once the option exists, the $default arg and the default filter are ignored.
add_option( 'doesnotexist', $value );
$this->assertEquals( $value, get_option( 'doesnotexist', 'foo' ) );
add_filter( 'default_option_doesnotexist', array( $this, '__return_foo' ) );
$this->assertEquals( $value, get_option( 'doesnotexist', 'foo' ) );
remove_filter( 'default_option_doesnotexist', array( $this, '__return_foo' ) );
// Cleanup.
$this->assertTrue( delete_option( 'doesnotexist' ) );
$this->assertFalse( get_option( 'doesnotexist' ) );
}
/**
* @ticket 31047
*/
public function test_add_option_should_respect_default_option_filter() {
add_filter( 'default_option_doesnotexist', array( $this, '__return_foo' ) );
$added = add_option( 'doesnotexist', 'bar' );
remove_filter( 'default_option_doesnotexist', array( $this, '__return_foo' ) );
$this->assertTrue( $added );
$this->assertSame( 'bar', get_option( 'doesnotexist' ) );
}
function test_serialized_data() {
$key = __FUNCTION__;
$value = array(
'foo' => true,
'bar' => true,
);
$this->assertTrue( add_option( $key, $value ) );
$this->assertEquals( $value, get_option( $key ) );
$value = (object) $value;
$this->assertTrue( update_option( $key, $value ) );
$this->assertEquals( $value, get_option( $key ) );
$this->assertTrue( delete_option( $key ) );
}
/**
* @ticket 23289
*/
function test_bad_option_names() {
foreach ( array( '', '0', ' ', 0, false, null ) as $empty ) {
$this->assertFalse( get_option( $empty ) );
$this->assertFalse( add_option( $empty, '' ) );
$this->assertFalse( update_option( $empty, '' ) );
$this->assertFalse( delete_option( $empty ) );
}
}
/**
* @ticket 23289
* @expectedException WPDieException
*/
function test_special_option_name_alloption() {
delete_option( 'alloptions' );
}
/**
* @ticket 23289
* @expectedException WPDieException
*/
function test_special_option_name_notoptions() {
delete_option( 'notoptions' );
}
function data_option_autoloading() {
return array(
array( 'autoload_yes', 'yes', 'yes' ),
array( 'autoload_true', true, 'yes' ),
array( 'autoload_string', 'foo', 'yes' ),
array( 'autoload_int', 123456, 'yes' ),
array( 'autoload_array', array(), 'yes' ),
array( 'autoload_no', 'no', 'no' ),
array( 'autoload_false', false, 'no' ),
);
}
/**
* Options should be autoloaded unless they were added with "no" or `false`.
*
* @ticket 31119
* @dataProvider data_option_autoloading
*/
function test_option_autoloading( $name, $autoload_value, $expected ) {
global $wpdb;
$added = add_option( $name, 'Autoload test', '', $autoload_value );
$this->assertTrue( $added );
$actual = $wpdb->get_row( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s LIMIT 1", $name ) );
$this->assertEquals( $expected, $actual->autoload );
}
}