Skip to content

Commit c0f3bd3

Browse files
Copilotswissspidy
andcommitted
Fix network ID resolution and add true multinetwork test
- Fix update_db to call get_current_network_id() first (WP 4.9+) so that the --url parameter actually determines the target network. Previously, SITE_ID_CURRENT_SITE (always 1 in wp-config.php) was checked first, making the fallback to get_current_network_id() unreachable. - Replace the simplistic scenario with a proper two-network setup: creates sites in both networks, verifies the primary network run only upgrades its 3 sites, then verifies --url targets network 2 and upgrades its 2 sites. Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 7c99105 commit c0f3bd3

2 files changed

Lines changed: 28 additions & 16 deletions

File tree

‎features/core-update-db.feature‎

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ Feature: Update core's database
153153

154154
# This test downgrades to an older WordPress version, but the SQLite plugin requires 6.0+
155155
@require-mysql
156-
Scenario: Update db respects current network in multinetwork setup
156+
Scenario: Update db targets only the specified network in multinetwork setup
157157
Given a WP multisite install
158158
And a disable_sidebar_check.php file:
159159
"""
@@ -166,18 +166,21 @@ Feature: Update core's database
166166
And I run `wp core download --version=5.4 --force`
167167
And I run `wp option update db_version 45805 --require=disable_sidebar_check.php`
168168
And I run `wp site option update wpmu_upgrade_site 45805`
169-
And I run `wp site create --slug=foo`
170-
And I run `wp site create --slug=bar`
171-
172-
When I run `wp eval "echo defined('SITE_ID_CURRENT_SITE') ? SITE_ID_CURRENT_SITE : 'not defined';"`
173-
Then STDOUT should contain:
174-
"""
175-
1
176-
"""
169+
# Create two sites in network 1 (main site already exists; total 3)
170+
And I run `wp site create --slug=net1-site1`
171+
And I run `wp site create --slug=net1-site2`
172+
# Create two sites that will be moved to a second network
173+
And I run `wp site create --slug=net2-site1 --porcelain`
174+
And save STDOUT as {NET2_SITE1_ID}
175+
And I run `wp site create --slug=net2-site2 --porcelain`
176+
And save STDOUT as {NET2_SITE2_ID}
177+
# Register a second network and reassign those two sites to it
178+
And I run `wp eval 'global $wpdb; $wpdb->insert( $wpdb->site, [ "domain" => "localhost", "path" => "/network2/" ] ); $n2 = $wpdb->insert_id; $wpdb->update( $wpdb->blogs, [ "site_id" => $n2 ], [ "blog_id" => {NET2_SITE1_ID} ] ); $wpdb->update( $wpdb->blogs, [ "site_id" => $n2 ], [ "blog_id" => {NET2_SITE2_ID} ] );'`
177179

178180
When I run `wp site option get wpmu_upgrade_site`
179181
Then save STDOUT as {UPDATE_VERSION}
180182

183+
# Running without --url targets the current (primary) network: 3 sites in network 1
181184
When I run `wp core update-db --network`
182185
Then STDOUT should contain:
183186
"""
@@ -190,6 +193,13 @@ Feature: Update core's database
190193
{UPDATE_VERSION}
191194
"""
192195

196+
# Running with --url targeting a network 2 site should process only network 2's 2 sites
197+
When I run `wp core update-db --network --url=localhost/net2-site1/`
198+
Then STDOUT should contain:
199+
"""
200+
Success: WordPress database upgraded on 2/2 sites.
201+
"""
202+
193203
Scenario: Ensure update-db sets WP_INSTALLING constant
194204
Given a WP install
195205
And a before.php file:

‎src/Core_Command.php‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1494,14 +1494,16 @@ public function update_db( $args, $assoc_args ) {
14941494
}
14951495

14961496
if ( $network ) {
1497-
// Determine the network ID to update
1498-
// In multinetwork setups, use the current network (determined by --url parameter)
1499-
$network_id = defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : null;
1500-
if ( null === $network_id && function_exists( 'get_current_network_id' ) ) {
1497+
// Determine the network ID to update.
1498+
// get_current_network_id() (available since WP 4.9) reflects the network
1499+
// determined from the --url context, enabling per-network updates in
1500+
// multinetwork setups. Fall back to SITE_ID_CURRENT_SITE for older WordPress.
1501+
if ( function_exists( 'get_current_network_id' ) ) {
15011502
$network_id = get_current_network_id();
1502-
}
1503-
if ( ! $network_id ) {
1504-
$network_id = 1; // Default to primary network
1503+
} elseif ( defined( 'SITE_ID_CURRENT_SITE' ) ) {
1504+
$network_id = (int) SITE_ID_CURRENT_SITE;
1505+
} else {
1506+
$network_id = 1;
15051507
}
15061508

15071509
$iterator_args = [

0 commit comments

Comments
 (0)