Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] fix choosing random elements on <select> #822

Merged
merged 1 commit into from
Sep 29, 2020

Conversation

browner12
Copy link
Contributor

Problem

The browser select() method will currently choose a random option from a <select> input if the second parameter evaluates to null. This normally works fine if it is used like:

$user = (new UserFactory)->make();

$browser->visit('/user/create')
    ->name('name', $user->name)
    ->select('status');

However, it becomes problematic when a second argument is passed that evaluates as null.

$user = (new UserFactory)->make([
    'status' => $faker->optional()->randomElement(['active', 'inactive']),
]);

$browser->visit('/user/create')
    ->name('name', $user->name)
    ->select('status', $user->status);

In this scenario, if $user->status is null, we actually want the <select> to not be set, but instead it will select a random <option>.

It also makes it more difficult to edit a value from a selected option to a null option.

(new UserFactory)->create([
    'status' => 'active',
]);

$user = (new UserFactory)->make([
    'status' => null,
]);

$browser->visit('/user/1/edit')
    ->name('name', $user->name)
    ->select('status', $user->status);

This will again select a random <option>, when we really want the value to be cleared out.

Solution

This PR will switch from checking if the second argument is null, to checking if it was even passed.

This will select a random option:

$browser->select('status');

while this will specifically try to select an option whose value evaluates to an empty string:

$browser->select('status', null);

I go back and forth on if this is a breaking change or a bug fix. If we go by the documentation (https://laravel.com/docs/8.x/dusk#using-forms), this is a bug fix. If you guys deem it as a BC, and can resend to master.

only choose a random element from a `<select>` input if no second parameter is passed
@taylorotwell taylorotwell merged commit 15ae198 into laravel:6.x Sep 29, 2020
@browner12 browner12 deleted the select-random branch September 29, 2020 20:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants