silverstripe /
silverstripe-fulltextsearch
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SilverStripe\FullTextSearch\Tests; |
||
| 4 | |||
| 5 | use SilverStripe\Core\Config\Config; |
||
| 6 | use SilverStripe\Core\Injector\Injector; |
||
| 7 | use SilverStripe\Dev\SapphireTest; |
||
| 8 | use SilverStripe\FullTextSearch\Search\FullTextSearch; |
||
| 9 | use SilverStripe\FullTextSearch\Search\Processors\SearchUpdateProcessor; |
||
| 10 | use SilverStripe\FullTextSearch\Search\Processors\SearchUpdateImmediateProcessor; |
||
| 11 | use SilverStripe\FullTextSearch\Search\Updaters\SearchUpdater; |
||
| 12 | use SilverStripe\FullTextSearch\Tests\SearchUpdaterTest\SearchUpdaterTest_Container; |
||
| 13 | use SilverStripe\FullTextSearch\Tests\SearchUpdaterTest\SearchUpdaterTest_HasOne; |
||
| 14 | use SilverStripe\FullTextSearch\Tests\SearchUpdaterTest\SearchUpdaterTest_HasMany; |
||
| 15 | use SilverStripe\FullTextSearch\Tests\SearchUpdaterTest\SearchUpdaterTest_Index; |
||
| 16 | |||
| 17 | class SearchUpdaterTest extends SapphireTest |
||
| 18 | { |
||
| 19 | protected $usesDatabase = true; |
||
| 20 | |||
| 21 | private static $index = null; |
||
| 22 | |||
| 23 | protected function setUp() |
||
| 24 | { |
||
| 25 | parent::setUp(); |
||
| 26 | |||
| 27 | if (self::$index === null) { |
||
| 28 | self::$index = SearchUpdaterTest_Index::singleton(); |
||
| 29 | } else { |
||
| 30 | self::$index->reset(); |
||
| 31 | } |
||
| 32 | |||
| 33 | Config::modify()->set(Injector::class, SearchUpdateProcessor::class, array( |
||
| 34 | 'class' => SearchUpdateImmediateProcessor::class |
||
| 35 | )); |
||
| 36 | |||
| 37 | FullTextSearch::force_index_list(self::$index); |
||
| 38 | SearchUpdater::clear_dirty_indexes(); |
||
| 39 | } |
||
| 40 | |||
| 41 | public function testBasic() |
||
| 42 | { |
||
| 43 | $item = new SearchUpdaterTest_Container(); |
||
| 44 | $item->write(); |
||
| 45 | |||
| 46 | // TODO: Make sure changing field1 updates item. |
||
| 47 | // TODO: Get updating just field2 to not update item (maybe not possible - variants complicate) |
||
| 48 | } |
||
| 49 | |||
| 50 | public function testHasOneHook() |
||
| 51 | { |
||
| 52 | $hasOne = new SearchUpdaterTest_HasOne(); |
||
| 53 | $hasOne->write(); |
||
| 54 | |||
| 55 | $alternateHasOne = new SearchUpdaterTest_HasOne(); |
||
| 56 | $alternateHasOne->write(); |
||
| 57 | |||
| 58 | $container1 = new SearchUpdaterTest_Container(); |
||
| 59 | $container1->HasOneObjectID = $hasOne->ID; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 60 | $container1->write(); |
||
| 61 | |||
| 62 | $container2 = new SearchUpdaterTest_Container(); |
||
| 63 | $container2->HasOneObjectID = $hasOne->ID; |
||
| 64 | $container2->write(); |
||
| 65 | |||
| 66 | $container3 = new SearchUpdaterTest_Container(); |
||
| 67 | $container3->HasOneObjectID = $alternateHasOne->ID; |
||
| 68 | $container3->write(); |
||
| 69 | |||
| 70 | // Check the default "writing a document updates the document" |
||
| 71 | SearchUpdater::flush_dirty_indexes(); |
||
| 72 | |||
| 73 | $added = self::$index->getAdded(['ID']); |
||
| 74 | // Some databases don't output $added in a consistent order; that's okay |
||
| 75 | usort($added, function ($a, $b) { |
||
| 76 | return $a['ID']-$b['ID']; |
||
| 77 | }); |
||
| 78 | |||
| 79 | $this->assertEquals([ |
||
| 80 | ['ID' => $container1->ID], |
||
| 81 | ['ID' => $container2->ID], |
||
| 82 | ['ID' => $container3->ID], |
||
| 83 | ], $added); |
||
| 84 | |||
| 85 | // Check writing a has_one tracks back to the origin documents |
||
| 86 | |||
| 87 | self::$index->reset(); |
||
| 88 | |||
| 89 | $hasOne->Field1 = "Updated"; |
||
|
0 ignored issues
–
show
The property
Field1 does not exist on SilverStripe\FullTextSea...earchUpdaterTest_HasOne. Since you implemented __set, consider adding a @property annotation.
Loading history...
|
|||
| 90 | $hasOne->write(); |
||
| 91 | |||
| 92 | SearchUpdater::flush_dirty_indexes(); |
||
| 93 | $added = self::$index->getAdded(['ID']); |
||
| 94 | |||
| 95 | // Some databases don't output $added in a consistent order; that's okay |
||
| 96 | usort($added, function ($a, $b) { |
||
| 97 | return $a['ID']-$b['ID']; |
||
| 98 | }); |
||
| 99 | |||
| 100 | $this->assertEquals([ |
||
| 101 | ['ID' => $container1->ID], |
||
| 102 | ['ID' => $container2->ID], |
||
| 103 | ], $added); |
||
| 104 | |||
| 105 | // Check updating an unrelated field doesn't track back |
||
| 106 | |||
| 107 | self::$index->reset(); |
||
| 108 | |||
| 109 | $hasOne->Field2 = "Updated"; |
||
|
0 ignored issues
–
show
The property
Field2 does not exist on SilverStripe\FullTextSea...earchUpdaterTest_HasOne. Since you implemented __set, consider adding a @property annotation.
Loading history...
|
|||
| 110 | $hasOne->write(); |
||
| 111 | |||
| 112 | SearchUpdater::flush_dirty_indexes(); |
||
| 113 | $this->assertEquals([], self::$index->getAdded(['ID'])); |
||
| 114 | |||
| 115 | // Check writing a has_one tracks back to the origin documents |
||
| 116 | |||
| 117 | self::$index->reset(); |
||
| 118 | |||
| 119 | $alternateHasOne->Field1= "Updated"; |
||
| 120 | $alternateHasOne->write(); |
||
| 121 | |||
| 122 | SearchUpdater::flush_dirty_indexes(); |
||
| 123 | $this->assertEquals([ |
||
| 124 | ['ID' => $container3->ID], |
||
| 125 | ], self::$index->getAdded(['ID'])); |
||
| 126 | } |
||
| 127 | |||
| 128 | public function testHasManyHook() |
||
| 129 | { |
||
| 130 | $container1 = new SearchUpdaterTest_Container(); |
||
| 131 | $container1->write(); |
||
| 132 | |||
| 133 | $container2 = new SearchUpdaterTest_Container(); |
||
| 134 | $container2->write(); |
||
| 135 | |||
| 136 | //self::$index->reset(); |
||
| 137 | //SearchUpdater::clear_dirty_indexes(); |
||
| 138 | |||
| 139 | $hasMany1 = new SearchUpdaterTest_HasMany(); |
||
| 140 | $hasMany1->HasManyContainerID = $container1->ID; |
||
|
0 ignored issues
–
show
The property
HasManyContainerID does not exist on SilverStripe\FullTextSea...archUpdaterTest_HasMany. Since you implemented __set, consider adding a @property annotation.
Loading history...
|
|||
| 141 | $hasMany1->write(); |
||
| 142 | |||
| 143 | $hasMany2 = new SearchUpdaterTest_HasMany(); |
||
| 144 | $hasMany2->HasManyContainerID = $container1->ID; |
||
| 145 | $hasMany2->write(); |
||
| 146 | |||
| 147 | SearchUpdater::flush_dirty_indexes(); |
||
| 148 | |||
| 149 | $this->assertEquals([ |
||
| 150 | ['ID' => $container1->ID], |
||
| 151 | ['ID' => $container2->ID], |
||
| 152 | ], self::$index->getAdded(['ID'])); |
||
| 153 | |||
| 154 | self::$index->reset(); |
||
| 155 | |||
| 156 | $hasMany1->Field1 = 'Updated'; |
||
|
0 ignored issues
–
show
The property
Field1 does not exist on SilverStripe\FullTextSea...archUpdaterTest_HasMany. Since you implemented __set, consider adding a @property annotation.
Loading history...
|
|||
| 157 | $hasMany1->write(); |
||
| 158 | |||
| 159 | $hasMany2->Field1 = 'Updated'; |
||
| 160 | $hasMany2->write(); |
||
| 161 | |||
| 162 | SearchUpdater::flush_dirty_indexes(); |
||
| 163 | $this->assertEquals([ |
||
| 164 | ['ID' => $container1->ID], |
||
| 165 | ], self::$index->getAdded(['ID'])); |
||
| 166 | } |
||
| 167 | } |
||
| 168 |