Passed
Push — master ( 5a35a4...3b5e66 )
by Florian
01:37
created

SectionIOTest::tearDown()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Innoweb\SectionIO\Tests;
4
5
use Innoweb\SectionIO\SectionIO;
6
use Innoweb\SectionIO\Extensions\SectionIOFileExtension;
7
use Innoweb\SectionIO\Extensions\SectionIOSiteTreeExtension;
8
use SilverStripe\Assets\File;
9
use SilverStripe\Assets\Image;
10
use SilverStripe\CMS\Model\SiteTree;
11
use SilverStripe\Core\Config\Config;
12
use SilverStripe\Dev\SapphireTest;
13
use SilverStripe\ORM\DataObject;
14
use Page;
0 ignored issues
show
Bug introduced by
The type Page was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

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