gordonbanderson /
freetextsearch
| 1 | <?php declare(strict_types = 1); |
||||
| 2 | |||||
| 3 | namespace Suilven\FreeTextSearch\Tests\Factory; |
||||
| 4 | |||||
| 5 | use League\CLImate\CLImate; |
||||
| 6 | use SilverStripe\CMS\Model\SiteTree; |
||||
| 7 | use SilverStripe\Core\Injector\Injector; |
||||
| 8 | use SilverStripe\Dev\SapphireTest; |
||||
| 9 | use SilverStripe\ORM\DataObject; |
||||
| 10 | use SilverStripe\ORM\DB; |
||||
| 11 | use SilverStripe\SiteConfig\SiteConfig; |
||||
| 12 | use Suilven\FreeTextSearch\Helper\BulkIndexingHelper; |
||||
| 13 | use Suilven\FreeTextSearch\Tests\Mock\BulkIndexer; |
||||
| 14 | use Symbiote\QueuedJobs\DataObjects\QueuedJobDescriptor; |
||||
| 15 | use Symbiote\QueuedJobs\Services\QueuedJob; |
||||
| 16 | |||||
| 17 | class BulkIndexerTest extends SapphireTest |
||||
| 18 | { |
||||
| 19 | protected static $fixture_file = ['tests/fixtures/sitetree.yml']; |
||||
| 20 | |||||
| 21 | public function setUp(): void |
||||
| 22 | { |
||||
| 23 | parent::setUp(); |
||||
| 24 | |||||
| 25 | $config = SiteConfig::current_site_config(); |
||||
| 26 | $config->FreeTextSearchIndexingModeInBulk = 1; |
||||
| 27 | $config->write(); |
||||
| 28 | } |
||||
| 29 | |||||
| 30 | |||||
| 31 | public function testSingleDocumentBeingQueuedForBulkIndexing(): void |
||||
| 32 | { |
||||
| 33 | BulkIndexer::resetIndexedPayload(); |
||||
| 34 | |||||
| 35 | // only one job is created per index when there are dirty objects to index. This is dealt with within the |
||||
| 36 | // queued jobs module, in that the job parameters are identical. One job will already be present from |
||||
| 37 | // loading of the fixtures. As such, clear the queue |
||||
| 38 | DB::query('DELETE FROM "QueuedJobDescriptor"'); |
||||
| 39 | |||||
| 40 | $before = QueuedJobDescriptor::get()->count(); |
||||
| 41 | |||||
| 42 | $page = $this->objFromFixture(SiteTree::class, 'sitetree_10'); |
||||
| 43 | $page->Title = 'This is a new title'; |
||||
| 44 | $page->write(); |
||||
| 45 | |||||
| 46 | $after = QueuedJobDescriptor::get()->count(); |
||||
| 47 | |||||
| 48 | // assert that 1 job has been created, then inspect the contents |
||||
| 49 | $this->assertEquals(1, $after - $before); |
||||
| 50 | |||||
| 51 | // check the contents of the job queue |
||||
| 52 | $jobDescriptor = QueuedJobDescriptor::get()->first(); |
||||
| 53 | $this->assertEquals('Bulk Index Dirty DataObjects', $jobDescriptor->JobTitle); |
||||
| 54 | $this->assertEquals( |
||||
| 55 | 'O:8:"stdClass":1:{s:9:"indexName";s:8:"sitetree";}', |
||||
| 56 | $jobDescriptor->SavedJobData |
||||
| 57 | ); |
||||
| 58 | |||||
| 59 | // create the job class |
||||
| 60 | $impl = $jobDescriptor->Implementation; |
||||
| 61 | /** @var \Suilven\FreeTextSearch\Tests\Factory\BulkIndexDirtyJob $job */ |
||||
| 62 | $job = Injector::inst()->create($impl); |
||||
| 63 | |||||
| 64 | // populate data - taken from QueuedJobService |
||||
| 65 | $jobData = null; |
||||
|
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||||
| 66 | $messages = null; |
||||
|
0 ignored issues
–
show
|
|||||
| 67 | |||||
| 68 | // switching to php's serialize methods... not sure why this wasn't done from the start! |
||||
| 69 | $jobData = @\unserialize($jobDescriptor->SavedJobData); |
||||
|
0 ignored issues
–
show
It seems like
$jobDescriptor->SavedJobData can also be of type null; however, parameter $data of unserialize() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 70 | $messages = @\unserialize($jobDescriptor->SavedJobMessages); |
||||
| 71 | |||||
| 72 | // try decoding as json if null |
||||
| 73 | $jobData = $jobData |
||||
| 74 | ? $jobData |
||||
| 75 | : \json_decode($jobDescriptor->SavedJobData); |
||||
|
0 ignored issues
–
show
It seems like
$jobDescriptor->SavedJobData can also be of type null; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 76 | $messages = $messages |
||||
| 77 | ? $messages |
||||
| 78 | : \json_decode($jobDescriptor->SavedJobMessages); |
||||
| 79 | |||||
| 80 | $job->setJobData( |
||||
| 81 | $jobDescriptor->TotalSteps, |
||||
| 82 | $jobDescriptor->StepsProcessed, |
||||
| 83 | $jobDescriptor->JobStatus === QueuedJob::STATUS_COMPLETE, |
||||
| 84 | $jobData, |
||||
| 85 | $messages |
||||
| 86 | ); |
||||
| 87 | |||||
| 88 | $job->setup(); |
||||
| 89 | $job->process(); |
||||
| 90 | |||||
| 91 | $this->checkBulkIndexedPayload(); |
||||
| 92 | } |
||||
| 93 | |||||
| 94 | |||||
| 95 | public function testBulkIndexAll(): void |
||||
| 96 | { |
||||
| 97 | BulkIndexer::resetIndexedPayload(); |
||||
| 98 | $helper = new BulkIndexingHelper(); |
||||
| 99 | $helper->bulkIndex('sitetree', false, new CLImate()); |
||||
| 100 | |||||
| 101 | $this->assertEquals('sitetree', BulkIndexer::getIndexName()); |
||||
| 102 | |||||
| 103 | $this->checkBulkIndexedPayload(); |
||||
| 104 | } |
||||
| 105 | |||||
| 106 | |||||
| 107 | public function tearDown(): void |
||||
| 108 | { |
||||
| 109 | parent::tearDown(); |
||||
| 110 | |||||
| 111 | $config = SiteConfig::current_site_config(); |
||||
| 112 | $config->FreeTextSearchIndexingModeInBulk = 0; |
||||
| 113 | $config->write(); |
||||
| 114 | } |
||||
| 115 | |||||
| 116 | |||||
| 117 | private function checkBulkIndexedPayload(): void |
||||
| 118 | { |
||||
| 119 | foreach (BulkIndexer::getIndexedPayload() as $documentPayload) { |
||||
| 120 | $id = $documentPayload['ID']; |
||||
| 121 | $page = DataObject::get_by_id(SiteTree::class, $id); |
||||
| 122 | $this->assertEquals($page->Title, $documentPayload['sitetree']['Title']); |
||||
| 123 | $this->assertEquals($page->Content, $documentPayload['sitetree']['Content']); |
||||
| 124 | } |
||||
| 125 | } |
||||
| 126 | } |
||||
| 127 |