|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Innoweb\SectionIO\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Innoweb\SectionIO\SectionIO; |
|
6
|
|
|
use Innoweb\SectionIO\Extensions\SectionIOFileExtension; |
|
7
|
|
|
use Innoweb\SectionIO\Extensions\SectionIOSiteTreeExtension; |
|
8
|
|
|
use SilverStripe\Assets\File; |
|
9
|
|
|
use SilverStripe\Assets\Image; |
|
10
|
|
|
use SilverStripe\Assets\Dev\TestAssetStore; |
|
11
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
|
12
|
|
|
use SilverStripe\Core\Config\Config; |
|
13
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
14
|
|
|
use SilverStripe\ORM\DataObject; |
|
15
|
|
|
|
|
16
|
|
|
class SectionIOTest extends SapphireTest |
|
17
|
|
|
{ |
|
18
|
|
|
protected static $fixture_file = 'SectionIOTest.yml'; |
|
19
|
|
|
|
|
20
|
|
|
public function setUpBeforeClass() |
|
21
|
|
|
{ |
|
22
|
|
|
parent::setUpBeforeClass(); |
|
23
|
|
|
|
|
24
|
|
|
// add config values |
|
25
|
|
|
Config::modify()->set(SectionIO::class, 'flush_on_dev_build', true); |
|
26
|
|
|
Config::modify()->set(SectionIO::class, 'api_url', 'https://example.com'); |
|
27
|
|
|
Config::modify()->set(SectionIO::class, 'account_id', '123456'); |
|
28
|
|
|
Config::modify()->set(SectionIO::class, 'application_id', '987654'); |
|
29
|
|
|
Config::modify()->set(SectionIO::class, 'environment_name', 'Production'); |
|
30
|
|
|
Config::modify()->set(SectionIO::class, 'proxy_name', 'myproxy'); |
|
31
|
|
|
Config::modify()->set(SectionIO::class, 'username', 'someuser'); |
|
32
|
|
|
Config::modify()->set(SectionIO::class, 'password', 'MySafePassword'); |
|
33
|
|
|
Config::modify()->set(SectionIO::class, 'verify_ssl', false); |
|
34
|
|
|
|
|
35
|
|
|
// remove extensions otherwise the fixtures will break the tests (by calling the live flush) |
|
36
|
|
|
File::remove_extension(SectionIOFileExtension::class); |
|
37
|
|
|
SiteTree::remove_extension(SectionIOSiteTreeExtension::class); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function setUp() |
|
41
|
|
|
{ |
|
42
|
|
|
parent::setUp(); |
|
43
|
|
|
|
|
44
|
|
|
// Set backend root to /ImageTest |
|
45
|
|
|
TestAssetStore::activate('FileTest'); |
|
46
|
|
|
|
|
47
|
|
|
// Create a test files for each of the fixture references |
|
48
|
|
|
$fileIDs = array_merge( |
|
49
|
|
|
$this->allFixtureIDs(File::class), |
|
50
|
|
|
$this->allFixtureIDs(Image::class) |
|
51
|
|
|
); |
|
52
|
|
|
foreach ($fileIDs as $fileID) { |
|
53
|
|
|
/** @var File $file */ |
|
54
|
|
|
$file = DataObject::get_by_id(File::class, $fileID); |
|
55
|
|
|
$file->setFromString(str_repeat('x', 1000000), $file->getFilename()); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function tearDownAfterClass() |
|
61
|
|
|
{ |
|
62
|
|
|
parent::tearDownAfterClass(); |
|
63
|
|
|
|
|
64
|
|
|
// re-add extensions |
|
65
|
|
|
File::add_extension('SectionIOFileExtension'); |
|
66
|
|
|
SiteTree::add_extension('SectionIOSiteTreeExtension'); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function tearDown() |
|
70
|
|
|
{ |
|
71
|
|
|
TestAssetStore::reset(); |
|
72
|
|
|
|
|
73
|
|
|
parent::tearDown(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testFlushAll() |
|
77
|
|
|
{ |
|
78
|
|
|
$result = SectionIOTest_MySectionIO::flushAll(); |
|
79
|
|
|
|
|
80
|
|
|
$this->assertCount( |
|
81
|
|
|
1, |
|
82
|
|
|
$result, |
|
83
|
|
|
'one url returned for one application id' |
|
84
|
|
|
); |
|
85
|
|
|
|
|
86
|
|
|
// url |
|
87
|
|
|
$this->assertEquals( |
|
88
|
|
|
'https://example.com/account/123456/application/987654/environment/Production/proxy/myproxy/state', |
|
89
|
|
|
$result[0]['url'], |
|
90
|
|
|
'URL is concatenated correctly' |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
|
|
// ban expression |
|
94
|
|
|
$this->assertEquals( |
|
95
|
|
|
'obj.http.x-url ~ /', |
|
96
|
|
|
$result[0]['banExpression'], |
|
97
|
|
|
'ban expression is correct' |
|
98
|
|
|
); |
|
99
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function testFlush() |
|
103
|
|
|
{ |
|
104
|
|
|
$result = SectionIOTest_MySectionIO::flush(); |
|
105
|
|
|
|
|
106
|
|
|
$this->assertCount( |
|
107
|
|
|
1, |
|
108
|
|
|
$result, |
|
109
|
|
|
'one url returned for one application id' |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
|
|
// url |
|
113
|
|
|
$this->assertEquals( |
|
114
|
|
|
'https://example.com/account/123456/application/987654/environment/Production/proxy/myproxy/state', |
|
115
|
|
|
$result[0]['url'], |
|
116
|
|
|
'URL is concatenated correctly' |
|
117
|
|
|
); |
|
118
|
|
|
|
|
119
|
|
|
// ban expression |
|
120
|
|
|
$this->assertEquals( |
|
121
|
|
|
'obj.http.x-url ~ /', |
|
122
|
|
|
$result[0]['banExpression'], |
|
123
|
|
|
'ban expression is correct' |
|
124
|
|
|
); |
|
125
|
|
|
|
|
126
|
|
|
// test deactivated flush on build |
|
127
|
|
|
Config::modify()->set(SectionIO::class, 'flush_on_dev_build', false); |
|
128
|
|
|
$result = SectionIOTest_MySectionIO::flush(); |
|
129
|
|
|
$this->assertNull( |
|
130
|
|
|
$result, |
|
131
|
|
|
'null returned if flush on build deactivated' |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function testMultipleApplicationIDs() |
|
136
|
|
|
{ |
|
137
|
|
|
// add second application to config |
|
138
|
|
|
Config::modify()->set(SectionIO::class, 'application_id', '2546987,856954'); |
|
139
|
|
|
|
|
140
|
|
|
$result = SectionIOTest_MySectionIO::flushAll(); |
|
141
|
|
|
|
|
142
|
|
|
$this->assertCount( |
|
143
|
|
|
2, |
|
144
|
|
|
$result, |
|
145
|
|
|
'two urls returned for two application id' |
|
146
|
|
|
); |
|
147
|
|
|
|
|
148
|
|
|
// url |
|
149
|
|
|
$this->assertEquals( |
|
150
|
|
|
'https://example.com/account/123456/application/2546987/environment/Production/proxy/myproxy/state', |
|
151
|
|
|
$result[0]['url'], |
|
152
|
|
|
'URL is concatenated correctly for app 1' |
|
153
|
|
|
); |
|
154
|
|
|
$this->assertEquals( |
|
155
|
|
|
'https://example.com/account/123456/application/856954/environment/Production/proxy/myproxy/state', |
|
156
|
|
|
$result[1]['url'], |
|
157
|
|
|
'URL is concatenated correctly for app 2' |
|
158
|
|
|
); |
|
159
|
|
|
|
|
160
|
|
|
// add second application to config with spaces in csv |
|
161
|
|
|
Config::modify()->set(SectionIO::class, 'application_id', '741852, 369258'); |
|
162
|
|
|
|
|
163
|
|
|
$result = SectionIOTest_MySectionIO::flushAll(); |
|
164
|
|
|
|
|
165
|
|
|
$this->assertCount( |
|
166
|
|
|
2, |
|
167
|
|
|
$result, |
|
168
|
|
|
'two urls returned for two application id' |
|
169
|
|
|
); |
|
170
|
|
|
|
|
171
|
|
|
// url |
|
172
|
|
|
$this->assertEquals( |
|
173
|
|
|
'https://example.com/account/123456/application/741852/environment/Production/proxy/myproxy/state', |
|
174
|
|
|
$result[0]['url'], |
|
175
|
|
|
'URL is concatenated correctly for app 1' |
|
176
|
|
|
); |
|
177
|
|
|
$this->assertEquals( |
|
178
|
|
|
'https://example.com/account/123456/application/369258/environment/Production/proxy/myproxy/state', |
|
179
|
|
|
$result[1]['url'], |
|
180
|
|
|
'URL is concatenated correctly for app 2' |
|
181
|
|
|
); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
public function testFlushImage() |
|
185
|
|
|
{ |
|
186
|
|
|
$imageId = $this->idFromFixture('Image', 'testImage'); |
|
187
|
|
|
|
|
188
|
|
|
$result = SectionIOTest_MySectionIO::flushImage($imageId); |
|
189
|
|
|
|
|
190
|
|
|
// ban expression |
|
191
|
|
|
$this->assertEquals( |
|
192
|
|
|
'obj.http.x-url ~ "^/assets/SectionTest/test_image\.png$"' |
|
193
|
|
|
.' || obj.http.x-url ~ "^/assets/SectionTest/test_image__[a-zA-Z0-9_]*\.png$"', |
|
194
|
|
|
$result[0]['banExpression'], |
|
195
|
|
|
'ban expression is correct' |
|
196
|
|
|
); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
public function testFlushFile() |
|
200
|
|
|
{ |
|
201
|
|
|
$fileId = $this->idFromFixture('File', 'testFile'); |
|
202
|
|
|
|
|
203
|
|
|
$result = SectionIOTest_MySectionIO::flushFile($fileId); |
|
204
|
|
|
|
|
205
|
|
|
// ban expression |
|
206
|
|
|
$this->assertEquals( |
|
207
|
|
|
'obj.http.x-url ~ "^/assets/SectionTest/test_document\.pdf$"', |
|
208
|
|
|
$result[0]['banExpression'], |
|
209
|
|
|
'ban expression is correct' |
|
210
|
|
|
); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
public function testFlushSiteTree() |
|
214
|
|
|
{ |
|
215
|
|
|
$pageId = $this->idFromFixture('Page', 'ceo'); |
|
216
|
|
|
|
|
217
|
|
|
// test single page flush |
|
218
|
|
|
Config::modify()->set(SectionIO::class, 'sitetree_flush_strategy', 'single'); |
|
219
|
|
|
$result = SectionIOTest_MySectionIO::flushSiteTree($pageId); |
|
220
|
|
|
$this->assertEquals( |
|
221
|
|
|
'obj.http.content-type ~ "text/html"' |
|
222
|
|
|
.' && obj.http.x-url ~ "^/about\-us/my\-staff/ceo/$"', |
|
223
|
|
|
$result[0]['banExpression'], |
|
224
|
|
|
'ban expression is correct' |
|
225
|
|
|
); |
|
226
|
|
|
|
|
227
|
|
|
// test parents flush |
|
228
|
|
|
Config::modify()->set(SectionIO::class, 'sitetree_flush_strategy', 'parents'); |
|
229
|
|
|
$result = SectionIOTest_MySectionIO::flushSiteTree($pageId); |
|
230
|
|
|
$this->assertEquals( |
|
231
|
|
|
'obj.http.content-type ~ "text/html"' |
|
232
|
|
|
.' && (obj.http.x-url ~ "^/about\-us/my\-staff/ceo/$" || obj.http.x-url ~ "^/about\-us/my\-staff/$" || obj.http.x-url ~ "^/about\-us/$")', |
|
233
|
|
|
$result[0]['banExpression'], |
|
234
|
|
|
'ban expression is correct' |
|
235
|
|
|
); |
|
236
|
|
|
|
|
237
|
|
|
// test all pages flush |
|
238
|
|
|
Config::modify()->set(SectionIO::class, 'sitetree_flush_strategy', 'all'); |
|
239
|
|
|
$result = SectionIOTest_MySectionIO::flushSiteTree($pageId); |
|
240
|
|
|
$this->assertEquals( |
|
241
|
|
|
'obj.http.content-type ~ "text/html"', |
|
242
|
|
|
$result[0]['banExpression'], |
|
243
|
|
|
'ban expression is correct' |
|
244
|
|
|
); |
|
245
|
|
|
|
|
246
|
|
|
// test whole site flush |
|
247
|
|
|
Config::modify()->set(SectionIO::class, 'sitetree_flush_strategy', 'everything'); |
|
248
|
|
|
$result = SectionIOTest_MySectionIO::flushSiteTree($pageId); |
|
249
|
|
|
$this->assertEquals( |
|
250
|
|
|
'obj.http.x-url ~ /', |
|
251
|
|
|
$result[0]['banExpression'], |
|
252
|
|
|
'ban expression is correct' |
|
253
|
|
|
); |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
class SectionIOTest_MySectionIO extends SectionIO |
|
258
|
|
|
{ |
|
259
|
|
|
protected static function performFlush($banExpression) |
|
260
|
|
|
{ |
|
261
|
|
|
$result = array(); |
|
262
|
|
|
$urls = static::getUrls(); |
|
263
|
|
|
if (count($urls) > 0) { |
|
264
|
|
|
foreach ($urls as $url) { |
|
265
|
|
|
|
|
266
|
|
|
$data = array(); |
|
267
|
|
|
$data['url'] = $url; |
|
268
|
|
|
$data['banExpression'] = $banExpression; |
|
269
|
|
|
$result[] = $data; |
|
270
|
|
|
|
|
271
|
|
|
} |
|
272
|
|
|
} |
|
273
|
|
|
return $result; |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
|