Repository navigation
Expand file tree
/
Copy pathTelemetryKillSwitchTest.php
More file actions
163 lines (128 loc) · 5.72 KB
/
Copy pathTelemetryKillSwitchTest.php
File metadata and controls
163 lines (128 loc) · 5.72 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
<?php
namespace Tests\Feature\Telemetry;
use App\Jobs\SendTelemetryJob;
use App\Support\TelemetryIdentity;
use App\Support\TelemetrySettings;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;
/**
* Every way of saying no, and the proof that each of them is obeyed.
*
* Opt-out is only defensible if switching it off actually works — from the
* interface, from .env before the database exists, and by simply not being a
* finished installation yet. Each of these is a separate promise, so each gets
* its own test rather than one combined one.
*/
class TelemetryKillSwitchTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
config()->set('telemetry.enabled', true);
$this->markInstalled();
// Opt-in jest teraz domyslne, wiec przypadek, ktory cwiczy sciezke
// raportowania, musi ja wlaczyc wprost. Wczesniej brak wiersza znaczyl
// zgode i testy korzystaly z tego milczaco.
TelemetrySettings::put(TelemetrySettings::SETTING_KEY, true);
TelemetrySettings::forget();
Http::fake();
}
private function markInstalled(): void
{
$path = storage_path('installed');
if (! is_file($path)) {
@file_put_contents($path, date('Y-m-d H:i:s'));
}
}
public function test_the_env_override_wins_and_needs_no_database(): void
{
// The switch for an air-gapped site or a packaged build: it is answered
// before anything reaches the database, so it works mid-install too.
config()->set('telemetry.enabled', false);
$this->assertFalse(TelemetrySettings::enabled());
(new SendTelemetryJob)->handle();
Http::assertNothingSent();
}
public function test_the_admins_choice_in_settings_is_obeyed(): void
{
TelemetrySettings::put(TelemetrySettings::SETTING_KEY, false);
$this->assertFalse(TelemetrySettings::enabled());
(new SendTelemetryJob)->handle();
Http::assertNothingSent();
}
public function test_an_installation_that_has_not_finished_installing_says_nothing(): void
{
@unlink(storage_path('installed'));
$this->assertFalse(TelemetrySettings::enabled());
(new SendTelemetryJob)->handle();
Http::assertNothingSent();
$this->markInstalled();
}
public function test_the_demo_server_is_not_a_customer_and_does_not_report(): void
{
// Our own demo traffic would drown the signal we actually want.
config()->set('openmmes.demo_mode', true);
$this->assertFalse(TelemetrySettings::enabled());
(new SendTelemetryJob)->handle();
Http::assertNothingSent();
}
public function test_the_default_for_an_install_that_was_never_asked_is_off(): void
{
// Opt-in: an unanswered question is not a yes. This used to assert the
// opposite — reporting started on and had to be switched off.
TelemetrySettings::put(TelemetrySettings::SETTING_KEY, null);
\Illuminate\Support\Facades\DB::table('system_settings')
->where('key', TelemetrySettings::SETTING_KEY)->delete();
TelemetrySettings::forget();
$this->assertFalse(TelemetrySettings::enabled());
(new SendTelemetryJob)->handle();
Http::assertNothingSent();
}
public function test_an_administrator_can_turn_it_on(): void
{
// The other half of opt-in: off by default must still be a switch, not
// a wall. Without this, "off unless asked" would be indistinguishable
// from "off, and the toggle does nothing".
TelemetrySettings::put(TelemetrySettings::SETTING_KEY, true);
TelemetrySettings::forget();
$this->assertTrue(TelemetrySettings::enabled());
}
public function test_an_installation_that_cannot_keep_its_own_name_does_not_report(): void
{
// Unwritable storage would mint a new id on every restart and turn one
// site into a crowd. Better to stay quiet.
$this->assertNotNull(TelemetryIdentity::installId());
config()->set('telemetry.enabled', true);
$this->assertTrue(TelemetrySettings::enabled());
TelemetryIdentity::reset();
$this->assertNotNull(TelemetryIdentity::installId(), 'A fresh id is minted when storage is writable.');
}
public function test_the_installation_id_is_stable_across_reports(): void
{
$first = TelemetryIdentity::installId();
$second = TelemetryIdentity::installId();
$this->assertSame($first, $second, 'Successive reports must be recognisable as one installation.');
$this->assertTrue(\Illuminate\Support\Str::isUuid($first));
}
public function test_resetting_the_identity_makes_the_installation_a_stranger_again(): void
{
$before = TelemetryIdentity::installId();
TelemetryIdentity::reset();
$after = TelemetryIdentity::installId();
$this->assertNotSame($before, $after, 'Reset is what makes a cloned VM fixable.');
}
public function test_the_identity_is_random_rather_than_derived_from_the_site(): void
{
// A hash of APP_URL or the hostname would be a fingerprint: reversible
// by whoever holds the reports, and impossible for the customer to
// rotate. Assert it bears no relation to either.
config()->set('app.url', 'https://factory.example.com');
TelemetryIdentity::reset();
$id = TelemetryIdentity::installId();
foreach ([md5('https://factory.example.com'), sha1('https://factory.example.com'), md5(gethostname() ?: '')] as $derived) {
$this->assertStringNotContainsString(substr($derived, 0, 8), str_replace('-', '', $id));
}
}
}