|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Automattic\WP\Cron_Control\Tests; |
| 4 | + |
| 5 | +use Automattic\WP\Cron_Control\CLI; |
| 6 | +use WP_Site; |
| 7 | + |
| 8 | +class Orchestrate_Sites_Tests extends \WP_UnitTestCase { |
| 9 | + function setUp() { |
| 10 | + if ( ! is_multisite() ) { |
| 11 | + $this->markTestSkipped( 'Skipping tests that only run on multisites.' ); |
| 12 | + } |
| 13 | + |
| 14 | + parent::setUp(); |
| 15 | + } |
| 16 | + |
| 17 | + function tearDown() { |
| 18 | + parent::tearDown(); |
| 19 | + } |
| 20 | + |
| 21 | + function test_list_sites_removes_inactive_subsites() { |
| 22 | + add_filter( 'sites_pre_query', [ $this, 'mock_get_sites' ], 10, 2 ); |
| 23 | + |
| 24 | + // The archived/spam/deleted subsites should not be returned. |
| 25 | + $expected = wp_json_encode( [ [ 'url' => 'site1.com' ], [ 'url' => 'site2.com/two' ], [ 'url' => 'site3.com/three' ], [ 'url' => 'site7.com/seven' ] ] ); |
| 26 | + $this->expectOutputString( $expected ); |
| 27 | + ( new CLI\Orchestrate_Sites() )->list(); |
| 28 | + |
| 29 | + remove_filter( 'sites_pre_query', [ $this, 'mock_get_sites' ], 10, 2 ); |
| 30 | + } |
| 31 | + |
| 32 | + function test_list_sites_2_hosts() { |
| 33 | + add_filter( 'sites_pre_query', [ $this, 'mock_get_sites' ], 10, 2 ); |
| 34 | + |
| 35 | + // With two hosts, all active sites should still be returned. |
| 36 | + $this->mock_hosts_list( 2 ); |
| 37 | + $expected = wp_json_encode( [ [ 'url' => 'site1.com' ], [ 'url' => 'site2.com/two' ], [ 'url' => 'site3.com/three' ], [ 'url' => 'site7.com/seven' ] ] ); |
| 38 | + $this->expectOutputString( $expected ); |
| 39 | + ( new CLI\Orchestrate_Sites() )->list(); |
| 40 | + |
| 41 | + remove_filter( 'sites_pre_query', [ $this, 'mock_get_sites' ], 10, 2 ); |
| 42 | + } |
| 43 | + |
| 44 | + function test_list_sites_7_hosts() { |
| 45 | + add_filter( 'sites_pre_query', [ $this, 'mock_get_sites' ], 10, 2 ); |
| 46 | + |
| 47 | + // With seven hosts, our current request should only be given two of the active sites. |
| 48 | + $this->mock_hosts_list( 7 ); |
| 49 | + $expected = wp_json_encode( [ [ 'url' => 'site1.com' ], [ 'url' => 'site7.com/seven' ] ] ); |
| 50 | + $this->expectOutputString( $expected ); |
| 51 | + ( new CLI\Orchestrate_Sites() )->list(); |
| 52 | + |
| 53 | + remove_filter( 'sites_pre_query', [ $this, 'mock_get_sites' ], 10, 2 ); |
| 54 | + } |
| 55 | + |
| 56 | + function mock_hosts_list( $number_of_hosts ) { |
| 57 | + // Always have the "current" host. |
| 58 | + $heartbeats = [ gethostname() => time() ]; |
| 59 | + |
| 60 | + if ( $number_of_hosts > 1 ) { |
| 61 | + for ( $i = 1; $i < $number_of_hosts; $i++ ) { |
| 62 | + $heartbeats[ "test_$i" ] = time(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + wp_cache_set( CLI\Orchestrate_Sites::RUNNER_HOST_HEARTBEAT_KEY, $heartbeats ); |
| 67 | + } |
| 68 | + |
| 69 | + function mock_get_sites( $site_data, $query_class ) { |
| 70 | + if ( $query_class->query_vars['count'] ) { |
| 71 | + return 7; |
| 72 | + } |
| 73 | + |
| 74 | + return [ |
| 75 | + new WP_Site( (object) [ 'domain' => 'site1.com', 'path' => '/' ] ), |
| 76 | + new WP_Site( (object) [ 'domain' => 'site2.com', 'path' => '/two' ] ), |
| 77 | + new WP_Site( (object) [ 'domain' => 'site3.com', 'path' => '/three' ] ), |
| 78 | + new WP_Site( (object) [ 'domain' => 'site4.com', 'path' => '/four', 'archived' => '1' ] ), |
| 79 | + new WP_Site( (object) [ 'domain' => 'site5.com', 'path' => '/five', 'spam' => '1' ] ), |
| 80 | + new WP_Site( (object) [ 'domain' => 'site6.com', 'path' => '/six', 'deleted' => '1' ] ), |
| 81 | + new WP_Site( (object) [ 'domain' => 'site7.com', 'path' => '/seven' ] ), |
| 82 | + ]; |
| 83 | + } |
| 84 | +} |
0 commit comments