|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace WordPress\Blueprints\Tests\Unit\Steps; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use WordPress\Blueprints\Exception\BlueprintExecutionException; |
| 7 | +use WordPress\Blueprints\Progress\Tracker; |
| 8 | +use WordPress\Blueprints\Steps\EnableMultisiteStep; |
| 9 | + |
| 10 | +class EnableMultisiteStepTest extends StepTestCase { |
| 11 | + public function setUp(): void { |
| 12 | + parent::setUp(); |
| 13 | + |
| 14 | + // Set site URL. |
| 15 | + $this->runtime->evalPhpCodeInSubProcess( |
| 16 | + <<<'PHP' |
| 17 | +<?php |
| 18 | +require_once getenv('DOCROOT') . '/wp-load.php'; |
| 19 | +update_option( 'siteurl', 'http://localhost' ); |
| 20 | +PHP |
| 21 | + ); |
| 22 | + } |
| 23 | + |
| 24 | + public function testEnableMultisite() { |
| 25 | + $step = new EnableMultisiteStep(); |
| 26 | + $tracker = new Tracker(); |
| 27 | + $step->run( $this->runtime, $tracker ); |
| 28 | + |
| 29 | + // Verify that multisite is set up and enabled. |
| 30 | + $result = $this->runtime->evalPhpCodeInSubProcess( |
| 31 | + <<<'PHP' |
| 32 | +<?php |
| 33 | +
|
| 34 | +// Load WordPress environment |
| 35 | +require_once getenv('DOCROOT') . '/wp-load.php'; |
| 36 | +
|
| 37 | +// Verify multisite setup |
| 38 | +append_output( |
| 39 | + json_encode( [ |
| 40 | + 'is_multisite' => is_multisite(), |
| 41 | + 'name' => get_bloginfo( 'name' ), |
| 42 | + 'wpurl' => get_bloginfo( 'wpurl' ), |
| 43 | + 'url' => get_bloginfo( 'url' ), |
| 44 | + 'network' => get_network(), |
| 45 | + 'constants' => [ |
| 46 | + 'WP_ALLOW_MULTISITE' => defined( 'WP_ALLOW_MULTISITE' ) ? WP_ALLOW_MULTISITE : null, |
| 47 | + 'MULTISITE' => defined( 'MULTISITE' ) ? MULTISITE : null, |
| 48 | + 'SUBDOMAIN_INSTALL' => defined( 'SUBDOMAIN_INSTALL' ) ? SUBDOMAIN_INSTALL : null, |
| 49 | + 'DOMAIN_CURRENT_SITE' => defined( 'DOMAIN_CURRENT_SITE' ) ? DOMAIN_CURRENT_SITE : null, |
| 50 | + 'PATH_CURRENT_SITE' => defined( 'PATH_CURRENT_SITE' ) ? PATH_CURRENT_SITE : null, |
| 51 | + 'SITE_ID_CURRENT_SITE' => defined( 'SITE_ID_CURRENT_SITE' ) ? SITE_ID_CURRENT_SITE : null, |
| 52 | + 'BLOG_ID_CURRENT_SITE' => defined( 'BLOG_ID_CURRENT_SITE' ) ? BLOG_ID_CURRENT_SITE : null, |
| 53 | + ], |
| 54 | + ] ) |
| 55 | +); |
| 56 | + |
| 57 | +PHP |
| 58 | + ); |
| 59 | + |
| 60 | + $output = json_decode( $result->outputFileContent, true ); |
| 61 | + $this->assertSame( true, $output['is_multisite'] ); |
| 62 | + $this->assertSame( 'WordPress Site', $output['name'] ); |
| 63 | + $this->assertSame( 'http://localhost', $output['wpurl'] ); |
| 64 | + $this->assertSame( 'http://127.0.0.1:80', $output['url'] ); |
| 65 | + |
| 66 | + $network = $output['network']; |
| 67 | + $this->assertSame( 'localhost', $network['domain'] ); |
| 68 | + $this->assertSame( 'localhost', $network['cookie_domain'] ); |
| 69 | + $this->assertSame( '/', $network['path'] ); |
| 70 | + $this->assertSame( 'WordPress Site Sites', $network['site_name'] ); |
| 71 | + |
| 72 | + $constants = $output['constants']; |
| 73 | + $this->assertSame( true, $constants['WP_ALLOW_MULTISITE'] ); |
| 74 | + $this->assertSame( true, $constants['MULTISITE'] ); |
| 75 | + $this->assertSame( false, $constants['SUBDOMAIN_INSTALL'] ); |
| 76 | + $this->assertSame( 'localhost', $constants['DOMAIN_CURRENT_SITE'] ); |
| 77 | + $this->assertSame( '/', $constants['PATH_CURRENT_SITE'] ); |
| 78 | + $this->assertSame( 1, $constants['SITE_ID_CURRENT_SITE'] ); |
| 79 | + $this->assertSame( 1, $constants['BLOG_ID_CURRENT_SITE'] ); |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + public function testEnableMultisiteRedirectsWhenSiteNotFound() { |
| 84 | + $step = new EnableMultisiteStep(); |
| 85 | + $tracker = new Tracker(); |
| 86 | + $step->run( $this->runtime, $tracker ); |
| 87 | + |
| 88 | + // Verify that multisite is set up and enabled. |
| 89 | + $result = $this->runtime->evalPhpCodeInSubProcess( |
| 90 | + <<<'PHP' |
| 91 | +<?php |
| 92 | +
|
| 93 | +register_shutdown_function( function() { |
| 94 | + if ( did_action( 'ms_site_not_found' ) ) { |
| 95 | + append_output( 'redirected' ); |
| 96 | + } |
| 97 | +}); |
| 98 | +
|
| 99 | +$_SERVER['HTTP_HOST'] = 'http://unknown'; |
| 100 | +$_SERVER['REQUEST_URI'] = '/'; |
| 101 | +
|
| 102 | +// Load WordPress environment |
| 103 | +require_once getenv('DOCROOT') . '/wp-load.php'; |
| 104 | +append_output( 'not_redirected' ); |
| 105 | + |
| 106 | +PHP |
| 107 | + ); |
| 108 | + |
| 109 | + // In the CLI SAPI, the "header()" function is a no-op. We can only test |
| 110 | + // that "ms_site_not_found" was called and that the process exited early. |
| 111 | + $this->assertSame( 'redirected', $result->outputFileContent ); |
| 112 | + } |
| 113 | + |
| 114 | + public function testEnableMultisiteFailsOnNon80Port() { |
| 115 | + $this->runtime->evalPhpCodeInSubProcess( |
| 116 | + <<<'PHP' |
| 117 | +<?php |
| 118 | +require_once getenv('DOCROOT') . '/wp-load.php'; |
| 119 | +update_option( 'siteurl', 'http://localhost:8080' ); |
| 120 | +PHP |
| 121 | + ); |
| 122 | + |
| 123 | + $this->expectException( BlueprintExecutionException::class ); |
| 124 | + $this->expectExceptionMessage( 'The current host is "localhost:8080", but WordPress multisites do not support custom ports.' ); |
| 125 | + $step = new EnableMultisiteStep(); |
| 126 | + $tracker = new Tracker(); |
| 127 | + $step->run( $this->runtime, $tracker ); |
| 128 | + } |
| 129 | + |
| 130 | + public function testEnableMultisiteFailsWhenAlreadyEnabled() { |
| 131 | + $step = new EnableMultisiteStep(); |
| 132 | + $tracker = new Tracker(); |
| 133 | + $step->run( $this->runtime, $tracker ); |
| 134 | + |
| 135 | + $this->expectException( BlueprintExecutionException::class ); |
| 136 | + $this->expectExceptionMessage( '[siteid_exists] The network already exists.' ); |
| 137 | + $step->run( $this->runtime, $tracker ); |
| 138 | + } |
| 139 | + |
| 140 | + public function testEnableMultisiteFailsWhenConfigInvalid() { |
| 141 | + $this->runtime->evalPhpCodeInSubProcess( |
| 142 | + <<<'PHP' |
| 143 | +<?php |
| 144 | +require_once getenv('DOCROOT') . '/wp-load.php'; |
| 145 | +delete_option( 'siteurl' ); |
| 146 | +PHP |
| 147 | + ); |
| 148 | + |
| 149 | + $this->expectException( BlueprintExecutionException::class ); |
| 150 | + $this->expectExceptionMessage( 'Failed to enable multisite' ); |
| 151 | + $step = new EnableMultisiteStep(); |
| 152 | + $tracker = new Tracker(); |
| 153 | + $step->run( $this->runtime, $tracker ); |
| 154 | + } |
| 155 | +} |
0 commit comments