<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[452] wp-testcase/test_ms.php: Multisite test cases.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://unit-tests.trac.wordpress.org/changeset/452">452</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2011-10-08 22:13:42 +0000 (Sat, 08 Oct 2011)</dd>
</dl>

<h3>Log Message</h3>
<pre>Multisite test cases. Props jakub.tyrcha</pre>

<h3>Added Paths</h3>
<ul>
<li><a href="#wptestcasetest_msphp">wp-testcase/test_ms.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="wptestcasetest_msphp"></a>
<div class="addfile"><h4>Added: wp-testcase/test_ms.php (0 => 452)</h4>
<pre class="diff"><span>
<span class="info">--- wp-testcase/test_ms.php                                (rev 0)
+++ wp-testcase/test_ms.php        2011-10-08 22:13:42 UTC (rev 452)
</span><span class="lines">@@ -0,0 +1,317 @@
</span><ins>+&lt;?php
+
+// A set of unit tests for WordPress MultiSite
+
+if ( is_multisite() ) :
+
+$plugin_hook = 0;
+define('TEST_BLOGS_COUNT', 10);
+
+class WPTestMS extends _WPEmptyBlog {
+
+        function setUp() {
+                parent::setUp();
+        }
+
+        function tearDown() {
+                parent::tearDown();
+        }
+
+        function test_create_and_delete_blog() {
+                global $wpdb, $current_site;
+
+                // initialise the users
+                $user1_id = $this-&gt;_make_user('administrator');
+                $user2_id = $this-&gt;_make_user('administrator');
+                $user1 = new WP_User($user1_id);
+                $user2 = new WP_User($user2_id);
+                $blog_ids = array();
+
+                for ( $i=1; $i &lt;= TEST_BLOGS_COUNT; $i++ ) {
+                        $id = ( $i &amp; 1 ) ? $user1_id : $user2_id;
+                        $blog_id = wpmu_create_blog( $current_site-&gt;domain, 'path'.$i, &quot;Title&quot;.$i, $id );
+                        $this-&gt;assertInternalType( 'int', $blog_id );
+                        $blog_ids[] = $blog_id;
+                }
+
+                // update the blog count cache to use get_blog_count()
+                wp_update_network_counts(); 
+                $this-&gt;assertEquals( TEST_BLOGS_COUNT + 1, (int) get_blog_count() );
+
+                $drop_tables = false;
+                // delete all blogs
+                foreach ( $blog_ids as $blog_id ) {
+                        // drop tables for every second blog
+                        $drop_tables = ! $drop_tables;
+                        $blog_prefix = $wpdb-&gt;get_blog_prefix( $blog_id );
+                        wpmu_delete_blog( $blog_id, $drop_tables );
+
+                        // test the $drop argument
+                        $blog_tables = $wpdb-&gt;get_results( &quot;SHOW TABLES LIKE '{$blog_prefix}%'&quot;, ARRAY_A );
+                        if ( $drop_tables )
+                                $this-&gt;assertEquals( 0 , count( $blog_tables ) );
+                        else
+                                $this-&gt;assertGreaterThan( 0 , count( $blog_tables ) );
+                }
+
+                // update the blog count cache to use get_blog_count()
+                wp_update_network_counts(); 
+                $this-&gt;assertEquals( 1 , get_blog_count() );
+        }
+
+        function test_domain_exists() {
+                global $current_site;
+
+                $user1_id = $this-&gt;_make_user('administrator');
+
+                $blog_ids = array();
+                for ( $i=1; $i &lt;= TEST_BLOGS_COUNT; $i++ ) {
+                        $id = ( $i &amp; 1 ) ? $user1_id : $user2_id;
+                        $blog_id = wpmu_create_blog(  $current_site-&gt;domain, 'path'.$i, &quot;Title&quot;.$i, $id );
+                        $this-&gt;assertInternalType( 'int', $blog_id );
+                        $blog_ids[] = $blog_id;
+                        $this-&gt;assertEquals( $blog_id , domain_exists( $current_site-&gt;domain, 'path'.$i ) );
+                        // Wrong site id should fail.
+                        $this-&gt;assertFalse( $blog_id , domain_exists( $current_site-&gt;domain, 'path'.$i ), 999 );
+                }
+
+                $this-&gt;assertFalse( domain_exists( $current_site-&gt;domain, rand_str() ) );
+
+                foreach ( $blog_ids as $blog_id )                                
+                        wpmu_delete_blog( $blog_id );
+        }
+
+        function test_get_blogs_of_user() {
+                global $current_site;
+
+                $user1_id = $this-&gt;_make_user('administrator');
+
+                $blog_ids = array();
+                for ( $i=1; $i &lt;= 10; $i++ ) {
+                        $blog_id = wpmu_create_blog( $current_site-&gt;domain, 'testpath'.$i, &quot;testTitle&quot;.$i, $user1_id );
+                        $this-&gt;assertInternalType( 'int', $blog_id );
+                        $blog_ids[] = $blog_id;
+                }
+
+                $this-&gt;assertEquals ( array_merge( array( 1 ), $blog_ids), array_keys( get_blogs_of_user( $user1_id, $all = false ) ) );
+
+                $this-&gt;assertTrue( remove_user_from_blog( $user1_id, 1 ) );
+
+                $this-&gt;assertEquals ( $blog_ids, array_keys( get_blogs_of_user( $user1_id, $all = false ) ) );
+
+                foreach ( $blog_ids as $blog_id ) 
+                        wpmu_delete_blog( $blog_id );
+        }
+
+        function test_active_network_plugins() {
+                $path = &quot;hello.php&quot;;
+
+                // local activate, should be invisible for the network
+                activate_plugin($path); // $network_wide = false
+                $active_plugins = wp_get_active_network_plugins();
+                $this-&gt;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-&gt;assertEquals( Array(WP_PLUGIN_DIR . '/hello.php'), $active_plugins ); 
+
+                //deactivate the plugin
+                deactivate_plugins($path);
+                $active_plugins = wp_get_active_network_plugins();
+                $this-&gt;assertEquals( Array(), $active_plugins );
+
+                global $plugin_hook;
+                $this-&gt;assertEquals( 1, $plugin_hook ); // testing actions and silent mode
+
+                activate_plugin($path, '', $network_wide = true);
+                deactivate_plugins($path, true); // silent
+
+                $this-&gt;assertEquals( 1, $plugin_hook ); // testing actions and silent mode
+        }
+
+        function test_get_user_count() {
+                // Refresh the cache
+                wp_update_network_counts();
+                $start_count = get_user_count();
+
+                $this-&gt;_make_user('administrator');
+
+                $count = get_user_count(); // No change, cache not refreshed
+                $this-&gt;assertEquals( $start_count, $count );
+
+                wp_update_network_counts(); // Magic happens here
+
+                $count = get_user_count();
+                $this-&gt;assertEquals( $start_count + 1, $count );
+        }
+
+        function test_wp_schedule_update_network_counts() {
+                $this-&gt;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-&gt;assertInternalType('int', wp_next_scheduled('update_network_counts'));        
+        }
+
+        function test_users_can_register_signup_filter() {
+
+                $registration = get_site_option('registration');
+                $this-&gt;assertFalse( users_can_register_signup_filter() );
+
+                update_site_option('registration', 'all');
+                $this-&gt;assertTrue( users_can_register_signup_filter() );        
+
+                update_site_option('registration', 'user');
+                $this-&gt;assertTrue( users_can_register_signup_filter() );
+
+                update_site_option('registration', 'none');
+                $this-&gt;assertFalse( users_can_register_signup_filter() );                
+        }
+
+        function test_get_dashboard_blog() {
+                global $current_site;
+
+                // if there is no dashboard blog set, current blog is used
+                $dashboard_blog = get_dashboard_blog();
+                $this-&gt;assertEquals( 1, $dashboard_blog-&gt;blog_id );
+
+                $user_id = $this-&gt;_make_user('administrator');
+                $blog_id = wpmu_create_blog( $current_site-&gt;domain, 'testpath999', &quot;testTitle999&quot;, $user_id );
+                $this-&gt;assertInternalType( 'int', $blog_id );
+
+                // set the dashboard blog to another one
+                update_site_option( 'dashboard_blog', $blog_id );
+                $dashboard_blog = get_dashboard_blog();
+                $this-&gt;assertEquals( $blog_id, $dashboard_blog-&gt;blog_id );
+                wpmu_delete_blog( $blog_id );
+        }
+
+        function test_wpmu_log_new_registrations() {
+                global $wpdb;
+
+                $user = new WP_User( 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
+                $reg_blog = $wpdb-&gt;get_col( &quot;SELECT email FROM {$wpdb-&gt;registration_log} WHERE {$wpdb-&gt;registration_log}.blog_id = 1 AND IP LIKE '&quot; . $ip . &quot;'&quot; );
+                $this-&gt;assertEquals( $user-&gt;user_email, $reg_blog[ count( $reg_blog )-1 ] );
+        }
+
+        function test_upload_is_user_over_quota() {
+                $this-&gt;knownWPBug( 18119 );
+                $this-&gt;assertFalse( upload_is_user_over_quota() );
+                $this-&gt;assertTrue( is_upload_space_available() );
+
+                update_site_option('upload_space_check_disabled', true);
+                $this-&gt;assertFalse( upload_is_user_over_quota() );
+                $this-&gt;assertTrue( is_upload_space_available() );
+
+                update_site_option( 'blog_upload_space', 0 );
+                $this-&gt;assertFalse( upload_is_user_over_quota() );
+                $this-&gt;assertEquals( 0, get_space_allowed() );
+                $this-&gt;assertTrue( is_upload_space_available() );
+
+                update_site_option('upload_space_check_disabled', false);
+                $this-&gt;assertFalse( upload_is_user_over_quota() );
+                $this-&gt;assertTrue( is_upload_space_available() );
+
+                update_site_option( 'blog_upload_space', -1 );
+                $this-&gt;assertTrue( upload_is_user_over_quota() );
+                $this-&gt;assertEquals( -1, get_space_allowed() );
+                $this-&gt;assertFalse( is_upload_space_available() );
+
+                update_option( 'blog_upload_space', 0 );
+                $this-&gt;assertFalse( upload_is_user_over_quota() );
+                $this-&gt;assertEquals( 0, get_space_allowed() );
+                $this-&gt;assertTrue( is_upload_space_available() );
+
+                update_option( 'blog_upload_space', -1 );
+                $this-&gt;assertTrue( upload_is_user_over_quota() );
+                $this-&gt;assertEquals( -1, get_space_allowed() );
+                $this-&gt;assertFalse( is_upload_space_available() );
+        }
+
+        function test_wpmu_update_blogs_date() {
+                global $wpdb;
+
+                wpmu_update_blogs_date();
+
+                // compare the update time with the current time, allow delta &lt; 2
+                $blog = get_blog_details( $wpdb-&gt;blogid );
+                $current_time = time();
+                $time_difference = $current_time - strtotime( $blog-&gt;last_updated );
+                $this-&gt;assertLessThan( 2, $time_difference );
+        }
+
+        function test_getters(){
+                global $current_site;
+
+                $blog_id = get_current_blog_id();
+                $blog = get_blog_details( $blog_id );
+                $this-&gt;assertEquals( $blog_id, $blog-&gt;blog_id );
+                $this-&gt;assertEquals( $current_site-&gt;domain, $blog-&gt;domain );
+                $this-&gt;assertEquals( '/', $blog-&gt;path );
+
+                $user_id = $this-&gt;_make_user('administrator');
+                $blog_id = wpmu_create_blog( $current_site-&gt;domain, '/test_blogname', &quot;Test Title&quot;, $user_id );
+                $this-&gt;assertInternalType( 'int', $blog_id );
+
+                $this-&gt;assertEquals( 'http://' . DOMAIN_CURRENT_SITE . PATH_CURRENT_SITE . 'test_blogname/', get_blogaddress_by_name('test_blogname') );
+
+                $this-&gt;assertEquals( $blog_id, get_id_from_blogname('test_blogname') );
+
+                wpmu_delete_blog( $blog_id );
+        }
+
+        function test_update_blog_details() {
+                global $current_site;
+
+                $user_id = $this-&gt;_make_user('administrator');
+                $blog_id = wpmu_create_blog( $current_site-&gt;domain, 'test_blogpath', &quot;Test Title&quot;, $user_id );
+                $this-&gt;assertInternalType( 'int', $blog_id );
+                $now = time();
+
+                $result = update_blog_details( $blog_id, array('domain' =&gt; 'example.com', 'path' =&gt; 'my_path/', 'last_updated' =&gt; $now ) );
+                $this-&gt;assertTrue( $result );
+
+                $blog = get_blog_details( $blog_id );
+                $this-&gt;assertEquals( 'example.com', $blog-&gt;domain );
+                $this-&gt;assertEquals( 'my_path/', $blog-&gt;path );
+                $this-&gt;assertEquals( $now, strtotime( $blog-&gt;last_updated ) );
+                $this-&gt;assertEquals( '0', $blog-&gt;spam );        
+
+                $result = update_blog_details( $blog_id, array('domain' =&gt; 'example2.com','spam' =&gt; 1) );
+                $this-&gt;assertTrue( $result );
+                $blog = get_blog_details( $blog_id );
+                $this-&gt;assertEquals( 'example2.com', $blog-&gt;domain );
+                $this-&gt;assertEquals( 'my_path/', $blog-&gt;path );
+                $this-&gt;assertEquals( $now, strtotime( $blog-&gt;last_updated ) );
+                $this-&gt;assertEquals( '1', $blog-&gt;spam );
+
+                $result = update_blog_details( $blog_id );
+                $this-&gt;assertFalse( $result );
+                $blog = get_blog_details( $blog_id );
+                $this-&gt;assertEquals( 'example2.com', $blog-&gt;domain );
+                $this-&gt;assertEquals( 'my_path/', $blog-&gt;path );
+                $this-&gt;assertEquals( '1', $blog-&gt;spam );
+                $this-&gt;assertFalse( $result );
+        }
+}
+
+/*
+Helper for plugin testing, helpful with silent mode testing
+*/
+function helper_deactivate_hook($plugin, $network_wide) {
+        global $plugin_hook;
+        $plugin_hook++;
+}
+
+endif;
+
+?&gt;
</ins></span></pre>
</div>
</div>

</body>
</html>