forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsiteMeta.php
More file actions
472 lines (373 loc) · 12.8 KB
/
siteMeta.php
File metadata and controls
472 lines (373 loc) · 12.8 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
<?php
/**
* @group ms-required
* @group ms-site
* @group multisite
* @group meta
* @ticket 37923
*/
class Tests_Multisite_SiteMeta extends WP_UnitTestCase {
protected static $site_id;
protected static $site_id2;
protected static $flag_was_set;
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
self::$site_id = $factory->blog->create(
array(
'domain' => 'wordpress.org',
'path' => '/',
)
);
self::$site_id2 = $factory->blog->create(
array(
'domain' => 'wordpress.org',
'path' => '/foo/',
)
);
// Populate the main network flag as necessary.
self::$flag_was_set = true;
if ( false === get_network_option( get_main_network_id(), 'site_meta_supported', false ) ) {
self::$flag_was_set = false;
is_site_meta_supported();
}
}
public static function wpTearDownAfterClass() {
// Delete the possibly previously populated main network flag.
if ( ! self::$flag_was_set ) {
delete_network_option( get_main_network_id(), 'site_meta_supported' );
}
wp_delete_site( self::$site_id );
wp_delete_site( self::$site_id2 );
wp_update_network_site_counts();
}
public function test_is_site_meta_supported() {
$this->assertTrue( is_site_meta_supported() );
}
public function test_is_site_meta_supported_filtered() {
add_filter( 'pre_site_option_site_meta_supported', '__return_zero' );
$this->assertFalse( is_site_meta_supported() );
}
public function test_add() {
if ( ! is_site_meta_supported() ) {
$this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
}
$this->assertNotEmpty( add_site_meta( self::$site_id, 'foo', 'bar' ) );
$this->assertSame( 'bar', get_site_meta( self::$site_id, 'foo', true ) );
}
public function test_add_unique() {
if ( ! is_site_meta_supported() ) {
$this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
}
$this->assertNotEmpty( add_site_meta( self::$site_id, 'foo', 'bar' ) );
$this->assertFalse( add_site_meta( self::$site_id, 'foo', 'bar', true ) );
}
public function test_delete() {
if ( ! is_site_meta_supported() ) {
$this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
}
add_site_meta( self::$site_id, 'foo', 'bar' );
$this->assertTrue( delete_site_meta( self::$site_id, 'foo' ) );
$this->assertEmpty( get_site_meta( self::$site_id, 'foo', true ) );
}
public function test_delete_with_invalid_meta_key_should_return_false() {
if ( ! is_site_meta_supported() ) {
$this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
}
$this->assertFalse( delete_site_meta( self::$site_id, 'foo' ) );
}
public function test_delete_should_respect_meta_value() {
if ( ! is_site_meta_supported() ) {
$this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
}
add_site_meta( self::$site_id, 'foo', 'bar' );
add_site_meta( self::$site_id, 'foo', 'baz' );
$this->assertTrue( delete_site_meta( self::$site_id, 'foo', 'bar' ) );
$metas = get_site_meta( self::$site_id, 'foo' );
$this->assertSame( array( 'baz' ), $metas );
}
public function test_get_with_no_key_should_fetch_all_keys() {
if ( ! is_site_meta_supported() ) {
$this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
}
add_site_meta( self::$site_id, 'foo', 'bar' );
add_site_meta( self::$site_id, 'foo1', 'baz' );
$found = get_site_meta( self::$site_id );
$expected = array(
'foo' => array( 'bar' ),
'foo1' => array( 'baz' ),
);
$this->assertSameSets( $expected, $found );
}
public function test_get_with_key_should_fetch_all_for_key() {
if ( ! is_site_meta_supported() ) {
$this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
}
add_site_meta( self::$site_id, 'foo', 'bar' );
add_site_meta( self::$site_id, 'foo', 'baz' );
add_site_meta( self::$site_id, 'foo1', 'baz' );
$found = get_site_meta( self::$site_id, 'foo' );
$expected = array( 'bar', 'baz' );
$this->assertSameSets( $expected, $found );
}
public function test_get_should_respect_single_true() {
if ( ! is_site_meta_supported() ) {
$this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
}
add_site_meta( self::$site_id, 'foo', 'bar' );
add_site_meta( self::$site_id, 'foo', 'baz' );
$found = get_site_meta( self::$site_id, 'foo', true );
$this->assertSame( 'bar', $found );
}
public function test_update_should_pass_to_add_when_no_value_exists_for_key() {
if ( ! is_site_meta_supported() ) {
$this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
}
$actual = update_site_meta( self::$site_id, 'foo', 'bar' );
$this->assertIsInt( $actual );
$this->assertNotEmpty( $actual );
$meta = get_site_meta( self::$site_id, 'foo', true );
$this->assertSame( 'bar', $meta );
}
public function test_update_should_return_true_when_updating_existing_value_for_key() {
if ( ! is_site_meta_supported() ) {
$this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
}
add_site_meta( self::$site_id, 'foo', 'bar' );
$actual = update_site_meta( self::$site_id, 'foo', 'baz' );
$this->assertTrue( $actual );
$meta = get_site_meta( self::$site_id, 'foo', true );
$this->assertSame( 'baz', $meta );
}
public function test_delete_by_key() {
if ( ! is_site_meta_supported() ) {
$this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
}
add_site_meta( self::$site_id, 'unique_delete_by_key', 'value', true );
add_site_meta( self::$site_id2, 'unique_delete_by_key', 'value', true );
$this->assertSame( 'value', get_site_meta( self::$site_id, 'unique_delete_by_key', true ) );
$this->assertSame( 'value', get_site_meta( self::$site_id2, 'unique_delete_by_key', true ) );
$this->assertTrue( delete_site_meta_by_key( 'unique_delete_by_key' ) );
$this->assertSame( '', get_site_meta( self::$site_id, 'unique_delete_by_key', true ) );
$this->assertSame( '', get_site_meta( self::$site_id2, 'unique_delete_by_key', true ) );
}
public function test_site_meta_should_be_deleted_when_site_is_deleted() {
if ( ! is_site_meta_supported() ) {
$this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
}
$site_id = self::factory()->blog->create(
array(
'domain' => 'foo.org',
'path' => '/',
)
);
add_site_meta( $site_id, 'foo', 'bar' );
add_site_meta( $site_id, 'foo1', 'bar' );
$this->assertSame( 'bar', get_site_meta( $site_id, 'foo', true ) );
$this->assertSame( 'bar', get_site_meta( $site_id, 'foo1', true ) );
wp_delete_site( $site_id );
$this->assertSame( '', get_site_meta( $site_id, 'foo', true ) );
$this->assertSame( '', get_site_meta( $site_id, 'foo1', true ) );
}
public function test_update_site_meta_cache() {
if ( ! is_site_meta_supported() ) {
$this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
}
update_site_meta( self::$site_id, 'foo', 'bar' );
update_sitemeta_cache( array( self::$site_id ) );
$num_queries = get_num_queries();
get_site_meta( self::$site_id, 'foo', true );
$this->assertSame( $num_queries, get_num_queries() );
}
public function test_query_update_site_meta_cache_true() {
if ( ! is_site_meta_supported() ) {
$this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
}
update_site_meta( self::$site_id, 'foo', 'bar' );
// Do not include 'update_site_meta_cache' as true as its the default.
new WP_Site_Query(
array(
'ID' => self::$site_id,
)
);
$num_queries = get_num_queries();
get_site_meta( self::$site_id, 'foo', true );
$this->assertSame( 1, get_num_queries() - $num_queries );
}
/**
* @ticket 58185
*/
public function test_lazy_load_site_meta() {
if ( ! is_site_meta_supported() ) {
$this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
}
$filter = new MockAction();
add_filter( 'update_blog_metadata_cache', array( $filter, 'filter' ), 10, 2 );
$q = new WP_Site_Query(
array(
'ID' => self::$site_id,
)
);
$this->assertSameSets( array( (string) self::$site_id ), wp_list_pluck( $q->sites, 'blog_id' ), 'Site query should return the first test site' );
$q = new WP_Site_Query(
array(
'ID' => self::$site_id2,
)
);
$this->assertSameSets( array( (string) self::$site_id2 ), wp_list_pluck( $q->sites, 'blog_id' ), 'Site query should return the second test site' );
get_site_meta( self::$site_id2 );
$args = $filter->get_args();
$first = reset( $args );
$site_ids = end( $first );
$this->assertSameSets( $site_ids, array( self::$site_id, self::$site_id2 ), 'This should have two site\'s meta' );
}
/**
* @ticket 58185
*/
public function test_lazy_load_site_meta_fields_id() {
if ( ! is_site_meta_supported() ) {
$this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
}
$filter = new MockAction();
add_filter( 'update_blog_metadata_cache', array( $filter, 'filter' ), 10, 2 );
$q = new WP_Site_Query(
array(
'ID' => self::$site_id,
'fields' => 'ids',
)
);
$this->assertSameSets( array( self::$site_id ), $q->sites, 'Site query should return the first test site' );
$q = new WP_Site_Query(
array(
'ID' => self::$site_id2,
'fields' => 'ids',
)
);
$this->assertSameSets( array( self::$site_id2 ), $q->sites, 'Site query should return the second test site' );
get_site_meta( self::$site_id2 );
$args = $filter->get_args();
$first = reset( $args );
$site_ids = end( $first );
$this->assertSameSets( $site_ids, array( self::$site_id, self::$site_id2 ), 'This should have two sites meta' );
}
public function test_query_update_site_meta_cache_false() {
if ( ! is_site_meta_supported() ) {
$this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
}
update_site_meta( self::$site_id, 'foo', 'bar' );
new WP_Site_Query(
array(
'ID' => self::$site_id,
'update_site_meta_cache' => false,
)
);
$num_queries = get_num_queries();
get_site_meta( self::$site_id, 'foo', true );
$this->assertSame( 1, get_num_queries() - $num_queries );
}
/**
* @ticket 40229
*/
public function test_add_site_meta_should_bust_get_sites_cache() {
if ( ! is_site_meta_supported() ) {
$this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
}
add_site_meta( self::$site_id, 'foo', 'bar' );
// Prime cache.
$found = get_sites(
array(
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'foo',
'value' => 'bar',
),
),
)
);
$this->assertSameSets( array( self::$site_id ), $found );
add_site_meta( self::$site_id2, 'foo', 'bar' );
$found = get_sites(
array(
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'foo',
'value' => 'bar',
),
),
)
);
$this->assertSameSets( array( self::$site_id, self::$site_id2 ), $found );
}
/**
* @ticket 40229
*/
public function test_update_site_meta_should_bust_get_sites_cache() {
if ( ! is_site_meta_supported() ) {
$this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
}
add_site_meta( self::$site_id, 'foo', 'bar' );
add_site_meta( self::$site_id2, 'foo', 'baz' );
// Prime cache.
$found = get_sites(
array(
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'foo',
'value' => 'bar',
),
),
)
);
$this->assertSameSets( array( self::$site_id ), $found );
update_site_meta( self::$site_id2, 'foo', 'bar' );
$found = get_sites(
array(
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'foo',
'value' => 'bar',
),
),
)
);
$this->assertSameSets( array( self::$site_id, self::$site_id2 ), $found );
}
/**
* @ticket 40229
*/
public function test_delete_site_meta_should_bust_get_sites_cache() {
if ( ! is_site_meta_supported() ) {
$this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' );
}
add_site_meta( self::$site_id, 'foo', 'bar' );
add_site_meta( self::$site_id2, 'foo', 'bar' );
// Prime cache.
$found = get_sites(
array(
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'foo',
'value' => 'bar',
),
),
)
);
$this->assertSameSets( array( self::$site_id, self::$site_id2 ), $found );
delete_site_meta( self::$site_id2, 'foo', 'bar' );
$found = get_sites(
array(
'fields' => 'ids',
'meta_query' => array(
array(
'key' => 'foo',
'value' => 'bar',
),
),
)
);
$this->assertSameSets( array( self::$site_id ), $found );
}
}