Passed
Push — master ( c5b045...5ff871 )
by Florian
02:13
created

SectionIOTest_MySectionIO::performFlush()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 2
nop 1
dl 0
loc 15
rs 9.9666
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 Innoweb\SectionIO\Tests\SectionIOTest\CustomSectionIO;
9
use SilverStripe\Assets\File;
10
use SilverStripe\Assets\Image;
11
use SilverStripe\CMS\Model\SiteTree;
12
use SilverStripe\Core\Config\Config;
13
use SilverStripe\Dev\SapphireTest;
14
use SilverStripe\ORM\DataObject;
15
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...
16
17
class SectionIOTest extends SapphireTest
18
{
19
    protected static $fixture_file = 'SectionIOTest.yml';
20
21
    public static function setUpBeforeClass()
22
    {
23
        parent::setUpBeforeClass();
24
        
25
        // add config values
26
        Config::modify()->set(SectionIO::class, 'flush_on_dev_build', true);
27
        Config::modify()->set(SectionIO::class, 'api_url', 'https://example.com');
28
        Config::modify()->set(SectionIO::class, 'account_id', '123456');
29
        Config::modify()->set(SectionIO::class, 'application_id', '987654');
30
        Config::modify()->set(SectionIO::class, 'environment_name', 'Production');
31
        Config::modify()->set(SectionIO::class, 'proxy_name', 'myproxy');
32
        Config::modify()->set(SectionIO::class, 'username', 'someuser');
33
        Config::modify()->set(SectionIO::class, 'password', 'MySafePassword');
34
        Config::modify()->set(SectionIO::class, 'verify_ssl', false);
35
36
        // remove extensions otherwise the fixtures will break the tests (by calling the live flush)
37
        File::remove_extension(SectionIOFileExtension::class);
38
        SiteTree::remove_extension(SectionIOSiteTreeExtension::class);
39
    }
40
41
    public function setUp()
42
    {
43
        parent::setUp();
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
        }
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 = CustomSectionIO::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
    public function testFlush()
92
    {
93
        $result = CustomSectionIO::flush();
94
95
        $this->assertCount(
96
            1,
97
            $result,
98
            'one url returned for one application id'
99
        );
100
101
        // url
102
        $this->assertEquals(
103
            'https://example.com/account/123456/application/987654/environment/Production/proxy/myproxy/state',
104
            $result[0]['url'],
105
            'URL is concatenated correctly'
106
        );
107
108
        // ban expression
109
        $this->assertEquals(
110
            'obj.http.x-url ~ /',
111
            $result[0]['banExpression'],
112
            'ban expression is correct'
113
        );
114
115
        // test deactivated flush on build
116
        Config::modify()->set(SectionIO::class, 'flush_on_dev_build', false);
117
        $result = CustomSectionIO::flush();
118
        $this->assertNull(
119
            $result,
120
            'null returned if flush on build deactivated'
121
        );
122
    }
123
124
    public function testMultipleApplicationIDs()
125
    {
126
        // add second application to config
127
        Config::modify()->set(SectionIO::class, 'application_id', '2546987,856954');
128
129
        $result = CustomSectionIO::flushAll();
130
131
        $this->assertCount(
132
            2,
133
            $result,
134
            'two urls returned for two application id'
135
        );
136
137
        // url
138
        $this->assertEquals(
139
            'https://example.com/account/123456/application/2546987/environment/Production/proxy/myproxy/state',
140
            $result[0]['url'],
141
            'URL is concatenated correctly for app 1'
142
        );
143
        $this->assertEquals(
144
            'https://example.com/account/123456/application/856954/environment/Production/proxy/myproxy/state',
145
            $result[1]['url'],
146
            'URL is concatenated correctly for app 2'
147
        );
148
149
        // add second application to config with spaces in csv
150
        Config::modify()->set(SectionIO::class, 'application_id', '741852, 369258');
151
152
        $result = CustomSectionIO::flushAll();
153
154
        $this->assertCount(
155
            2,
156
            $result,
157
            'two urls returned for two application id'
158
        );
159
160
        // url
161
        $this->assertEquals(
162
            'https://example.com/account/123456/application/741852/environment/Production/proxy/myproxy/state',
163
            $result[0]['url'],
164
            'URL is concatenated correctly for app 1'
165
        );
166
        $this->assertEquals(
167
            'https://example.com/account/123456/application/369258/environment/Production/proxy/myproxy/state',
168
            $result[1]['url'],
169
            'URL is concatenated correctly for app 2'
170
        );
171
    }
172
173
    public function testFlushImage()
174
    {
175
        $imageId = $this->idFromFixture(Image::class, 'testImage');
176
177
        $result = CustomSectionIO::flushImage($imageId);
178
        
179
        // ban expression
180
        $this->assertThat(
181
            $result[0]['banExpression'],
182
            $this->logicalOr(
183
                'obj.http.x-url ~ "^/assets/SectionTest/test_image\.png$"'
184
                    .' || obj.http.x-url ~ "^/assets/SectionTest/test_image__[a-zA-Z0-9_]*\.png$"',
185
                'obj.http.x-url ~ "^/assets/SectionTest/55b443b601/test_image\.png$"'
186
                    .' || obj.http.x-url ~ "^/assets/SectionTest/55b443b601/test_image__[a-zA-Z0-9_]*\.png$"'
187
            ),
188
            'ban expression is correct'
189
        );
190
    }
191
192
    public function testFlushFile()
193
    {
194
        $fileId = $this->idFromFixture(File::class, 'testFile');
195
196
        $result = CustomSectionIO::flushFile($fileId);
197
198
        // ban expression
199
        $this->assertThat(
200
            $result[0]['banExpression'],
201
            $this->logicalOr(
202
                'obj.http.x-url ~ "^/assets/SectionTest/test_document\.pdf$"',
203
                'obj.http.x-url ~ "^/assets/SectionTest/55b443b601/test_document\.pdf$"'
204
            ),
205
            'ban expression is correct'
206
        );
207
    }
208
209
    public function testFlushSiteTree()
210
    {
211
        $pageId = $this->idFromFixture(Page::class, 'ceo');
212
213
        // test single page flush
214
        Config::modify()->set(SectionIO::class, 'sitetree_flush_strategy', 'single');
215
        $result = CustomSectionIO::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 = CustomSectionIO::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 = CustomSectionIO::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 = CustomSectionIO::flushSiteTree($pageId);
245
        $this->assertEquals(
246
            'obj.http.x-url ~ /',
247
            $result[0]['banExpression'],
248
            'ban expression is correct'
249
        );
250
    }
251
}
252