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.
test: SearchableSensitiveAttributesTest added
- Loading branch information
Showing
2 changed files
with
96 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Laravel\Scout\Tests\Fixtures; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Laravel\Scout\Searchable; | ||
|
||
class SearchableModelWithSensitiveAttributes extends Model | ||
{ | ||
use Searchable; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = ['first_name', 'last_name', 'remember_token', 'password']; | ||
|
||
/** | ||
* Get the update-sensitive attributes that, when changed, trigger an engine update. | ||
* | ||
* @return string[] | ||
*/ | ||
public function scoutSensitiveAttributes() | ||
{ | ||
return ['first_name', 'last_name']; | ||
} | ||
} |
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,68 @@ | ||
<?php | ||
|
||
namespace Laravel\Scout\Tests\Unit; | ||
|
||
use Laravel\Scout\Tests\Fixtures\SearchableModel; | ||
use Laravel\Scout\Tests\Fixtures\SearchableModelWithSensitiveAttributes; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class SearchableSensitiveAttributesTest extends TestCase | ||
{ | ||
public function test_update_on_sensitive_attributes_triggers_search() | ||
{ | ||
$model = new SearchableModelWithSensitiveAttributes([ | ||
'first_name' => 'taylor', | ||
'last_name' => 'Otwell', | ||
'remember_token' => 123, | ||
'password' => 'secret', | ||
]); | ||
|
||
// Let's pretend it's in sync with the database. | ||
$model->syncOriginal(); | ||
|
||
// Update | ||
$model->password = 'extremelySecurePassword'; | ||
$model->first_name = 'Taylor'; | ||
|
||
$this->assertTrue($model->searchShouldUpdate(), 'Model should update given that the first_name changed.'); | ||
} | ||
|
||
public function test_update_on_non_sensitive_attributes_doesnt_trigger_search() | ||
{ | ||
$model = new SearchableModelWithSensitiveAttributes([ | ||
'first_name' => 'taylor', | ||
'last_name' => 'Otwell', | ||
'remember_token' => 123, | ||
'password' => 'secret', | ||
]); | ||
|
||
// Let's pretend it's in sync with the database. | ||
$model->syncOriginal(); | ||
|
||
// Update | ||
$model->password = 'extremelySecurePassword'; | ||
$model->remember_token = 456; | ||
|
||
$this->assertFalse($model->searchShouldUpdate(), | ||
'Model should not update given that no sensitive attributes changed.'); | ||
} | ||
|
||
public function test_always_should_update_when_sensitive_attributes_are_not_defined() | ||
{ | ||
$model = (new SearchableModel())->forceFill([ | ||
'first_name' => 'taylor', | ||
'last_name' => 'Otwell', | ||
'remember_token' => 123, | ||
'password' => 'secret', | ||
]); | ||
|
||
// Let's pretend it's in sync with the database. | ||
$model->syncOriginal(); | ||
|
||
// Update | ||
$model->password = 'extremelySecurePassword'; | ||
|
||
$this->assertTrue($model->searchShouldUpdate(), | ||
'Model should always update since sensitive attributes were not defined.'); | ||
} | ||
} |