Passed
Pull Request — master (#28)
by
unknown
02:56
created

testUrlsToCrawlSegmentChanged()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 59
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 24
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 59
rs 9.536

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Ichaber\SSSwiftype\Tests\Extensions;
4
5
use Exception;
6
use Ichaber\SSSwiftype\Extensions\SwiftypeFileCrawlerExtension;
7
use Ichaber\SSSwiftype\Extensions\SwiftypeSiteTreeCrawlerExtension;
8
use Ichaber\SSSwiftype\Tests\Fake\SwiftypeFile;
9
use SilverStripe\Assets\Dev\TestAssetStore;
10
use SilverStripe\Core\Config\Config;
11
use SilverStripe\Core\Injector\Injector;
12
use SilverStripe\Dev\SapphireTest;
13
Use SilverStripe\Assets\File;
14
use SilverStripe\ORM\DataObject;
15
use SilverStripe\Versioned\Versioned;
16
17
/**
18
 * Class SwiftypeFileCrawlerExtensionTest
19
 *
20
 * @package Ichaber\SSSwiftype\Tests\Extensions
21
 */
22
class SwiftypeFileCrawlerExtensionTest extends SapphireTest
23
{
24
    /**
25
     * @var string
26
     */
27
    protected static $fixture_file = 'SwiftypeFileCrawlerExtensionTest.yml';
28
29
    /**
30
     * expected array of files to be indexed
31
     *
32
     * @var string[]
33
     */
34
    protected $expectedUrls = [
35
        'localhost/assets/SwiftypeFileCrawlerExtensionTest/dummy.pdf',
36
    ];
37
38
    public function setUp(): void
39
    {
40
        parent::setUp();
41
42
        // Make sure that our cache is cleared between tests
43
        /** @var SwiftypeFileCrawlerExtension $crawlerExtension */
44
        $crawlerExtension = Injector::inst()->get(SwiftypeFileCrawlerExtension::class);
45
        $crawlerExtension->clearCacheAll();
46
47
        // Set our config to not clear caches after un/publish, so that we can easily fetch the Urls for our test
48
        Config::inst()->update(
49
            SwiftypeFileCrawlerExtension::class,
50
            'clear_cache_disabled',
51
            true
52
        );
53
54
        // Set backend assets store root to /SwiftypeFileCrawlerExtensionTest
55
        TestAssetStore::activate('SwiftypeFileCrawlerExtensionTest');
56
    }
57
58
    /**
59
     * @throws Exception
60
     */
61
    public function testUrlsToCrawlPublished(): void
62
    {
63
        /** @var SwiftypeFile $file */
64
        $file = $this->objFromFixture(SwiftypeFile::class, 'file_pdf');
65
        $sourcePath = __DIR__ . '/../Fixtures/' . $file->Name;
66
        $file->setFromLocalFile($sourcePath, $file->Filename);
67
68
        // check our urls are not populated before we publish.
69
        $urls = [];
70
        $this->assertEquals($urls, $file->getUrlsToCrawl());
71
72
        // Publish single so that Urls to crawl is populated
73
        $file->publishSingle();
74
75
        // Grab the Urls that we expect to have been collated
76
        $key = str_replace('\\', '', $file->ClassName . $file->ID);
77
        $urlsToCrawl = $file->getUrlsToCrawl();
78
79
        // Check that the key exists for our file
80
        $this->assertArrayHasKey($key, $urlsToCrawl);
81
82
        // Grab the Urls that are for our page
83
        $urlsToCrawl = $urlsToCrawl[$key];
84
85
        // Strip out any http/https stuff
86
        foreach ($urlsToCrawl as $urlToCrawl) {
87
            $url = str_replace('http://', '', $urlToCrawl);
88
            $url = str_replace('https://', '', $url);
89
90
            $urls[] = $url;
91
        }
92
93
        $this->assertEquals($this->expectedUrls, $urls, '', 0.0, 10, true);
94
    }
95
96
    /**
97
     * @throws Exception
98
     */
99
    public function testUrlsToCrawlUnPublished(): void
100
    {
101
        /** @var SwiftypeFile $file */
102
        $file = $this->objFromFixture(SwiftypeFile::class, 'file_pdf');
103
        $sourcePath = __DIR__ . '/../Fixtures/' . $file->Name;
104
        $file->setFromLocalFile($sourcePath, $file->Filename);
105
106
        // check our urls are not populated before we publish.
107
        $urls = [];
108
        $this->assertEquals($urls, $file->getUrlsToCrawl());
109
110
        // Make sure our file is published before we begin
111
        $file->publishSingle();
112
113
        // Make sure we don't have any Cache set from the above publishing
114
        $file->flushCache();
115
116
        // now we call the service to remove this file from index
117
        $file->doUnpublish();
118
119
        // Grab the Urls that we expect to have been collated
120
        $key = str_replace('\\', '', $file->ClassName . $file->ID);
121
        $urlsToCrawl = $file->getUrlsToCrawl();
122
123
        // Check that the key exists for our page
124
        $this->assertArrayHasKey($key, $urlsToCrawl);
125
126
        // Grab the Urls that are for our page
127
        $urlsToCrawl = $urlsToCrawl[$key];
128
129
        // Strip out any http/https stuff
130
        foreach ($urlsToCrawl as $urlToCrawl) {
131
            $url = str_replace('http://', '', $urlToCrawl);
132
            $url = str_replace('https://', '', $url);
133
134
            $urls[] = $url;
135
        }
136
137
        $this->assertEquals($this->expectedUrls, $urls, '', 0.0, 10, true);
138
    }
139
140
    /**
141
     * @throws Exception
142
     */
143
    public function testUrlsNotToCrawlPublished(): void
144
    {
145
        /** @var SwiftypeFile $file */
146
        $file = $this->objFromFixture(SwiftypeFile::class, 'file_jpg');
147
        $sourcePath = __DIR__ . '/../Fixtures/' . $file->Name;
148
        $file->setFromLocalFile($sourcePath, $file->Filename);
149
150
        // check our urls are not populated before we publish.
151
        $urls = [];
152
        $this->assertEquals($urls, $file->getUrlsToCrawl());
153
154
        // Publish single so that Urls to crawl is populated
155
        $file->publishSingle();
156
157
        // Grab the Urls that we expect to have been collated
158
        $urlsToCrawl = $file->getUrlsToCrawl();
159
        $this->assertEquals($urlsToCrawl, $file->getUrlsToCrawl());
160
161
        // Check that the key does not exist for our File
162
        $key = str_replace('\\', '', $file->ClassName . $file->ID);
163
        $this->assertArrayNotHasKey($key, $urlsToCrawl);
164
    }
165
166
    /**
167
     * @throws Exception
168
     */
169
    public function testUrlsNotToCrawlUnpublished(): void
170
    {
171
        /** @var SwiftypeFile $file */
172
        $file = $this->objFromFixture(SwiftypeFile::class, 'file_jpg');
173
        $sourcePath = __DIR__ . '/../Fixtures/' . $file->Name;
174
        $file->setFromLocalFile($sourcePath, $file->Filename);
175
176
        // check our urls are not populated before we publish.
177
        $urls = [];
178
        $this->assertEquals($urls, $file->getUrlsToCrawl());
179
180
        // Make sure our file is published before we begin
181
        $file->publishSingle();
182
183
        // Make sure we don't have any Cache set from the above publishing
184
        $file->flushCache();
185
186
        // now we call the service to remove this file from index
187
        $file->doUnpublish();
188
189
        // Grab the Urls that we expect to have been collated
190
        $urlsToCrawl = $file->getUrlsToCrawl();
191
        $this->assertEquals($urlsToCrawl, $file->getUrlsToCrawl());
192
193
        // Check that the key does not exist for our File
194
        $key = str_replace('\\', '', $file->ClassName . $file->ID);
195
        $this->assertArrayNotHasKey($key, $urlsToCrawl);
196
    }
197
198
    /**
199
     * @throws Exception
200
     */
201
    public function testUrlsToCrawlSegmentChanged(): void
202
    {
203
        /** @var SwiftypeFile $file */
204
        $file = $this->objFromFixture(SwiftypeFile::class, 'file_pdf');
205
        $sourcePath = __DIR__ . '/../Fixtures/' . $file->Name;
206
        $file->setFromLocalFile($sourcePath, $file->Filename);
207
208
        // check our urls are not populated before we publish anything.
209
        $urls = [];
210
        $this->assertEquals($urls, $file->getUrlsToCrawl());
211
212
        // Make sure our file is published before we begin
213
        $file->publishSingle();
214
215
        $key = str_replace('\\', '', $file->ClassName . $file->ID);
216
217
        // Make sure our cache is flushed from the above publishing
218
        $file->flushCache();
219
220
        /**
221
         * Note:
222
         *  Somehow because File DBObject records in Stage.Stage create a hash path in the URL to access the file,
223
         *  the asserted array contained 3 elements instead of 2. To ignore this we only asserted
224
         *  that our old and new file URL's exist in the array that is sent for reindexing.
225
         */
226
        // Update our URL Segment
227
        Versioned::withVersionedMode(function () use ($file) {
228
            Versioned::set_reading_mode('Stage.Stage');
229
            $file->renameFile('dummy-new.pdf');
230
        });
231
232
        // publish our file again to get new URL.
233
        $file->publishSingle();
234
235
        // We expect two URL's now. One from before the file rename change, and one from after it
236
        $this->expectedUrls = [
237
            'old_file' => 'localhost/assets/SwiftypeFileCrawlerExtensionTest/dummy.pdf',
238
            'new_file' => 'localhost/assets/SwiftypeFileCrawlerExtensionTest/dummy-new.pdf',
239
        ];
240
241
        // Grab the Urls that we expect to have been collated
242
        $urlsToCrawl = $file->getUrlsToCrawl();
243
244
        // Check that the key exists for our page
245
        $this->assertArrayHasKey($key, $urlsToCrawl);
246
247
        // Grab the Urls that are for our file
248
        $urlsToCrawl = $urlsToCrawl[$key];
249
250
        // Strip out any http/https stuff
251
        foreach ($urlsToCrawl as $urlToCrawl) {
252
            $url = str_replace('http://', '', $urlToCrawl);
253
            $url = str_replace('https://', '', $url);
254
255
            $urls[] = $url;
256
        }
257
258
        $this->assertContains($this->expectedUrls['old_file'], $urls);
259
        $this->assertContains($this->expectedUrls['new_file'], $urls);
260
    }
261
262
    public function testUrlsToCrawlCacheCleared(): void
263
    {
264
        // Since asserting cache is cleared we want to reenable cache for this test.
265
        Config::inst()->update(
266
            SwiftypeFileCrawlerExtension::class,
267
            'clear_cache_disabled',
268
            false
269
        );
270
271
        /** @var SwiftypeFile $file */
272
        $file = $this->objFromFixture(SwiftypeFile::class, 'file_pdf');
273
        $sourcePath = __DIR__ . '/../Fixtures/' . $file->Name;
274
        $file->setFromLocalFile($sourcePath, $file->Filename);
275
276
        // check our urls are not populated before we publish anything.
277
        $urls = [];
278
        $this->assertEquals($urls, $file->getUrlsToCrawl());
279
280
        // Publish single so that Urls to crawl is populated
281
        $file->publishSingle();
282
        $key = str_replace('\\', '', $file->ClassName . $file->ID);
283
284
        // Grab the Urls that we expect to have been collated
285
        $urlsToCrawl = $file->getUrlsToCrawl();
286
287
        // Check that the key exists for our page
288
        $this->assertArrayNotHasKey($key, $urlsToCrawl);
289
    }
290
291
    public function tearDown()
292
    {
293
        TestAssetStore::reset();
294
        parent::tearDown();
295
    }
296
}
297