| | 1 | <?php |
| | 2 | |
| | 3 | if ( is_multisite() ) : |
| | 4 | |
| | 5 | /** |
| | 6 | * Tests for the populate_network hooks. |
| | 7 | * |
| | 8 | * @group ms-network |
| | 9 | * @group ms-populate-network |
| | 10 | * @group multisite |
| | 11 | */ |
| | 12 | class Tests_Multisite_PopulateNetworkHooks extends WP_UnitTestCase { |
| | 13 | protected $action_counts = array( |
| | 14 | 'before_populate_network' => 0, |
| | 15 | 'after_upgrade_to_multisite' => 0, |
| | 16 | 'after_populate_network' => 0, |
| | 17 | ); |
| | 18 | |
| | 19 | protected $action_args = array(); |
| | 20 | |
| | 21 | /** |
| | 22 | * Flag to track if hook was called. |
| | 23 | */ |
| | 24 | public $hook_called = false; |
| | 25 | |
| | 26 | public function hook_action_counter( $network_id, $domain, $email, $site_name, $path, $subdomain_install ) { |
| | 27 | $action = current_filter(); |
| | 28 | ++$this->action_counts[ $action ]; |
| | 29 | $this->action_args[ $action ] = array( |
| | 30 | 'network_id' => $network_id, |
| | 31 | 'domain' => $domain, |
| | 32 | 'email' => $email, |
| | 33 | 'site_name' => $site_name, |
| | 34 | 'path' => $path, |
| | 35 | 'subdomain_install' => $subdomain_install, |
| | 36 | ); |
| | 37 | } |
| | 38 | |
| | 39 | /** |
| | 40 | * Test that the before_populate_network hook fires. |
| | 41 | * |
| | 42 | * @ticket 27289 |
| | 43 | */ |
| | 44 | public function test_before_populate_network_hook() { |
| | 45 | $this->action_counts = array_fill_keys( array_keys( $this->action_counts ), 0 ); |
| | 46 | $this->action_args = array(); |
| | 47 | |
| | 48 | add_action( 'before_populate_network', array( $this, 'hook_action_counter' ), 10, 6 ); |
| | 49 | add_action( 'after_populate_network', array( $this, 'hook_action_counter' ), 10, 6 ); |
| | 50 | |
| | 51 | $domain = 'example' . time() . '.org'; |
| | 52 | $network_id = self::factory()->network->create( |
| | 53 | array( |
| | 54 | 'domain' => $domain, |
| | 55 | 'path' => '/', |
| | 56 | ) |
| | 57 | ); |
| | 58 | |
| | 59 | $this->assertSame( 1, $this->action_counts['before_populate_network'], 'before_populate_network action should fire once' ); |
| | 60 | $this->assertSame( 1, $this->action_counts['after_populate_network'], 'after_populate_network action should fire once' ); |
| | 61 | |
| | 62 | $this->assertEquals( $network_id, $this->action_args['before_populate_network']['network_id'], 'Network ID should match in before_populate_network hook' ); |
| | 63 | $this->assertEquals( $domain, $this->action_args['before_populate_network']['domain'], 'Domain should match in before_populate_network hook' ); |
| | 64 | $this->assertEquals( $network_id, $this->action_args['after_populate_network']['network_id'], 'Network ID should match in after_populate_network hook' ); |
| | 65 | $this->assertEquals( $domain, $this->action_args['after_populate_network']['domain'], 'Domain should match in after_populate_network hook' ); |
| | 66 | |
| | 67 | remove_action( 'before_populate_network', array( $this, 'hook_action_counter' ), 10 ); |
| | 68 | remove_action( 'after_populate_network', array( $this, 'hook_action_counter' ), 10 ); |
| | 69 | |
| | 70 | global $wpdb; |
| | 71 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $network_id ) ); |
| | 72 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id = %d", $network_id ) ); |
| | 73 | } |
| | 74 | |
| | 75 | /** |
| | 76 | * Test that the hooks can modify parameters. |
| | 77 | * |
| | 78 | * @ticket 27289 |
| | 79 | */ |
| | 80 | public function test_populate_network_hook_filter() { |
| | 81 | $this->hook_called = false; |
| | 82 | |
| | 83 | add_action( 'before_populate_network', array( $this, 'modify_domain_hook' ), 10, 6 ); |
| | 84 | |
| | 85 | $domain = 'example' . time() . '.org'; |
| | 86 | $network_id = self::factory()->network->create( |
| | 87 | array( |
| | 88 | 'domain' => $domain, |
| | 89 | 'path' => '/', |
| | 90 | ) |
| | 91 | ); |
| | 92 | |
| | 93 | $this->assertTrue( $this->hook_called, 'The modify_domain_hook action should have been called' ); |
| | 94 | |
| | 95 | remove_action( 'before_populate_network', array( $this, 'modify_domain_hook' ), 10 ); |
| | 96 | |
| | 97 | global $wpdb; |
| | 98 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", $network_id ) ); |
| | 99 | $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id = %d", $network_id ) ); |
| | 100 | } |
| | 101 | |
| | 102 | /** |
| | 103 | * Action to track if hooks are being executed. |
| | 104 | */ |
| | 105 | public function modify_domain_hook( $network_id, $domain, $email, $site_name, $path, $subdomain_install ) { |
| | 106 | $this->hook_called = true; |
| | 107 | } |
| | 108 | } |
| | 109 | |
| | 110 | endif; |