/
BootHandlerUnitTest.php
170 lines (118 loc) · 3.9 KB
/
BootHandlerUnitTest.php
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
<?php
namespace Elgg\Application;
use Elgg\Application;
use Elgg\Event;
use Elgg\Mocks\Di\MockServiceProvider;
use Elgg\UnitTestCase;
use Elgg\Helpers\CustomUser;
/**
* @group Boot
* @group Application
*/
class BootHandlerUnitTest extends UnitTestCase {
public function up() {
}
public function down() {
}
/**
* @return Application
*/
function createMockApplication(array $params = []) {
$config = self::getTestingConfig();
$sp = new MockServiceProvider($config);
// persistentLogin service needs this set to instantiate without calling DB
$sp->config->getCookieConfig();
$sp->config->boot_complete = false;
$sp->config->system_cache_enabled = false;
$sp->config->site = new \ElggSite((object) [
'guid' => 1,
]);
$sp->config->site->name = 'Testing Site';
$app = Application::factory(array_merge([
'service_provider' => $sp,
'handle_exceptions' => false,
'handle_shutdown' => false,
'set_start_time' => false,
], $params));
Application::setInstance($app);
return $app;
}
public function testCanDoFullBoot() {
$app = $this->createMockApplication();
$app->bootCore();
$this->assertTrue($app->_services->config->boot_complete);
}
public function testCanDoFullBootWithoutDb() {
// this needs fixing
// https://github.com/Elgg/Elgg/issues/12029
$this->markTestSkipped();
$app = $this->createMockApplication();
$app->_services->setValue('db', null);
$app->bootCore();
$this->assertTrue($app->_services->config->boot_complete);
$this->assertFalse($app->_services->config->_service_boot_complete);
$this->assertFalse($app->_services->config->_plugins_boot_complete);
$this->assertFalse($app->_services->config->_application_boot_complete);
}
public function testCanBootServices() {
$app = $this->createMockApplication();
$boot = new BootHandler($app);
$boot->bootServices();
$this->assertTrue($app->_services->config->_service_boot_complete);
}
public function testCanBootPlugins() {
$app = $this->createMockApplication();
$boot = new BootHandler($app);
$boot->bootServices();
$boot->bootPlugins();
$this->assertTrue($app->_services->config->_plugins_boot_complete);
}
public function testCanBootApplication() {
$app = $this->createMockApplication();
$boot = new BootHandler($app);
$boot->bootServices();
$boot->bootPlugins();
$boot->bootApplication();
$this->assertTrue($app->_services->config->_application_boot_complete);
}
public function testBootEventCalls() {
$calls = new \stdClass();
$calls->{'plugins_load:before'} = 0;
$calls->{'plugins_load'} = 0;
$calls->{'plugins_load:after'} = 0;
$calls->{'plugins_boot:before'} = 0;
$calls->{'plugins_boot'} = 0;
$calls->{'plugins_boot:after'} = 0;
$calls->{'init:before'} = 0;
$calls->{'init'} = 0;
$calls->{'init:after'} = 0;
$calls->{'ready:before'} = 0;
$calls->{'ready'} = 0;
$calls->{'ready:after'} = 0;
$app = $this->createMockApplication();
$app->_services->events->registerHandler('all', 'system', function(Event $event) use (&$calls) {
$type = $event->getName();
$calls->$type += 1;
});
$app->bootCore();
$this->assertTrue($app->_services->config->boot_complete);
foreach ($calls as $event => $count) {
$this->assertEquals(1, $count);
}
}
public function testCanSetCustomUserClassDuringBootSequence() {
$app = $this->createMockApplication();
$app->_services->events->registerHandler('plugins_load', 'system', function(Event $event) {
elgg_set_entity_class('user', 'custom_user', CustomUser::class);
});
$user = $this->createUser([
'subtype' => 'custom_user',
]);
$app->_services->session->set('guid', $user->guid);
$app->_services->session->setUserToken($user); // normally this is done during login()
$user->invalidateCache();
$app->bootCore();
$this->assertInstanceOf(CustomUser::class, $app->_services->session->getLoggedInUser());
$app->_services->session->removeLoggedInUser();
}
}