Make WordPress Core

Ticket #30080: 30080.diff

File 30080.diff, 6.5 KB (added by jeremyfelt, 11 years ago)
  • tests/phpunit/tests/multisite/site.php

     
    7171                $this->assertFalse( restore_current_blog() );
    7272        }
    7373
    74         function test_create_and_delete_blog() {
     74        /**
     75         * Test the cache keys and database tables setup through the creation of a site.
     76         */
     77        function test_created_site_details() {
    7578                global $wpdb;
    7679
    77                 $blog_ids = $this->factory->blog->create_many( 4 );
    78                 foreach ( $blog_ids as $blog_id ) {
    79                         $this->assertInternalType( 'int', $blog_id );
    80                         $prefix = $wpdb->get_blog_prefix( $blog_id );
     80                $blog_id = $this->factory->blog->create();
    8181
    82                         // $get_all = false
    83                         $details = get_blog_details( $blog_id, false );
    84                         $this->assertEquals( $details, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
    85 
    86                         // get_id_from_blogname(), see #20950
    87                         $this->assertEquals( $blog_id, get_id_from_blogname( $details->path ) );
    88                         $this->assertEquals( $blog_id, wp_cache_get( 'get_id_from_blogname_' . trim( $details->path, '/' ), 'blog-details' ) );
     82                $this->assertInternalType( 'int', $blog_id );
     83                $prefix = $wpdb->get_blog_prefix( $blog_id );
    8984
    90                         // get_blog_id_from_url()
    91                         $this->assertEquals( $blog_id, get_blog_id_from_url( $details->domain, $details->path ) );
    92                         $key = md5( $details->domain . $details->path );
    93                         $this->assertEquals( $blog_id, wp_cache_get( $key, 'blog-id-cache' ) );
     85                // $get_all = false, only retrieve details from the blogs table
     86                $details = get_blog_details( $blog_id, false );
    9487
    95                         // These are empty until get_blog_details() is called with $get_all = true
    96                         $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
    97                         $key = md5( $details->domain . $details->path );
    98                         $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
     88                // Combine domain and path for a site specific cache key.
     89                $key = md5( $details->domain . $details->path );
    9990
    100                         // $get_all = true should propulate the full blog-details cache and the blog slug lookup cache
    101                         $details = get_blog_details( $blog_id, true );
    102                         $this->assertEquals( $details, wp_cache_get( $blog_id, 'blog-details' ) );
    103                         $this->assertEquals( $details, wp_cache_get( $key, 'blog-lookup' ) );
     91                $this->assertEquals( $details, wp_cache_get( $blog_id . 'short', 'blog-details' ) );
    10492
    105                         foreach ( $wpdb->tables( 'blog', false ) as $table ) {
    106                                 $suppress = $wpdb->suppress_errors();
    107                                 $table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
    108                                 $wpdb->suppress_errors( $suppress );
    109                                 $this->assertNotEmpty( $table_fields );
    110                                 $result = $wpdb->get_results( "SELECT * FROM $prefix$table LIMIT 1" );
    111                                 if ( 'commentmeta' == $table || 'links' == $table )
    112                                         $this->assertEmpty( $result );
    113                                 else
    114                                         $this->assertNotEmpty( $result );
     93                // get_id_from_blogname(), see #20950
     94                $this->assertEquals( $blog_id, get_id_from_blogname( $details->path ) );
     95                $this->assertEquals( $blog_id, wp_cache_get( 'get_id_from_blogname_' . trim( $details->path, '/' ), 'blog-details' ) );
     96
     97                // get_blogaddress_by_name()
     98                $this->assertEquals( 'http://' . $details->domain . $details->path, get_blogaddress_by_name( trim( $details->path, '/' ) ) );
     99
     100                // These are empty until get_blog_details() is called with $get_all = true
     101                $this->assertEquals( false, wp_cache_get( $blog_id, 'blog-details' ) );
     102                $this->assertEquals( false, wp_cache_get( $key, 'blog-lookup' ) );
     103
     104                // $get_all = true, populate the full blog-details cache and the blog slug lookup cache
     105                $details = get_blog_details( $blog_id, true );
     106                $this->assertEquals( $details, wp_cache_get( $blog_id, 'blog-details' ) );
     107                $this->assertEquals( $details, wp_cache_get( $key, 'blog-lookup' ) );
     108
     109                // Check existence of each database table for the created site.
     110                foreach ( $wpdb->tables( 'blog', false ) as $table ) {
     111                        $suppress     = $wpdb->suppress_errors();
     112                        $table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
     113                        $wpdb->suppress_errors( $suppress );
     114
     115                        // The table should exist.
     116                        $this->assertNotEmpty( $table_fields );
     117
     118                        // And the table should not be empty, unless commentmeta or links.
     119                        $result = $wpdb->get_results( "SELECT * FROM $prefix$table LIMIT 1" );
     120                        if ( 'commentmeta' == $table || 'links' == $table ) {
     121                                $this->assertEmpty( $result );
     122                        } else {
     123                                $this->assertNotEmpty( $result );
    115124                        }
    116125                }
    117126
    118127                // update the blog count cache to use get_blog_count()
    119128                wp_update_network_counts();
    120                 $this->assertEquals( 4 + 1, (int) get_blog_count() );
     129                $this->assertEquals( 2, (int) get_blog_count() );
     130        }
     131
     132        /**
     133         * Test the deletion of a site, including a case where database tables are
     134         * intentionally not deleted.
     135         */
     136        function test_wpmu_delete_blog() {
     137                global $wpdb;
     138
     139                $blog_ids = $this->factory->blog->create_many( 2 );
    121140
    122141                $drop_tables = false;
    123                 // delete all blogs
     142
     143                // Delete both sites, but keep the database tables for one.
    124144                foreach ( $blog_ids as $blog_id ) {
    125145                        // drop tables for every second blog
    126146                        $drop_tables = ! $drop_tables;
     147
    127148                        $details = get_blog_details( $blog_id, false );
    128149
    129150                        wpmu_delete_blog( $blog_id, $drop_tables );
     
    140161                                $suppress = $wpdb->suppress_errors();
    141162                                $table_fields = $wpdb->get_results( "DESCRIBE $prefix$table;" );
    142163                                $wpdb->suppress_errors( $suppress );
    143                                 if ( $drop_tables )
     164                                if ( $drop_tables ) {
    144165                                        $this->assertEmpty( $table_fields );
    145                                 else
     166                                } else {
    146167                                        $this->assertNotEmpty( $table_fields, $prefix . $table );
     168                                }
    147169                        }
    148170                }
    149171
     
    164186                $this->assertLessThan( 2, $time_difference );
    165187        }
    166188
    167         function test_getters(){
    168                 global $current_site;
    169 
    170                 $blog_id = get_current_blog_id();
    171                 $blog = get_blog_details( $blog_id );
    172                 $this->assertEquals( $blog_id, $blog->blog_id );
    173                 $this->assertEquals( $current_site->domain, $blog->domain );
    174                 $this->assertEquals( '/', $blog->path );
    175 
    176                 // Test defaulting to current blog
    177                 $this->assertEquals( $blog, get_blog_details() );
    178 
    179                 $user_id = $this->factory->user->create( array( 'role' => 'administrator' ) );
    180                 $blog_id = $this->factory->blog->create( array( 'user_id' => $user_id, 'path' => '/test_blogname', 'title' => 'Test Title' ) );
    181                 $this->assertInternalType( 'int', $blog_id );
    182 
    183                 $this->assertEquals( 'http://' . $current_site->domain . $current_site->path . 'test_blogname/', get_blogaddress_by_name('test_blogname') );
    184 
    185                 $this->assertEquals( $blog_id, get_id_from_blogname('test_blogname') );
    186         }
    187 
    188189        function test_update_blog_details() {
    189190                global $test_action_counter;
    190191