Changeset 60148 for trunk/tests/phpunit/tests/option/multisite.php
- Timestamp:
- 04/09/2025 01:29:39 PM (6 weeks ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/option/multisite.php
r54637 r60148 1 1 <?php 2 2 3 if ( is_multisite() ) : 4 5 /** 6 * Tests specific to network and site options in Multisite. 7 * 8 * @group option 9 * @group ms-option 3 /** 4 * Tests specific to network and site options in Multisite. 5 * 6 * @group option 7 * @group ms-option 8 * @group ms-required 9 * @group multisite 10 */ 11 class Tests_Option_Multisite extends WP_UnitTestCase { 12 13 /** 14 * @covers ::get_blog_option 15 * @covers ::get_option 16 * @covers ::add_blog_option 17 * @covers ::update_blog_option 18 * @covers ::delete_blog_option 19 */ 20 public function test_from_same_site() { 21 $key = __FUNCTION__ . '_1'; 22 $key2 = __FUNCTION__ . '_2'; 23 $value = __FUNCTION__ . '_val1'; 24 $value2 = __FUNCTION__ . '_val2'; 25 26 $this->assertFalse( get_blog_option( 1, 'doesnotexist' ) ); 27 $this->assertFalse( get_option( 'doesnotexist' ) ); // Check get_option(). 28 29 $this->assertTrue( add_blog_option( 1, $key, $value ) ); 30 // Assert all values of $blog_id that means the current or main blog (the same here). 31 $this->assertSame( $value, get_blog_option( 1, $key ) ); 32 $this->assertSame( $value, get_blog_option( null, $key ) ); 33 $this->assertSame( $value, get_blog_option( '1', $key ) ); 34 $this->assertSame( $value, get_option( $key ) ); // Check get_option(). 35 36 $this->assertFalse( add_blog_option( 1, $key, $value ) ); // Already exists. 37 $this->assertFalse( update_blog_option( 1, $key, $value ) ); // Value is the same. 38 $this->assertTrue( update_blog_option( 1, $key, $value2 ) ); 39 $this->assertSame( $value2, get_blog_option( 1, $key ) ); 40 $this->assertSame( $value2, get_option( $key ) ); // Check get_option(). 41 $this->assertFalse( add_blog_option( 1, $key, $value ) ); 42 $this->assertSame( $value2, get_blog_option( 1, $key ) ); 43 $this->assertSame( $value2, get_option( $key ) ); // Check get_option(). 44 45 $this->assertTrue( delete_blog_option( 1, $key ) ); 46 $this->assertFalse( get_blog_option( 1, $key ) ); 47 $this->assertFalse( get_option( $key ) ); // Check get_option(). 48 $this->assertFalse( delete_blog_option( 1, $key ) ); 49 $this->assertTrue( update_blog_option( 1, $key2, $value2 ) ); 50 $this->assertSame( $value2, get_blog_option( 1, $key2 ) ); 51 $this->assertSame( $value2, get_option( $key2 ) ); // Check get_option(). 52 $this->assertTrue( delete_blog_option( 1, $key2 ) ); 53 $this->assertFalse( get_blog_option( 1, $key2 ) ); 54 $this->assertFalse( get_option( $key2 ) ); // Check get_option(). 55 } 56 57 /** 58 * @covers ::get_blog_option 59 * @covers ::get_option 60 * @covers ::add_blog_option 61 * @covers ::update_blog_option 62 * @covers ::delete_blog_option 63 */ 64 public function test_from_same_site_with_null_blog_id() { 65 $key = __FUNCTION__ . '_1'; 66 $key2 = __FUNCTION__ . '_2'; 67 $value = __FUNCTION__ . '_val1'; 68 $value2 = __FUNCTION__ . '_val2'; 69 70 $this->assertFalse( get_blog_option( null, 'doesnotexist' ) ); 71 $this->assertFalse( get_option( 'doesnotexist' ) ); // Check get_option(). 72 73 $this->assertTrue( add_blog_option( null, $key, $value ) ); 74 // Assert all values of $blog_id that means the current or main blog (the same here). 75 $this->assertSame( $value, get_blog_option( null, $key ) ); 76 $this->assertSame( $value, get_blog_option( null, $key ) ); 77 $this->assertSame( $value, get_option( $key ) ); // Check get_option(). 78 79 $this->assertFalse( add_blog_option( null, $key, $value ) ); // Already exists. 80 $this->assertFalse( update_blog_option( null, $key, $value ) ); // Value is the same. 81 $this->assertTrue( update_blog_option( null, $key, $value2 ) ); 82 $this->assertSame( $value2, get_blog_option( null, $key ) ); 83 $this->assertSame( $value2, get_option( $key ) ); // Check get_option(). 84 $this->assertFalse( add_blog_option( null, $key, $value ) ); 85 $this->assertSame( $value2, get_blog_option( null, $key ) ); 86 $this->assertSame( $value2, get_option( $key ) ); // Check get_option(). 87 88 $this->assertTrue( delete_blog_option( null, $key ) ); 89 $this->assertFalse( get_blog_option( null, $key ) ); 90 $this->assertFalse( get_option( $key ) ); // Check get_option(). 91 $this->assertFalse( delete_blog_option( null, $key ) ); 92 $this->assertTrue( update_blog_option( null, $key2, $value2 ) ); 93 $this->assertSame( $value2, get_blog_option( null, $key2 ) ); 94 $this->assertSame( $value2, get_option( $key2 ) ); // Check get_option(). 95 $this->assertTrue( delete_blog_option( null, $key2 ) ); 96 $this->assertFalse( get_blog_option( null, $key2 ) ); 97 $this->assertFalse( get_option( $key2 ) ); // Check get_option(). 98 } 99 100 /** 101 * @covers ::get_blog_option 102 * @covers ::get_option 103 * @covers ::add_blog_option 104 * @covers ::update_blog_option 105 * @covers ::delete_blog_option 106 */ 107 public function test_with_another_site() { 108 $user_id = self::factory()->user->create(); 109 $this->assertIsInt( $user_id ); 110 111 $blog_id = self::factory()->blog->create( 112 array( 113 'user_id' => $user_id, 114 'public' => 1, 115 ) 116 ); 117 $this->assertIsInt( $blog_id ); 118 119 $key = __FUNCTION__ . '_key1'; 120 $key2 = __FUNCTION__ . '_key2'; 121 $value = __FUNCTION__ . '_val1'; 122 $value2 = __FUNCTION__ . '_val2'; 123 124 $this->assertFalse( get_blog_option( $blog_id, 'doesnotexist' ) ); 125 // $this->assertFalse( get_option( 'doesnotexist' ) ); // Check get_option(). 126 127 $this->assertTrue( add_blog_option( $blog_id, $key, $value ) ); 128 // Assert all values of $blog_id that means the current or main blog (the same here). 129 $this->assertSame( $value, get_blog_option( $blog_id, $key ) ); 130 $this->assertSame( $value, get_blog_option( (string) $blog_id, $key ) ); 131 // $this->assertSame( $value, get_option( $key ) ); // Check get_option(). 132 133 $this->assertFalse( add_blog_option( $blog_id, $key, $value ) ); // Already exists. 134 $this->assertFalse( update_blog_option( $blog_id, $key, $value ) ); // Value is the same. 135 $this->assertTrue( update_blog_option( $blog_id, $key, $value2 ) ); 136 $this->assertSame( $value2, get_blog_option( $blog_id, $key ) ); 137 // $this->assertSame( $value2, get_option( $key ) ); // Check get_option(). 138 $this->assertFalse( add_blog_option( $blog_id, $key, $value ) ); 139 $this->assertSame( $value2, get_blog_option( $blog_id, $key ) ); 140 // $this->assertSame( $value2, get_option( $key ) ); // Check get_option(). 141 142 $this->assertTrue( delete_blog_option( $blog_id, $key ) ); 143 $this->assertFalse( get_blog_option( $blog_id, $key ) ); 144 // $this->assertFalse( get_option( $key ) ); // Check get_option(). 145 $this->assertFalse( delete_blog_option( $blog_id, $key ) ); 146 $this->assertTrue( update_blog_option( $blog_id, $key2, $value2 ) ); 147 $this->assertSame( $value2, get_blog_option( $blog_id, $key2 ) ); 148 // $this->assertSame( $value2, get_option( $key2 ) ); // Check get_option(). 149 $this->assertTrue( delete_blog_option( $blog_id, $key2 ) ); 150 $this->assertFalse( get_blog_option( $blog_id, $key2 ) ); 151 // $this->assertFalse( get_option( $key2 ) ); // Check get_option(). 152 } 153 154 /** 10 155 * @group multisite 11 */ 12 class Tests_Option_Multisite extends WP_UnitTestCase { 13 14 /** 15 * @covers ::get_blog_option 16 * @covers ::get_option 17 * @covers ::add_blog_option 18 * @covers ::update_blog_option 19 * @covers ::delete_blog_option 20 */ 21 public function test_from_same_site() { 22 $key = __FUNCTION__ . '_1'; 23 $key2 = __FUNCTION__ . '_2'; 24 $value = __FUNCTION__ . '_val1'; 25 $value2 = __FUNCTION__ . '_val2'; 26 27 $this->assertFalse( get_blog_option( 1, 'doesnotexist' ) ); 28 $this->assertFalse( get_option( 'doesnotexist' ) ); // Check get_option(). 29 30 $this->assertTrue( add_blog_option( 1, $key, $value ) ); 31 // Assert all values of $blog_id that means the current or main blog (the same here). 32 $this->assertSame( $value, get_blog_option( 1, $key ) ); 33 $this->assertSame( $value, get_blog_option( null, $key ) ); 34 $this->assertSame( $value, get_blog_option( '1', $key ) ); 35 $this->assertSame( $value, get_option( $key ) ); // Check get_option(). 36 37 $this->assertFalse( add_blog_option( 1, $key, $value ) ); // Already exists. 38 $this->assertFalse( update_blog_option( 1, $key, $value ) ); // Value is the same. 39 $this->assertTrue( update_blog_option( 1, $key, $value2 ) ); 40 $this->assertSame( $value2, get_blog_option( 1, $key ) ); 41 $this->assertSame( $value2, get_option( $key ) ); // Check get_option(). 42 $this->assertFalse( add_blog_option( 1, $key, $value ) ); 43 $this->assertSame( $value2, get_blog_option( 1, $key ) ); 44 $this->assertSame( $value2, get_option( $key ) ); // Check get_option(). 45 46 $this->assertTrue( delete_blog_option( 1, $key ) ); 47 $this->assertFalse( get_blog_option( 1, $key ) ); 48 $this->assertFalse( get_option( $key ) ); // Check get_option(). 49 $this->assertFalse( delete_blog_option( 1, $key ) ); 50 $this->assertTrue( update_blog_option( 1, $key2, $value2 ) ); 51 $this->assertSame( $value2, get_blog_option( 1, $key2 ) ); 52 $this->assertSame( $value2, get_option( $key2 ) ); // Check get_option(). 53 $this->assertTrue( delete_blog_option( 1, $key2 ) ); 54 $this->assertFalse( get_blog_option( 1, $key2 ) ); 55 $this->assertFalse( get_option( $key2 ) ); // Check get_option(). 56 } 57 58 /** 59 * @covers ::get_blog_option 60 * @covers ::get_option 61 * @covers ::add_blog_option 62 * @covers ::update_blog_option 63 * @covers ::delete_blog_option 64 */ 65 public function test_from_same_site_with_null_blog_id() { 66 $key = __FUNCTION__ . '_1'; 67 $key2 = __FUNCTION__ . '_2'; 68 $value = __FUNCTION__ . '_val1'; 69 $value2 = __FUNCTION__ . '_val2'; 70 71 $this->assertFalse( get_blog_option( null, 'doesnotexist' ) ); 72 $this->assertFalse( get_option( 'doesnotexist' ) ); // Check get_option(). 73 74 $this->assertTrue( add_blog_option( null, $key, $value ) ); 75 // Assert all values of $blog_id that means the current or main blog (the same here). 76 $this->assertSame( $value, get_blog_option( null, $key ) ); 77 $this->assertSame( $value, get_blog_option( null, $key ) ); 78 $this->assertSame( $value, get_option( $key ) ); // Check get_option(). 79 80 $this->assertFalse( add_blog_option( null, $key, $value ) ); // Already exists. 81 $this->assertFalse( update_blog_option( null, $key, $value ) ); // Value is the same. 82 $this->assertTrue( update_blog_option( null, $key, $value2 ) ); 83 $this->assertSame( $value2, get_blog_option( null, $key ) ); 84 $this->assertSame( $value2, get_option( $key ) ); // Check get_option(). 85 $this->assertFalse( add_blog_option( null, $key, $value ) ); 86 $this->assertSame( $value2, get_blog_option( null, $key ) ); 87 $this->assertSame( $value2, get_option( $key ) ); // Check get_option(). 88 89 $this->assertTrue( delete_blog_option( null, $key ) ); 90 $this->assertFalse( get_blog_option( null, $key ) ); 91 $this->assertFalse( get_option( $key ) ); // Check get_option(). 92 $this->assertFalse( delete_blog_option( null, $key ) ); 93 $this->assertTrue( update_blog_option( null, $key2, $value2 ) ); 94 $this->assertSame( $value2, get_blog_option( null, $key2 ) ); 95 $this->assertSame( $value2, get_option( $key2 ) ); // Check get_option(). 96 $this->assertTrue( delete_blog_option( null, $key2 ) ); 97 $this->assertFalse( get_blog_option( null, $key2 ) ); 98 $this->assertFalse( get_option( $key2 ) ); // Check get_option(). 99 } 100 101 /** 102 * @covers ::get_blog_option 103 * @covers ::get_option 104 * @covers ::add_blog_option 105 * @covers ::update_blog_option 106 * @covers ::delete_blog_option 107 */ 108 public function test_with_another_site() { 109 $user_id = self::factory()->user->create(); 110 $this->assertIsInt( $user_id ); 111 112 $blog_id = self::factory()->blog->create( 113 array( 114 'user_id' => $user_id, 115 'public' => 1, 116 ) 117 ); 118 $this->assertIsInt( $blog_id ); 119 120 $key = __FUNCTION__ . '_key1'; 121 $key2 = __FUNCTION__ . '_key2'; 122 $value = __FUNCTION__ . '_val1'; 123 $value2 = __FUNCTION__ . '_val2'; 124 125 $this->assertFalse( get_blog_option( $blog_id, 'doesnotexist' ) ); 126 // $this->assertFalse( get_option( 'doesnotexist' ) ); // Check get_option(). 127 128 $this->assertTrue( add_blog_option( $blog_id, $key, $value ) ); 129 // Assert all values of $blog_id that means the current or main blog (the same here). 130 $this->assertSame( $value, get_blog_option( $blog_id, $key ) ); 131 $this->assertSame( $value, get_blog_option( (string) $blog_id, $key ) ); 132 // $this->assertSame( $value, get_option( $key ) ); // Check get_option(). 133 134 $this->assertFalse( add_blog_option( $blog_id, $key, $value ) ); // Already exists. 135 $this->assertFalse( update_blog_option( $blog_id, $key, $value ) ); // Value is the same. 136 $this->assertTrue( update_blog_option( $blog_id, $key, $value2 ) ); 137 $this->assertSame( $value2, get_blog_option( $blog_id, $key ) ); 138 // $this->assertSame( $value2, get_option( $key ) ); // Check get_option(). 139 $this->assertFalse( add_blog_option( $blog_id, $key, $value ) ); 140 $this->assertSame( $value2, get_blog_option( $blog_id, $key ) ); 141 // $this->assertSame( $value2, get_option( $key ) ); // Check get_option(). 142 143 $this->assertTrue( delete_blog_option( $blog_id, $key ) ); 144 $this->assertFalse( get_blog_option( $blog_id, $key ) ); 145 // $this->assertFalse( get_option( $key ) ); // Check get_option(). 146 $this->assertFalse( delete_blog_option( $blog_id, $key ) ); 147 $this->assertTrue( update_blog_option( $blog_id, $key2, $value2 ) ); 148 $this->assertSame( $value2, get_blog_option( $blog_id, $key2 ) ); 149 // $this->assertSame( $value2, get_option( $key2 ) ); // Check get_option(). 150 $this->assertTrue( delete_blog_option( $blog_id, $key2 ) ); 151 $this->assertFalse( get_blog_option( $blog_id, $key2 ) ); 152 // $this->assertFalse( get_option( $key2 ) ); // Check get_option(). 153 } 154 155 /** 156 * @group multisite 157 * 158 * @covers ::get_site_option 159 */ 160 public function test_site_notoptions() { 161 $network_id = get_current_network_id(); 162 $notoptions_key = "{$network_id}:notoptions"; 163 164 $_notoptions = wp_cache_get( 'notoptions', 'site-options' ); 165 $this->assertEmpty( $_notoptions ); 166 $_notoptions1 = wp_cache_get( $notoptions_key, 'site-options' ); 167 $this->assertEmpty( $_notoptions1 ); 168 169 get_site_option( 'burrito' ); 170 171 $notoptions = wp_cache_get( 'notoptions', 'site-options' ); 172 $this->assertEmpty( $notoptions ); 173 $notoptions1 = wp_cache_get( $notoptions_key, 'site-options' ); 174 $this->assertNotEmpty( $notoptions1 ); 175 } 176 177 /** 178 * @covers ::users_can_register_signup_filter 179 * @covers ::get_site_option 180 */ 181 public function test_users_can_register_signup_filter() { 182 183 get_site_option( 'registration' ); 184 $this->assertFalse( users_can_register_signup_filter() ); 185 186 update_site_option( 'registration', 'all' ); 187 $this->assertTrue( users_can_register_signup_filter() ); 188 189 update_site_option( 'registration', 'user' ); 190 $this->assertTrue( users_can_register_signup_filter() ); 191 192 update_site_option( 'registration', 'none' ); 193 $this->assertFalse( users_can_register_signup_filter() ); 194 } 195 196 /** 197 * @dataProvider data_illegal_names 198 * 199 * @covers ::update_site_option 200 * @covers ::get_site_option 201 */ 202 public function test_sanitize_network_option_illegal_names( $option_value, $sanitized_option_value ) { 203 update_site_option( 'illegal_names', $option_value ); 204 $this->assertSame( $sanitized_option_value, get_site_option( 'illegal_names' ) ); 205 } 206 207 public function data_illegal_names() { 208 return array( 209 array( array( '', 'Woo', '' ), array( 'Woo' ) ), 210 array( 'foo bar', array( 'foo', 'bar' ) ), 211 array( array(), '' ), 212 ); 213 } 214 215 /** 216 * @dataProvider data_email_domains 217 * 218 * @param $option_value 219 * @param $sanitized_option_value 220 * 221 * @covers ::update_site_option 222 * @covers ::get_site_option 223 */ 224 public function test_sanitize_network_option_limited_email_domains( $option_value, $sanitized_option_value ) { 225 update_site_option( 'limited_email_domains', $option_value ); 226 $this->assertSame( $sanitized_option_value, get_site_option( 'limited_email_domains' ) ); 227 } 228 229 /** 230 * @dataProvider data_email_domains 231 * 232 * @param $option_value 233 * @param $sanitized_option_value 234 * 235 * @covers ::update_site_option 236 * @covers ::get_site_option 237 */ 238 public function test_sanitize_network_option_banned_email_domains( $option_value, $sanitized_option_value ) { 239 update_site_option( 'banned_email_domains', $option_value ); 240 $this->assertSame( $sanitized_option_value, get_site_option( 'banned_email_domains' ) ); 241 } 242 243 public function data_email_domains() { 244 return array( 245 array( array( 'woo', '', 'boo.com', 'foo.net.biz..' ), array( 'woo', 'boo.com' ) ), 246 array( "foo\nbar", array( 'foo', 'bar' ) ), 247 array( "foo\n\nbar", array( 'foo', 'bar' ) ), 248 array( "\nfoo\nbar\n", array( 'foo', 'bar' ) ), 249 array( "foo\nfoo.net.biz..", array( 'foo' ) ), 250 array( "foo\nfoo.net.biz..\nbar.com", array( 'foo', 'bar.com' ) ), 251 array( 'foo.', array( 'foo.' ) ), 252 array( '.foo', array( '.foo' ) ), 253 array( 'foo^net', '' ), 254 array( array(), '' ), 255 ); 256 } 257 } 258 259 endif; 156 * 157 * @covers ::get_site_option 158 */ 159 public function test_site_notoptions() { 160 $network_id = get_current_network_id(); 161 $notoptions_key = "{$network_id}:notoptions"; 162 163 $_notoptions = wp_cache_get( 'notoptions', 'site-options' ); 164 $this->assertEmpty( $_notoptions ); 165 $_notoptions1 = wp_cache_get( $notoptions_key, 'site-options' ); 166 $this->assertEmpty( $_notoptions1 ); 167 168 get_site_option( 'burrito' ); 169 170 $notoptions = wp_cache_get( 'notoptions', 'site-options' ); 171 $this->assertEmpty( $notoptions ); 172 $notoptions1 = wp_cache_get( $notoptions_key, 'site-options' ); 173 $this->assertNotEmpty( $notoptions1 ); 174 } 175 176 /** 177 * @covers ::users_can_register_signup_filter 178 * @covers ::get_site_option 179 */ 180 public function test_users_can_register_signup_filter() { 181 182 get_site_option( 'registration' ); 183 $this->assertFalse( users_can_register_signup_filter() ); 184 185 update_site_option( 'registration', 'all' ); 186 $this->assertTrue( users_can_register_signup_filter() ); 187 188 update_site_option( 'registration', 'user' ); 189 $this->assertTrue( users_can_register_signup_filter() ); 190 191 update_site_option( 'registration', 'none' ); 192 $this->assertFalse( users_can_register_signup_filter() ); 193 } 194 195 /** 196 * @dataProvider data_illegal_names 197 * 198 * @covers ::update_site_option 199 * @covers ::get_site_option 200 */ 201 public function test_sanitize_network_option_illegal_names( $option_value, $sanitized_option_value ) { 202 update_site_option( 'illegal_names', $option_value ); 203 $this->assertSame( $sanitized_option_value, get_site_option( 'illegal_names' ) ); 204 } 205 206 public function data_illegal_names() { 207 return array( 208 array( array( '', 'Woo', '' ), array( 'Woo' ) ), 209 array( 'foo bar', array( 'foo', 'bar' ) ), 210 array( array(), '' ), 211 ); 212 } 213 214 /** 215 * @dataProvider data_email_domains 216 * 217 * @param $option_value 218 * @param $sanitized_option_value 219 * 220 * @covers ::update_site_option 221 * @covers ::get_site_option 222 */ 223 public function test_sanitize_network_option_limited_email_domains( $option_value, $sanitized_option_value ) { 224 update_site_option( 'limited_email_domains', $option_value ); 225 $this->assertSame( $sanitized_option_value, get_site_option( 'limited_email_domains' ) ); 226 } 227 228 /** 229 * @dataProvider data_email_domains 230 * 231 * @param $option_value 232 * @param $sanitized_option_value 233 * 234 * @covers ::update_site_option 235 * @covers ::get_site_option 236 */ 237 public function test_sanitize_network_option_banned_email_domains( $option_value, $sanitized_option_value ) { 238 update_site_option( 'banned_email_domains', $option_value ); 239 $this->assertSame( $sanitized_option_value, get_site_option( 'banned_email_domains' ) ); 240 } 241 242 public function data_email_domains() { 243 return array( 244 array( array( 'woo', '', 'boo.com', 'foo.net.biz..' ), array( 'woo', 'boo.com' ) ), 245 array( "foo\nbar", array( 'foo', 'bar' ) ), 246 array( "foo\n\nbar", array( 'foo', 'bar' ) ), 247 array( "\nfoo\nbar\n", array( 'foo', 'bar' ) ), 248 array( "foo\nfoo.net.biz..", array( 'foo' ) ), 249 array( "foo\nfoo.net.biz..\nbar.com", array( 'foo', 'bar.com' ) ), 250 array( 'foo.', array( 'foo.' ) ), 251 array( '.foo', array( '.foo' ) ), 252 array( 'foo^net', '' ), 253 array( array(), '' ), 254 ); 255 } 256 }
Note: See TracChangeset
for help on using the changeset viewer.