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 all 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: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ before_script:

# Reference and enable rules in build site.
- ln -s $TESTDIR modules/rules
- drush --yes dl webprofiler
- drush --yes pm-enable simpletest rules
- drush cr
- drush --yes pm-enable 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
1 change: 0 additions & 1 deletion config/install/rules.settings.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
log_errors: warning
debug_log: false
debug: false
log_level: info
7 changes: 2 additions & 5 deletions config/schema/rules.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,11 @@ rules.settings:
label: 'Rules settings'
mapping:
log_errors:
type: integer
type: string
label: 'Logging of Rules evaluation errors'
debug_log:
type: boolean
label: 'Log debug information to the available loggers'
debug:
type: boolean
label: 'Show debug information'
log_level:
type: integer
type: string
label: 'Log level'
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 = [];
}

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

/**
* @file
* Contains \Drupal\rules\Logger\RulesStubLogger.
*/

namespace Drupal\rules\Logger;

use Drupal\Core\Logger\RfcLoggerTrait;
use Psr\Log\LoggerInterface;

/**
* Logs events into the memory with ability check it afterwards.
*/
class RulesStubLogger implements LoggerInterface {
use RfcLoggerTrait;

/**
* The database connection object.
*
* @var array
*/
protected $logs;

/**
* {@inheritdoc}
*/
public function log($level, $message, array $context = array()) {
$this->logs[] = [
'level' => $level,
'message' => $message,
'context' => $context,
];
}

/**
* Clears static logs storage.
*/
public function cleanLogs() {
$this->logs = array();
}

/**
* Returns statically saved logs.
*
* @return array
* Array of logs.
*/
public function getLogs() {
return $this->logs;
}

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

/**
* @file
* Contains \Drupal\rules\RulesServiceProvider.
*/

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 WebProfiler module.
*/
class RulesServiceProvider extends ServiceProviderBase {

/**
* {@inheritdoc}
*/
public function register(ContainerBuilder $container) {
if (FALSE !== $container->hasDefinition('logger.channel.rules') && $container->hasDefinition('webprofiler.drupal')) {
$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');
}
}

}
1 change: 0 additions & 1 deletion src/Tests/ConfigEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,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');
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

We need a stub logger for that (as discussed).

Expand Down
24 changes: 11 additions & 13 deletions src/Tests/RulesDrupalTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Drupal\rules\Tests;

use Drupal\rules\Logger\RulesStubLogger;
use Drupal\simpletest\KernelTestBase;

/**
Expand Down Expand Up @@ -35,14 +36,6 @@ abstract class RulesDrupalTestBase extends KernelTestBase {
*/
protected $typedDataManager;


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

/**
* Modules to enable.
*
Expand All @@ -56,10 +49,15 @@ 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();
// Prepare Rules logging for testing.
$this->installConfig(array('rules'));
$logger = new RulesStubLogger();
$this->container->get('config.factory')
->getEditable('rules.settings')
->set('debug_log', 1)
->save();
$this->container->set('logger', $logger);
$this->container->get('logger')->cleanLogs();

$this->expressionManager = $this->container->get('plugin.manager.rules_expression');
$this->conditionManager = $this->container->get('plugin.manager.condition');
Expand Down Expand Up @@ -92,7 +90,7 @@ protected function createCondition($id) {
*/
protected function assertRulesLogEntryExists($message, $log_item_index = 0) {
// Test that the action has logged something.
$logs = $this->logger->getLogs();
$logs = $this->container->get('logger')->getLogs();
$this->assertEqual($logs[$log_item_index]['message'], $message);
}

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

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

namespace Drupal\rules\Tests;

use Drupal\simpletest\WebTestBase;

/**
* Rules base web test.
*
* @group rules
*/
abstract class RulesDrupalWebTestBase extends WebTestBase {

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

/**
* 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();
// @todo uncomment it when patch with permission comes.
// $this->adminUser = $this->drupalCreateUser($permissions);
}

}
2 changes: 0 additions & 2 deletions src/Tests/RulesEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,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,7 +98,6 @@ public function testProvidedVariables() {

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

// Test that the action logged something.
$this->assertRulesLogEntryExists('action called');
}
Expand Down
76 changes: 76 additions & 0 deletions src/Tests/RulesWebProfilerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?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.

*
* Tests integration Rules logging with WebProfiler module.
*
* @group rules
*/
class RulesWebProfilerTest extends RulesDrupalWebTestBase {

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

public static $modules = ['rules', 'webprofiler', 'block'];

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

// Enable rules logging.
$this->container->get('config.factory')
->getEditable('rules.settings')
->set('debug_log', 1)
->set('log_errors', 'debug')
->save();

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

// Enables rules web debugging with WebProfiler.
$this->container->get('config.factory')
->getEditable('webprofiler.config')
->set('active_toolbar_items.rules', 'rules')
->save();

$this->drupalLogin($this->webProfilerUser);
}

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

$this->drupalGet('admin/reports/profiler/list');
$links = $this->xpath('//main//table[1]//a');
$url = $this->getAbsoluteUrl($links[0]['href']);
$this->drupalGet($url);
$this->assertText('Rules logs', 'Rules logs table exists');
$this->assertText('critical message', 'Rules log entry exists');
}

}
Loading