Make WordPress Core

Changeset 65334


Ignore:
Timestamp:
11/07/2026 06:52:00 PM (12 hours ago)
Author:
lancewillett
Message:

Tests: Cover the self-demotion guard and empty user searches.

Log in as a second administrator and try to change their own role to Subscriber with the bulk Change role control on the Users screen. Check for the error notice and that the role is unchanged. Search users for a missing name and check for the empty result, with another user present so the test fails if search is ignored.

Developed in: ​https://github.com/WordPress/wordpress-develop/pull/15779

Props shailu25.
See #52895.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/e2e/specs/users.test.js

    r64615 r65334  
    155155                ] );
    156156        } );
     157
     158        test( 'prevents an administrator from changing their own role to subscriber', async ( {
     159                browser,
     160                requestUtils,
     161                baseURL,
     162        } ) => {
     163                // Use a disposable administrator so a regression cannot demote the shared
     164                // suite account and break cleanup or later tests.
     165                const username = `${ prefix }-self-role`;
     166                const password = randomUUID();
     167                usernames.push( username );
     168                const disposable = await requestUtils.createUser( {
     169                        username,
     170                        email: `${ username }@example.org`,
     171                        password,
     172                        roles: [ 'administrator' ],
     173                } );
     174
     175                const context = await browser.newContext( {
     176                        storageState: { cookies: [], origins: [] },
     177                } );
     178                try {
     179                        const page = await context.newPage();
     180                        await page.goto( new URL( 'wp-login.php', baseURL ).href );
     181                        await page
     182                                .getByLabel( 'Username or Email Address', { exact: true } )
     183                                .fill( username );
     184                        await page
     185                                .getByLabel( 'Password', { exact: true } )
     186                                .fill( password );
     187                        await page
     188                                .getByRole( 'button', { name: 'Log In', exact: true } )
     189                                .click();
     190                        await expect(
     191                                page.getByRole( 'heading', {
     192                                        name: 'Dashboard',
     193                                        exact: true,
     194                                } )
     195                        ).toBeVisible();
     196
     197                        await page.goto( new URL( 'wp-admin/profile.php', baseURL ).href );
     198                        await expect(
     199                                page.getByLabel( 'Role', { exact: true } )
     200                        ).toHaveCount( 0 );
     201
     202                        await page.goto(
     203                                new URL(
     204                                        `wp-admin/users.php?s=${ encodeURIComponent( username ) }`,
     205                                        baseURL
     206                                ).href
     207                        );
     208                        await page
     209                                .locator( `#user-${ disposable.id }` )
     210                                .getByRole( 'checkbox' )
     211                                .check();
     212                        // Top/bottom role controls share an accessible name, so use the top control ID.
     213                        await page.locator( '#new_role' ).selectOption( 'subscriber' );
     214                        await page.locator( '#changeit' ).click();
     215                        await expect( page.locator( '#message.error' ) ).toContainText(
     216                                'You cannot change your own role to one that does not allow managing other users. Your role was not changed.'
     217                        );
     218
     219                        const stillAdmin = await requestUtils.rest( {
     220                                path: `/wp/v2/users/${ disposable.id }`,
     221                                params: { context: 'edit' },
     222                        } );
     223                        expect( stillAdmin.roles ).toContain( 'administrator' );
     224                } finally {
     225                        await context.close();
     226                }
     227        } );
     228
     229        test( 'shows an empty result when searching for a missing user', async ( {
     230                admin,
     231                page,
     232                requestUtils,
     233        } ) => {
     234                // Ensure at least one non-matching user exists so ignoring `s` cannot
     235                // vacuously pass with an empty users table.
     236                const present = `${ prefix }-present`;
     237                usernames.push( present );
     238                await requestUtils.createUser( {
     239                        username: present,
     240                        email: `${ present }@example.org`,
     241                        password: randomUUID(),
     242                        roles: [ 'subscriber' ],
     243                } );
     244                const missing = `${ prefix }-missing-${ randomUUID().slice( 0, 8 ) }`;
     245
     246                await admin.visitAdminPage(
     247                        '/users.php',
     248                        `s=${ encodeURIComponent( missing ) }`
     249                );
     250                await expect(
     251                        page.getByRole( 'cell', {
     252                                name: 'No users found.',
     253                                exact: true,
     254                        } )
     255                ).toBeVisible();
     256        } );
    157257} );
Note: See TracChangeset for help on using the changeset viewer.