-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQueueSearchIndexBatchesTest.php
More file actions
143 lines (122 loc) · 5.14 KB
/
Copy pathQueueSearchIndexBatchesTest.php
File metadata and controls
143 lines (122 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace Tests\Jobs\CirrusSearch;
use App\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use App\Jobs\CirrusSearch\ElasticSearchIndexInit;
use App\Http\Curl\HttpRequest;
use App\WikiManager;
use App\WikiSetting;
use App\Wiki;
use Illuminate\Contracts\Queue\Job;
use App\WikiDb;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Support\Facades\DB;
use PHPUnit\TextUI\RuntimeException;
use App\Jobs\CirrusSearch\QueueSearchIndexBatches;
use App\Jobs\CirrusSearch\ForceSearchIndex;
use Illuminate\Support\Facades\Bus;
use Queue;
class QueueSearchIndexBatchesTest extends TestCase
{
use RefreshDatabase;
use DispatchesJobs;
private $wiki;
private $wikiDb;
private $user;
public function setUp(): void {
parent::setUp();
$this->user = User::factory()->create(['verified' => true]);
$this->wiki = Wiki::factory()->create();
WikiManager::factory()->create(['wiki_id' => $this->wiki->id, 'user_id' => $this->user->id]);
WikiSetting::factory()->create(
[
'wiki_id' => $this->wiki->id,
'name' => WikiSetting::wwExtEnableElasticSearch,
'value' => true
]
);
$this->wikiDb = WikiDb::factory()->create([
'wiki_id' => $this->wiki->id
]);
}
public function testSuccess()
{
Queue::fake();
$mockResponse = [
'warnings' => [],
'wbstackQueueSearchIndexBatches' => [
"return" => 0,
"output" => [
"php /var/www/html/w/extensions/CirrusSearch/maintenance/ForceSearchIndex.php --queue 1 --skipLinks 1 --indexOnSkip 1 --fromId 0 --toId 1000",
"php /var/www/html/w/extensions/CirrusSearch/maintenance/ForceSearchIndex.php --queue 1 --skipLinks 1 --indexOnSkip 1 --fromId 1001 --toId 1234",
"php /var/www/html/w/extensions/CirrusSearch/maintenance/ForceSearchIndex.php --queue 1 --skipLinks 1 --indexOnSkip 1 --fromId 1235 --toId 1236",
]
]
];
$request = $this->createMock(HttpRequest::class);
$request->method('execute')->willReturn(json_encode($mockResponse));
$request->expects($this->once())
->method('setOptions')
->with([
CURLOPT_URL => getenv('PLATFORM_MW_BACKEND_HOST').'/w/api.php?action=wbstackQueueSearchIndexBatches&format=json',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_TIMEOUT => 1000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => [
'content-type: application/x-www-form-urlencoded',
'host: '.$this->wiki->domain,
]
]);
$mockJob = $this->createMock(Job::class);
$mockJob->expects($this->never())
->method('fail')
->withAnyParameters();
$job = new QueueSearchIndexBatches($this->wiki->id);
$job->setJob($mockJob);
$job->handle($request);
Queue::assertPushed(function (ForceSearchIndex $job) {
return $job->wikiId() === $this->wiki->id
&& $job->fromId() === 0 && $job->toId() === 1000;
});
Queue::assertPushed(function (ForceSearchIndex $job) {
return $job->wikiId() === $this->wiki->id
&& $job->fromId() === 1001 && $job->toId() === 1234;
});
Queue::assertPushed(function (ForceSearchIndex $job) {
return $job->wikiId() === $this->wiki->id
&& $job->fromId() === 1235 && $job->toId() === 1236;
});
}
public function testMediawikiErrorResponse() {
$errorResponse = file_get_contents(
__DIR__.'/../../data/mediawiki-api-error-response.json'
);
$request = $this->createMock(HttpRequest::class);
$request->method('execute')->willReturn($errorResponse);
$request->expects($this->once())
->method('setOptions')
->with([
CURLOPT_URL => getenv('PLATFORM_MW_BACKEND_HOST').'/w/api.php?action=wbstackQueueSearchIndexBatches&format=json',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_TIMEOUT => 1000,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => [
'content-type: application/x-www-form-urlencoded',
'host: '.$this->wiki->domain,
]
]);
$mockJob = $this->createMock(Job::class);
$mockJob->expects($this->once())
->method('fail')
->with(new \RuntimeException('wbstackQueueSearchIndexBatches call failed with api error: Unrecognized value for parameter "action": wbstackQueueSearchIndexBatches.'));
$job = new QueueSearchIndexBatches($this->wiki->id);
$job->setJob($mockJob);
$job->handle($request);
}
}