Make WordPress Core

Ticket #65439: trac-65439-tests.diff

File trac-65439-tests.diff, 6.0 KB (added by vedantere, 5 months ago)

Tests: PHPUnit coverage for zero-quota cases in wp_dashboard_quota() and display_space_usage()

  • new file tests/phpunit/tests/multisite/wpDashboardQuota.php

    diff --git a/tests/phpunit/tests/multisite/wpDashboardQuota.php b/tests/phpunit/tests/multisite/wpDashboardQuota.php
    new file mode 100644
    index 0000000000..9bbfa88670
    - +  
     1<?php
     2
     3/**
     4 * Tests specific to `wp_dashboard_quota()` in multisite.
     5 *
     6 * These tests filter `get_space_allowed` and `pre_get_space_used`
     7 * as those functions are tested elsewhere.
     8 *
     9 * @ticket 65439
     10 * @group ms-required
     11 * @group multisite
     12 */
     13
     14class Tests_Multisite_WpDashboardQuota extends WP_UnitTestCase {
     15
     16        public static function set_up_before_class() {
     17                parent::set_up_before_class();
     18
     19                if ( ! function_exists( 'wp_dashboard_quota' ) ) {
     20                        require_once ABSPATH . 'wp-admin/includes/dashboard.php';
     21                }
     22        }
     23
     24        public function set_up() {
     25                parent::set_up();
     26                update_site_option( 'upload_space_check_disabled', false );
     27                wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
     28        }
     29
     30        /**
     31         * wp_dashboard_quota() must not throw a DivisionByZeroError when
     32         * quota is 0 and used space is 0.
     33         *
     34         * @ticket 65439
     35         */
     36        public function test_quota_zero_used_zero_does_not_throw() {
     37                add_filter( 'get_space_allowed', '__return_zero' );
     38                add_filter( 'pre_get_space_used', '__return_zero' );
     39
     40                $threw = false;
     41                try {
     42                        ob_start();
     43                        wp_dashboard_quota();
     44                        ob_end_clean();
     45                } catch ( \DivisionByZeroError $e ) {
     46                        ob_end_clean();
     47                        $threw = true;
     48                }
     49
     50                remove_filter( 'get_space_allowed', '__return_zero' );
     51                remove_filter( 'pre_get_space_used', '__return_zero' );
     52
     53                $this->assertFalse( $threw, 'wp_dashboard_quota() must not throw DivisionByZeroError when quota is 0.' );
     54        }
     55
     56        /**
     57         * wp_dashboard_quota() must not throw when quota is 0 and space is used.
     58         *
     59         * @ticket 65439
     60         */
     61        public function test_quota_zero_used_nonzero_does_not_throw() {
     62                add_filter( 'get_space_allowed', '__return_zero' );
     63                add_filter( 'pre_get_space_used', array( $this, '_filter_space_5' ) );
     64
     65                $threw = false;
     66                try {
     67                        ob_start();
     68                        wp_dashboard_quota();
     69                        ob_end_clean();
     70                } catch ( \DivisionByZeroError $e ) {
     71                        ob_end_clean();
     72                        $threw = true;
     73                }
     74
     75                remove_filter( 'get_space_allowed', '__return_zero' );
     76                remove_filter( 'pre_get_space_used', array( $this, '_filter_space_5' ) );
     77
     78                $this->assertFalse( $threw, 'wp_dashboard_quota() must not throw DivisionByZeroError when quota is 0 and space is used.' );
     79        }
     80
     81        /**
     82         * wp_dashboard_quota() must not throw when quota is negative.
     83         *
     84         * @ticket 65439
     85         */
     86        public function test_quota_negative_does_not_throw() {
     87                add_filter( 'get_space_allowed', array( $this, '_filter_space_negative' ) );
     88                add_filter( 'pre_get_space_used', '__return_zero' );
     89
     90                $threw = false;
     91                try {
     92                        ob_start();
     93                        wp_dashboard_quota();
     94                        ob_end_clean();
     95                } catch ( \DivisionByZeroError $e ) {
     96                        ob_end_clean();
     97                        $threw = true;
     98                }
     99
     100                remove_filter( 'get_space_allowed', array( $this, '_filter_space_negative' ) );
     101                remove_filter( 'pre_get_space_used', '__return_zero' );
     102
     103                $this->assertFalse( $threw, 'wp_dashboard_quota() must not throw when quota is negative.' );
     104        }
     105
     106        /**
     107         * wp_dashboard_quota() must render output normally for a non-zero quota.
     108         *
     109         * @ticket 65439
     110         */
     111        public function test_quota_nonzero_renders_output() {
     112                add_filter( 'get_space_allowed', array( $this, '_filter_space_100' ) );
     113                add_filter( 'pre_get_space_used', array( $this, '_filter_space_5' ) );
     114
     115                ob_start();
     116                wp_dashboard_quota();
     117                $output = ob_get_clean();
     118
     119                remove_filter( 'get_space_allowed', array( $this, '_filter_space_100' ) );
     120                remove_filter( 'pre_get_space_used', array( $this, '_filter_space_5' ) );
     121
     122                $this->assertStringContainsString( 'Storage Space', $output, 'wp_dashboard_quota() must render the Storage Space widget for a normal quota.' );
     123        }
     124
     125        /**
     126         * wp_dashboard_quota() must produce no output when quota is 0
     127         * (early return, nothing to display).
     128         *
     129         * @ticket 65439
     130         */
     131        public function test_quota_zero_produces_no_output() {
     132                add_filter( 'get_space_allowed', '__return_zero' );
     133                add_filter( 'pre_get_space_used', '__return_zero' );
     134
     135                ob_start();
     136                wp_dashboard_quota();
     137                $output = ob_get_clean();
     138
     139                remove_filter( 'get_space_allowed', '__return_zero' );
     140                remove_filter( 'pre_get_space_used', '__return_zero' );
     141
     142                $this->assertSame( '', $output, 'wp_dashboard_quota() must produce no output when quota is 0.' );
     143        }
     144
     145        public function _filter_space_5() {
     146                return 5;
     147        }
     148
     149        public function _filter_space_100() {
     150                return 100;
     151        }
     152
     153        public function _filter_space_negative() {
     154                return -1;
     155        }
     156
     157        /**
     158         * display_space_usage() must not throw a DivisionByZeroError when quota is 0.
     159         *
     160         * @ticket 65439
     161         */
     162        public function test_display_space_usage_quota_zero_does_not_throw() {
     163                add_filter( 'get_space_allowed', '__return_zero' );
     164                add_filter( 'pre_get_space_used', '__return_zero' );
     165
     166                $threw = false;
     167                try {
     168                        ob_start();
     169                        display_space_usage();
     170                        ob_end_clean();
     171                } catch ( \DivisionByZeroError $e ) {
     172                        ob_end_clean();
     173                        $threw = true;
     174                }
     175
     176                remove_filter( 'get_space_allowed', '__return_zero' );
     177                remove_filter( 'pre_get_space_used', '__return_zero' );
     178
     179                $this->assertFalse( $threw, 'display_space_usage() must not throw DivisionByZeroError when quota is 0.' );
     180        }
     181
     182        /**
     183         * display_space_usage() must render output normally for a non-zero quota.
     184         *
     185         * @ticket 65439
     186         */
     187        public function test_display_space_usage_quota_nonzero_renders_output() {
     188                add_filter( 'get_space_allowed', array( $this, '_filter_space_100' ) );
     189                add_filter( 'pre_get_space_used', array( $this, '_filter_space_5' ) );
     190
     191                ob_start();
     192                display_space_usage();
     193                $output = ob_get_clean();
     194
     195                remove_filter( 'get_space_allowed', array( $this, '_filter_space_100' ) );
     196                remove_filter( 'pre_get_space_used', array( $this, '_filter_space_5' ) );
     197
     198                $this->assertStringContainsString( 'Used:', $output, 'display_space_usage() must render usage output for a normal quota.' );
     199        }
     200}