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