Make WordPress Core

Changeset 55903


Ignore:
Timestamp:
06/10/2023 05:28:02 AM (3 years ago)
Author:
audrasjb
Message:

Plugins: Display Auto-updates filters when the current view is "Must Use" or "Drop-in".

This changeset fixes a bug where the "Auto-updates Enabled/Disabled" filters were not showing when the current view is "Must Use" or "Drop-in".

Props NekoJonez, pbiron, costdev, audrasjb.
Fixes #54309.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/class-wp-plugins-list-table.php

    r55732 r55903  
    6262                $this->show_autoupdates = wp_is_auto_update_enabled_for_type( 'plugin' )
    6363                        && current_user_can( 'update_plugins' )
    64                         && ( ! is_multisite() || $this->screen->in_admin( 'network' ) )
    65                         && ! in_array( $status, array( 'mustuse', 'dropins' ), true );
     64                        && ( ! is_multisite() || $this->screen->in_admin( 'network' ) );
    6665        }
    6766
     
    464463                );
    465464
    466                 if ( $this->show_autoupdates ) {
     465                if ( $this->show_autoupdates && ! in_array( $status, array( 'mustuse', 'dropins' ), true ) ) {
    467466                        $columns['auto-updates'] = __( 'Automatic Updates' );
    468467                }
     
    11551154                                        break;
    11561155                                case 'auto-updates':
    1157                                         if ( ! $this->show_autoupdates ) {
     1156                                        if ( ! $this->show_autoupdates || in_array( $status, array( 'mustuse', 'dropins' ), true ) ) {
    11581157                                                break;
    11591158                                        }
  • trunk/tests/phpunit/tests/admin/wpPluginsListTable.php

    r54215 r55903  
    1111         */
    1212        public $table = false;
     13
     14        /**
     15         * An admin user ID.
     16         *
     17         * @var int
     18         */
     19        private static $admin_id;
     20
     21        /**
     22         * Creates an admin user before any tests run.
     23         */
     24        public static function set_up_before_class() {
     25                parent::set_up_before_class();
     26
     27                self::$admin_id = self::factory()->user->create(
     28                        array(
     29                                'role'       => 'administrator',
     30                                'user_login' => 'test_wp_plugins_list_table',
     31                                'user_pass'  => 'password',
     32                                'user_email' => 'testadmin@test.com',
     33                        )
     34                );
     35        }
    1336
    1437        public function set_up() {
     
    5780                $this->assertSame( $expected, $actual );
    5881        }
     82
     83        /**
     84         * Tests that WP_Plugins_List_Table::__construct() does not set
     85         * the 'show_autoupdates' property to false for Must-Use and Drop-in
     86         * plugins.
     87         *
     88         * The 'ms-excluded' group is added as $this->show_autoupdates is already set to false for multisite.
     89         *
     90         * @ticket 54309
     91         * @group ms-excluded
     92         *
     93         * @covers WP_Plugins_List_Table::__construct()
     94         *
     95         * @dataProvider data_status_mustuse_and_dropins
     96         *
     97         * @param string $status The value for $_REQUEST['plugin_status'].
     98         */
     99        public function test_construct_should_not_set_show_autoupdates_to_false_for_mustuse_and_dropins( $status ) {
     100                $original_status           = isset( $_REQUEST['plugin_status'] ) ? $_REQUEST['plugin_status'] : null;
     101                $_REQUEST['plugin_status'] = $status;
     102
     103                // Enable plugin auto-updates.
     104                add_filter( 'plugins_auto_update_enabled', '__return_true' );
     105
     106                // Use a user with the 'manage_plugins' capability.
     107                wp_set_current_user( self::$admin_id );
     108
     109                $list_table       = new WP_Plugins_List_Table();
     110                $show_autoupdates = new ReflectionProperty( $list_table, 'show_autoupdates' );
     111
     112                $show_autoupdates->setAccessible( true );
     113                $actual = $show_autoupdates->getValue( $list_table );
     114                $show_autoupdates->setAccessible( false );
     115
     116                $_REQUEST['plugin_status'] = $original_status;
     117
     118                $this->assertTrue( $actual );
     119        }
     120
     121        /**
     122         * Tests that WP_Plugins_List_Table::get_columns() does not add
     123         * the auto-update column when not viewing Must-Use or Drop-in plugins.
     124         *
     125         * @ticket 54309
     126         *
     127         * @covers WP_Plugins_List_Table::get_columns
     128         *
     129         * @dataProvider data_status_mustuse_and_dropins
     130         *
     131         * @param string $test_status The value for the global $status variable.
     132         */
     133        public function test_get_columns_should_not_add_the_autoupdates_column_when_viewing_mustuse_or_dropins( $test_status ) {
     134                global $status;
     135
     136                $original_status = $status;
     137
     138                // Enable plugin auto-updates.
     139                add_filter( 'plugins_auto_update_enabled', '__return_true' );
     140
     141                // Use a user with the 'manage_plugins' capability.
     142                wp_set_current_user( self::$admin_id );
     143
     144                $status = $test_status;
     145                $actual = $this->table->get_columns();
     146                $status = $original_status;
     147
     148                $this->assertArrayNotHasKey( 'auto-updates', $actual );
     149        }
     150
     151        /**
     152         * Tests that WP_Plugins_List_Table::get_columns() does not add
     153         * the auto-update column when the 'plugins_auto_update_enabled'
     154         * filter returns false.
     155         *
     156         * @ticket 54309
     157         *
     158         * @covers WP_Plugins_List_Table::get_columns
     159         */
     160        public function test_get_columns_should_not_add_the_autoupdates_column_when_plugin_auto_update_is_disabled() {
     161                global $status;
     162
     163                $original_status = $status;
     164
     165                // Enable plugin auto-updates.
     166                add_filter( 'plugins_auto_update_enabled', '__return_false' );
     167
     168                // Use a user with the 'manage_plugins' capability.
     169                wp_set_current_user( self::$admin_id );
     170
     171                $status = 'all';
     172                $actual = $this->table->get_columns();
     173                $status = $original_status;
     174
     175                $this->assertArrayNotHasKey( 'auto-updates', $actual );
     176        }
     177
     178        /**
     179         * Tests that WP_Plugins_List_Table::single_row() does not output the
     180         * 'Auto-updates' column for Must-Use or Drop-in plugins.
     181         *
     182         * @ticket 54309
     183         *
     184         * @covers WP_Plugins_List_Table::single_row
     185         *
     186         * @dataProvider data_status_mustuse_and_dropins
     187         *
     188         * @param string $test_status The value for the global $status variable.
     189         */
     190        public function test_single_row_should_not_add_the_autoupdates_column_for_mustuse_or_dropins( $test_status ) {
     191                global $status;
     192
     193                $original_status = $status;
     194
     195                // Enable plugin auto-updates.
     196                add_filter( 'plugins_auto_update_enabled', '__return_true' );
     197
     198                // Use a user with the 'manage_plugins' capability.
     199                wp_set_current_user( self::$admin_id );
     200
     201                $column_info = array(
     202                        array(
     203                                'name'         => 'Plugin',
     204                                'description'  => 'Description',
     205                                'auto-updates' => 'Auto-updates',
     206                        ),
     207                        array(),
     208                        array(),
     209                        'name',
     210                );
     211
     212                // Mock WP_Plugins_List_Table
     213                $list_table_mock = $this->getMockBuilder( 'WP_Plugins_List_Table' )
     214                        // Note: setMethods() is deprecated in PHPUnit 9, but still supported.
     215                        ->setMethods( array( 'get_column_info' ) )
     216                        ->getMock();
     217
     218                // Force the return value of the get_column_info() method.
     219                $list_table_mock->expects( $this->once() )->method( 'get_column_info' )->willReturn( $column_info );
     220
     221                $single_row_args = array(
     222                        'advanced-cache.php',
     223                        array(
     224                                'Name'        => 'Advanced caching plugin',
     225                                'slug'        => 'advanced-cache',
     226                                'Description' => 'An advanced caching plugin.',
     227                                'Author'      => 'A plugin author',
     228                                'Version'     => '1.0.0',
     229                                'Author URI'  => 'http://example.org',
     230                                'Text Domain' => 'advanced-cache',
     231                        ),
     232                );
     233
     234                $status = $test_status;
     235                ob_start();
     236                $list_table_mock->single_row( $single_row_args );
     237                $actual = ob_get_clean();
     238                $status = $original_status;
     239
     240                $this->assertIsString( $actual, 'Output was not captured.' );
     241                $this->assertNotEmpty( $actual, 'The output string was empty.' );
     242                $this->assertStringNotContainsString( 'column-auto-updates', $actual, 'The auto-updates column was output.' );
     243        }
     244
     245        /**
     246         * Data provider.
     247         *
     248         * @return array[]
     249         */
     250        public function data_status_mustuse_and_dropins() {
     251                return array(
     252                        'Must-Use' => array( 'mustuse' ),
     253                        'Drop-ins' => array( 'dropins' ),
     254                );
     255        }
    59256}
Note: See TracChangeset for help on using the changeset viewer.