forked from laravel/scout
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/sensitive-attributes' into 9.x
- Loading branch information
Showing
7 changed files
with
273 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Laravel\Scout\Tests\Fixtures; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\SoftDeletes; | ||
use Laravel\Scout\Searchable; | ||
|
||
class SearchableModelWithSensitiveAttributes extends Model | ||
{ | ||
use Searchable; | ||
use SoftDeletes; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = ['first_name', 'last_name', 'remember_token', 'password']; | ||
|
||
/** | ||
* When updating a model, this method determines if we | ||
* should perform a search engine update or not. | ||
* | ||
* @return bool | ||
*/ | ||
public function searchShouldUpdate(): bool | ||
{ | ||
$sensitiveAttributeKeys = ['first_name', 'last_name']; | ||
|
||
return collect($this->getDirty())->keys() | ||
->intersect($sensitiveAttributeKeys) | ||
->isNotEmpty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace Laravel\Scout\Tests\Unit; | ||
|
||
use Illuminate\Support\Facades\Config; | ||
use Laravel\Scout\ModelObserver; | ||
use Laravel\Scout\Tests\Fixtures\SearchableModelWithSensitiveAttributes; | ||
use Laravel\Scout\Tests\Fixtures\SearchableModelWithSoftDeletes; | ||
use Mockery as m; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ModelObserverWithSoftDeletesTest extends TestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
Config::clearResolvedInstances(); | ||
Config::shouldReceive('get')->with('scout.after_commit', m::any())->andReturn(false); | ||
Config::shouldReceive('get')->with('scout.soft_delete', m::any())->andReturn(true); | ||
} | ||
|
||
protected function tearDown(): void | ||
{ | ||
m::close(); | ||
} | ||
|
||
public function test_deleted_handler_makes_model_unsearchable_when_it_should_not_be_searchable() | ||
{ | ||
$observer = new ModelObserver; | ||
$model = m::mock(SearchableModelWithSoftDeletes::class); | ||
$model->shouldReceive('searchShouldUpdate')->never(); // The saved event is forced | ||
$model->shouldReceive('shouldBeSearchable')->andReturn(false); // Should not be searchable | ||
$model->shouldReceive('wasSearchableBeforeDelete')->andReturn(true); | ||
$model->shouldReceive('wasSearchableBeforeUpdate')->andReturn(true); | ||
$model->shouldReceive('searchable')->never(); | ||
$model->shouldReceive('unsearchable')->once(); | ||
$observer->deleted($model); | ||
} | ||
|
||
public function test_deleted_handler_makes_model_searchable_when_it_should_be_searchable() | ||
{ | ||
$observer = new ModelObserver; | ||
$model = m::mock(SearchableModelWithSoftDeletes::class); | ||
$model->shouldReceive('searchShouldUpdate')->never(); // The saved event is forced | ||
$model->shouldReceive('shouldBeSearchable')->andReturn(true); // Should be searchable | ||
$model->shouldReceive('wasSearchableBeforeDelete')->andReturn(true); | ||
$model->shouldReceive('searchable')->once(); | ||
$model->shouldReceive('unsearchable')->never(); | ||
$observer->deleted($model); | ||
} | ||
|
||
public function test_restored_handler_makes_model_searchable() | ||
{ | ||
$observer = new ModelObserver; | ||
$model = m::mock(SearchableModelWithSoftDeletes::class); | ||
$model->shouldReceive('searchShouldUpdate')->never(); | ||
$model->shouldReceive('shouldBeSearchable')->andReturn(true); | ||
$model->shouldReceive('searchable')->once(); | ||
$model->shouldReceive('unsearchable')->never(); | ||
$observer->restored($model); | ||
} | ||
|
||
public function test_unsearchable_should_be_called_when_deleting() | ||
{ | ||
$model = m::mock( | ||
new SearchableModelWithSensitiveAttributes([ | ||
'first_name' => 'taylor', | ||
'last_name' => 'Otwell', | ||
'remember_token' => 123, | ||
'password' => 'secret', | ||
]) | ||
)->makePartial(); | ||
|
||
// Let's pretend it's in sync with the database. | ||
$model->syncOriginal(); | ||
|
||
// Assertions | ||
$model->shouldReceive('searchable')->once(); | ||
$model->shouldReceive('unsearchable')->never(); | ||
|
||
$observer = new ModelObserver; | ||
$observer->deleted($model); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters