Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebProfiler debugging integration #175

Open
wants to merge 11 commits into
base: 8.x-3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ before_script:

# Reference and enable rules in build site.
- ln -s $TESTDIR modules/rules
- drush --yes pm-enable simpletest rules
- drush --yes dl webprofiler
- drush --yes pm-enable simpletest rules webprofiler

# Start a web server on port 8080, run in the background; wait for
# initialization. This is temporarly disabled since there are no web tests
Expand Down
2 changes: 2 additions & 0 deletions rules.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ type: module
description: 'React on events and conditionally evaluate actions.'
package: Rules
core: 8.x
test_dependencies:
- webprofiler
30 changes: 0 additions & 30 deletions src/Logger/RulesLoggerChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ class RulesLoggerChannel extends LoggerChannel {
*/
protected $config;

/**
* Static storage of log entries.
*
* @var array
*/
protected $logs = [];

/**
* Creates RulesLoggerChannel object.
*
Expand All @@ -47,12 +40,6 @@ public function __construct(ConfigFactoryInterface $config_factory) {
* {@inheritdoc}
*/
public function log($level, $message, array $context = []) {
$this->logs[] = [
'level' => $level,
'message' => $message,
'context' => $context,
];

// Log message only if rules logging setting is enabled.
if ($this->config->get('debug_log')) {
if ($this->levelTranslation[$this->config->get('log_errors')] >= $this->levelTranslation[$level]) {
Expand All @@ -61,21 +48,4 @@ public function log($level, $message, array $context = []) {
}
}

/**
* Returns the structured array of entries.
*
* @return array
* Array of stored log entries.
*/
public function getLogs() {
return $this->logs;
}

/**
* Clears the static logs entries cache.
*/
public function clearLogs() {
$this->logs = [];
}

}
53 changes: 53 additions & 0 deletions src/RulesServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/**
* @file
* Contains \Drupal\rules\WebProfiler\WebProfilerServiceProvider.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope.

*/

namespace Drupal\rules;

use Drupal\Core\Config\BootstrapConfigStorageFactory;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase;
use Symfony\Component\DependencyInjection\Reference;

/**
* Defines a service profiler for the web profiler module.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope.

*/
class RulesServiceProvider extends ServiceProviderBase {

const CONFIG_PREFIX = 'webprofiler.config';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this const? Docblock plx.


/**
* {@inheritdoc}
*/
public function register(ContainerBuilder $container) {
if (FALSE !== $container->hasDefinition('logger.channel.rules') && $this->isRulesDebuggingEnabled()) {
$container->register('webprofiler.rules', 'Drupal\rules\WebProfiler\DataCollector\RulesDataCollector')
->addArgument(new Reference('logger.channel.rules'))
->addTag('data_collector', array(
'template' => '@rules/Collector/rules.html.twig',
'id' => 'rules',
'title' => 'Rules',
'priority' => 200,
));
// Replace the regular logger.channel.rules service with a traceable one.
$definition = $container->findDefinition('logger.channel.rules');
$definition->setClass('Drupal\rules\WebProfiler\RulesChannelLoggerWrapper');
}
}

/**
* Checks whether the site is multilingual.
*
* @return bool
* TRUE if the site is multilingual, FALSE otherwise.
*/
protected function isRulesDebuggingEnabled() {
$config_storage = BootstrapConfigStorageFactory::get();
$config = $config_storage->read(static::CONFIG_PREFIX);
return !empty($config['active_toolbar_items']['rules']);
}

}
6 changes: 0 additions & 6 deletions src/Tests/ConfigEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ public function testConfigAction() {
// Create the Rules expression object from the configuration.
$expression = $loaded_entity->getExpression();
$expression->execute();

// Test that the action logged something.
$this->assertRulesLogEntryExists('action called');
}

/**
Expand All @@ -89,9 +86,6 @@ public function testConfigRule() {
// Create the Rules expression object from the configuration.
$expression = $loaded_entity->getExpression();
$expression->execute();

// Test that the action logged something.
$this->assertRulesLogEntryExists('action called');
}

/**
Expand Down
27 changes: 0 additions & 27 deletions src/Tests/RulesDrupalTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,6 @@ abstract class RulesDrupalTestBase extends KernelTestBase {
*/
protected $typedDataManager;


/**
* Rules logger.
*
* @var \Drupal\rules\Logger\RulesLoggerChannel
*/
protected $logger;

/**
* Modules to enable.
*
Expand All @@ -56,11 +48,6 @@ abstract class RulesDrupalTestBase extends KernelTestBase {
public function setUp() {
parent::setUp();

$this->logger = $this->container->get('logger.channel.rules');
// Clear the log from any stale entries that are bleeding over from previous
// tests.
$this->logger->clearLogs();

$this->expressionManager = $this->container->get('plugin.manager.rules_expression');
$this->conditionManager = $this->container->get('plugin.manager.condition');
$this->typedDataManager = $this->container->get('typed_data_manager');
Expand All @@ -82,18 +69,4 @@ protected function createCondition($id) {
return $condition;
}

/**
* Checks if particular message is in the log with given delta.
*
* @param string $message
* Log message.
* @param int $log_item_index
* Log item's index in log entries stack.
*/
protected function assertRulesLogEntryExists($message, $log_item_index = 0) {
// Test that the action has logged something.
$logs = $this->logger->getLogs();
$this->assertEqual($logs[$log_item_index]['message'], $message);
}

}
51 changes: 51 additions & 0 deletions src/Tests/RulesDrupalWebTestBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* @file
* Contains \Drupal\rules\Tests\RulesDrupalWebTestBase
*/

namespace Drupal\rules\Tests;

use Drupal\simpletest\WebTestBase;

/**
* Tests that the webprofile shows rules debug log and respects rules settings.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WebProfiler and Rules

*
* @group block
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah... No :P

*/
class RulesDrupalWebTestBase extends WebTestBase {

/**
* Modules to install.
*
* @var array
*/
public static $modules = ['rules', 'rules_ui'];

/**
* Authenticated user.
*
* @var \Drupal\user\Entity\User
*/
protected $user;

/**
* User with administer rules permissions.
*
* @var \Drupal\user\Entity\User
*/
protected $adminUser;

/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();

$permissions = array('create page content', 'administer rules');
$this->user = $this->drupalCreateUser();
$this->adminUser = $this->drupalCreateUser($permissions);
}

}
9 changes: 0 additions & 9 deletions src/Tests/RulesEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ public function testRuleCreation() {
// Add an action to it and execute the rule.
$rule->addAction('rules_test_log');
$rule->execute();

// Test that the action logged something.
$this->assertRulesLogEntryExists('action called');
}

/**
Expand All @@ -79,9 +76,6 @@ public function testContextPassing() {
$rule->addAction('rules_test_log');
$rule->setContextValue('test', 'test value');
$rule->execute();

// Test that the action logged something.
$this->assertRulesLogEntryExists('action called');
}

/**
Expand All @@ -99,9 +93,6 @@ public function testProvidedVariables() {

$rule->addAction('rules_test_log');
$rule->execute();

// Test that the action logged something.
$this->assertRulesLogEntryExists('action called');
}

/**
Expand Down
105 changes: 105 additions & 0 deletions src/Tests/RulesWebProfilerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

/**
* @file
* Contains \Drupal\rules\Tests\RulesWebProfilerTest
*/

namespace Drupal\rules\Tests;

/**
* Class RulesWebProfilerTest
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a proper description.

* @group Rules
*/
class RulesWebProfilerTest extends RulesDrupalWebTestBase {

/**
* Authenticated user with access to web profiler.
*
* @var \Drupal\user\Entity\User
*/
protected $webProfilerUser;

/**
* {@inheritdoc}
*/
public function setUp() {
parent::setup();

$this->webProfilerUser = $this->drupalCreateUser(array(
'access webprofiler',
'view webprofiler toolbar',
));

// Enables rules web debugging with web profiler.
$this->config('webprofiler.config')->set('active_toolbar_items.rules', 'rules');
$this->drupalLogin($this->webProfilerUser);
}

/**
* Tests does necessary information exist in WebProfiler toolbar.
*/
public function testWebProfilerToolbar() {
$this->drupalGet('<front>', array(
'log' => '1',
'log-level' => 'info',
'log-message' => 'info message',
'log-amount' => 5,
));

$this->assertText('Rules logs', 'Rules logs are visible in the toolbar.');
$this->assertText('Info log entries 5', 'Additional rules logs info is visible in the toolbar.');

$this->drupalGet('<front>', array(
'log' => '1',
'log-level' => 'critical',
'log-message' => 'critical message',
'log-amount' => 3,
));

$this->assertText('Rules logs', 'Rules logs are visible in the toolbar.');
$this->assertText('Error log entries 3', 'Additional rules logs info is visible in the toolbar.');

$this->drupalGet('<front>', array(
'log' => '0',
'log-level' => 'debug',
'log-message' => 'debug message',
'log-amount' => 3,
));

$this->assertText('Rules logs', 'Rules logs are visible in the toolbar.');
$this->assertText('Debug log entries 0', 'Additional rules logs info is visible in the toolbar.');

$this->config('webprofiler.config')->set('active_toolbar_items.rules', '');

$this->drupalGet('<front>', array(
'log' => '1',
'log-level' => 'debug',
'log-message' => 'debug message',
'log-amount' => 3,
));

$this->assertNoText('Rules logs', 'Rules logs are no visible in the toolbar.');
$this->assertNoText('Debug log entries 3', 'Additional rules logs info is not visible in the toolbar.');
}

/**
* Goes to WebProfiler page using link from toolbar and check entries there.
*/
public function testWebProfilerPage() {
$this->drupalGet('<front>', array(
'log' => '1',
'log-level' => 'info',
'log-message' => 'info message',
'log-amount' => 5,
));

$links = $this->xpath('//div[@class="sf-toolbar-icon"]/a[@title="Rules"]');

$url = $this->getAbsoluteUrl($links[0]['href']);
$this->drupalGet($url);
$this->assertText('Rules logs', 'Rules logs table exists');
$this->assertText('info message', 'Rules log entry exists');
}

}
Loading