Skip to content

Commit 423c167

Browse files
committed
Fix AggregatorObserver sync status
1 parent 127f03c commit 423c167

File tree

2 files changed

+105
-8
lines changed

2 files changed

+105
-8
lines changed

src/Searchable/AggregatorObserver.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,6 @@ public function saved($model): void
8585
*/
8686
public function deleted($model): void
8787
{
88-
if (static::syncingDisabledFor($model)) {
89-
return;
90-
}
91-
9288
if ($this->usesSoftDelete($model) && config('scout.soft_delete', false)) {
9389
$this->saved($model);
9490
} else {
@@ -112,10 +108,6 @@ public function deleted($model): void
112108
*/
113109
public function forceDeleted($model): void
114110
{
115-
if (static::syncingDisabledFor($model)) {
116-
return;
117-
}
118-
119111
$class = get_class($model);
120112

121113
if (! array_key_exists($class, $this->aggregators)) {

tests/Features/AggregatorTest.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use App\Thread;
1616
use App\User;
1717
use App\Wall;
18+
use Laravel\Scout\ModelObserver;
1819
use Laravel\Scout\Scout;
1920
use Mockery;
2021
use RuntimeException;
@@ -582,6 +583,110 @@ public function testWhenAggregatorIsBootedBeforePlainScoutSearchableTrait(): voi
582583

583584
$user->delete();
584585
}
586+
587+
public function testSkipDeletedWhenDisableSyncingFor(): void
588+
{
589+
$this->app['config']->set('scout.algolia.use_deprecated_delete_by', false);
590+
591+
$wallIndexMock = $this->mockIndex('wall');
592+
$usersIndexMock = $this->mockIndex('users');
593+
594+
$usersIndexMock->shouldReceive('saveObjects')->once();
595+
$user = factory(User::class)->create();
596+
597+
ModelObserver::disableSyncingFor(User::class);
598+
599+
Wall::bootSearchable();
600+
601+
try {
602+
// Expect browseObjects and deleteObjects to be called on the wall index
603+
$wallIndexMock->shouldReceive('browseObjects')->once()->with([
604+
'attributesToRetrieve' => ['objectID'],
605+
'tagFilters' => [['App\User::1']],
606+
])->andReturn([['objectID' => 'App\User::1']]);
607+
$wallIndexMock->shouldReceive('deleteObjects')->once()->with(['App\User::1']);
608+
609+
// Ensure browseObjects and deleteObjects are NOT called on the users index
610+
$usersIndexMock->shouldNotReceive('browseObjects')->with([
611+
'attributesToRetrieve' => ['objectID'],
612+
'tagFilters' => [['App\User::1']],
613+
])->andReturn([['objectID' => 'App\User::1']]);
614+
$usersIndexMock->shouldNotReceive('deleteObjects')->with(['App\User::1']);
615+
616+
$user->delete();
617+
} finally {
618+
ModelObserver::enableSyncingFor(User::class);
619+
}
620+
}
621+
622+
public function testSkipForceDeletedWhenDisableSyncingFor(): void
623+
{
624+
$this->app['config']->set('scout.algolia.use_deprecated_delete_by', false);
625+
626+
$wallIndexMock = $this->mockIndex('wall');
627+
$usersIndexMock = $this->mockIndex('users');
628+
629+
$usersIndexMock->shouldReceive('saveObjects')->once();
630+
$user = factory(User::class)->create();
631+
632+
ModelObserver::disableSyncingFor(User::class);
633+
634+
Wall::bootSearchable();
635+
636+
try {
637+
// Expect browseObjects and deleteObjects to be called on the wall index
638+
$wallIndexMock->shouldReceive('browseObjects')->once()->with([
639+
'attributesToRetrieve' => ['objectID',],
640+
'tagFilters' => [['App\User::1'],],
641+
])->andReturn([['objectID' => 'App\User::1']]);
642+
$wallIndexMock->shouldReceive('deleteObjects')->once()->with(['App\User::1']);
643+
644+
// Ensure browseObjects and deleteObjects are NOT called on the users index
645+
$usersIndexMock->shouldNotReceive('browseObjects')->with([
646+
'attributesToRetrieve' => ['objectID'],
647+
'tagFilters' => [['App\User::1']],
648+
])->andReturn([['objectID' => 'App\User::1']]);
649+
$usersIndexMock->shouldNotReceive('deleteObjects')->with(['App\User::1']);
650+
651+
$user->forceDelete();
652+
} finally {
653+
ModelObserver::enableSyncingFor(User::class);
654+
}
655+
}
656+
657+
public function testSkipSavedWhenDisableSyncingFor(): void
658+
{
659+
$wallIndexMock = $this->mockIndex('wall');
660+
$usersIndexMock = $this->mockIndex('users');
661+
662+
$usersIndexMock->shouldReceive('saveObjects')->once();
663+
$user = factory(User::class)->create();
664+
665+
ModelObserver::disableSyncingFor(User::class);
666+
667+
try {
668+
// Expect saveObjects to be called once for the wall index
669+
$wallIndexMock->shouldReceive('saveObjects')
670+
->once()
671+
->with(Mockery::on(function ($argument) {
672+
return count($argument) === 1 && array_key_exists('email', $argument[0]) &&
673+
$argument[0]['objectID'] === 'App\User::1';
674+
}));
675+
676+
// Ensure saveObjects is NOT called for the users index
677+
$usersIndexMock->shouldNotReceive('saveObjects')
678+
->with(Mockery::on(function ($argument) {
679+
return count($argument) === 1 && array_key_exists('email', $argument[0]) &&
680+
$argument[0]['objectID'] === 'App\User::1';
681+
}));
682+
683+
Wall::bootSearchable();
684+
685+
$user->save();
686+
} finally {
687+
ModelObserver::enableSyncingFor(User::class);
688+
}
689+
}
585690
}
586691

587692
class DummyRemoveFromSearch {

0 commit comments

Comments
 (0)