forked from WordPress/wordcamp.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphpunit-database-testcase.php
More file actions
149 lines (129 loc) · 5.11 KB
/
phpunit-database-testcase.php
File metadata and controls
149 lines (129 loc) · 5.11 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
<?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 $events_root_site_id;
protected static $wordcamp_root_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;
protected static $events_rome_training_site_id;
/**
* Create sites we'll need for the tests.
*
* @param WP_UnitTest_Factory $factory
*/
public static function wpSetUpBeforeClass( $factory ) {
ms_upload_constants();
global $wpdb;
// Reset the sites table, so the IDs are predictable. WordPress doesn't respect network_id=1.
$wpdb->query( "TRUNCATE TABLE $wpdb->site" );
$wpdb->query( "TRUNCATE TABLE $wpdb->sitemeta" );
$factory->network->create( array(
'domain' => 'wordcamp.test',
'path' => '/',
'subdomain_install' => true,
'network_id' => WORDCAMP_NETWORK_ID,
) );
$factory->network->create( array(
'domain' => 'events.wordpress.test',
'path' => '/',
'network_id' => EVENTS_NETWORK_ID,
) );
self::$wordcamp_root_site_id = $factory->blog->create( array(
'domain' => 'wordcamp.test',
'path' => '/',
'blog_id' => WORDCAMP_ROOT_BLOG_ID,
'network_id' => WORDCAMP_NETWORK_ID,
) );
self::$events_root_site_id = $factory->blog->create( array(
'domain' => 'events.wordpress.test',
'path' => '/',
'blog_id' => EVENTS_ROOT_BLOG_ID,
'network_id' => EVENTS_NETWORK_ID,
) );
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::$events_rome_training_site_id = $factory->blog->create( array(
'domain' => 'events.wordpress.test',
'path' => '/rome/2024/training/',
'network_id' => EVENTS_NETWORK_ID,
) );
// Simulate renamed sites by adding old_home_url blogmeta.
add_site_meta( self::$slash_year_2020_site_id, 'old_home_url', 'https://vancouver.wordcamp.test/2019/' );
add_site_meta( self::$events_rome_training_site_id, 'old_home_url', 'https://events.wordpress.test/rome/2023/training/' );
}
/**
* 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::$wordcamp_root_site_id );
wp_delete_site( self::$events_root_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 );
wp_delete_site( self::$events_rome_training_site_id );
foreach ( [ WORDCAMP_NETWORK_ID, EVENTS_NETWORK_ID ] as $network_id ) {
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $network_id ) );
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id = %d", $network_id ) );
}
}
}