users_[] = $this->_make_user('administrator'); $this->users_[] = $this->_make_user('administrator'); $user1_id = $this->users_[0]; $user2_id = $this->users_[1]; for( $i=1; $i<=3; $i++ ) { $id = ( $i & 1 ) ? $user1_id : $user2_id; $this->blog_ids[] = wpmu_create_blog( 'd'.$i, 'p'.$i, "Title ".$i, $id ); } } function tearDown() { parent::tearDown(); // delete any users blogs created during tests and not deleted later foreach ($this->blog_ids as $id) wpmu_delete_blog($id); } /* Tests for successfull creation of blogs under WordPress MultiSite */ function test_create_and_delete_blog() { global $wpdb; // initialise the users $user1_id = $this->users_[0]; $user2_id = $this->users_[1]; $user1 = new WP_User($user1_id); $user2 = new WP_User($user1_id); $temp_blog_ids = Array(); for( $i=1; $i<=TEST_BLOGS_COUNT; $i++ ) { $id = ( $i & 1 ) ? $user1_id : $user2_id; $blog = wpmu_create_blog( 'domain'.$i, 'path'.$i, "Title".$i, $id ); $this->assertInternalType( 'int', $blog ); $temp_blog_ids[] = $blog; } // update the blog count cache to use get_blog_count() wp_update_network_counts(); $this->assertEquals( ( TEST_BLOGS_COUNT+3 ) ,(int) get_blog_count() ); $drop_tables = false; //delete all blogs foreach($temp_blog_ids as $blog) { // drop tables for every second blog $drop_tables = ! $drop_tables; if ( substr( $blog_prefix, -1 ) == '_' ) $blog_prefix = $wpdb->get_blog_prefix( $blog ); $blog = wpmu_delete_blog( $blog, $drop_tables ); // test the $drop argument $blog_tables = $wpdb->get_results( "SHOW TABLES LIKE '{$blog_prefix}%'", ARRAY_A ); if( $drop_tables ) $this->assertEquals( 0 , count($blog_tables) ); else $this->assertGreaterThan( 0 , count($blog_tables) ); } // update the blog count cache to use get_blog_count() wp_update_network_counts(); $this->assertEquals( '' , get_blog_count() ); } /* Tests the get_admin_users_for_domain() function */ function test_get_admin_users_and_domain_exists() { $temp_blog_ids = Array(); $user1_id = $this->users_[0]; $user2_id = $this->users_[1]; $user1 = new WP_User($user1_id); $user2 = new WP_User($user1_id); // Initialize the blogs for( $i=1; $i<=TEST_BLOGS_COUNT; $i++ ) { $id = ( $i & 1 ) ? $user1_id : $user2_id; $blog = wpmu_create_blog( 'domain'.$i+20, 'path'.$i+20, "Title".$i, $id ); $temp_blog_ids[] = $blog; } $admin_users = array( get_admin_users_for_domain( $sitedomain = 'domain11', $path = 'path11/' ), get_admin_users_for_domain( $sitedomain = 'domain11', $path = 'path11' ), get_admin_users_for_domain( $sitedomain = 'domain21', $path = 'path21/' ), get_admin_users_for_domain( $sitedomain = 'domain21', $path = 'path21' ), get_admin_users_for_domain( $sitedomain = 'domain11', $path = 'path21/' ), get_admin_users_for_domain( $sitedomain = 'domain21', $path = 'path11' ), get_admin_users_for_domain( $sitedomain = 'd1', $path = 'p1' ), get_admin_users_for_domain( $sitedomain = 'd2', $path = 'p2' ) ); // expected get_admin_users_for_domain() outputs for $admin_users $expected_admin_users = array( $user1_id, $user2_id, $user1_id, $user2_id, Array(), Array(), $user1_id, $user2_id ); $domains = array( 21, 22, 24, 23, 26, 25, 30 ); $expected_domains = array( 17, 18, 20, 19, 22, 21, 26 ); $not_expected_domains = array( '1', '1/', '2', '2/', '3', '3/' ); // initialised for get_admin_users_for_domain(), all good foreach( $domains as $k => $v ) $this->assertEquals( $expected_domains[$k] , domain_exists('domain'.$v, 'path'.$v ) ); // wrong site_id , all false foreach( $domains as $k => $v ) $this->assertFalse( domain_exists( 'domain' . $v, 'path' . $v, 9 ) ); // initialised on other occasions, all false foreach( $not_expected_domains as $domain ) $this->assertFalse( domain_exists( 'domain' . $domain, 'path' . $domain ) ); // test for expected get_admin_users_for_domain values foreach( $admin_users as $k => $current_admin ) $this->assertEquals( $expected_admin_users[$k] , $current_admin[0] ); // delete all unnecessary blogs foreach($temp_blog_ids as $blog) wpmu_delete_blog( $blog ); } /* Tests the get_blogs_of_user() and the internal sorting function See #15828 about the sorting enhancement */ function test_get_blogs_of_user() { $this->knownWPBug(15828); $temp_blog_ids = array(); $user1_id = $this->users_[0]; $user2 = new WP_User($user1_id); // initialize the blogs for( $i=1; $i<=10; $i++ ) { $blog = wpmu_create_blog( 'testdomain'.$i, 'testpath'.$i, "testTitle".$i, $user1_id, '', (10-$i) ); $this->assertInternalType( 'int', $blog ); $temp_blog_ids[] = $blog; } // test sorting $this->assertEquals ( array( 29, 28, 17, 19, 20, 27, 26, 25, 24, 23, 22, 21 ), array_keys( get_blogs_of_user( $user1_id, $all = false, $order_by = 'site_id' ) ) ); // test without sorting $this->assertEquals ( array( 17, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 ), array_keys( get_blogs_of_user( $user1_id, $all = false ) ) ); // delete all unnecessary blogs foreach($temp_blog_ids as $blog) $blog = wpmu_delete_blog( $blog ); } /* Tests the following functions: * wp_get_active_network_plugins * activate_plugin */ function test_active_network_plugins() { $path = "hello.php"; // local activate, should be invisible for the network activate_plugin($path); // $network_wide = false $active_plugins = wp_get_active_network_plugins(); $this->assertEquals( Array(), $active_plugins ); add_action( 'deactivated_plugin', 'helper_deactivate_hook', 10, 2); // activate the plugin sitewide activate_plugin($path, '', $network_wide = true); $active_plugins = wp_get_active_network_plugins(); $this->assertEquals( Array(WP_PLUGIN_DIR . '/hello.php'), $active_plugins ); //deactivate the plugin deactivate_plugins($path); $active_plugins = wp_get_active_network_plugins(); $this->assertEquals( Array(), $active_plugins ); global $plugin_hook; $this->assertEquals( 1, $plugin_hook ); // testing actions and silent mode activate_plugin($path, '', $network_wide = true); deactivate_plugins($path, true); // silent $this->assertEquals( 1, $plugin_hook ); // testing actions and silent mode } /* Tests the get_user_count() function */ function test_get_user_count() { // Refresh the cache wp_update_network_counts(); $count = get_user_count(); $this->assertEquals( 11, $count ); $this->_make_user('administrator'); $count = get_user_count(); // No change, cache not refreshed $this->assertEquals( 11, $count ); wp_update_network_counts(); // Magic happens here $count = get_user_count(); $this->assertEquals( 12, $count ); } /* Tests the following functions: * wp_next_scheduled * wp_schedule_event */ function test_wp_schedule_update_network_counts() { $this->assertFalse(wp_next_scheduled('update_network_counts')); // We can't use wp_schedule_update_network_counts() because WP_INSTALLING is set wp_schedule_event(time(), 'twicedaily', 'update_network_counts'); $this->assertInternalType('int', wp_next_scheduled('update_network_counts')); } /* Tests the users_can_register_signup_filter() function */ function test_users_can_register_signup_filter() { $registration = get_site_option('registration'); $this->assertFalse( users_can_register_signup_filter() ); update_site_option('registration', 'all'); $this->assertTrue( users_can_register_signup_filter() ); update_site_option('registration', 'user'); $this->assertTrue( users_can_register_signup_filter() ); update_site_option('registration', 'none'); $this->assertFalse( users_can_register_signup_filter() ); } /* Tests the test_get_dashboard_blog() function */ function test_get_dashboard_blog() { // if there is no dashboard blog set, current blog is used / why is it this way? $dashboard_blog = get_dashboard_blog(); $this->assertEquals( 1, $dashboard_blog->blog_id ); // set the dashboard blog to another one update_site_option('dashboard_blog', 2); $dashboard_blog = get_dashboard_blog(); $this->assertEquals( 2, $dashboard_blog->blog_id ); } /* Tests the wpmu_log_new_registrations() function */ function test_wpmu_log_new_registrations() { global $wpdb; $user = new WP_User( (int) 1 ); $ip = preg_replace( '/[^0-9., ]/', '',$_SERVER['REMOTE_ADDR'] ); wpmu_log_new_registrations(1,1); // currently there is no wrapper function for the registration_log // TODO: rewrite it using the wrapper when it appears $reg_blog = $wpdb->get_col( "SELECT email FROM {$wpdb->registration_log} WHERE {$wpdb->registration_log}.blog_id = 1 AND IP LIKE '" . $ip . "'" ); $this->assertEquals( $user->user_email, $reg_blog[ count( $reg_blog )-1 ] ); } /* Tests the following functions: * upload_is_user_over_quota * is_upload_space_available * get_space_allowed */ function test_upload_is_user_over_quota() { $this->knownWPBug( 18119 ); $this->assertFalse( upload_is_user_over_quota() ); $this->assertTrue( is_upload_space_available() ); update_site_option('upload_space_check_disabled', true); $this->assertFalse( upload_is_user_over_quota() ); $this->assertTrue( is_upload_space_available() ); update_site_option( 'blog_upload_space', 0 ); $this->assertFalse( upload_is_user_over_quota() ); $this->assertEquals( 0, get_space_allowed() ); $this->assertTrue( is_upload_space_available() ); update_site_option('upload_space_check_disabled', false); $this->assertFalse( upload_is_user_over_quota() ); $this->assertTrue( is_upload_space_available() ); update_site_option( 'blog_upload_space', -1 ); $this->assertTrue( upload_is_user_over_quota() ); $this->assertEquals( -1, get_space_allowed() ); $this->assertFalse( is_upload_space_available() ); update_option( 'blog_upload_space', 0 ); $this->assertFalse( upload_is_user_over_quota() ); $this->assertEquals( 0, get_space_allowed() ); $this->assertTrue( is_upload_space_available() ); update_option( 'blog_upload_space', -1 ); $this->assertTrue( upload_is_user_over_quota() ); $this->assertEquals( -1, get_space_allowed() ); $this->assertFalse( is_upload_space_available() ); } /* Tests the wpmu_update_blogs_date() function */ function test_wpmu_update_blogs_date() { global $wpdb; // set the blogid for wpmu_update_blogs_date $wpdb->blogid = $this->blog_ids[0]; wpmu_update_blogs_date(); // compare the update time with the current time, allow delta < 2 $blog = get_blog_details($this->blog_ids[0]); $current_time = time(); $time_difference = $current_time - strtotime($blog->last_updated); $this->assertLessThan( 2, $time_difference ); } /* Tests the following functions: * get_blog_details * get_blogaddress_by_name * get_id_from_blogname */ function test_getters(){ update_blog_details( $this->blog_ids[0], array('blogname' => 'test_blogname') ); // testing get_blog_details and getting the baseline blog info $blog = get_blog_details($this->blog_ids[0]); $this->assertEquals( 54, $blog->blog_id ); $this->assertEquals( 'd1', $blog->domain ); $this->assertEquals( 'p1/', $blog->path ); // testing get_blogaddress_by_name $this->assertEquals( 'http://' . DOMAIN_CURRENT_SITE . PATH_CURRENT_SITE . 'test_blogname/', get_blogaddress_by_name('test_blogname') ); // testing get_blogaddress_by_name $this->assertEquals( $blog->blogid, get_id_from_blogname('test_blogname') ); } /* Tests the update_blog_details() function */ function test_update_blog_details(){ $now = time(); update_blog_details( $this->blog_ids[0], array('domain' => 'example.com', 'path' => 'my_path/', 'last_updated' => $now) ); $blog = get_blog_details($this->blog_ids[0]); $this->assertEquals( 'example.com', $blog->domain ); $this->assertEquals( 'my_path/', $blog->path ); $this->assertEquals( $now, strtotime( $blog->last_updated ) ); $this->assertEquals( '0', $blog->spam ); $result = update_blog_details( $this->blog_ids[0], array('domain' => 'example2.com','spam' => 1) ); $blog = get_blog_details($this->blog_ids[0]); $this->assertEquals( 'example2.com', $blog->domain ); $this->assertEquals( 'my_path/', $blog->path ); $this->assertEquals( $now, strtotime( $blog->last_updated ) ); $this->assertEquals( '1', $blog->spam ); $this->assertTrue( $result ); $result = update_blog_details( $this->blog_ids[0] ); $blog = get_blog_details($this->blog_ids[0]); $this->assertEquals( 'example2.com', $blog->domain ); $this->assertEquals( 'my_path/', $blog->path ); $this->assertEquals( '1', $blog->spam ); $this->assertFalse( $result ); } } /* Helper for plugin testing, helpful with silent mode testing */ function helper_deactivate_hook($plugin, $network_wide) { global $plugin_hook; $plugin_hook++; } ?>