|
15 | 15 | use App\Thread; |
16 | 16 | use App\User; |
17 | 17 | use App\Wall; |
| 18 | +use Laravel\Scout\ModelObserver; |
18 | 19 | use Laravel\Scout\Scout; |
19 | 20 | use Mockery; |
20 | 21 | use RuntimeException; |
@@ -582,6 +583,110 @@ public function testWhenAggregatorIsBootedBeforePlainScoutSearchableTrait(): voi |
582 | 583 |
|
583 | 584 | $user->delete(); |
584 | 585 | } |
| 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 | + } |
585 | 690 | } |
586 | 691 |
|
587 | 692 | class DummyRemoveFromSearch { |
|
0 commit comments