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->clearCacheAll(); |
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
|
|
|
* Files that hold no text (e.g. images) should not be indexed |
142
|
|
|
* |
143
|
|
|
* @throws Exception |
144
|
|
|
*/ |
145
|
|
|
public function testUrlsNotToCrawlPublished(): void |
146
|
|
|
{ |
147
|
|
|
/** @var SwiftypeFile $file */ |
148
|
|
|
$file = $this->objFromFixture(SwiftypeFile::class, 'file_jpg'); |
149
|
|
|
$sourcePath = __DIR__ . '/../Fixtures/' . $file->Name; |
150
|
|
|
$file->setFromLocalFile($sourcePath, $file->Filename); |
151
|
|
|
|
152
|
|
|
// check our urls are not populated before we publish. |
153
|
|
|
$urls = []; |
154
|
|
|
$this->assertEquals($urls, $file->getUrlsToCrawl()); |
155
|
|
|
|
156
|
|
|
// Publish single so that Urls to crawl is populated |
157
|
|
|
$file->publishSingle(); |
158
|
|
|
|
159
|
|
|
// Grab the Urls that we expect to have been collated |
160
|
|
|
$urlsToCrawl = $file->getUrlsToCrawl(); |
|
|
|
|
161
|
|
|
$this->assertEquals($urls, $file->getUrlsToCrawl()); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Files that hold no text (e.g. images) should not be indexed |
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->clearCacheAll(); |
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->clearCacheAll(); |
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
|
|
|
|
283
|
|
|
$key = str_replace('\\', '', $file->ClassName . $file->ID); |
284
|
|
|
|
285
|
|
|
// Grab the Urls that we expect to have been collated |
286
|
|
|
$urlsToCrawl = $file->getUrlsToCrawl(); |
287
|
|
|
|
288
|
|
|
// Check that the key exists for our page |
289
|
|
|
$this->assertArrayNotHasKey($key, $urlsToCrawl); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
public function tearDown() |
293
|
|
|
{ |
294
|
|
|
TestAssetStore::reset(); |
295
|
|
|
parent::tearDown(); |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|