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

testUrlsNotToCrawlUnpublished()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 27
rs 9.8666
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
        // Make sure we don't have any Cache set from the above publishing
76
        $file->clearCacheAll();
77
78
        // Grab the Urls that we expect to have been collated
79
        $key = str_replace('\\', '', $file->ClassName . $file->ID);
80
        $urlsToCrawl = $file->getUrlsToCrawl();
81
82
        // Check that the key exists for our file
83
        $this->assertArrayHasKey($key, $urlsToCrawl);
84
85
        // Grab the Urls that are for our page
86
        $urlsToCrawl = $urlsToCrawl[$key];
87
88
        // Strip out any http/https stuff
89
        foreach ($urlsToCrawl as $urlToCrawl) {
90
            $url = str_replace('http://', '', $urlToCrawl);
91
            $url = str_replace('https://', '', $url);
92
93
            $urls[] = $url;
94
        }
95
96
        $this->assertEquals($this->expectedUrls, $urls, '', 0.0, 10, true);
97
    }
98
99
    /**
100
     * @throws Exception
101
     */
102
    public function testUrlsToCrawlUnPublished(): void
103
    {
104
        /** @var SwiftypeFile $file */
105
        $file = $this->objFromFixture(SwiftypeFile::class, 'file_pdf');
106
        $sourcePath = __DIR__ . '/../Fixtures/' . $file->Name;
107
        $file->setFromLocalFile($sourcePath, $file->Filename);
108
109
        // check our urls are not populated before we publish.
110
        $urls = [];
111
        $this->assertEquals($urls, $file->getUrlsToCrawl());
112
113
        // Make sure our file is published before we begin
114
        $file->publishSingle();
115
116
        // Make sure we don't have any Cache set from the above publishing
117
        $file->clearCacheAll();
118
119
        // now we call the service to remove this file from index
120
        $file->doUnpublish();
121
122
        // Grab the Urls that we expect to have been collated
123
        $key = str_replace('\\', '', $file->ClassName . $file->ID);
124
        $urlsToCrawl = $file->getUrlsToCrawl();
125
126
        // Check that the key exists for our page
127
        $this->assertArrayHasKey($key, $urlsToCrawl);
128
129
        // Grab the Urls that are for our page
130
        $urlsToCrawl = $urlsToCrawl[$key];
131
132
        // Strip out any http/https stuff
133
        foreach ($urlsToCrawl as $urlToCrawl) {
134
            $url = str_replace('http://', '', $urlToCrawl);
135
            $url = str_replace('https://', '', $url);
136
137
            $urls[] = $url;
138
        }
139
140
        $this->assertEquals($this->expectedUrls, $urls, '', 0.0, 10, true);
141
    }
142
143
    /**
144
     * Files that hold no text (e.g. images) should not be indexed
145
     *
146
     * @throws Exception
147
     */
148
    public function testUrlsNotToCrawlPublished(): void
149
    {
150
        /** @var SwiftypeFile $file */
151
        $file = $this->objFromFixture(SwiftypeFile::class, 'file_jpg');
152
        $sourcePath = __DIR__ . '/../Fixtures/' . $file->Name;
153
        $file->setFromLocalFile($sourcePath, $file->Filename);
154
155
        // check our urls are not populated before we publish.
156
        $urls = [];
157
        $this->assertEquals($urls, $file->getUrlsToCrawl());
158
159
        // Publish single so that Urls to crawl is populated
160
        $file->publishSingle();
161
162
        // Make sure we don't have any Cache set from the above publishing
163
        $file->clearCacheAll();
164
165
        // Make sure we don't have any Cache set from the above publishing
166
        $this->assertEquals($urls, $file->getUrlsToCrawl());
167
168
        // Grab the Urls that we expect to have been collated
169
        $urlsToCrawl = $file->getUrlsToCrawl();
170
        $this->assertEquals($urls, $file->getUrlsToCrawl());
171
172
        // Check that the key does not exist for our File
173
        $key = str_replace('\\', '', $file->ClassName . $file->ID);
174
        $this->assertArrayNotHasKey($key, $urlsToCrawl);
175
    }
176
177
    /**
178
     * Files that hold no text (e.g. images) should not be indexed
179
     *
180
     * @throws Exception
181
     */
182
    public function testUrlsNotToCrawlUnpublished(): void
183
    {
184
        /** @var SwiftypeFile $file */
185
        $file = $this->objFromFixture(SwiftypeFile::class, 'file_jpg');
186
        $sourcePath = __DIR__ . '/../Fixtures/' . $file->Name;
187
        $file->setFromLocalFile($sourcePath, $file->Filename);
188
189
        // check our urls are not populated before we publish.
190
        $urls = [];
191
        $this->assertEquals($urls, $file->getUrlsToCrawl());
192
193
        // Make sure our file is published before we begin
194
        $file->publishSingle();
195
196
        // Make sure we don't have any Cache set from the above publishing
197
        $file->clearCacheAll();
198
199
        // now we call the service to remove this file from index
200
        $file->doUnpublish();
201
202
        // Grab the Urls that we expect to have been collated
203
        $urlsToCrawl = $file->getUrlsToCrawl();
204
        $this->assertEquals($urlsToCrawl, $file->getUrlsToCrawl());
205
206
        // Check that the key does not exist for our File
207
        $key = str_replace('\\', '', $file->ClassName . $file->ID);
208
        $this->assertArrayNotHasKey($key, $urlsToCrawl);
209
    }
210
211
    /**
212
     * @throws Exception
213
     */
214
    public function testUrlsToCrawlSegmentChanged(): void
215
    {
216
        /** @var SwiftypeFile $file */
217
        $file = $this->objFromFixture(SwiftypeFile::class, 'file_pdf');
218
        $sourcePath = __DIR__ . '/../Fixtures/' . $file->Name;
219
        $file->setFromLocalFile($sourcePath, $file->Filename);
220
221
        // check our urls are not populated before we publish anything.
222
        $urls = [];
223
        $this->assertEquals($urls, $file->getUrlsToCrawl());
224
225
        // Make sure our file is published before we begin
226
        $file->publishSingle();
227
228
        $key = str_replace('\\', '', $file->ClassName . $file->ID);
229
230
        // Make sure our cache is flushed from the above publishing
231
        $file->clearCacheAll();
232
233
        /**
234
         * Note:
235
         *  Somehow because File DBObject records in Stage.Stage create a hash path in the URL to access the file,
236
         *  the asserted array contained 3 elements instead of 2. To ignore this we only asserted
237
         *  that our old and new file URL's exist in the array that is sent for reindexing.
238
         */
239
        // Update our URL Segment
240
        Versioned::withVersionedMode(function () use ($file) {
241
            Versioned::set_reading_mode('Stage.Stage');
242
            $file->renameFile('dummy-new.pdf');
243
        });
244
245
        // publish our file again to get new URL.
246
        $file->publishSingle();
247
248
        // We expect two URL's now. One from before the file rename change, and one from after it
249
        $this->expectedUrls = [
250
            'old_file' => 'localhost/assets/SwiftypeFileCrawlerExtensionTest/dummy.pdf',
251
            'new_file' => 'localhost/assets/SwiftypeFileCrawlerExtensionTest/dummy-new.pdf',
252
        ];
253
254
        // Grab the Urls that we expect to have been collated
255
        $urlsToCrawl = $file->getUrlsToCrawl();
256
257
        // Check that the key exists for our page
258
        $this->assertArrayHasKey($key, $urlsToCrawl);
259
260
        // Grab the Urls that are for our file
261
        $urlsToCrawl = $urlsToCrawl[$key];
262
263
        // Strip out any http/https stuff
264
        foreach ($urlsToCrawl as $urlToCrawl) {
265
            $url = str_replace('http://', '', $urlToCrawl);
266
            $url = str_replace('https://', '', $url);
267
268
            $urls[] = $url;
269
        }
270
271
        $this->assertContains($this->expectedUrls['old_file'], $urls);
272
        $this->assertContains($this->expectedUrls['new_file'], $urls);
273
    }
274
275
    public function testUrlsToCrawlCacheCleared(): void
276
    {
277
        // Since asserting cache is cleared we want to reenable cache for this test.
278
        Config::inst()->update(
279
            SwiftypeFileCrawlerExtension::class,
280
            'clear_cache_disabled',
281
            false
282
        );
283
284
        /** @var SwiftypeFile $file */
285
        $file = $this->objFromFixture(SwiftypeFile::class, 'file_pdf');
286
        $sourcePath = __DIR__ . '/../Fixtures/' . $file->Name;
287
        $file->setFromLocalFile($sourcePath, $file->Filename);
288
289
        // check our urls are not populated before we publish anything.
290
        $urls = [];
291
        $this->assertEquals($urls, $file->getUrlsToCrawl());
292
293
        // Publish single so that Urls to crawl is populated
294
        $file->publishSingle();
295
296
        // Make sure we don't have any Cache set from the above publishing
297
        $file->clearCacheAll();
298
299
        $key = str_replace('\\', '', $file->ClassName . $file->ID);
300
301
        // Grab the Urls that we expect to have been collated
302
        $urlsToCrawl = $file->getUrlsToCrawl();
303
304
        // Check that the key exists for our page
305
        $this->assertArrayNotHasKey($key, $urlsToCrawl);
306
    }
307
308
    public function tearDown()
309
    {
310
        TestAssetStore::reset();
311
        parent::tearDown();
312
    }
313
}
314