-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathElasticSearchIndexDeleteTest.php
More file actions
164 lines (135 loc) · 5.44 KB
/
Copy pathElasticSearchIndexDeleteTest.php
File metadata and controls
164 lines (135 loc) · 5.44 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace Tests\Jobs\ElasticSearch;
use App\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use App\Http\Curl\HttpRequest;
use App\WikiManager;
use App\WikiSetting;
use App\Wiki;
use App\Jobs\ElasticSearchIndexDelete;
use App\WikiDb;
use Illuminate\Contracts\Queue\Job;
use Illuminate\Support\Facades\Config;
class ElasticSearchIndexDeleteTest extends TestCase
{
use RefreshDatabase;
private $wiki;
private $user;
private $wikiDb;
private $elasticSearchHost;
public function setUp(): void {
parent::setUp();
$this->elasticSearchHost = Config::get('wbstack.elasticsearch_host');
$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 testDeletesElasticSearchIndex()
{
$this->wiki->delete();
$wikiBaseName = $this->wikiDb->name;
$mockResponse = "index\n" . "{$wikiBaseName}_content_blabla\n" . "{$wikiBaseName}_general_bla\n";
$mockJsonSuccess = '{"acknowledged" : true}';
$request = $this->createMock(HttpRequest::class);
$request->expects($this->exactly(2))
->method('execute')
->willReturnOnConsecutiveCalls($mockResponse, $mockJsonSuccess);
$request->expects($this->exactly(2))
->method('setOptions')
->withConsecutive(
[[
CURLOPT_URL => $this->elasticSearchHost.'/_cat/indices/'.$this->wikiDb->name.'*?v&s=index&h=index',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_TIMEOUT => 10,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
]],
[[
CURLOPT_URL => $this->elasticSearchHost.'/'.$this->wikiDb->name.'*',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_TIMEOUT => 60,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'DELETE',
]]);
$mockJob = $this->createMock(Job::class);
$mockJob->expects($this->never())->method('fail');
$job = new ElasticSearchIndexDelete( $this->wiki->id );
$job->setJob( $mockJob );
$job->handle( $request );
// feature should get disabled
$this->assertSame(
1,
WikiSetting::where( ['wiki_id' => $this->wiki->id, 'name' => WikiSetting::wwExtEnableElasticSearch, 'value' => false])->count()
);
}
/**
* @dataProvider failureProvider
*/
public function testFailure( $request, string $expectedFailure, $mockResponse, $settingStateInDatabase, $deleteWiki = true )
{
if ( $deleteWiki ) {
$this->wiki->delete();
}
$expectedFailure = str_replace('<WIKI_ID>', $this->wiki->id, $expectedFailure);
$expectedFailure = str_replace('<WIKI_DB_NAME>', $this->wikiDb->name, $expectedFailure);
$mockJob = $this->createMock(Job::class);
$mockJob->expects($this->once())
->method('fail')
->with(new \RuntimeException($expectedFailure));
$request->method('execute')
->willReturnOnConsecutiveCalls( ...$mockResponse );
$job = new ElasticSearchIndexDelete($this->wiki->id);
$job->setJob($mockJob);
$job->handle( $request );
$this->assertSame(
1,
WikiSetting::where( ['wiki_id' => $this->wiki->id, 'name' => WikiSetting::wwExtEnableElasticSearch, 'value' => $settingStateInDatabase])->count()
);
}
public function failureProvider() {
$mockResponse = "index\n" . "some_index_content_blabla\n" . "some_index_general_bla\n";
$mockJsonSuccess = '{"acknowledged" : true}';
$mockFailure = '{"acknowledged" : false}';
yield [
$this->createMock(HttpRequest::class),
'ElasticSearchIndexDelete job for <WIKI_ID> was not successful: {"acknowledged" : false}',
[ $mockResponse, $mockFailure ],
true
];
yield [
$this->createMock(HttpRequest::class),
'ElasticSearchIndexDelete job for <WIKI_ID> was not successful: <html>',
[ $mockResponse, '<html>' ],
true
];
if ( $this->elasticSearchHost ) {
yield [
$this->createMock(HttpRequest::class),
'Response looks weird when querying http://'.$this->elasticSearchHost.'/_cat/indices/<WIKI_DB_NAME>*?v&s=index&h=index',
[ "<html>asdasd\n\nasdasd", $mockJsonSuccess ],
true
];
}
yield [
$this->createMock(HttpRequest::class),
'ElasticSearchIndexDelete job for <WIKI_ID> but that wiki is not marked as deleted.',
[$mockResponse, $mockJsonSuccess ],
true,
false // will not delete the wiki
];
}
}