Completed
Pull Request — master (#91)
by Andrew
02:51
created

SilverStripeCollectorTest::testGetSessionData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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