forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetworkOption.php
More file actions
231 lines (193 loc) · 6.08 KB
/
Copy pathnetworkOption.php
File metadata and controls
231 lines (193 loc) · 6.08 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
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
<?php
/**
* Tests specific to managing network options in multisite.
*
* Some tests will run in single site as the `_network_option()` functions
* are available and internally use `_option()` functions as fallbacks.
*
* @group option
* @group ms-option
* @group multisite
*/
class Tests_Option_NetworkOption extends WP_UnitTestCase {
/**
* @group ms-required
*
* @covers ::add_site_option
*/
public function test_add_network_option_not_available_on_other_network() {
$id = self::factory()->network->create();
$option = __FUNCTION__;
$value = __FUNCTION__;
add_site_option( $option, $value );
$this->assertFalse( get_network_option( $id, $option, false ) );
}
/**
* @group ms-required
*
* @covers ::add_network_option
*/
public function test_add_network_option_available_on_same_network() {
$id = self::factory()->network->create();
$option = __FUNCTION__;
$value = __FUNCTION__;
add_network_option( $id, $option, $value );
$this->assertSame( $value, get_network_option( $id, $option, false ) );
}
/**
* @group ms-required
*
* @covers ::delete_site_option
*/
public function test_delete_network_option_on_only_one_network() {
$id = self::factory()->network->create();
$option = __FUNCTION__;
$value = __FUNCTION__;
add_site_option( $option, $value );
add_network_option( $id, $option, $value );
delete_site_option( $option );
$this->assertSame( $value, get_network_option( $id, $option, false ) );
}
/**
* @ticket 22846
* @group ms-excluded
*
* @covers ::add_network_option
*/
public function test_add_network_option_is_not_stored_as_autoload_option() {
$key = __FUNCTION__;
add_network_option( null, $key, 'Not an autoload option' );
$options = wp_load_alloptions();
$this->assertArrayNotHasKey( $key, $options );
}
/**
* @ticket 22846
* @group ms-excluded
*
* @covers ::update_network_option
*/
public function test_update_network_option_is_not_stored_as_autoload_option() {
$key = __FUNCTION__;
update_network_option( null, $key, 'Not an autoload option' );
$options = wp_load_alloptions();
$this->assertArrayNotHasKey( $key, $options );
}
/**
* @dataProvider data_network_id_parameter
*
* @param $network_id
* @param $expected_response
*
* @covers ::add_network_option
*/
public function test_add_network_option_network_id_parameter( $network_id, $expected_response ) {
$option = rand_str();
$value = rand_str();
$this->assertSame( $expected_response, add_network_option( $network_id, $option, $value ) );
}
/**
* @dataProvider data_network_id_parameter
*
* @param $network_id
* @param $expected_response
*
* @covers ::get_network_option
*/
public function test_get_network_option_network_id_parameter( $network_id, $expected_response ) {
$option = rand_str();
$this->assertSame( $expected_response, get_network_option( $network_id, $option, true ) );
}
public function data_network_id_parameter() {
return array(
// Numeric values should always be accepted.
array( 1, true ),
array( '1', true ),
array( 2, true ),
// Null, false, and zero will be treated as the current network.
array( null, true ),
array( false, true ),
array( 0, true ),
array( '0', true ),
// Other truthy or string values should be rejected.
array( true, false ),
array( 'string', false ),
);
}
/**
* @ticket 43506
* @group ms-required
*
* @covers ::get_network_option
* @covers ::wp_cache_get
* @covers ::wp_cache_delete
*/
public function test_get_network_option_sets_notoptions_if_option_found() {
$network_id = get_current_network_id();
$notoptions_key = "$network_id:notoptions";
$original_cache = wp_cache_get( $notoptions_key, 'site-options' );
if ( false !== $original_cache ) {
wp_cache_delete( $notoptions_key, 'site-options' );
}
// Retrieve any existing option.
get_network_option( $network_id, 'site_name' );
$cache = wp_cache_get( $notoptions_key, 'site-options' );
if ( false !== $original_cache ) {
wp_cache_set( $notoptions_key, $original_cache, 'site-options' );
}
$this->assertSame( array(), $cache );
}
/**
* @ticket 43506
* @group ms-required
*
* @covers ::get_network_option
* @covers ::wp_cache_get
*/
public function test_get_network_option_sets_notoptions_if_option_not_found() {
$network_id = get_current_network_id();
$notoptions_key = "$network_id:notoptions";
$original_cache = wp_cache_get( $notoptions_key, 'site-options' );
if ( false !== $original_cache ) {
wp_cache_delete( $notoptions_key, 'site-options' );
}
// Retrieve any non-existing option.
get_network_option( $network_id, 'this_does_not_exist' );
$cache = wp_cache_get( $notoptions_key, 'site-options' );
if ( false !== $original_cache ) {
wp_cache_set( $notoptions_key, $original_cache, 'site-options' );
}
$this->assertSame( array( 'this_does_not_exist' => true ), $cache );
}
/**
* Ensure updating network options containing an object do not result in unneeded database calls.
*
* @ticket 44956
*
* @covers ::update_network_option
*/
public function test_update_network_option_array_with_object() {
$array_w_object = array(
'url' => 'http://src.wordpress-develop.dev/wp-content/uploads/2016/10/cropped-Blurry-Lights.jpg',
'meta_data' => (object) array(
'attachment_id' => 292,
'height' => 708,
'width' => 1260,
),
);
$array_w_object_2 = array(
'url' => 'http://src.wordpress-develop.dev/wp-content/uploads/2016/10/cropped-Blurry-Lights.jpg',
'meta_data' => (object) array(
'attachment_id' => 292,
'height' => 708,
'width' => 1260,
),
);
// Add the option, it did not exist before this.
add_network_option( null, 'array_w_object', $array_w_object );
$num_queries_pre_update = get_num_queries();
// Update the option using the same array with an object for the value.
$this->assertFalse( update_network_option( null, 'array_w_object', $array_w_object_2 ) );
// Check that no new database queries were performed.
$this->assertSame( $num_queries_pre_update, get_num_queries() );
}
}