Skip to content

Commit

Permalink
test: SearchableSensitiveAttributesTest added
Browse files Browse the repository at this point in the history
  • Loading branch information
juampi92 committed Jul 1, 2021
1 parent d755f97 commit 05679ce
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
28 changes: 28 additions & 0 deletions tests/Fixtures/SearchableModelWithSensitiveAttributes.php
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'];
}
}
68 changes: 68 additions & 0 deletions tests/Unit/SearchableSensitiveAttributesTest.php
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.');
}
}

0 comments on commit 05679ce

Please sign in to comment.