forked from WordPress/wordcamp.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phpunit-database-testcase.php
105 lines (90 loc) · 3.49 KB
/
phpunit-database-testcase.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
<?php
namespace WordCamp\Tests;
use WP_UnitTestCase, WP_UnitTest_Factory;
/**
* Provides a mock WordCamp.org network of sites to test against.
*
* Test classes that need to interact with the database should extend this class, instead of extending
* `WP_UnitTestCase` directly.
*
* Other test cases should extend `WP_UnitTestCase` directly, to avoid the performance delays that this
* introduces.
*/
class Database_TestCase extends WP_UnitTestCase {
protected static $central_site_id;
protected static $year_dot_2018_site_id;
protected static $year_dot_2019_site_id;
protected static $slash_year_2016_site_id;
protected static $slash_year_2018_dev_site_id;
protected static $slash_year_2020_site_id;
protected static $yearless_site_id;
protected static $slash_year_with_yearless_site_id;
/**
* Create sites we'll need for the tests.
*
* @param WP_UnitTest_Factory $factory
*/
public static function wpSetUpBeforeClass( $factory ) {
self::$central_site_id = $factory->blog->create( array(
'domain' => 'central.wordcamp.test',
'path' => '/',
'network_id' => WORDCAMP_NETWORK_ID,
) );
self::$year_dot_2018_site_id = $factory->blog->create( array(
'domain' => '2018.seattle.wordcamp.test',
'path' => '/',
'network_id' => WORDCAMP_NETWORK_ID,
) );
self::$year_dot_2019_site_id = $factory->blog->create( array(
'domain' => '2019.seattle.wordcamp.test',
'path' => '/',
'network_id' => WORDCAMP_NETWORK_ID,
) );
self::$slash_year_2016_site_id = $factory->blog->create( array(
'domain' => 'vancouver.wordcamp.test',
'path' => '/2016/',
'network_id' => WORDCAMP_NETWORK_ID,
) );
self::$slash_year_2018_dev_site_id = $factory->blog->create( array(
'domain' => 'vancouver.wordcamp.test',
'path' => '/2018-developers/',
'network_id' => WORDCAMP_NETWORK_ID,
) );
self::$slash_year_2020_site_id = $factory->blog->create( array(
'domain' => 'vancouver.wordcamp.test',
'path' => '/2020/',
'network_id' => WORDCAMP_NETWORK_ID,
) );
// Sites like this are old edge cases from before a consistent structure was enforced.
self::$yearless_site_id = $factory->blog->create( array(
'domain' => 'japan.wordcamp.test',
'path' => '/',
'network_id' => WORDCAMP_NETWORK_ID,
) );
// Sites like this are newer and conform to the existing structure, but share a domain with a site that doesn't.
self::$slash_year_with_yearless_site_id = $factory->blog->create( array(
'domain' => 'japan.wordcamp.test',
'path' => '/2021/',
'network_id' => WORDCAMP_NETWORK_ID,
) );
self::$slash_year_with_yearless_site_id = $factory->blog->create( array(
'domain' => 'events.wordpress.test',
'path' => '/rome/2024/training/',
'network_id' => EVENTS_NETWORK_ID,
) );
}
/**
* Revert the persistent changes from `wpSetUpBeforeClass()` that won't be automatically cleaned up.
*/
public static function wpTearDownAfterClass() {
global $wpdb;
wp_delete_site( self::$central_site_id );
wp_delete_site( self::$year_dot_2018_site_id );
wp_delete_site( self::$year_dot_2019_site_id );
wp_delete_site( self::$slash_year_2016_site_id );
wp_delete_site( self::$slash_year_2018_dev_site_id );
wp_delete_site( self::$slash_year_2020_site_id );
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", WORDCAMP_NETWORK_ID ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id = %d", WORDCAMP_NETWORK_ID ) );
}
}