@@ -2,378 +2,378 @@ |
||
2 | 2 | |
3 | 3 | class SectionIOTest extends SapphireTest |
4 | 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 | - 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 | - 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 | - // options |
|
152 | - $this->assertArrayHasKey( |
|
153 | - CURLOPT_SSL_VERIFYPEER, |
|
154 | - $result[0]['options'], |
|
155 | - 'ssl verify is set' |
|
156 | - ); |
|
157 | - $this->assertEquals( |
|
158 | - 1, |
|
159 | - $result[0]['options'][CURLOPT_SSL_VERIFYPEER], |
|
160 | - 'ssl verfify is activated' |
|
161 | - ); |
|
162 | - $this->assertArrayHasKey( |
|
163 | - CURLOPT_SSL_VERIFYHOST, |
|
164 | - $result[0]['options'], |
|
165 | - 'ssl verfi host os set' |
|
166 | - ); |
|
167 | - $this->assertEquals( |
|
168 | - 2, |
|
169 | - $result[0]['options'][CURLOPT_SSL_VERIFYHOST], |
|
170 | - 'ssl verfify host is set to 2' |
|
171 | - ); |
|
172 | - $this->assertArrayHasKey( |
|
173 | - CURLOPT_CAINFO, |
|
174 | - $result[0]['options'], |
|
175 | - 'ca info is set' |
|
176 | - ); |
|
177 | - $this->assertNotEmpty( |
|
178 | - $result[0]['options'][CURLOPT_CAINFO], |
|
179 | - 'ca info is not empty' |
|
180 | - ); |
|
181 | - |
|
182 | - // service |
|
183 | - $this->assertInstanceOf( |
|
184 | - 'RestfulService', |
|
185 | - $result[0]['service'], |
|
186 | - 'service is of type RestfulService' |
|
187 | - ); |
|
188 | - } |
|
189 | - |
|
190 | - public function testFlush() |
|
191 | - { |
|
192 | - $result = SectionIOTest_MySectionIO::flush(); |
|
193 | - |
|
194 | - $this->assertCount( |
|
195 | - 1, |
|
196 | - $result, |
|
197 | - 'one url returned for one application id' |
|
198 | - ); |
|
199 | - |
|
200 | - // url |
|
201 | - $this->assertEquals( |
|
202 | - 'https://example.com/account/123456/application/987654/environment/Production/proxy/myproxy/state', |
|
203 | - $result[0]['url'], |
|
204 | - 'URL is concatenated correctly' |
|
205 | - ); |
|
206 | - |
|
207 | - // ban expression |
|
208 | - $this->assertEquals( |
|
209 | - 'obj.http.x-url ~ /', |
|
210 | - $result[0]['banExpression'], |
|
211 | - 'ban expression is correct' |
|
212 | - ); |
|
213 | - |
|
214 | - // test deactivated flush on build |
|
215 | - Config::inst()->update('SectionIO', 'flush_on_dev_build', false); |
|
216 | - $result = SectionIOTest_MySectionIO::flush(); |
|
217 | - $this->assertNull( |
|
218 | - $result, |
|
219 | - 'null returned if flush on build deactivated' |
|
220 | - ); |
|
221 | - } |
|
222 | - |
|
223 | - public function testMultipleApplicationIDs() |
|
224 | - { |
|
225 | - // add second application to config |
|
226 | - Config::inst()->update('SectionIO', 'application_id', '2546987,856954'); |
|
227 | - |
|
228 | - $result = SectionIOTest_MySectionIO::flushAll(); |
|
229 | - |
|
230 | - $this->assertCount( |
|
231 | - 2, |
|
232 | - $result, |
|
233 | - 'two urls returned for two application id' |
|
234 | - ); |
|
235 | - |
|
236 | - // url |
|
237 | - $this->assertEquals( |
|
238 | - 'https://example.com/account/123456/application/2546987/environment/Production/proxy/myproxy/state', |
|
239 | - $result[0]['url'], |
|
240 | - 'URL is concatenated correctly for app 1' |
|
241 | - ); |
|
242 | - $this->assertEquals( |
|
243 | - 'https://example.com/account/123456/application/856954/environment/Production/proxy/myproxy/state', |
|
244 | - $result[1]['url'], |
|
245 | - 'URL is concatenated correctly for app 2' |
|
246 | - ); |
|
247 | - |
|
248 | - // add second application to config with spaces in csv |
|
249 | - Config::inst()->update('SectionIO', 'application_id', '741852, 369258'); |
|
250 | - |
|
251 | - $result = SectionIOTest_MySectionIO::flushAll(); |
|
252 | - |
|
253 | - $this->assertCount( |
|
254 | - 2, |
|
255 | - $result, |
|
256 | - 'two urls returned for two application id' |
|
257 | - ); |
|
258 | - |
|
259 | - // url |
|
260 | - $this->assertEquals( |
|
261 | - 'https://example.com/account/123456/application/741852/environment/Production/proxy/myproxy/state', |
|
262 | - $result[0]['url'], |
|
263 | - 'URL is concatenated correctly for app 1' |
|
264 | - ); |
|
265 | - $this->assertEquals( |
|
266 | - 'https://example.com/account/123456/application/369258/environment/Production/proxy/myproxy/state', |
|
267 | - $result[1]['url'], |
|
268 | - 'URL is concatenated correctly for app 2' |
|
269 | - ); |
|
270 | - } |
|
271 | - |
|
272 | - public function testFlushImage() |
|
273 | - { |
|
274 | - $imageId = $this->idFromFixture('Image', 'testImage'); |
|
275 | - |
|
276 | - $result = SectionIOTest_MySectionIO::flushImage($imageId); |
|
277 | - |
|
278 | - // ban expression |
|
279 | - $this->assertEquals( |
|
280 | - 'obj.http.x-url ~ "^/assets/SectionTest/test_image\.png$"' |
|
281 | - .' || obj.http.x-url ~ "^/assets/SectionTest/_resampled/(.*)\-test_image\.png$"', |
|
282 | - $result[0]['banExpression'], |
|
283 | - 'ban expression is correct' |
|
284 | - ); |
|
285 | - } |
|
286 | - |
|
287 | - public function testFlushFile() |
|
288 | - { |
|
289 | - $fileId = $this->idFromFixture('File', 'testFile'); |
|
290 | - |
|
291 | - $result = SectionIOTest_MySectionIO::flushFile($fileId); |
|
292 | - |
|
293 | - // ban expression |
|
294 | - $this->assertEquals( |
|
295 | - 'obj.http.x-url ~ "^/assets/SectionTest/test_document\.pdf$"', |
|
296 | - $result[0]['banExpression'], |
|
297 | - 'ban expression is correct' |
|
298 | - ); |
|
299 | - } |
|
300 | - |
|
301 | - public function testFlushSiteTree() |
|
302 | - { |
|
303 | - $pageId = $this->idFromFixture('Page', 'ceo'); |
|
304 | - |
|
305 | - // test single page flush |
|
306 | - Config::inst()->update('SectionIO', 'sitetree_flush_strategy', 'single'); |
|
307 | - $result = SectionIOTest_MySectionIO::flushSiteTree($pageId); |
|
308 | - $this->assertEquals( |
|
309 | - 'obj.http.content-type ~ "text/html"' |
|
310 | - .' && obj.http.x-url ~ "^/about\-us/my\-staff/ceo/$"', |
|
311 | - $result[0]['banExpression'], |
|
312 | - 'ban expression is correct' |
|
313 | - ); |
|
314 | - |
|
315 | - // test parents flush |
|
316 | - Config::inst()->update('SectionIO', 'sitetree_flush_strategy', 'parents'); |
|
317 | - $result = SectionIOTest_MySectionIO::flushSiteTree($pageId); |
|
318 | - $this->assertEquals( |
|
319 | - 'obj.http.content-type ~ "text/html"' |
|
320 | - .' && (obj.http.x-url ~ "^/about\-us/my\-staff/ceo/$" || obj.http.x-url ~ "^/about\-us/my\-staff/$" || obj.http.x-url ~ "^/about\-us/$")', |
|
321 | - $result[0]['banExpression'], |
|
322 | - 'ban expression is correct' |
|
323 | - ); |
|
324 | - |
|
325 | - // test all pages flush |
|
326 | - Config::inst()->update('SectionIO', 'sitetree_flush_strategy', 'all'); |
|
327 | - $result = SectionIOTest_MySectionIO::flushSiteTree($pageId); |
|
328 | - $this->assertEquals( |
|
329 | - 'obj.http.content-type ~ "text/html"', |
|
330 | - $result[0]['banExpression'], |
|
331 | - 'ban expression is correct' |
|
332 | - ); |
|
333 | - |
|
334 | - // test whole site flush |
|
335 | - Config::inst()->update('SectionIO', 'sitetree_flush_strategy', 'everything'); |
|
336 | - $result = SectionIOTest_MySectionIO::flushSiteTree($pageId); |
|
337 | - $this->assertEquals( |
|
338 | - 'obj.http.x-url ~ /', |
|
339 | - $result[0]['banExpression'], |
|
340 | - 'ban expression is correct' |
|
341 | - ); |
|
342 | - } |
|
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 | + 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 | + 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 | + // options |
|
152 | + $this->assertArrayHasKey( |
|
153 | + CURLOPT_SSL_VERIFYPEER, |
|
154 | + $result[0]['options'], |
|
155 | + 'ssl verify is set' |
|
156 | + ); |
|
157 | + $this->assertEquals( |
|
158 | + 1, |
|
159 | + $result[0]['options'][CURLOPT_SSL_VERIFYPEER], |
|
160 | + 'ssl verfify is activated' |
|
161 | + ); |
|
162 | + $this->assertArrayHasKey( |
|
163 | + CURLOPT_SSL_VERIFYHOST, |
|
164 | + $result[0]['options'], |
|
165 | + 'ssl verfi host os set' |
|
166 | + ); |
|
167 | + $this->assertEquals( |
|
168 | + 2, |
|
169 | + $result[0]['options'][CURLOPT_SSL_VERIFYHOST], |
|
170 | + 'ssl verfify host is set to 2' |
|
171 | + ); |
|
172 | + $this->assertArrayHasKey( |
|
173 | + CURLOPT_CAINFO, |
|
174 | + $result[0]['options'], |
|
175 | + 'ca info is set' |
|
176 | + ); |
|
177 | + $this->assertNotEmpty( |
|
178 | + $result[0]['options'][CURLOPT_CAINFO], |
|
179 | + 'ca info is not empty' |
|
180 | + ); |
|
181 | + |
|
182 | + // service |
|
183 | + $this->assertInstanceOf( |
|
184 | + 'RestfulService', |
|
185 | + $result[0]['service'], |
|
186 | + 'service is of type RestfulService' |
|
187 | + ); |
|
188 | + } |
|
189 | + |
|
190 | + public function testFlush() |
|
191 | + { |
|
192 | + $result = SectionIOTest_MySectionIO::flush(); |
|
193 | + |
|
194 | + $this->assertCount( |
|
195 | + 1, |
|
196 | + $result, |
|
197 | + 'one url returned for one application id' |
|
198 | + ); |
|
199 | + |
|
200 | + // url |
|
201 | + $this->assertEquals( |
|
202 | + 'https://example.com/account/123456/application/987654/environment/Production/proxy/myproxy/state', |
|
203 | + $result[0]['url'], |
|
204 | + 'URL is concatenated correctly' |
|
205 | + ); |
|
206 | + |
|
207 | + // ban expression |
|
208 | + $this->assertEquals( |
|
209 | + 'obj.http.x-url ~ /', |
|
210 | + $result[0]['banExpression'], |
|
211 | + 'ban expression is correct' |
|
212 | + ); |
|
213 | + |
|
214 | + // test deactivated flush on build |
|
215 | + Config::inst()->update('SectionIO', 'flush_on_dev_build', false); |
|
216 | + $result = SectionIOTest_MySectionIO::flush(); |
|
217 | + $this->assertNull( |
|
218 | + $result, |
|
219 | + 'null returned if flush on build deactivated' |
|
220 | + ); |
|
221 | + } |
|
222 | + |
|
223 | + public function testMultipleApplicationIDs() |
|
224 | + { |
|
225 | + // add second application to config |
|
226 | + Config::inst()->update('SectionIO', 'application_id', '2546987,856954'); |
|
227 | + |
|
228 | + $result = SectionIOTest_MySectionIO::flushAll(); |
|
229 | + |
|
230 | + $this->assertCount( |
|
231 | + 2, |
|
232 | + $result, |
|
233 | + 'two urls returned for two application id' |
|
234 | + ); |
|
235 | + |
|
236 | + // url |
|
237 | + $this->assertEquals( |
|
238 | + 'https://example.com/account/123456/application/2546987/environment/Production/proxy/myproxy/state', |
|
239 | + $result[0]['url'], |
|
240 | + 'URL is concatenated correctly for app 1' |
|
241 | + ); |
|
242 | + $this->assertEquals( |
|
243 | + 'https://example.com/account/123456/application/856954/environment/Production/proxy/myproxy/state', |
|
244 | + $result[1]['url'], |
|
245 | + 'URL is concatenated correctly for app 2' |
|
246 | + ); |
|
247 | + |
|
248 | + // add second application to config with spaces in csv |
|
249 | + Config::inst()->update('SectionIO', 'application_id', '741852, 369258'); |
|
250 | + |
|
251 | + $result = SectionIOTest_MySectionIO::flushAll(); |
|
252 | + |
|
253 | + $this->assertCount( |
|
254 | + 2, |
|
255 | + $result, |
|
256 | + 'two urls returned for two application id' |
|
257 | + ); |
|
258 | + |
|
259 | + // url |
|
260 | + $this->assertEquals( |
|
261 | + 'https://example.com/account/123456/application/741852/environment/Production/proxy/myproxy/state', |
|
262 | + $result[0]['url'], |
|
263 | + 'URL is concatenated correctly for app 1' |
|
264 | + ); |
|
265 | + $this->assertEquals( |
|
266 | + 'https://example.com/account/123456/application/369258/environment/Production/proxy/myproxy/state', |
|
267 | + $result[1]['url'], |
|
268 | + 'URL is concatenated correctly for app 2' |
|
269 | + ); |
|
270 | + } |
|
271 | + |
|
272 | + public function testFlushImage() |
|
273 | + { |
|
274 | + $imageId = $this->idFromFixture('Image', 'testImage'); |
|
275 | + |
|
276 | + $result = SectionIOTest_MySectionIO::flushImage($imageId); |
|
277 | + |
|
278 | + // ban expression |
|
279 | + $this->assertEquals( |
|
280 | + 'obj.http.x-url ~ "^/assets/SectionTest/test_image\.png$"' |
|
281 | + .' || obj.http.x-url ~ "^/assets/SectionTest/_resampled/(.*)\-test_image\.png$"', |
|
282 | + $result[0]['banExpression'], |
|
283 | + 'ban expression is correct' |
|
284 | + ); |
|
285 | + } |
|
286 | + |
|
287 | + public function testFlushFile() |
|
288 | + { |
|
289 | + $fileId = $this->idFromFixture('File', 'testFile'); |
|
290 | + |
|
291 | + $result = SectionIOTest_MySectionIO::flushFile($fileId); |
|
292 | + |
|
293 | + // ban expression |
|
294 | + $this->assertEquals( |
|
295 | + 'obj.http.x-url ~ "^/assets/SectionTest/test_document\.pdf$"', |
|
296 | + $result[0]['banExpression'], |
|
297 | + 'ban expression is correct' |
|
298 | + ); |
|
299 | + } |
|
300 | + |
|
301 | + public function testFlushSiteTree() |
|
302 | + { |
|
303 | + $pageId = $this->idFromFixture('Page', 'ceo'); |
|
304 | + |
|
305 | + // test single page flush |
|
306 | + Config::inst()->update('SectionIO', 'sitetree_flush_strategy', 'single'); |
|
307 | + $result = SectionIOTest_MySectionIO::flushSiteTree($pageId); |
|
308 | + $this->assertEquals( |
|
309 | + 'obj.http.content-type ~ "text/html"' |
|
310 | + .' && obj.http.x-url ~ "^/about\-us/my\-staff/ceo/$"', |
|
311 | + $result[0]['banExpression'], |
|
312 | + 'ban expression is correct' |
|
313 | + ); |
|
314 | + |
|
315 | + // test parents flush |
|
316 | + Config::inst()->update('SectionIO', 'sitetree_flush_strategy', 'parents'); |
|
317 | + $result = SectionIOTest_MySectionIO::flushSiteTree($pageId); |
|
318 | + $this->assertEquals( |
|
319 | + 'obj.http.content-type ~ "text/html"' |
|
320 | + .' && (obj.http.x-url ~ "^/about\-us/my\-staff/ceo/$" || obj.http.x-url ~ "^/about\-us/my\-staff/$" || obj.http.x-url ~ "^/about\-us/$")', |
|
321 | + $result[0]['banExpression'], |
|
322 | + 'ban expression is correct' |
|
323 | + ); |
|
324 | + |
|
325 | + // test all pages flush |
|
326 | + Config::inst()->update('SectionIO', 'sitetree_flush_strategy', 'all'); |
|
327 | + $result = SectionIOTest_MySectionIO::flushSiteTree($pageId); |
|
328 | + $this->assertEquals( |
|
329 | + 'obj.http.content-type ~ "text/html"', |
|
330 | + $result[0]['banExpression'], |
|
331 | + 'ban expression is correct' |
|
332 | + ); |
|
333 | + |
|
334 | + // test whole site flush |
|
335 | + Config::inst()->update('SectionIO', 'sitetree_flush_strategy', 'everything'); |
|
336 | + $result = SectionIOTest_MySectionIO::flushSiteTree($pageId); |
|
337 | + $this->assertEquals( |
|
338 | + 'obj.http.x-url ~ /', |
|
339 | + $result[0]['banExpression'], |
|
340 | + 'ban expression is correct' |
|
341 | + ); |
|
342 | + } |
|
343 | 343 | } |
344 | 344 | |
345 | 345 | class SectionIOTest_MySectionIO extends SectionIO |
346 | 346 | { |
347 | - protected static function performFlush($banExpression) |
|
348 | - { |
|
349 | - $result = array(); |
|
350 | - $urls = static::getUrls(); |
|
351 | - // config loaded successfully |
|
352 | - if ($urls) { |
|
353 | - foreach ($urls as $url) { |
|
354 | - |
|
355 | - // get restful service object |
|
356 | - $service = static::getService($url, $banExpression); |
|
357 | - |
|
358 | - // prepare headers |
|
359 | - $headers = static::getHeaders(); |
|
360 | - |
|
361 | - // prepare curl options |
|
362 | - $options = static::getOptions(); |
|
363 | - |
|
364 | - // store data for return |
|
365 | - $data = array(); |
|
366 | - $data['url'] = $url; |
|
367 | - $data['banExpression'] = $banExpression; |
|
368 | - $data['headers'] = $headers; |
|
369 | - $data['options'] = $options; |
|
370 | - $data['service'] = $service; |
|
371 | - $result[] = $data; |
|
372 | - } |
|
373 | - } else { |
|
374 | - user_error('SectionIOTest_MySectionIO::performFlush :: no URLs loaded for ban.', E_USER_WARNING); |
|
375 | - } |
|
376 | - |
|
377 | - return $result; |
|
378 | - } |
|
347 | + protected static function performFlush($banExpression) |
|
348 | + { |
|
349 | + $result = array(); |
|
350 | + $urls = static::getUrls(); |
|
351 | + // config loaded successfully |
|
352 | + if ($urls) { |
|
353 | + foreach ($urls as $url) { |
|
354 | + |
|
355 | + // get restful service object |
|
356 | + $service = static::getService($url, $banExpression); |
|
357 | + |
|
358 | + // prepare headers |
|
359 | + $headers = static::getHeaders(); |
|
360 | + |
|
361 | + // prepare curl options |
|
362 | + $options = static::getOptions(); |
|
363 | + |
|
364 | + // store data for return |
|
365 | + $data = array(); |
|
366 | + $data['url'] = $url; |
|
367 | + $data['banExpression'] = $banExpression; |
|
368 | + $data['headers'] = $headers; |
|
369 | + $data['options'] = $options; |
|
370 | + $data['service'] = $service; |
|
371 | + $result[] = $data; |
|
372 | + } |
|
373 | + } else { |
|
374 | + user_error('SectionIOTest_MySectionIO::performFlush :: no URLs loaded for ban.', E_USER_WARNING); |
|
375 | + } |
|
376 | + |
|
377 | + return $result; |
|
378 | + } |
|
379 | 379 | } |
@@ -2,116 +2,116 @@ discard block |
||
2 | 2 | |
3 | 3 | class SectionIO extends Object implements Flushable |
4 | 4 | { |
5 | - private static $flush_on_dev_build = true; |
|
6 | - |
|
7 | - private static $sitetree_flush_strategy = 'single'; |
|
8 | - |
|
9 | - private static $api_url = 'https://aperture.section.io/api/v1'; |
|
10 | - private static $account_id = ''; |
|
11 | - private static $application_id = ''; |
|
12 | - private static $environment_name = ''; |
|
13 | - private static $proxy_name = ''; |
|
14 | - private static $username = ''; |
|
15 | - private static $password = ''; |
|
16 | - private static $verify_ssl = true; |
|
17 | - |
|
18 | - /** |
|
19 | - * Implementation of Flushable::flush() |
|
20 | - * Is triggered on dev/build and ?flush=1. |
|
21 | - */ |
|
22 | - public static function flush() |
|
23 | - { |
|
24 | - if (Config::inst()->get('SectionIO', 'flush_on_dev_build')) { |
|
25 | - return static::flushAll(); |
|
26 | - } |
|
27 | - |
|
28 | - return; |
|
29 | - } |
|
30 | - |
|
31 | - public static function flushAll() |
|
32 | - { |
|
33 | - $exp = 'obj.http.x-url ~ /'; |
|
34 | - |
|
35 | - return static::performFlush($exp); |
|
36 | - } |
|
37 | - |
|
38 | - public static function flushImage($imageID) |
|
39 | - { |
|
40 | - $image = Image::get()->byID($imageID); |
|
41 | - if ($image && $image->exists()) { |
|
42 | - $exp = 'obj.http.x-url ~ "^/'.preg_quote($image->getFilename()).'$"'; // image itself |
|
43 | - $exp .= ' || obj.http.x-url ~ "^/'.preg_quote($image->Parent()->getFilename()) |
|
44 | - .'_resampled/(.*)\-'.preg_quote($image->Name).'$"'; // resampled versions |
|
45 | - return static::performFlush($exp); |
|
46 | - } |
|
47 | - |
|
48 | - return false; |
|
49 | - } |
|
50 | - |
|
51 | - public static function flushFile($fileID) |
|
52 | - { |
|
53 | - $file = File::get()->byID($fileID); |
|
54 | - if ($file && $file->exists()) { |
|
55 | - $exp = 'obj.http.x-url ~ "^/'.preg_quote($file->getFilename()).'$"'; |
|
56 | - return static::performFlush($exp); |
|
57 | - } |
|
58 | - |
|
59 | - return false; |
|
60 | - } |
|
61 | - |
|
62 | - public static function flushSiteTree($sitetreeID) |
|
63 | - { |
|
64 | - $sitetree = SiteTree::get()->byID($sitetreeID); |
|
65 | - if ($sitetree && $sitetree->exists()) { |
|
66 | - $strategy = Config::inst()->get('SectionIO', 'sitetree_flush_strategy'); |
|
67 | - switch ($strategy) { |
|
68 | - |
|
69 | - case 'single': |
|
70 | - $exp = 'obj.http.content-type ~ "'.preg_quote('text/html').'"'; |
|
71 | - $exp .= ' && obj.http.x-url ~ "^'.preg_quote($sitetree->Link()).'$"'; |
|
72 | - break; |
|
73 | - |
|
74 | - case 'parents': |
|
75 | - $exp = 'obj.http.content-type ~ "'.preg_quote('text/html').'"'; |
|
76 | - $exp .= ' && (obj.http.x-url ~ "^'.preg_quote($sitetree->Link()).'$"'; |
|
77 | - $parent = $sitetree->getParent(); |
|
78 | - while ($parent && $parent->exists()) { |
|
79 | - $exp .= ' || obj.http.x-url ~ "^'.preg_quote($parent->Link()).'$"'; |
|
80 | - $parent = $parent->getParent(); |
|
81 | - } |
|
82 | - $exp .= ')'; |
|
83 | - break; |
|
84 | - |
|
85 | - case 'all': |
|
86 | - $exp = 'obj.http.content-type ~ "'.preg_quote('text/html').'"'; |
|
87 | - break; |
|
88 | - |
|
89 | - case 'everyting': |
|
90 | - default: |
|
91 | - $exp = 'obj.http.x-url ~ /'; |
|
92 | - break; |
|
93 | - |
|
94 | - } |
|
95 | - |
|
96 | - return static::performFlush($exp); |
|
97 | - } |
|
98 | - |
|
99 | - return false; |
|
100 | - } |
|
5 | + private static $flush_on_dev_build = true; |
|
6 | + |
|
7 | + private static $sitetree_flush_strategy = 'single'; |
|
8 | + |
|
9 | + private static $api_url = 'https://aperture.section.io/api/v1'; |
|
10 | + private static $account_id = ''; |
|
11 | + private static $application_id = ''; |
|
12 | + private static $environment_name = ''; |
|
13 | + private static $proxy_name = ''; |
|
14 | + private static $username = ''; |
|
15 | + private static $password = ''; |
|
16 | + private static $verify_ssl = true; |
|
17 | + |
|
18 | + /** |
|
19 | + * Implementation of Flushable::flush() |
|
20 | + * Is triggered on dev/build and ?flush=1. |
|
21 | + */ |
|
22 | + public static function flush() |
|
23 | + { |
|
24 | + if (Config::inst()->get('SectionIO', 'flush_on_dev_build')) { |
|
25 | + return static::flushAll(); |
|
26 | + } |
|
27 | + |
|
28 | + return; |
|
29 | + } |
|
30 | + |
|
31 | + public static function flushAll() |
|
32 | + { |
|
33 | + $exp = 'obj.http.x-url ~ /'; |
|
34 | + |
|
35 | + return static::performFlush($exp); |
|
36 | + } |
|
37 | + |
|
38 | + public static function flushImage($imageID) |
|
39 | + { |
|
40 | + $image = Image::get()->byID($imageID); |
|
41 | + if ($image && $image->exists()) { |
|
42 | + $exp = 'obj.http.x-url ~ "^/'.preg_quote($image->getFilename()).'$"'; // image itself |
|
43 | + $exp .= ' || obj.http.x-url ~ "^/'.preg_quote($image->Parent()->getFilename()) |
|
44 | + .'_resampled/(.*)\-'.preg_quote($image->Name).'$"'; // resampled versions |
|
45 | + return static::performFlush($exp); |
|
46 | + } |
|
47 | + |
|
48 | + return false; |
|
49 | + } |
|
50 | + |
|
51 | + public static function flushFile($fileID) |
|
52 | + { |
|
53 | + $file = File::get()->byID($fileID); |
|
54 | + if ($file && $file->exists()) { |
|
55 | + $exp = 'obj.http.x-url ~ "^/'.preg_quote($file->getFilename()).'$"'; |
|
56 | + return static::performFlush($exp); |
|
57 | + } |
|
58 | + |
|
59 | + return false; |
|
60 | + } |
|
61 | + |
|
62 | + public static function flushSiteTree($sitetreeID) |
|
63 | + { |
|
64 | + $sitetree = SiteTree::get()->byID($sitetreeID); |
|
65 | + if ($sitetree && $sitetree->exists()) { |
|
66 | + $strategy = Config::inst()->get('SectionIO', 'sitetree_flush_strategy'); |
|
67 | + switch ($strategy) { |
|
68 | + |
|
69 | + case 'single': |
|
70 | + $exp = 'obj.http.content-type ~ "'.preg_quote('text/html').'"'; |
|
71 | + $exp .= ' && obj.http.x-url ~ "^'.preg_quote($sitetree->Link()).'$"'; |
|
72 | + break; |
|
73 | + |
|
74 | + case 'parents': |
|
75 | + $exp = 'obj.http.content-type ~ "'.preg_quote('text/html').'"'; |
|
76 | + $exp .= ' && (obj.http.x-url ~ "^'.preg_quote($sitetree->Link()).'$"'; |
|
77 | + $parent = $sitetree->getParent(); |
|
78 | + while ($parent && $parent->exists()) { |
|
79 | + $exp .= ' || obj.http.x-url ~ "^'.preg_quote($parent->Link()).'$"'; |
|
80 | + $parent = $parent->getParent(); |
|
81 | + } |
|
82 | + $exp .= ')'; |
|
83 | + break; |
|
84 | + |
|
85 | + case 'all': |
|
86 | + $exp = 'obj.http.content-type ~ "'.preg_quote('text/html').'"'; |
|
87 | + break; |
|
88 | + |
|
89 | + case 'everyting': |
|
90 | + default: |
|
91 | + $exp = 'obj.http.x-url ~ /'; |
|
92 | + break; |
|
93 | + |
|
94 | + } |
|
95 | + |
|
96 | + return static::performFlush($exp); |
|
97 | + } |
|
98 | + |
|
99 | + return false; |
|
100 | + } |
|
101 | 101 | |
102 | - public static function flushURL($url) { |
|
103 | - if ($url) { |
|
104 | - $exp = 'obj.http.x-url ~ "^'.preg_quote($url).'$"'; |
|
105 | - return static::performFlush($exp); |
|
106 | - } |
|
107 | - return false; |
|
108 | - } |
|
109 | - |
|
110 | - protected static function performFlush($banExpression) |
|
111 | - { |
|
112 | - $success = true; |
|
113 | - $urls = static::getUrls(); |
|
114 | - // config loaded successfully |
|
102 | + public static function flushURL($url) { |
|
103 | + if ($url) { |
|
104 | + $exp = 'obj.http.x-url ~ "^'.preg_quote($url).'$"'; |
|
105 | + return static::performFlush($exp); |
|
106 | + } |
|
107 | + return false; |
|
108 | + } |
|
109 | + |
|
110 | + protected static function performFlush($banExpression) |
|
111 | + { |
|
112 | + $success = true; |
|
113 | + $urls = static::getUrls(); |
|
114 | + // config loaded successfully |
|
115 | 115 | if (static::checkConfig()) { |
116 | 116 | if (count($urls) > 0) { |
117 | 117 | foreach ($urls as $url) { |
@@ -140,126 +140,126 @@ discard block |
||
140 | 140 | } |
141 | 141 | } |
142 | 142 | |
143 | - return $success; |
|
144 | - } |
|
145 | - |
|
146 | - protected static function getService($url, $banExpression) |
|
147 | - { |
|
148 | - // prepare API call |
|
149 | - $service = new RestfulService( |
|
150 | - $url, |
|
151 | - 0 // expiry time 0: do not cache the API call |
|
152 | - ); |
|
153 | - // set basic auth |
|
154 | - $username = Config::inst()->get('SectionIO', 'username'); |
|
155 | - $password = Config::inst()->get('SectionIO', 'password'); |
|
156 | - $service->basicAuth($username, $password); |
|
157 | - // set query string (ban expression) |
|
158 | - $service->setQueryString(array( |
|
159 | - 'banExpression' => $banExpression, |
|
160 | - )); |
|
161 | - |
|
162 | - return $service; |
|
163 | - } |
|
164 | - |
|
165 | - protected static function getOptions() |
|
166 | - { |
|
167 | - // prepare curl options for ssl verification |
|
168 | - if (Config::inst()->get('SectionIO', 'verify_ssl')) { |
|
169 | - return array( |
|
170 | - CURLOPT_SSL_VERIFYPEER => 0, |
|
171 | - CURLOPT_SSL_VERIFYHOST => 0, |
|
172 | - ); |
|
173 | - } |
|
174 | - return ayya(); |
|
175 | - } |
|
176 | - |
|
177 | - protected static function getHeaders() |
|
178 | - { |
|
179 | - $headers = array( |
|
180 | - 'Content-Type: application/json', |
|
181 | - 'Accept: application/json', |
|
182 | - ); |
|
183 | - |
|
184 | - return $headers; |
|
185 | - } |
|
186 | - |
|
187 | - protected static function getUrls() |
|
188 | - { |
|
189 | - $urls = array(); |
|
190 | - |
|
191 | - if (static::checkConfig()) { |
|
192 | - $api_url = Config::inst()->get('SectionIO', 'api_url'); |
|
193 | - $account_id = Config::inst()->get('SectionIO', 'account_id'); |
|
194 | - $application_id = Config::inst()->get('SectionIO', 'application_id'); |
|
195 | - $application_ids = array(); |
|
196 | - if (is_string($application_id)) { |
|
197 | - $application_ids = preg_split("/[\s,]+/", $application_id); |
|
198 | - } elseif (is_array($application_id)) { |
|
199 | - $application_ids = $application_id; |
|
200 | - } |
|
201 | - $environment_name = Config::inst()->get('SectionIO', 'environment_name'); |
|
202 | - $proxy_name = Config::inst()->get('SectionIO', 'proxy_name'); |
|
203 | - |
|
204 | - foreach ($application_ids as $appid) { |
|
205 | - // build API URL: /account/{accountId}/application/{applicationId}/environment/{environmentName}/proxy/{proxyName}/state |
|
206 | - $urls[] = Controller::join_links( |
|
207 | - $api_url, |
|
208 | - 'account', |
|
209 | - $account_id, |
|
210 | - 'application', |
|
211 | - $appid, |
|
212 | - 'environment', |
|
213 | - $environment_name, |
|
214 | - 'proxy', |
|
215 | - $proxy_name, |
|
216 | - 'state' |
|
217 | - ); |
|
218 | - } |
|
219 | - } |
|
220 | - |
|
221 | - return $urls; |
|
222 | - } |
|
223 | - |
|
224 | - protected static function checkConfig() |
|
225 | - { |
|
226 | - $missing = array(); |
|
227 | - // check config |
|
228 | - $api_url = Config::inst()->get('SectionIO', 'api_url'); |
|
229 | - if (!isset($api_url) || strlen($api_url) < 1) { |
|
230 | - $missing[] = 'SectionIO.api_url'; |
|
231 | - } |
|
232 | - $account_id = Config::inst()->get('SectionIO', 'account_id'); |
|
233 | - if (!isset($account_id) || strlen($account_id) < 1) { |
|
234 | - $missing[] = 'SectionIO.account_id'; |
|
235 | - } |
|
236 | - $application_id = Config::inst()->get('SectionIO', 'application_id'); |
|
237 | - if (!isset($application_id) || (!is_array($application_id) && strlen((string) $application_id) < 1)) { |
|
238 | - $missing[] = 'SectionIO.application_id'; |
|
239 | - } |
|
240 | - $environment_name = Config::inst()->get('SectionIO', 'environment_name'); |
|
241 | - if (!isset($environment_name) || strlen($environment_name) < 1) { |
|
242 | - $missing[] = 'SectionIO.environment_name'; |
|
243 | - } |
|
244 | - $proxy_name = Config::inst()->get('SectionIO', 'proxy_name'); |
|
245 | - if (!isset($proxy_name) || strlen($proxy_name) < 1) { |
|
246 | - $missing[] = 'SectionIO.proxy_name'; |
|
247 | - } |
|
248 | - $username = Config::inst()->get('SectionIO', 'username'); |
|
249 | - if (!isset($username) || strlen($username) < 1) { |
|
250 | - $missing[] = 'SectionIO.username'; |
|
251 | - } |
|
252 | - $password = Config::inst()->get('SectionIO', 'password'); |
|
253 | - if (!isset($password) || strlen($password) < 1) { |
|
254 | - $missing[] = 'SectionIO.password'; |
|
255 | - } |
|
143 | + return $success; |
|
144 | + } |
|
145 | + |
|
146 | + protected static function getService($url, $banExpression) |
|
147 | + { |
|
148 | + // prepare API call |
|
149 | + $service = new RestfulService( |
|
150 | + $url, |
|
151 | + 0 // expiry time 0: do not cache the API call |
|
152 | + ); |
|
153 | + // set basic auth |
|
154 | + $username = Config::inst()->get('SectionIO', 'username'); |
|
155 | + $password = Config::inst()->get('SectionIO', 'password'); |
|
156 | + $service->basicAuth($username, $password); |
|
157 | + // set query string (ban expression) |
|
158 | + $service->setQueryString(array( |
|
159 | + 'banExpression' => $banExpression, |
|
160 | + )); |
|
161 | + |
|
162 | + return $service; |
|
163 | + } |
|
164 | + |
|
165 | + protected static function getOptions() |
|
166 | + { |
|
167 | + // prepare curl options for ssl verification |
|
168 | + if (Config::inst()->get('SectionIO', 'verify_ssl')) { |
|
169 | + return array( |
|
170 | + CURLOPT_SSL_VERIFYPEER => 0, |
|
171 | + CURLOPT_SSL_VERIFYHOST => 0, |
|
172 | + ); |
|
173 | + } |
|
174 | + return ayya(); |
|
175 | + } |
|
176 | + |
|
177 | + protected static function getHeaders() |
|
178 | + { |
|
179 | + $headers = array( |
|
180 | + 'Content-Type: application/json', |
|
181 | + 'Accept: application/json', |
|
182 | + ); |
|
183 | + |
|
184 | + return $headers; |
|
185 | + } |
|
186 | + |
|
187 | + protected static function getUrls() |
|
188 | + { |
|
189 | + $urls = array(); |
|
190 | + |
|
191 | + if (static::checkConfig()) { |
|
192 | + $api_url = Config::inst()->get('SectionIO', 'api_url'); |
|
193 | + $account_id = Config::inst()->get('SectionIO', 'account_id'); |
|
194 | + $application_id = Config::inst()->get('SectionIO', 'application_id'); |
|
195 | + $application_ids = array(); |
|
196 | + if (is_string($application_id)) { |
|
197 | + $application_ids = preg_split("/[\s,]+/", $application_id); |
|
198 | + } elseif (is_array($application_id)) { |
|
199 | + $application_ids = $application_id; |
|
200 | + } |
|
201 | + $environment_name = Config::inst()->get('SectionIO', 'environment_name'); |
|
202 | + $proxy_name = Config::inst()->get('SectionIO', 'proxy_name'); |
|
203 | + |
|
204 | + foreach ($application_ids as $appid) { |
|
205 | + // build API URL: /account/{accountId}/application/{applicationId}/environment/{environmentName}/proxy/{proxyName}/state |
|
206 | + $urls[] = Controller::join_links( |
|
207 | + $api_url, |
|
208 | + 'account', |
|
209 | + $account_id, |
|
210 | + 'application', |
|
211 | + $appid, |
|
212 | + 'environment', |
|
213 | + $environment_name, |
|
214 | + 'proxy', |
|
215 | + $proxy_name, |
|
216 | + 'state' |
|
217 | + ); |
|
218 | + } |
|
219 | + } |
|
220 | + |
|
221 | + return $urls; |
|
222 | + } |
|
223 | + |
|
224 | + protected static function checkConfig() |
|
225 | + { |
|
226 | + $missing = array(); |
|
227 | + // check config |
|
228 | + $api_url = Config::inst()->get('SectionIO', 'api_url'); |
|
229 | + if (!isset($api_url) || strlen($api_url) < 1) { |
|
230 | + $missing[] = 'SectionIO.api_url'; |
|
231 | + } |
|
232 | + $account_id = Config::inst()->get('SectionIO', 'account_id'); |
|
233 | + if (!isset($account_id) || strlen($account_id) < 1) { |
|
234 | + $missing[] = 'SectionIO.account_id'; |
|
235 | + } |
|
236 | + $application_id = Config::inst()->get('SectionIO', 'application_id'); |
|
237 | + if (!isset($application_id) || (!is_array($application_id) && strlen((string) $application_id) < 1)) { |
|
238 | + $missing[] = 'SectionIO.application_id'; |
|
239 | + } |
|
240 | + $environment_name = Config::inst()->get('SectionIO', 'environment_name'); |
|
241 | + if (!isset($environment_name) || strlen($environment_name) < 1) { |
|
242 | + $missing[] = 'SectionIO.environment_name'; |
|
243 | + } |
|
244 | + $proxy_name = Config::inst()->get('SectionIO', 'proxy_name'); |
|
245 | + if (!isset($proxy_name) || strlen($proxy_name) < 1) { |
|
246 | + $missing[] = 'SectionIO.proxy_name'; |
|
247 | + } |
|
248 | + $username = Config::inst()->get('SectionIO', 'username'); |
|
249 | + if (!isset($username) || strlen($username) < 1) { |
|
250 | + $missing[] = 'SectionIO.username'; |
|
251 | + } |
|
252 | + $password = Config::inst()->get('SectionIO', 'password'); |
|
253 | + if (!isset($password) || strlen($password) < 1) { |
|
254 | + $missing[] = 'SectionIO.password'; |
|
255 | + } |
|
256 | 256 | |
257 | - if (count($missing) > 0) { |
|
257 | + if (count($missing) > 0) { |
|
258 | 258 | if (!Director::isDev()) { |
259 | 259 | SS_Log::log('SectionIO:: config parameters missing: ' . implode(', ', $missing), SS_Log::WARN); |
260 | 260 | } |
261 | - return false; |
|
262 | - } |
|
263 | - return true; |
|
264 | - } |
|
261 | + return false; |
|
262 | + } |
|
263 | + return true; |
|
264 | + } |
|
265 | 265 | } |
@@ -39,9 +39,9 @@ discard block |
||
39 | 39 | { |
40 | 40 | $image = Image::get()->byID($imageID); |
41 | 41 | if ($image && $image->exists()) { |
42 | - $exp = 'obj.http.x-url ~ "^/'.preg_quote($image->getFilename()).'$"'; // image itself |
|
43 | - $exp .= ' || obj.http.x-url ~ "^/'.preg_quote($image->Parent()->getFilename()) |
|
44 | - .'_resampled/(.*)\-'.preg_quote($image->Name).'$"'; // resampled versions |
|
42 | + $exp = 'obj.http.x-url ~ "^/' . preg_quote($image->getFilename()) . '$"'; // image itself |
|
43 | + $exp .= ' || obj.http.x-url ~ "^/' . preg_quote($image->Parent()->getFilename()) |
|
44 | + .'_resampled/(.*)\-' . preg_quote($image->Name) . '$"'; // resampled versions |
|
45 | 45 | return static::performFlush($exp); |
46 | 46 | } |
47 | 47 | |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | { |
53 | 53 | $file = File::get()->byID($fileID); |
54 | 54 | if ($file && $file->exists()) { |
55 | - $exp = 'obj.http.x-url ~ "^/'.preg_quote($file->getFilename()).'$"'; |
|
55 | + $exp = 'obj.http.x-url ~ "^/' . preg_quote($file->getFilename()) . '$"'; |
|
56 | 56 | return static::performFlush($exp); |
57 | 57 | } |
58 | 58 | |
@@ -67,23 +67,23 @@ discard block |
||
67 | 67 | switch ($strategy) { |
68 | 68 | |
69 | 69 | case 'single': |
70 | - $exp = 'obj.http.content-type ~ "'.preg_quote('text/html').'"'; |
|
71 | - $exp .= ' && obj.http.x-url ~ "^'.preg_quote($sitetree->Link()).'$"'; |
|
70 | + $exp = 'obj.http.content-type ~ "' . preg_quote('text/html') . '"'; |
|
71 | + $exp .= ' && obj.http.x-url ~ "^' . preg_quote($sitetree->Link()) . '$"'; |
|
72 | 72 | break; |
73 | 73 | |
74 | 74 | case 'parents': |
75 | - $exp = 'obj.http.content-type ~ "'.preg_quote('text/html').'"'; |
|
76 | - $exp .= ' && (obj.http.x-url ~ "^'.preg_quote($sitetree->Link()).'$"'; |
|
75 | + $exp = 'obj.http.content-type ~ "' . preg_quote('text/html') . '"'; |
|
76 | + $exp .= ' && (obj.http.x-url ~ "^' . preg_quote($sitetree->Link()) . '$"'; |
|
77 | 77 | $parent = $sitetree->getParent(); |
78 | 78 | while ($parent && $parent->exists()) { |
79 | - $exp .= ' || obj.http.x-url ~ "^'.preg_quote($parent->Link()).'$"'; |
|
79 | + $exp .= ' || obj.http.x-url ~ "^' . preg_quote($parent->Link()) . '$"'; |
|
80 | 80 | $parent = $parent->getParent(); |
81 | 81 | } |
82 | 82 | $exp .= ')'; |
83 | 83 | break; |
84 | 84 | |
85 | 85 | case 'all': |
86 | - $exp = 'obj.http.content-type ~ "'.preg_quote('text/html').'"'; |
|
86 | + $exp = 'obj.http.content-type ~ "' . preg_quote('text/html') . '"'; |
|
87 | 87 | break; |
88 | 88 | |
89 | 89 | case 'everyting': |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | |
102 | 102 | public static function flushURL($url) { |
103 | 103 | if ($url) { |
104 | - $exp = 'obj.http.x-url ~ "^'.preg_quote($url).'$"'; |
|
104 | + $exp = 'obj.http.x-url ~ "^' . preg_quote($url) . '$"'; |
|
105 | 105 | return static::performFlush($exp); |
106 | 106 | } |
107 | 107 | return false; |
@@ -129,10 +129,10 @@ discard block |
||
129 | 129 | $conn = $service->request(null, 'POST', null, $headers, $options); |
130 | 130 | |
131 | 131 | if ($conn->isError()) { |
132 | - SS_Log::log('SectionIO::performFlush :: '.$conn->getStatusCode().' : '.$conn->getStatusDescription().' : '.$url, SS_Log::ERR); |
|
132 | + SS_Log::log('SectionIO::performFlush :: ' . $conn->getStatusCode() . ' : ' . $conn->getStatusDescription() . ' : ' . $url, SS_Log::ERR); |
|
133 | 133 | $success = $success && false; |
134 | 134 | } else { |
135 | - SS_Log::log('SectionIO::performFlush :: ban successful. url: '.$url."; ban expression: '".$banExpression."'", SS_Log::NOTICE); |
|
135 | + SS_Log::log('SectionIO::performFlush :: ban successful. url: ' . $url . "; ban expression: '" . $banExpression . "'", SS_Log::NOTICE); |
|
136 | 136 | } |
137 | 137 | } |
138 | 138 | } else { |