| 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\SapphireTest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SilverStripe\SiteConfig\SiteConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use SilverStripe\View\Requirements; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class SilverStripeCollectorTest extends SapphireTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var SilverStripeCollector | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $collector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $usesDatabase = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         parent::setUp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         DebugBar::initDebugBar(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->collector = DebugBar::getDebugBar()->getCollector('silverstripe'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     public function tearDown() | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         DebugBar::clearDebugBar(); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         $this->collector = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         parent::tearDown(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     public function testCollectorExists() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $this->assertInstanceOf(SilverStripeCollector::class, $this->collector); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public function testCollect() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->logInWithPermission('ADMIN'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         Config::modify()->set(VersionProvider::class, 'modules', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             'silverstripe/framework' => 'Framework', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             'silverstripe/cms' => 'CMS', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $data = $this->collector->collect(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $this->assertArrayHasKey('debug', $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $this->assertArrayHasKey('locale', $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $this->assertArrayHasKey('parameters', $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $this->assertArrayHasKey('templates', $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         // TODO: see how to make this test relevant | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         // $this->assertContains('Framework', $data['version']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->assertSame(SiteConfig::class, $data['config']['ClassName']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this->assertSame('User, ADMIN', $data['user']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $this->assertCount(0, $data['requirements']['list']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $this->logOut(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $data = $this->collector->collect(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->assertSame('Not logged in', $data['user']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     public function testShowRequirements() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         Requirements::css('debugbar/assets/debugbar.css'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $data = $this->collector->collect(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $this->assertArrayHasKey('requirements', $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $this->assertNotEmpty($data['requirements']['list']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $this->assertGreaterThan(0, $data['requirements']['count']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $this->assertArrayHasKey('debugbar.css', $data['requirements']['list']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     public function testShowRequestParameters() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $controller = new Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $controller->doInit(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $controller->setRequest( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             new HTTPRequest( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 'GET', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 '/', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 array('getvar' => 'value', 'foo' => 'bar'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 array('postvar' => 'value', 'bar' => 'baz') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $controller->getRequest()->setRouteParams(array('something' => 'here')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $this->collector->setController($controller); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $this->assertSame($controller, $this->collector->getController()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $result = SilverStripeCollector::getRequestParameters(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $this->assertSame('value', $result['GET - getvar']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $this->assertSame('baz', $result['POST - bar']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $this->assertSame('here', $result['ROUTE - something']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     public function testGetSessionData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         Controller::curr()->getRequest()->getSession()->set('DebugBarTesting', 'test value'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $result = SilverStripeCollector::getSessionData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $this->assertSame('test value', $result['DebugBarTesting']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     public function testGetConfigData() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $result = SilverStripeCollector::getConfigData(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $this->assertSame(SiteConfig::class, $result['ClassName']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $this->assertArrayHasKey('Title', $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $this->assertArrayHasKey('ID', $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $this->assertArrayHasKey('Created', $result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     public function testGetWidgets() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $this->logInWithPermission('ADMIN'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $this->collector->collect(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $result = $this->collector->getWidgets(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $expectedKeys = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             'user', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             'version', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             'locale', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             'parameters', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             'requirements', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             'templates' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         foreach ($expectedKeys as $expectedKey) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             $this->assertArrayHasKey($expectedKey, $result, "$expectedKey not found in widgets"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         $this->logOut(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     public function testGetAssets() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $config = $this->collector->getAssets(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $this->assertArrayHasKey('base_path', $config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $this->assertArrayHasKey('base_url', $config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $this->assertArrayHasKey('css', $config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         $this->assertArrayHasKey('js', $config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         // No CSS for this one | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $this->assertFileExists(implode(DIRECTORY_SEPARATOR, [BASE_PATH, $config['base_path'], $config['js']])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 149 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 150 |  |  |  |