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