Newer
Older

Adam G-H
committed
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
176
177
178
179
180
181
<?php
namespace Drupal\Tests\package_manager\Kernel\PathExcluder;
use Drupal\Core\Database\Connection;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\package_manager\Event\PreCreateEvent;
use Drupal\package_manager\PathExcluder\SqliteDatabaseExcluder;
use Drupal\Tests\package_manager\Kernel\PackageManagerKernelTestBase;
/**
* @covers \Drupal\package_manager\PathExcluder\SqliteDatabaseExcluder
*
* @group package_manager
*/
class SqliteDatabaseExcluderTest extends PackageManagerKernelTestBase {
/**
* {@inheritdoc}
*/
protected function setUp(): void {
// In this test, we want to disable the lock file validator because, even
// though both the active and stage directories will have a valid lock file,
// this validator will complain because they don't differ at all.
$this->disableValidators[] = 'package_manager.validator.lock_file';
parent::setUp();
}
/**
* {@inheritdoc}
*/
public function register(ContainerBuilder $container) {
parent::register($container);
$container->getDefinition('package_manager.sqlite_excluder')
->setClass(TestSqliteDatabaseExcluder::class);
}
/**
* Mocks a SQLite database connection for the event subscriber.
*
* @param array $connection_options
* The connection options for the mocked database connection.
*/
private function mockDatabase(array $connection_options): void {
$database = $this->prophesize(Connection::class);
$database->driver()->willReturn('sqlite');
$database->getConnectionOptions()->willReturn($connection_options);
$this->container->get('package_manager.sqlite_excluder')
->database = $database->reveal();
}
/**
* Tests that SQLite database files are excluded from staging operations.
*/
public function testSqliteDatabaseFilesExcluded(): void {
// In this test, we want to perform the actual staging operations so that we
// can be sure that files are staged as expected.
$this->disableModules(['package_manager_bypass']);
// Ensure we have an up-to-date container.
$this->container = $this->container->get('kernel')->getContainer();
$this->createTestProject();
$active_dir = $this->container->get('package_manager.path_locator')
->getProjectRoot();
// Mock a SQLite database connection to a file in the active directory. The
// file should not be staged.
$this->mockDatabase([
'database' => 'sites/example.com/db.sqlite',
]);
$stage = $this->createStage();
$stage->create();
$stage_dir = $stage->getStageDirectory();
$ignored = [
"sites/example.com/db.sqlite",
"sites/example.com/db.sqlite-shm",
"sites/example.com/db.sqlite-wal",
];
foreach ($ignored as $path) {
$this->assertFileExists("$active_dir/$path");
$this->assertFileDoesNotExist("$stage_dir/$path");
}
$stage->apply();
// The ignored files should still be in the active directory.
foreach ($ignored as $path) {
$this->assertFileExists("$active_dir/$path");
}
}
/**
* Data provider for ::testPathProcessing().
*
* @return array[]
* Sets of arguments to pass to the test method.
*/
public function providerPathProcessing(): array {
$drupal_root = $this->getDrupalRoot();
return [
'relative path, in site directory' => [
'sites/example.com/db.sqlite',
[
'sites/example.com/db.sqlite',
'sites/example.com/db.sqlite-shm',
'sites/example.com/db.sqlite-wal',
],
],
'relative path, at root' => [
'db.sqlite',
[
'db.sqlite',
'db.sqlite-shm',
'db.sqlite-wal',
],
],
'absolute path, in site directory' => [
$drupal_root . '/sites/example.com/db.sqlite',
[
'sites/example.com/db.sqlite',
'sites/example.com/db.sqlite-shm',
'sites/example.com/db.sqlite-wal',
],
],
'absolute path, at root' => [
$drupal_root . '/db.sqlite',
[
'db.sqlite',
'db.sqlite-shm',
'db.sqlite-wal',
],
],
];
}
/**
* Tests SQLite database path processing.
*
* This test ensures that SQLite database paths are processed properly (e.g.,
* converting an absolute path to a relative path) before being flagged for
* exclusion.
*
* @param string $database_path
* The path of the SQLite database, as set in the database connection
* options.
* @param string[] $expected_exclusions
* The database paths which should be flagged for exclusion.
*
* @dataProvider providerPathProcessing
*/
public function testPathProcessing(string $database_path, array $expected_exclusions): void {
$this->mockDatabase([
'database' => $database_path,
]);
$event = new PreCreateEvent($this->createStage());
// Invoke the event subscriber directly, so we can check that the database
// was correctly excluded.
$this->container->get('package_manager.sqlite_excluder')
->excludeDatabaseFiles($event);
// All of the expected exclusions should be flagged.
$this->assertEmpty(array_diff($expected_exclusions, $event->getExcludedPaths()));
}
}
/**
* A test-only version of the SQLite database excluder, to expose internals.
*/
class TestSqliteDatabaseExcluder extends SqliteDatabaseExcluder {
/**
* {@inheritdoc}
*/
public $database;
}