Make WordPress Core


Ignore:
Timestamp:
04/09/2025 01:29:39 PM (5 weeks ago)
Author:
SergeyBiryukov
Message:

Tests: Use the ms-required group where appropriate.

This replaces the if ( is_multisite() ) conditional wrapping entire test classes with the ms-required group for more consistency across the test suite.

Follow-up to [40520].

See #63167.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/multisite/getSpaceUsed.php

    r52010 r60148  
    11<?php
    22
    3 if ( is_multisite() ) :
     3/**
     4 * @group ms-required
     5 * @group multisite
     6 * @covers ::get_space_used
     7 */
     8class Tests_Multisite_GetSpaceUsed extends WP_UnitTestCase {
     9
     10    public function test_get_space_used_switched_site() {
     11        $blog_id = self::factory()->blog->create();
     12        switch_to_blog( $blog_id );
     13
     14        // Our comparison of space relies on an initial value of 0. If a previous test has failed or if the
     15        // src directory already contains a content directory with site content, then the initial expectation
     16        // will be polluted. We create sites until an empty one is available.
     17        while ( 0 !== get_space_used() ) {
     18            restore_current_blog();
     19            $blog_id = self::factory()->blog->create();
     20            switch_to_blog( $blog_id );
     21        }
     22
     23        // Upload a file to the new site.
     24        $filename = __FUNCTION__ . '.jpg';
     25        $contents = __FUNCTION__ . '_contents';
     26        $file     = wp_upload_bits( $filename, null, $contents );
     27
     28        // get_space_used() is measures in MB, get the size of the new file in MB.
     29        $size = filesize( $file['file'] ) / 1024 / 1024;
     30
     31        delete_transient( 'dirsize_cache' );
     32
     33        $this->assertSame( $size, get_space_used() );
     34        $upload_dir = wp_upload_dir();
     35        $this->remove_added_uploads();
     36        $this->delete_folders( $upload_dir['basedir'] );
     37        restore_current_blog();
     38    }
    439
    540    /**
    6      * @group multisite
    7      * @covers ::get_space_used
     41     * Directories of sub sites on a network should not count against the same spaced used total for
     42     * the main site.
    843     */
    9     class Tests_Multisite_GetSpaceUsed extends WP_UnitTestCase {
     44    public function test_get_space_used_main_site() {
     45        $space_used = get_space_used();
    1046
    11         public function test_get_space_used_switched_site() {
     47        $blog_id = self::factory()->blog->create();
     48        switch_to_blog( $blog_id );
     49
     50        // We don't rely on an initial value of 0 for space used, but should have a clean space available
     51        // so that we can remove any uploaded files and directories without concern of a conflict with
     52        // existing content directories in src.
     53        while ( 0 !== get_space_used() ) {
     54            restore_current_blog();
    1255            $blog_id = self::factory()->blog->create();
    1356            switch_to_blog( $blog_id );
    14 
    15             // Our comparison of space relies on an initial value of 0. If a previous test has failed or if the
    16             // src directory already contains a content directory with site content, then the initial expectation
    17             // will be polluted. We create sites until an empty one is available.
    18             while ( 0 !== get_space_used() ) {
    19                 restore_current_blog();
    20                 $blog_id = self::factory()->blog->create();
    21                 switch_to_blog( $blog_id );
    22             }
    23 
    24             // Upload a file to the new site.
    25             $filename = __FUNCTION__ . '.jpg';
    26             $contents = __FUNCTION__ . '_contents';
    27             $file     = wp_upload_bits( $filename, null, $contents );
    28 
    29             // get_space_used() is measures in MB, get the size of the new file in MB.
    30             $size = filesize( $file['file'] ) / 1024 / 1024;
    31 
    32             delete_transient( 'dirsize_cache' );
    33 
    34             $this->assertSame( $size, get_space_used() );
    35             $upload_dir = wp_upload_dir();
    36             $this->remove_added_uploads();
    37             $this->delete_folders( $upload_dir['basedir'] );
    38             restore_current_blog();
    3957        }
    4058
    41         /**
    42          * Directories of sub sites on a network should not count against the same spaced used total for
    43          * the main site.
    44          */
    45         public function test_get_space_used_main_site() {
    46             $space_used = get_space_used();
     59        // Upload a file to the new site.
     60        $filename = __FUNCTION__ . '.jpg';
     61        $contents = __FUNCTION__ . '_contents';
     62        wp_upload_bits( $filename, null, $contents );
    4763
    48             $blog_id = self::factory()->blog->create();
    49             switch_to_blog( $blog_id );
     64        restore_current_blog();
    5065
    51             // We don't rely on an initial value of 0 for space used, but should have a clean space available
    52             // so that we can remove any uploaded files and directories without concern of a conflict with
    53             // existing content directories in src.
    54             while ( 0 !== get_space_used() ) {
    55                 restore_current_blog();
    56                 $blog_id = self::factory()->blog->create();
    57                 switch_to_blog( $blog_id );
    58             }
     66        delete_transient( 'dirsize_cache' );
    5967
    60             // Upload a file to the new site.
    61             $filename = __FUNCTION__ . '.jpg';
    62             $contents = __FUNCTION__ . '_contents';
    63             wp_upload_bits( $filename, null, $contents );
     68        $this->assertSame( $space_used, get_space_used() );
    6469
    65             restore_current_blog();
     70        // Switch back to the new site to remove the uploaded file.
     71        switch_to_blog( $blog_id );
     72        $upload_dir = wp_upload_dir();
     73        $this->remove_added_uploads();
     74        $this->delete_folders( $upload_dir['basedir'] );
     75        restore_current_blog();
     76    }
    6677
    67             delete_transient( 'dirsize_cache' );
     78    public function test_get_space_used_pre_get_spaced_used_filter() {
     79        add_filter( 'pre_get_space_used', array( $this, 'filter_space_used' ) );
    6880
    69             $this->assertSame( $space_used, get_space_used() );
     81        $this->assertSame( 300, get_space_used() );
    7082
    71             // Switch back to the new site to remove the uploaded file.
    72             switch_to_blog( $blog_id );
    73             $upload_dir = wp_upload_dir();
    74             $this->remove_added_uploads();
    75             $this->delete_folders( $upload_dir['basedir'] );
    76             restore_current_blog();
    77         }
     83        remove_filter( 'pre_get_space_used', array( $this, 'filter_space_used' ) );
     84    }
    7885
    79         public function test_get_space_used_pre_get_spaced_used_filter() {
    80             add_filter( 'pre_get_space_used', array( $this, 'filter_space_used' ) );
    81 
    82             $this->assertSame( 300, get_space_used() );
    83 
    84             remove_filter( 'pre_get_space_used', array( $this, 'filter_space_used' ) );
    85         }
    86 
    87         public function filter_space_used() {
    88             return 300;
    89         }
     86    public function filter_space_used() {
     87        return 300;
    9088    }
    91 endif;
     89}
Note: See TracChangeset for help on using the changeset viewer.