1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class SectionIOTest extends SapphireTest |
4
|
|
|
{ |
5
|
|
|
protected static $fixture_file = 'SectionIOTest.yml'; |
6
|
|
|
|
7
|
|
|
public function setUpOnce() |
8
|
|
|
{ |
9
|
|
|
parent::setUpOnce(); |
10
|
|
|
|
11
|
|
|
// add config values |
12
|
|
|
Config::inst()->update('SectionIO', 'flush_on_dev_build', true); |
13
|
|
|
Config::inst()->update('SectionIO', 'api_url', 'https://example.com'); |
14
|
|
|
Config::inst()->update('SectionIO', 'account_id', '123456'); |
15
|
|
|
Config::inst()->update('SectionIO', 'application_id', '987654'); |
16
|
|
|
Config::inst()->update('SectionIO', 'environment_name', 'Production'); |
17
|
|
|
Config::inst()->update('SectionIO', 'proxy_name', 'myproxy'); |
18
|
|
|
Config::inst()->update('SectionIO', 'username', 'someuser'); |
19
|
|
|
Config::inst()->update('SectionIO', 'password', 'MySafePassword'); |
20
|
|
|
Config::inst()->update('SectionIO', 'verify_ssl', false); |
21
|
|
|
|
22
|
|
|
// remove extensions otherwise the fixtures will break the tests (by calling the live flush) |
23
|
|
|
File::remove_extension('SectionIOFileExtension'); |
24
|
|
|
SiteTree::remove_extension('SectionIOSiteTreeExtension'); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function setUp() |
28
|
|
|
{ |
29
|
|
|
parent::setUp(); |
30
|
|
|
|
31
|
|
|
if (!file_exists(ASSETS_PATH)) { |
32
|
|
|
mkdir(ASSETS_PATH); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
// Create a test folders for each of the fixture references |
36
|
|
|
$folderIDs = $this->allFixtureIDs('Folder'); |
37
|
|
|
foreach ($folderIDs as $folderID) { |
|
|
|
|
38
|
|
|
$folder = DataObject::get_by_id('Folder', $folderID); |
39
|
|
|
if (!file_exists(BASE_PATH."/$folder->Filename")) { |
40
|
|
|
mkdir(BASE_PATH."/$folder->Filename"); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// Copy test images for each of the fixture references |
45
|
|
|
$imageIDs = $this->allFixtureIDs('Image'); |
46
|
|
View Code Duplication |
foreach ($imageIDs as $imageID) { |
|
|
|
|
47
|
|
|
$image = DataObject::get_by_id('Image', $imageID); |
48
|
|
|
$filePath = BASE_PATH."/$image->Filename"; |
49
|
|
|
$sourcePath = str_replace('assets/SectionTest/', 'section-io/tests/testfiles/', $filePath); |
50
|
|
|
if (!file_exists($filePath)) { |
51
|
|
|
if (!copy($sourcePath, $filePath)) { |
52
|
|
|
user_error('Failed to copy test images', E_USER_ERROR); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Copy test files for each of the fixture references |
58
|
|
|
$fileIDs = $this->allFixtureIDs('File'); |
59
|
|
View Code Duplication |
foreach ($fileIDs as $fileID) { |
|
|
|
|
60
|
|
|
$file = DataObject::get_by_id('File', $fileID); |
61
|
|
|
$filePath = BASE_PATH."/$file->Filename"; |
62
|
|
|
$sourcePath = str_replace('assets/SectionTest/', 'section-io/tests/testfiles/', $filePath); |
63
|
|
|
if (!file_exists($filePath)) { |
64
|
|
|
if (!copy($sourcePath, $filePath)) { |
65
|
|
|
user_error('Failed to copy test files', E_USER_ERROR); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function tearDownOnce() |
72
|
|
|
{ |
73
|
|
|
parent::tearDownOnce(); |
74
|
|
|
|
75
|
|
|
// re-add extensions |
76
|
|
|
File::add_extension('SectionIOFileExtension'); |
77
|
|
|
SiteTree::add_extension('SectionIOSiteTreeExtension'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function tearDown() |
81
|
|
|
{ |
82
|
|
|
// Remove the test images that we've created |
83
|
|
|
$imageIDs = $this->allFixtureIDs('Image'); |
84
|
|
|
foreach ($imageIDs as $imageID) { |
|
|
|
|
85
|
|
|
$image = DataObject::get_by_id('Image', $imageID); |
86
|
|
|
if ($image && file_exists(BASE_PATH."/$image->Filename")) { |
87
|
|
|
unlink(BASE_PATH."/$image->Filename"); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// Remove the test files that we've created |
92
|
|
|
$fileIDs = $this->allFixtureIDs('File'); |
93
|
|
|
foreach ($fileIDs as $fileID) { |
|
|
|
|
94
|
|
|
$file = DataObject::get_by_id('File', $fileID); |
95
|
|
|
if ($file && file_exists(BASE_PATH."/$file->Filename")) { |
96
|
|
|
unlink(BASE_PATH."/$file->Filename"); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// Remove the test folders that we've created |
101
|
|
|
$folderIDs = $this->allFixtureIDs('Folder'); |
102
|
|
|
foreach ($folderIDs as $folderID) { |
103
|
|
|
$folder = DataObject::get_by_id('Folder', $folderID); |
104
|
|
|
if ($folder && file_exists(BASE_PATH.'/'.$folder->Filename.'_resampled')) { |
105
|
|
|
Filesystem::removeFolder(BASE_PATH.'/'.$folder->Filename.'_resampled'); |
106
|
|
|
} |
107
|
|
|
if ($folder && file_exists(BASE_PATH."/$folder->Filename")) { |
108
|
|
|
Filesystem::removeFolder(BASE_PATH."/$folder->Filename"); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
parent::tearDown(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testFlushAll() |
116
|
|
|
{ |
117
|
|
|
$result = SectionIOTest_MySectionIO::flushAll(); |
118
|
|
|
|
119
|
|
|
$this->assertCount( |
|
|
|
|
120
|
|
|
1, |
121
|
|
|
$result, |
122
|
|
|
'one url returned for one application id' |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
// url |
126
|
|
|
$this->assertEquals( |
|
|
|
|
127
|
|
|
'https://example.com/account/123456/application/987654/environment/Production/proxy/myproxy/state', |
128
|
|
|
$result[0]['url'], |
129
|
|
|
'URL is concatenated correctly' |
130
|
|
|
); |
131
|
|
|
|
132
|
|
|
// ban expression |
133
|
|
|
$this->assertEquals( |
|
|
|
|
134
|
|
|
'obj.http.x-url ~ /', |
135
|
|
|
$result[0]['banExpression'], |
136
|
|
|
'ban expression is correct' |
137
|
|
|
); |
138
|
|
|
|
139
|
|
|
// headers |
140
|
|
|
$this->assertContains( |
141
|
|
|
'Content-Type: application/json', |
142
|
|
|
$result[0]['headers'], |
143
|
|
|
'content type header is correct' |
144
|
|
|
); |
145
|
|
|
$this->assertContains( |
146
|
|
|
'Accept: application/json', |
147
|
|
|
$result[0]['headers'], |
148
|
|
|
'accept header is correct' |
149
|
|
|
); |
150
|
|
|
|
151
|
|
|
// service |
152
|
|
|
$this->assertInstanceOf( |
|
|
|
|
153
|
|
|
'RestfulService', |
154
|
|
|
$result[0]['service'], |
155
|
|
|
'service is of type RestfulService' |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
public function testFlush() |
160
|
|
|
{ |
161
|
|
|
$result = SectionIOTest_MySectionIO::flush(); |
162
|
|
|
|
163
|
|
|
$this->assertCount( |
|
|
|
|
164
|
|
|
1, |
165
|
|
|
$result, |
166
|
|
|
'one url returned for one application id' |
167
|
|
|
); |
168
|
|
|
|
169
|
|
|
// url |
170
|
|
|
$this->assertEquals( |
|
|
|
|
171
|
|
|
'https://example.com/account/123456/application/987654/environment/Production/proxy/myproxy/state', |
172
|
|
|
$result[0]['url'], |
173
|
|
|
'URL is concatenated correctly' |
174
|
|
|
); |
175
|
|
|
|
176
|
|
|
// ban expression |
177
|
|
|
$this->assertEquals( |
|
|
|
|
178
|
|
|
'obj.http.x-url ~ /', |
179
|
|
|
$result[0]['banExpression'], |
180
|
|
|
'ban expression is correct' |
181
|
|
|
); |
182
|
|
|
|
183
|
|
|
// test deactivated flush on build |
184
|
|
|
Config::inst()->update('SectionIO', 'flush_on_dev_build', false); |
185
|
|
|
$result = SectionIOTest_MySectionIO::flush(); |
186
|
|
|
$this->assertNull( |
|
|
|
|
187
|
|
|
$result, |
188
|
|
|
'null returned if flush on build deactivated' |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function testMultipleApplicationIDs() |
193
|
|
|
{ |
194
|
|
|
// add second application to config |
195
|
|
|
Config::inst()->update('SectionIO', 'application_id', '2546987,856954'); |
196
|
|
|
|
197
|
|
|
$result = SectionIOTest_MySectionIO::flushAll(); |
198
|
|
|
|
199
|
|
|
$this->assertCount( |
|
|
|
|
200
|
|
|
2, |
201
|
|
|
$result, |
202
|
|
|
'two urls returned for two application id' |
203
|
|
|
); |
204
|
|
|
|
205
|
|
|
// url |
206
|
|
|
$this->assertEquals( |
|
|
|
|
207
|
|
|
'https://example.com/account/123456/application/2546987/environment/Production/proxy/myproxy/state', |
208
|
|
|
$result[0]['url'], |
209
|
|
|
'URL is concatenated correctly for app 1' |
210
|
|
|
); |
211
|
|
|
$this->assertEquals( |
|
|
|
|
212
|
|
|
'https://example.com/account/123456/application/856954/environment/Production/proxy/myproxy/state', |
213
|
|
|
$result[1]['url'], |
214
|
|
|
'URL is concatenated correctly for app 2' |
215
|
|
|
); |
216
|
|
|
|
217
|
|
|
// add second application to config with spaces in csv |
218
|
|
|
Config::inst()->update('SectionIO', 'application_id', '741852, 369258'); |
219
|
|
|
|
220
|
|
|
$result = SectionIOTest_MySectionIO::flushAll(); |
221
|
|
|
|
222
|
|
|
$this->assertCount( |
|
|
|
|
223
|
|
|
2, |
224
|
|
|
$result, |
225
|
|
|
'two urls returned for two application id' |
226
|
|
|
); |
227
|
|
|
|
228
|
|
|
// url |
229
|
|
|
$this->assertEquals( |
|
|
|
|
230
|
|
|
'https://example.com/account/123456/application/741852/environment/Production/proxy/myproxy/state', |
231
|
|
|
$result[0]['url'], |
232
|
|
|
'URL is concatenated correctly for app 1' |
233
|
|
|
); |
234
|
|
|
$this->assertEquals( |
|
|
|
|
235
|
|
|
'https://example.com/account/123456/application/369258/environment/Production/proxy/myproxy/state', |
236
|
|
|
$result[1]['url'], |
237
|
|
|
'URL is concatenated correctly for app 2' |
238
|
|
|
); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
public function testFlushImage() |
242
|
|
|
{ |
243
|
|
|
$imageId = $this->idFromFixture('Image', 'testImage'); |
244
|
|
|
|
245
|
|
|
$result = SectionIOTest_MySectionIO::flushImage($imageId); |
246
|
|
|
|
247
|
|
|
// ban expression |
248
|
|
|
$this->assertEquals( |
|
|
|
|
249
|
|
|
'obj.http.x-url ~ "^/assets/SectionTest/test_image\.png$"' |
250
|
|
|
.' || obj.http.x-url ~ "^/assets/SectionTest/_resampled/(.*)\-test_image\.png$"', |
251
|
|
|
$result[0]['banExpression'], |
252
|
|
|
'ban expression is correct' |
253
|
|
|
); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
public function testFlushFile() |
257
|
|
|
{ |
258
|
|
|
$fileId = $this->idFromFixture('File', 'testFile'); |
259
|
|
|
|
260
|
|
|
$result = SectionIOTest_MySectionIO::flushFile($fileId); |
261
|
|
|
|
262
|
|
|
// ban expression |
263
|
|
|
$this->assertEquals( |
|
|
|
|
264
|
|
|
'obj.http.x-url ~ "^/assets/SectionTest/test_document\.pdf$"', |
265
|
|
|
$result[0]['banExpression'], |
266
|
|
|
'ban expression is correct' |
267
|
|
|
); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
public function testFlushSiteTree() |
271
|
|
|
{ |
272
|
|
|
$pageId = $this->idFromFixture('Page', 'ceo'); |
273
|
|
|
|
274
|
|
|
// test single page flush |
275
|
|
|
Config::inst()->update('SectionIO', 'sitetree_flush_strategy', 'single'); |
276
|
|
|
$result = SectionIOTest_MySectionIO::flushSiteTree($pageId); |
277
|
|
|
$this->assertEquals( |
|
|
|
|
278
|
|
|
'obj.http.content-type ~ "text/html"' |
279
|
|
|
.' && obj.http.x-url ~ "^/about\-us/my\-staff/ceo/$"', |
280
|
|
|
$result[0]['banExpression'], |
281
|
|
|
'ban expression is correct' |
282
|
|
|
); |
283
|
|
|
|
284
|
|
|
// test parents flush |
285
|
|
|
Config::inst()->update('SectionIO', 'sitetree_flush_strategy', 'parents'); |
286
|
|
|
$result = SectionIOTest_MySectionIO::flushSiteTree($pageId); |
287
|
|
|
$this->assertEquals( |
|
|
|
|
288
|
|
|
'obj.http.content-type ~ "text/html"' |
289
|
|
|
.' && (obj.http.x-url ~ "^/about\-us/my\-staff/ceo/$" || obj.http.x-url ~ "^/about\-us/my\-staff/$" || obj.http.x-url ~ "^/about\-us/$")', |
290
|
|
|
$result[0]['banExpression'], |
291
|
|
|
'ban expression is correct' |
292
|
|
|
); |
293
|
|
|
|
294
|
|
|
// test all pages flush |
295
|
|
|
Config::inst()->update('SectionIO', 'sitetree_flush_strategy', 'all'); |
296
|
|
|
$result = SectionIOTest_MySectionIO::flushSiteTree($pageId); |
297
|
|
|
$this->assertEquals( |
|
|
|
|
298
|
|
|
'obj.http.content-type ~ "text/html"', |
299
|
|
|
$result[0]['banExpression'], |
300
|
|
|
'ban expression is correct' |
301
|
|
|
); |
302
|
|
|
|
303
|
|
|
// test whole site flush |
304
|
|
|
Config::inst()->update('SectionIO', 'sitetree_flush_strategy', 'everything'); |
305
|
|
|
$result = SectionIOTest_MySectionIO::flushSiteTree($pageId); |
306
|
|
|
$this->assertEquals( |
|
|
|
|
307
|
|
|
'obj.http.x-url ~ /', |
308
|
|
|
$result[0]['banExpression'], |
309
|
|
|
'ban expression is correct' |
310
|
|
|
); |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
class SectionIOTest_MySectionIO extends SectionIO |
315
|
|
|
{ |
316
|
|
|
protected static function performFlush($banExpression) |
317
|
|
|
{ |
318
|
|
|
$result = array(); |
319
|
|
|
$urls = static::getUrls(); |
320
|
|
|
// config loaded successfully |
321
|
|
|
if ($urls) { |
|
|
|
|
322
|
|
|
foreach ($urls as $url) { |
323
|
|
|
|
324
|
|
|
// get restful service object |
325
|
|
|
$service = static::getService($url, $banExpression); |
326
|
|
|
|
327
|
|
|
// prepare headers |
328
|
|
|
$headers = static::getHeaders(); |
329
|
|
|
|
330
|
|
|
// prepare curl options |
331
|
|
|
$options = static::getOptions(); |
332
|
|
|
|
333
|
|
|
// store data for return |
334
|
|
|
$data = array(); |
335
|
|
|
$data['url'] = $url; |
336
|
|
|
$data['banExpression'] = $banExpression; |
337
|
|
|
$data['headers'] = $headers; |
338
|
|
|
$data['options'] = $options; |
339
|
|
|
$data['service'] = $service; |
340
|
|
|
$result[] = $data; |
341
|
|
|
} |
342
|
|
|
} else { |
343
|
|
|
user_error('SectionIOTest_MySectionIO::performFlush :: no URLs loaded for ban.', E_USER_WARNING); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
return $result; |
|
|
|
|
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.