|
8 | 8 | use Algolia\AlgoliaSearch\Iterators\ObjectIterator; |
9 | 9 | use Algolia\ScoutExtended\Searchable\Aggregator; |
10 | 10 | use Algolia\ScoutExtended\Searchable\AggregatorCollection; |
| 11 | +use Algolia\ScoutExtended\Searchable\AggregatorObserver; |
11 | 12 | use Algolia\ScoutExtended\Searchable\Aggregators; |
12 | 13 | use App\All; |
13 | 14 | use App\News; |
@@ -692,6 +693,115 @@ public function testSkipSavedWhenDisableSyncingFor(): void |
692 | 693 | ModelObserver::enableSyncingFor(User::class); |
693 | 694 | } |
694 | 695 | } |
| 696 | + |
| 697 | + public function testSkipSavedWhenDisableSyncingForAggregator(): void |
| 698 | + { |
| 699 | + $wallIndexMock = $this->mockIndex('wall'); |
| 700 | + $usersIndexMock = $this->mockIndex('users'); |
| 701 | + |
| 702 | + $usersIndexMock->shouldReceive('saveObjects')->once(); |
| 703 | + |
| 704 | + $user = factory(User::class)->create(); |
| 705 | + |
| 706 | + AggregatorObserver::disableSyncingFor(Wall::class); |
| 707 | + |
| 708 | + Wall::bootSearchable(); |
| 709 | + try { |
| 710 | + // Ensure saveObjects is NOT called for the wall index (aggregator) |
| 711 | + $wallIndexMock->shouldNotReceive('saveObjects') |
| 712 | + ->with(Mockery::on(function ($argument) { |
| 713 | + return count($argument) === 1 && array_key_exists('email', $argument[0]) && |
| 714 | + $argument[0]['objectID'] === 'App\User::1'; |
| 715 | + })); |
| 716 | + |
| 717 | + // Expect saveObjects to be called once for the user index |
| 718 | + $usersIndexMock->shouldReceive('saveObjects') |
| 719 | + ->once() |
| 720 | + ->with(Mockery::on(function ($argument) { |
| 721 | + return count($argument) === 1 && array_key_exists('email', $argument[0]) && |
| 722 | + $argument[0]['objectID'] === 'App\User::1'; |
| 723 | + })); |
| 724 | + |
| 725 | + Wall::bootSearchable(); |
| 726 | + |
| 727 | + $user->save(); |
| 728 | + } finally { |
| 729 | + AggregatorObserver::enableSyncingFor(User::class); |
| 730 | + } |
| 731 | + } |
| 732 | + |
| 733 | + public function testSkipDeletedWhenDisableSyncingForAggregator(): void |
| 734 | + { |
| 735 | + $this->app['config']->set('scout.algolia.use_deprecated_delete_by', false); |
| 736 | + |
| 737 | + $wallIndexMock = $this->mockIndex('wall'); |
| 738 | + $usersIndexMock = $this->mockIndex('users'); |
| 739 | + |
| 740 | + $usersIndexMock->shouldReceive('saveObjects')->once(); |
| 741 | + |
| 742 | + $user = factory(User::class)->create(); |
| 743 | + |
| 744 | + AggregatorObserver::disableSyncingFor(Wall::class); |
| 745 | + |
| 746 | + Wall::bootSearchable(); |
| 747 | + |
| 748 | + try { |
| 749 | + // Ensure saveObjects is NOT called for the wall index (aggregator) |
| 750 | + $wallIndexMock->shouldNotReceive('browseObjects')->with([ |
| 751 | + 'attributesToRetrieve' => ['objectID'], |
| 752 | + 'tagFilters' => [['App\User::1']], |
| 753 | + ])->andReturn([['objectID' => 'App\User::1']]); |
| 754 | + $wallIndexMock->shouldNotReceive('deleteObjects')->with(['App\User::1']); |
| 755 | + |
| 756 | + // Expect browseObjects and deleteObjects to be called on the users index |
| 757 | + $usersIndexMock->shouldReceive('browseObjects')->once()->with([ |
| 758 | + 'attributesToRetrieve' => ['objectID'], |
| 759 | + 'tagFilters' => [['App\User::1']], |
| 760 | + ])->andReturn([['objectID' => 'App\User::1']]); |
| 761 | + $usersIndexMock->shouldReceive('deleteObjects')->once()->with(['App\User::1']); |
| 762 | + |
| 763 | + $user->delete(); |
| 764 | + } finally { |
| 765 | + AggregatorObserver::enableSyncingFor(Wall::class); |
| 766 | + } |
| 767 | + } |
| 768 | + |
| 769 | + public function testSkipForceDeletedWhenDisableSyncingForAggregator(): void |
| 770 | + { |
| 771 | + $this->app['config']->set('scout.algolia.use_deprecated_delete_by', false); |
| 772 | + |
| 773 | + $wallIndexMock = $this->mockIndex('wall'); |
| 774 | + $usersIndexMock = $this->mockIndex('users'); |
| 775 | + |
| 776 | + $usersIndexMock->shouldReceive('saveObjects')->once(); |
| 777 | + |
| 778 | + $user = factory(User::class)->create(); |
| 779 | + |
| 780 | + AggregatorObserver::disableSyncingFor(Wall::class); |
| 781 | + |
| 782 | + Wall::bootSearchable(); |
| 783 | + |
| 784 | + try { |
| 785 | + // Ensure saveObjects is NOT called for the wall index (aggregator) |
| 786 | + $wallIndexMock->shouldNotReceive('browseObjects')->with([ |
| 787 | + 'attributesToRetrieve' => ['objectID'], |
| 788 | + 'tagFilters' => [['App\User::1']], |
| 789 | + ])->andReturn([['objectID' => 'App\User::1']]); |
| 790 | + $wallIndexMock->shouldNotReceive('deleteObjects')->with(['App\User::1']); |
| 791 | + |
| 792 | + // Expect browseObjects and deleteObjects to be called on the users index |
| 793 | + $usersIndexMock->shouldReceive('browseObjects')->once()->with([ |
| 794 | + 'attributesToRetrieve' => ['objectID'], |
| 795 | + 'tagFilters' => [['App\User::1']], |
| 796 | + ])->andReturn([['objectID' => 'App\User::1']]); |
| 797 | + $usersIndexMock->shouldReceive('deleteObjects')->once()->with(['App\User::1']); |
| 798 | + |
| 799 | + $user->forceDelete(); |
| 800 | + } finally { |
| 801 | + AggregatorObserver::enableSyncingFor(Wall::class); |
| 802 | + } |
| 803 | + } |
| 804 | + |
695 | 805 | } |
696 | 806 |
|
697 | 807 | class DummyRemoveFromSearch { |
|
0 commit comments