@@ -365,4 +365,76 @@ public function update() {
365365 Register::remove_table ( $ klutz_table );
366366 Register::remove_table ( $ zorps_table );
367367 }
368+
369+ /**
370+ * @test
371+ */
372+ public function should_not_create_tables_during_switch_blog_if_blog_not_installed (): void {
373+ // Register the table.
374+ $ table = $ this ->get_simple_table ();
375+ Register::table ( $ table );
376+ $ table ->drop ();
377+ // Sanity check.
378+ $ this ->assertFalse ( $ table ->exists () );
379+ // Set up as if switching to a blog before it's installed, during its creeation.
380+ wp_cache_delete ( 'is_blog_installed ' );
381+ // Remove all other filters to avoid side-effects.
382+ remove_all_filters ( 'switch_blog ' );
383+
384+ $ builder = Schema::builder ();
385+
386+ add_action ( 'switch_blog ' , [ $ builder , 'update_blog_tables ' ] );
387+
388+ do_action ( 'switch_blog ' , 66 );
389+
390+ $ this ->assertFalse ( $ table ->exists () );
391+ }
392+
393+ /**
394+ * @test
395+ */
396+ public function should_create_tables_during_switch_blog_if_blog_installed (): void {
397+ // Register the table.
398+ $ table = $ this ->get_simple_table ();
399+ Register::table ( $ table );
400+ $ table ->drop ();
401+ // Sanity check.
402+ $ this ->assertFalse ( $ table ->exists () );
403+ // Set up as if switching to a blog after it's installed.
404+ wp_cache_set ( 'is_blog_installed ' , true );
405+ // Remove all other filters to avoid side-effects.
406+ remove_all_filters ( 'switch_blog ' );
407+
408+ $ builder = Schema::builder ();
409+
410+ add_action ( 'switch_blog ' , [ $ builder , 'update_blog_tables ' ] );
411+
412+ do_action ( 'switch_blog ' , 66 );
413+
414+ $ this ->assertTrue ( $ table ->exists () );
415+ }
416+
417+ /**
418+ * @test
419+ */
420+ public function should_create_tables_during_activate_blog (): void {
421+ // Register the table.
422+ $ table = $ this ->get_simple_table ();
423+ Register::table ( $ table );
424+ $ table ->drop ();
425+ // Sanity check.
426+ $ this ->assertFalse ( $ table ->exists () );
427+ // Set up as if switching to a blog after it's installed.
428+ wp_cache_set ( 'is_blog_installed ' , true );
429+ // Remove all other filters to avoid side-effects.
430+ remove_all_filters ( 'activate_blog ' );
431+
432+ $ builder = Schema::builder ();
433+
434+ add_action ( 'activate_blog ' , [ $ builder , 'update_blog_tables ' ] );
435+
436+ do_action ( 'activate_blog ' , 66 );
437+
438+ $ this ->assertTrue ( $ table ->exists () );
439+ }
368440}
0 commit comments