Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix network creation #184

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
12ce2cf
add wp-multi-network-functions
mlwilkerson Oct 25, 2022
04471bf
WIP: working toward a test of adding a network after activating this …
mlwilkerson Oct 25, 2022
d812349
complete a failing test for adding a network after activating this pl…
mlwilkerson Oct 25, 2022
f27c5c6
Fix ReleaseProvider options storage in multisite to use main network
mlwilkerson Oct 25, 2022
c9ad87f
code cleanup and auto-formatting
mlwilkerson Oct 25, 2022
e637cac
add failing tests for multisite upgrade
mlwilkerson Oct 25, 2022
8f4700c
test refactoring
mlwilkerson Oct 25, 2022
2ed559b
add exception for multisite upgrade
mlwilkerson Oct 25, 2022
6cf8697
implement multisite upgrade for moving release metadata network optio…
mlwilkerson Oct 25, 2022
6705c24
when delete_network_option when cleaning up ReleaseProvider
mlwilkerson Oct 25, 2022
15e7027
include network option deletion in the cleanup utility plugin
mlwilkerson Oct 25, 2022
2e05680
bump to verson 4.3.2-1
mlwilkerson Oct 25, 2022
3861bc7
fix and test multisite uninstall
mlwilkerson Oct 26, 2022
7d26a84
WIP: multisite test refactoring
mlwilkerson Oct 26, 2022
0029e3c
fix multisite-upgrade test
mlwilkerson Oct 26, 2022
15572f3
add additional assertion to multisite-activation test
mlwilkerson Oct 26, 2022
e5af8e0
more multisite test refactoring
mlwilkerson Oct 26, 2022
4169edd
test cleanup
mlwilkerson Oct 26, 2022
0e1e7fe
auto-formatting
mlwilkerson Oct 26, 2022
e61cf13
ignoreFile for phpcs
mlwilkerson Oct 26, 2022
bb4f7c9
add missing @throws
mlwilkerson Oct 26, 2022
55c94a7
exclude new multisite tests from non-multisite test config
mlwilkerson Oct 26, 2022
548ea9f
bump tested up to 6.1
mlwilkerson Oct 26, 2022
fa31ce5
use literal ints instead of constants to avoid problems on php 5.6
mlwilkerson Oct 26, 2022
7a1fb00
update test matrix to test with 6.1-RC3
mlwilkerson Oct 26, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add failing tests for multisite upgrade
  • Loading branch information
mlwilkerson committed Oct 25, 2022
commit e637cac9d6f9e9bc5240d24b4392868f9fa2e50c
173 changes: 173 additions & 0 deletions tests/test-multisite-upgrade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php
namespace FortAwesome;
/**
* Class OptionsTest
*
* @noinspection PhpCSValidationInspection
*/
// phpcs:ignoreFile Squiz.Commenting.ClassComment.Missing
// phpcs:ignoreFile Generic.Commenting.DocComment.MissingShort
require_once dirname( __FILE__ ) . '/../includes/class-fontawesome-activator.php';
require_once dirname( __FILE__ ) . '/_support/font-awesome-phpunit-util.php';
require_once dirname( __FILE__ ) . '/_support/wp-multi-network-functions.php';
use Yoast\WPTestUtils\WPIntegration\TestCase;

class MultisiteUpgradeTest extends TestCase {
protected $added_network_ids = array();

public function set_up() {
parent::set_up();
wp_cache_delete ( 'alloptions', 'options' );
reset_db();
delete_option( FontAwesome::OPTIONS_KEY );

/**
* We need this to be defined for this test, though it's normally defined in
* the top-level loader file that is not being included in this test configuration.
*/
if ( ! defined( 'FONTAWESOME_PLUGIN_FILE' ) ) {
define( 'FONTAWESOME_PLUGIN_FILE', 'font-awesome/index.php' );
}

FontAwesome::reset();
(new Mock_FontAwesome_Metadata_Provider())->mock(
array(
wp_json_encode(
array(
'data' => graphql_releases_query_fixture(),
)
),
wp_json_encode(
array(
'data' => graphql_releases_query_fixture(),
)
)
)
);

// This activates network wide, for all sites that exist at the time.
FontAwesome_Activator::initialize();

add_action( 'add_network', array( $this, 'handle_add_network' ), 99, 2 );
}

public function tear_down() {
FontAwesome_Metadata_Provider::reset();
remove_all_actions( 'add_network' );
}

public function test_try_upgrade_on_main_network_when_release_metatdata_stored_in_non_main_network() {
if ( ! is_multisite() ) {
throw new \Exception();
}

/**
* As of 4.3.2, the initialize() that will have run in the set_up() above will have put the release metadata
* in a network option associated with the main network.
* In 4.3.1, it would have been stored in a network option associated with the *current* network
* at the time of retrieval and storage.
*
* So to simulate the scenario that would have been possible in 4.3.1, we'll move it to a non-main network,
* such that the release metadata are stored on an option associated with the *current* network at the time
* of retrieval and storage.
*/

// Create a new, non-main network.
$new_network_id = self::add_network();
$main_network_id = get_main_network_id();

// Get the metadata that would have been stored on a main network option.
$opt = get_network_option( $main_network_id, FontAwesome_Release_Provider::OPTIONS_KEY );

// Put it on an option associated with the new network.
update_network_option( $new_network_id, FontAwesome_Release_Provider::OPTIONS_KEY, $opt );

// And get rid of the original one on the main network.
delete_network_option( $main_network_id, FontAwesome_Release_Provider::OPTIONS_KEY );

$this->assertFalse( get_network_option( $main_network_id, FontAwesome_Release_Provider::OPTIONS_KEY ) );
$this->assertArrayHasKey( 'refreshed_at', get_network_option( $new_network_id, FontAwesome_Release_Provider::OPTIONS_KEY ) );

// Clear options cache.
wp_cache_delete ( 'alloptions', 'options' );

// Expecting no exception to be thrown.
$this->assertNull( fa()->try_upgrade() );

$this->assertFalse( get_network_option( $new_network_id, FontAwesome_Release_Provider::OPTIONS_KEY ) );
$this->assertArrayHasKey( 'refreshed_at', get_network_option( $main_network_id, FontAwesome_Release_Provider::OPTIONS_KEY ) );
}

public function test_try_upgrade_on_non_main_network_when_release_metatdata_stored_in_non_main_network() {
if ( ! is_multisite() ) {
throw new \Exception();
}

/**
* As of 4.3.2, the initialize() that will have run in the set_up() above will have put the release metadata
* in a network option associated with the main network.
* In 4.3.1, it would have been stored in a network option associated with the *current* network
* at the time of retrieval and storage.
*
* So to simulate the scenario that would have been possible in 4.3.1, we'll move it to a non-main network,
* such that the release metadata are stored on an option associated with the *current* network at the time
* of retrieval and storage.
*/

// Create a new, non-main network.
$new_network_id = self::add_network();
$main_network_id = get_main_network_id();

// Get the metadata that would have been stored on a main network option.
$opt = get_network_option( $main_network_id, FontAwesome_Release_Provider::OPTIONS_KEY );

// Put it on an option associated with the new network.
update_network_option( $new_network_id, FontAwesome_Release_Provider::OPTIONS_KEY, $opt );

// And get rid of the original one on the main network.
delete_network_option( $main_network_id, FontAwesome_Release_Provider::OPTIONS_KEY );

$this->assertFalse( get_network_option( $main_network_id, FontAwesome_Release_Provider::OPTIONS_KEY ) );
$this->assertArrayHasKey( 'refreshed_at', get_network_option( $new_network_id, FontAwesome_Release_Provider::OPTIONS_KEY ) );

// Clear options cache.
wp_cache_delete ( 'alloptions', 'options' );

// Now switch to that new network.
\switch_to_network( $new_network_id );

// Expecting no exception to be thrown.
$this->assertNull( fa()->try_upgrade() );

$this->assertFalse( get_network_option( $new_network_id, FontAwesome_Release_Provider::OPTIONS_KEY ) );
$this->assertArrayHasKey( 'refreshed_at', get_network_option( $main_network_id, FontAwesome_Release_Provider::OPTIONS_KEY ) );
}

public function handle_add_network( $network_id, $params ) {
array_push( $this->added_network_ids, $network_id );
}

public static function add_network() {
$sub_domain = dechex( wp_rand( PHP_INT_MIN, PHP_INT_MAX ) );
$domain = "$sub_domain.example.com";
$path = '/';

$admin_user = get_users( array( 'role' => 'administrator' ) )[0];
$result = \add_network(
array(
'domain' => $domain,
'path' => '/',
'site_name' => $domain,
'network_name' => $domain,
'user_id' => $admin_user->ID,
'network_admin_id' => $admin_user->ID,
)
);

if ( is_wp_error( $result ) ) {
throw new \Exception( 'failed creating network' );
}

return $result;
}
}