Commit d33784ce authored by lussoluca's avatar lussoluca Committed by lussoluca

Issue #2827852 by lussoluca: Write test for Webprofiler

parent b91d021d
......@@ -56,7 +56,7 @@ class CacheBackendWrapper implements CacheBackendInterface, CacheTagsInvalidator
$cache = $this->cacheBackend->get($cid, $allow_invalid);
if ($cache) {
$cacheCopy = new \StdClass();
$cacheCopy = new \stdClass();
$cacheCopy->cid = $cache->cid;
$cacheCopy->expire = $cache->expire;
$cacheCopy->tags = $cache->tags;
......@@ -82,7 +82,7 @@ class CacheBackendWrapper implements CacheBackendInterface, CacheTagsInvalidator
$this->cacheDataCollector->registerCacheMiss($this->bin, $cid);
}
else {
$cacheCopy = new \StdClass();
$cacheCopy = new \stdClass();
$cacheCopy->cid = $cache[$cid]->cid;
$cacheCopy->expire = $cache[$cid]->expire;
$cacheCopy->tags = $cache[$cid]->tags;
......
<?php
namespace Drupal\Tests\webprofiler\FunctionalJavascript;
/**
* Tests the JavaScript functionality of webprofiler.
*
* @group webprofiler
*/
class ToolbarTest extends WebprofilerTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['webprofiler', 'node'];
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
\Drupal::configFactory()->getEditable('system.site')->set('page.front', '/node')->save(TRUE);
}
/**
* Tests if the toolbar appears on front page.
*/
public function testToolbarOnFrontPage() {
$this->loginForToolbar();
$this->drupalGet('<front>');
$this->waitForToolbar();
$assert = $this->assertSession();
$assert->pageTextContains(\Drupal::VERSION);
$assert->pageTextContains('Configure Webprofiler');
$assert->pageTextContains('View latest reports');
$assert->pageTextContains('Drupal Documentation');
$assert->pageTextContains('Get involved!');
}
/**
* Tests the toolbar report page.
*/
public function testToolbarReportPage() {
$this->loginForDashboard();
$this->drupalGet('<front>');
$token = $this->waitForToolbar();
$this->drupalGet('admin/reports/profiler/list');
$assert = $this->assertSession();
$assert->pageTextContains($token);
}
/**
* Tests the toolbar not appears on excluded path.
*/
public function testToolbarNotAppearsOnExcludedPath() {
$this->loginForDashboard();
$this->drupalGet('admin/config/development/devel');
$token = $this->waitForToolbar();
$assert = $this->assertSession();
$assert->pageTextContains($token);
$assert->pageTextContains('Configure Webprofiler');
$this->config('webprofiler.config')
->set('exclude', '/admin/config/development/devel')
->save();
$this->drupalGet('admin/config/development/devel');
$this->assertSession()->pageTextNotContains('sf-toolbar');
}
}
<?php
namespace Drupal\Tests\webprofiler\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
use PHPUnit_Framework_AssertionFailedError;
/**
* Class WebprofilerTestBase.
*
* @group webprofiler
*/
abstract class WebprofilerTestBase extends JavascriptTestBase {
/**
* Wait until the toolbar is present on page.
*/
protected function waitForToolbar() {
$session = $this->getSession();
$token = $this->getToken();
$page = $session->getPage();
$toolbar = $page->findById('webprofiler' . $token);
$this->assertTrue($toolbar->hasClass('sf-toolbar'), 'Toolbar loader is present in page');
$session->wait(1000, 'null !== document.getElementById(\'sfToolbarMainContent-' . $token . '\')');
return $token;
}
/**
* Return the Webprofiler token.
*
* @return null|string
* The page token
*/
protected function getToken() {
$token = $this->getSession()->getResponseHeader('X-Debug-Token');
if (NULL === $token) {
throw new PHPUnit_Framework_AssertionFailedError();
}
return $token;
}
/**
* Login with a user that can see the toolbar.
*/
protected function loginForToolbar() {
$admin_user = $this->drupalCreateUser(
[
'view webprofiler toolbar',
]
);
$this->drupalLogin($admin_user);
}
/**
* Login with a user that can see the toolbar and the dashboard.
*/
protected function loginForDashboard() {
$admin_user = $this->drupalCreateUser(
[
'view webprofiler toolbar',
'access webprofiler',
]
);
$this->drupalLogin($admin_user);
}
/**
* Flush cache.
*/
protected function flushCache() {
$module_handler = \Drupal::moduleHandler();
$module_handler->invokeAll('cache_flush');
}
}
<?php
namespace Drupal\Tests\webprofiler\Kernel;
use Drupal\KernelTests\KernelTestBase;
/**
* Class DecoratorTest.
*
* @group webprofiler
*/
class DecoratorTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['system', 'views'];
/**
* Tests the Entity Type Manager service decoration.
*
* @param string $service
* The service name.
* @param string $original
* The original class.
* @param string $decorated
* The decorated class.
*
* @dataProvider decorators
*/
public function testEntityTypeDecorator($service, $original, $decorated) {
$entityTypeManagerOriginal = $this->container->get($service);
$this->assertInstanceOf($original, $entityTypeManagerOriginal);
$this->container->get('module_installer')->install(['webprofiler']);
$entityTypeManagerDecorated = $this->container->get($service);
$this->assertInstanceOf($decorated, $entityTypeManagerDecorated);
}
/**
* DataProvider for testEntityTypeDecorator.
*
* @return array
* The array of values to run tests on.
*/
public function decorators() {
return array(
array('entity_type.manager', 'Drupal\Core\Entity\EntityTypeManager', 'Drupal\webprofiler\Entity\EntityManagerWrapper'),
array('cache_factory', 'Drupal\Core\Cache\MemoryBackendFactory', 'Drupal\webprofiler\Cache\CacheFactoryWrapper'),
array('asset.css.collection_renderer', 'Drupal\Core\Asset\CssCollectionRenderer', 'Drupal\webprofiler\Asset\CssCollectionRendererWrapper'),
array('asset.js.collection_renderer', 'Drupal\Core\Asset\JsCollectionRenderer', 'Drupal\webprofiler\Asset\JsCollectionRendererWrapper'),
array('state', 'Drupal\Core\State\State', 'Drupal\webprofiler\State\StateWrapper'),
array('views.executable', 'Drupal\views\ViewExecutableFactory', 'Drupal\webprofiler\Views\ViewExecutableFactoryWrapper'),
array('form_builder', 'Drupal\Core\Form\FormBuilder', 'Drupal\webprofiler\Form\FormBuilderWrapper'),
array('access_manager', 'Drupal\Core\Access\AccessManager', 'Drupal\webprofiler\Access\AccessManagerWrapper'),
array('theme.negotiator', 'Drupal\Core\Theme\ThemeNegotiator', 'Drupal\webprofiler\Theme\ThemeNegotiatorWrapper'),
array('config.factory', 'Drupal\Core\Config\ConfigFactory', 'Drupal\webprofiler\Config\ConfigFactoryWrapper'),
array('string_translation', 'Drupal\Core\StringTranslation\TranslationManager', 'Drupal\webprofiler\StringTranslation\TranslationManagerWrapper'),
);
}
}
<?php
namespace Drupal\Tests\webprofiler\Unit\DataCollector;
use Drupal\webprofiler\Asset\CssCollectionRendererWrapper;
use Drupal\webprofiler\DataCollector\AssetsDataCollector;
/**
* @coversDefaultClass \Drupal\webprofiler\DataCollector\AssetsDataCollector
*
* @group webprofiler
*/
class AssetsDataCollectorTest extends DataCollectorBaseTest {
/**
* @var \Drupal\webprofiler\DataCollector\AssetsDataCollector
*/
private $assetDataCollector;
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $assetCollectionRendererInterface;
const ROOT = 'test_root';
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->assetDataCollector = new AssetsDataCollector(AssetsDataCollectorTest::ROOT);
$this->assetCollectionRendererInterface = $this->getMock('Drupal\Core\Asset\AssetCollectionRendererInterface');
}
/**
* Tests the Assets data collector.
*/
public function testCSS() {
$css = [
'core/assets/vendor/normalize-css/normalize.css' => [
'weight' => -219.944,
'group' => 0,
'type' => 'file',
'data' => 'core\/assets\/vendor\/normalize-css\/normalize.css',
'version' => '3.0.3',
'media' => 'all',
'preprocess' => TRUE,
'browsers' => [
'IE' => TRUE,
'!IE' => TRUE,
],
],
];
$cssCollectionRendererWrapper = new CssCollectionRendererWrapper($this->assetCollectionRendererInterface, $this->assetDataCollector);
$cssCollectionRendererWrapper->render($css);
$this->assertEquals(1, $this->assetDataCollector->getCssCount());
$this->assetDataCollector->collect($this->request, $this->response, $this->exception);
$data = $this->assetDataCollector->getData();
$this->assertEquals(AssetsDataCollectorTest::ROOT . '/', $data['assets']['installation_path']);
}
}
<?php
namespace Drupal\Tests\webprofiler\Unit\Cache;
namespace Drupal\Tests\webprofiler\Unit\DataCollector;
use Drupal\Tests\UnitTestCase;
use Drupal\webprofiler\Cache\CacheBackendWrapper;
use Drupal\webprofiler\DataCollector\CacheDataCollector;
/**
* @coversDefaultClass \Drupal\webprofiler\DataCollector\CacheDataCollector
*
* @group webprofiler
*/
class CacheDataCollectorTest extends UnitTestCase {
class CacheDataCollectorTest extends DataCollectorBaseTest {
/**
* @var \Drupal\webprofiler\DataCollector\CacheDataCollector
......@@ -50,7 +50,7 @@ class CacheDataCollectorTest extends UnitTestCase {
* Tests the collection of a cache hit.
*/
public function testCacheCollectorHit() {
$cache = new \StdClass();
$cache = new \stdClass();
$cache->cid = 'cache_id';
$cache->expire = 1;
$cache->tags = ['tag1', 'tag2'];
......
<?php
namespace Drupal\Tests\webprofiler\Unit\DataCollector;
use Drupal\Tests\UnitTestCase;
/**
* Class DataCollectorBaseTest.
*
* @group webprofiler
*/
abstract class DataCollectorBaseTest extends UnitTestCase {
/**
* @var
*/
protected $request;
/**
* @var
*/
protected $response;
/**
* @var
*/
protected $exception;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->request = $this->getMock('Symfony\Component\HttpFoundation\Request');
$this->response = $this->getMock('Symfony\Component\HttpFoundation\Response');
$this->exception = $this->getMock('Exception');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment