laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.55k stars 11.03k forks source link

assertSoftDeleted() and assertNotSoftDeleted() discard $data argument value when first argument is a model #39667

Closed DaneEveritt closed 2 years ago

DaneEveritt commented 2 years ago

Description:

The testing helpers assertSoftDeleted() and assertNotSoftDeleted() both accept either a table name provided as a string, or a model that implements the SoftDeletes trait. When a model is provided these functions automatically filter the query to the table for the model, as well as looking for the record with the provided primary key.

However, when a model is provided any values provided in the second argument, $data, are discarded when the function recursively calls itself. As a result, what appears to be a valid test case is actually not executed with the expected behavior:

// Assume a database table (example_table) has the following record:
id  | col_a | col_b | deleted_at
1   | foo     | null     | 2021-11-16 00:00:00

// Assume we have a "$model" variable that exists for this record.
// Now, in a test we add the following assertion:

$this->assertSoftDeleted($model, ['col_b' => 'bar']);

The example above will pass, even though the database does not contain a record for that model where col_b is equal to bar. However, if you give that assertion the following code, it does fail as expected:

$this->assertSoftDeleted('example_table', ['id' => 1, 'col_b' => 'bar']);

The code responsible for these functions is here: https://github.com/laravel/framework/blob/8.x/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php#L90-L132

The specific chunk responsible for dropping the $data parameter is here: https://github.com/laravel/framework/blob/8.x/src/Illuminate/Foundation/Testing/Concerns/InteractsWithDatabase.php#L123-L125

driesvints commented 2 years ago

Thank you for reporting. I've sent in a fix for this to https://github.com/laravel/framework/pull/39673

driesvints commented 2 years ago

Will be in the next release.