1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\DebugBar\Test\Collector; |
4
|
|
|
|
5
|
|
|
use LeKoala\DebugBar\DebugBar; |
6
|
|
|
use LeKoala\DebugBar\Collector\SilverStripeCollector; |
7
|
|
|
use SilverStripe\Control\Controller; |
8
|
|
|
use SilverStripe\Control\HTTPRequest; |
9
|
|
|
use SilverStripe\Dev\FunctionalTest; |
10
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
11
|
|
|
use SilverStripe\View\Requirements; |
12
|
|
|
use SilverStripe\View\Requirements_Backend; |
13
|
|
|
|
14
|
|
|
class SilverStripeCollectorTest extends FunctionalTest |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var SilverStripeCollector |
18
|
|
|
*/ |
19
|
|
|
protected $collector; |
20
|
|
|
|
21
|
|
|
protected $usesDatabase = true; |
22
|
|
|
|
23
|
|
|
public function setUp() |
24
|
|
|
{ |
25
|
|
|
parent::setUp(); |
26
|
|
|
DebugBar::initDebugBar(); |
27
|
|
|
$this->collector = DebugBar::getDebugBar()->getCollector('silverstripe'); |
28
|
|
|
Requirements::set_backend(Requirements_Backend::create()); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function tearDown() |
32
|
|
|
{ |
33
|
|
|
parent::tearDown(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function testCollectorExists() |
37
|
|
|
{ |
38
|
|
|
$this->assertInstanceOf(SilverStripeCollector::class, $this->collector); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function testCollect() |
42
|
|
|
{ |
43
|
|
|
$this->logInWithPermission('ADMIN'); |
44
|
|
|
$data = $this->collector->collect(); |
45
|
|
|
|
46
|
|
|
$this->assertArrayHasKey('debug', $data); |
47
|
|
|
$this->assertArrayHasKey('locale', $data); |
48
|
|
|
$this->assertArrayHasKey('parameters', $data); |
49
|
|
|
$this->assertArrayHasKey('templates', $data); |
50
|
|
|
$this->assertContains('Framework', $data['version']); |
51
|
|
|
$this->assertSame(SiteConfig::class, $data['config']['ClassName']); |
52
|
|
|
$this->assertSame('User, ADMIN', $data['user']); |
53
|
|
|
$this->assertCount(0, $data['requirements']); |
54
|
|
|
|
55
|
|
|
$this->logOut(); |
56
|
|
|
|
57
|
|
|
$data = $this->collector->collect(); |
58
|
|
|
$this->assertSame('Not logged in', $data['user']); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testShowRequirements() |
62
|
|
|
{ |
63
|
|
|
Requirements::css('debugbar/assets/debugbar.css'); |
64
|
|
|
$data = $this->collector->collect(); |
65
|
|
|
$this->assertArrayHasKey('requirements', $data); |
66
|
|
|
$this->assertNotEmpty($data['requirements']); |
67
|
|
|
$this->assertContains('assets/debugbar.css', $data['requirements'][0]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testShowRequestParameters() |
71
|
|
|
{ |
72
|
|
|
$controller = new Controller; |
73
|
|
|
$controller->doInit(); |
74
|
|
|
$controller->setRequest( |
75
|
|
|
new HTTPRequest( |
76
|
|
|
'GET', |
77
|
|
|
'/', |
78
|
|
|
array('getvar' => 'value', 'foo' => 'bar'), |
79
|
|
|
array('postvar' => 'value', 'bar' => 'baz') |
80
|
|
|
) |
81
|
|
|
); |
82
|
|
|
$controller->getRequest()->setRouteParams(array('something' => 'here')); |
83
|
|
|
|
84
|
|
|
$this->collector->setController($controller); |
85
|
|
|
$this->assertSame($controller, $this->collector->getController()); |
86
|
|
|
|
87
|
|
|
$result = SilverStripeCollector::getRequestParameters(); |
88
|
|
|
$this->assertSame('value', $result['GET - getvar']); |
89
|
|
|
$this->assertSame('baz', $result['POST - bar']); |
90
|
|
|
$this->assertSame('here', $result['ROUTE - something']); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testGetSessionData() |
94
|
|
|
{ |
95
|
|
|
Controller::curr()->getRequest()->getSession()->set('DebugBarTesting', 'test value'); |
96
|
|
|
$result = SilverStripeCollector::getSessionData(); |
97
|
|
|
$this->assertSame('test value', $result['DebugBarTesting']); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testGetConfigData() |
101
|
|
|
{ |
102
|
|
|
$result = SilverStripeCollector::getConfigData(); |
103
|
|
|
$this->assertSame(SiteConfig::class, $result['ClassName']); |
104
|
|
|
$this->assertArrayHasKey('Title', $result); |
105
|
|
|
$this->assertArrayHasKey('ID', $result); |
106
|
|
|
$this->assertArrayHasKey('Created', $result); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testGetWidgets() |
110
|
|
|
{ |
111
|
|
|
$this->logInWithPermission('ADMIN'); |
112
|
|
|
$this->collector->collect(); |
113
|
|
|
$result = $this->collector->getWidgets(); |
114
|
|
|
// Stub out the dynamic data |
115
|
|
|
$result['version']['tooltip'] = 'Stub'; |
116
|
|
|
$result['locale']['tooltip'] = 'Stub'; |
117
|
|
|
$result['user']['tooltip'] = 'Current member'; |
118
|
|
|
|
119
|
|
|
$expected = array( |
120
|
|
|
'user' => array( |
121
|
|
|
'icon' => 'user', |
122
|
|
|
'tooltip' => 'Current member', |
123
|
|
|
'default' => '', |
124
|
|
|
), |
125
|
|
|
'version' => array( |
126
|
|
|
'icon' => 'desktop', |
127
|
|
|
'tooltip' => 'Stub', |
128
|
|
|
'default' => '', |
129
|
|
|
), |
130
|
|
|
'locale' => array( |
131
|
|
|
'icon' => 'globe', |
132
|
|
|
'tooltip' => 'Stub', |
133
|
|
|
'default' => '', |
134
|
|
|
), |
135
|
|
|
'session' => array( |
136
|
|
|
'icon' => 'archive', |
137
|
|
|
'widget' => 'PhpDebugBar.Widgets.VariableListWidget', |
138
|
|
|
'map' => 'silverstripe.session', |
139
|
|
|
'default' => '{}', |
140
|
|
|
), |
141
|
|
|
'cookies' => array( |
142
|
|
|
'icon' => 'asterisk', |
143
|
|
|
'widget' => 'PhpDebugBar.Widgets.VariableListWidget', |
144
|
|
|
'map' => 'silverstripe.cookies', |
145
|
|
|
'default' => '{}', |
146
|
|
|
), |
147
|
|
|
'parameters' => array( |
148
|
|
|
'icon' => 'arrow-right', |
149
|
|
|
'widget' => 'PhpDebugBar.Widgets.VariableListWidget', |
150
|
|
|
'map' => 'silverstripe.parameters', |
151
|
|
|
'default' => '{}', |
152
|
|
|
), |
153
|
|
|
'SiteConfig' => array( |
154
|
|
|
'icon' => 'gear', |
155
|
|
|
'widget' => 'PhpDebugBar.Widgets.VariableListWidget', |
156
|
|
|
'map' => 'silverstripe.config', |
157
|
|
|
'default' => '{}', |
158
|
|
|
), |
159
|
|
|
'requirements' => array( |
160
|
|
|
'icon' => 'file-o ', |
161
|
|
|
'widget' => 'PhpDebugBar.Widgets.ListWidget', |
162
|
|
|
'map' => 'silverstripe.requirements', |
163
|
|
|
'default' => '{}', |
164
|
|
|
), |
165
|
|
|
'templates' => array( |
166
|
|
|
'icon' => 'edit', |
167
|
|
|
'widget' => 'PhpDebugBar.Widgets.ListWidget', |
168
|
|
|
'map' => "silverstripe.templates.templates", |
169
|
|
|
'default' => '{}' |
170
|
|
|
), |
171
|
|
|
'templates:badge' => array( |
172
|
|
|
'map' => 'silverstripe.templates.count', |
173
|
|
|
'default' => 0 |
174
|
|
|
) |
175
|
|
|
); |
176
|
|
|
|
177
|
|
|
$this->assertSame($expected, $result); |
178
|
|
|
$this->logOut(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function testGetAssets() |
182
|
|
|
{ |
183
|
|
|
$config = $this->collector->getAssets(); |
184
|
|
|
|
185
|
|
|
$this->assertArrayHasKey('base_path', $config); |
186
|
|
|
$this->assertArrayHasKey('base_url', $config); |
187
|
|
|
$this->assertArrayHasKey('css', $config); |
188
|
|
|
$this->assertArrayHasKey('js', $config); |
189
|
|
|
// No CSS for this one |
190
|
|
|
$this->assertFileExists(implode(DIRECTORY_SEPARATOR, [BASE_PATH, $config['base_path'], $config['js']])); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|