Repository navigation
Expand file tree
/
Copy pathStaticSearchSubscriberTest.php
More file actions
175 lines (146 loc) · 6.16 KB
/
Copy pathStaticSearchSubscriberTest.php
File metadata and controls
175 lines (146 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
declare(strict_types=1);
namespace Pushword\Search\Tests;
use Loupe\Loupe\Internal\Index\IndexInfo;
use PDO;
use PHPUnit\Framework\Attributes\Group;
use Pushword\Core\Site\SiteRegistry;
use Pushword\Search\Event\SearchDocumentEvent;
use Pushword\StaticGenerator\Event\StaticPostGenerateEvent;
use function Safe\json_decode;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
#[Group('integration')]
final class StaticSearchSubscriberTest extends KernelTestCase
{
private const int JSON_CONTENT_LENGTH = 1000;
public function testEmitsSearchJsonAndIndexDb(): void
{
$dir = $this->generateStatic();
try {
self::assertFileExists($dir.'/search.json');
self::assertFileExists($dir.'/search/loupe.db');
// Loupe writes in WAL mode: the documents land in `loupe.db-wal` and
// reach `loupe.db` only once it is checkpointed. Copying the bare file
// before that ships a valid but empty index — the JSON fallback still
// works, so only counting the rows catches it.
self::assertGreaterThan(
0,
$this->countExportedDocuments($dir),
'the exported index must carry the documents, not just an empty header page',
);
// simple-jekyll-search calls .trim() on every field value, so each
// one must be a string — an array (e.g. tags) breaks client search.
foreach ($this->readSearchJson($dir) as $entry) {
self::assertIsArray($entry);
foreach ($entry as $value) {
self::assertIsString($value);
}
}
} finally {
new Filesystem()->remove($dir);
}
}
/**
* SearchDocumentEvent listeners receive `mixed` and may overwrite a core
* field. Loupe's schema catches that on searchable/filterable attributes,
* but `url` and `slug` are neither, so they reach search.json unchecked —
* coerce them, since simple-jekyll-search calls .trim() on every value.
*/
public function testCoercesNonStringFieldsSetByAListener(): void
{
$dir = $this->generateStatic(static function (SearchDocumentEvent $searchDocumentEvent): void {
$searchDocumentEvent->setAttribute('url', 42);
$searchDocumentEvent->setAttribute('slug', ['broken']);
});
try {
foreach ($this->readSearchJson($dir) as $entry) {
self::assertIsArray($entry);
self::assertSame('42', $entry['url']);
self::assertSame('', $entry['slug']);
}
} finally {
new Filesystem()->remove($dir);
}
}
public function testFlattensTagsAndTruncatesContent(): void
{
$dir = $this->generateStatic(static function (SearchDocumentEvent $searchDocumentEvent): void {
$searchDocumentEvent->setAttribute('tags', ['alpha', 'beta']);
$searchDocumentEvent->setAttribute('content', str_repeat('a', 2 * self::JSON_CONTENT_LENGTH));
});
try {
foreach ($this->readSearchJson($dir) as $entry) {
self::assertIsArray($entry);
self::assertSame('alpha beta', $entry['tags']);
self::assertIsString($entry['content']);
self::assertSame(self::JSON_CONTENT_LENGTH, mb_strlen($entry['content']));
}
} finally {
new Filesystem()->remove($dir);
}
}
/**
* A generation that reported errors would ship a search index built from a
* broken render, so nothing is written at all.
*/
public function testSkipsAFailedGeneration(): void
{
$dir = $this->generateStatic(null, ['/broken-page']);
try {
self::assertFileDoesNotExist($dir.'/search.json');
self::assertFileDoesNotExist($dir.'/search/loupe.db');
} finally {
new Filesystem()->remove($dir);
}
}
/**
* @param ?callable(SearchDocumentEvent): void $documentListener
* @param array<string> $errors
*
* @return string the static directory the subscriber wrote into
*/
private function generateStatic(?callable $documentListener = null, array $errors = []): string
{
self::bootKernel();
$container = self::getContainer();
$dispatcher = $container->get(EventDispatcherInterface::class);
$app = $container->get(SiteRegistry::class)->get('localhost.dev');
if (null !== $documentListener) {
$dispatcher->addListener(SearchDocumentEvent::class, $documentListener);
}
$dir = sys_get_temp_dir().'/pw-search-static-'.uniqid();
new Filesystem()->mkdir($dir);
$dispatcher->dispatch(new StaticPostGenerateEvent($app, $dir, false, $errors));
return $dir;
}
/**
* Read the exported index as a deployed endpoint would: straight off the
* copied file, with none of the sidecars that were next to the original.
*/
private function countExportedDocuments(string $dir): int
{
$pdo = new PDO('sqlite:'.$dir.'/search/loupe.db');
// Copied before the WAL was folded in, the file carries no schema at all —
// report that as zero documents so the assertion, not PDO, tells the story.
$tables = $pdo->query("SELECT name FROM sqlite_master WHERE type = 'table' AND name = '".IndexInfo::TABLE_NAME_DOCUMENTS."'");
self::assertNotFalse($tables);
if (false === $tables->fetchColumn()) {
return 0;
}
$statement = $pdo->query('SELECT COUNT(*) FROM '.IndexInfo::TABLE_NAME_DOCUMENTS);
self::assertNotFalse($statement);
return (int) $statement->fetchColumn();
}
/**
* @return array<mixed> the decoded search.json entries
*/
private function readSearchJson(string $dir): array
{
$json = json_decode((string) file_get_contents($dir.'/search.json'), true);
self::assertIsArray($json);
self::assertNotEmpty($json);
return $json;
}
}