Skip to content

Commit 41c021e

Browse files
author
Artyom Miroshnik
committed
Finish integration
1 parent 2398860 commit 41c021e

File tree

12 files changed

+387
-65
lines changed

12 files changed

+387
-65
lines changed

rules.info.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ type: module
33
description: 'React on events and conditionally evaluate actions.'
44
package: Rules
55
core: 8.x
6-
test_dependency:
6+
test_dependencies:
77
- webprofiler

src/Logger/RulesLoggerChannel.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@ class RulesLoggerChannel extends LoggerChannel {
2525
*/
2626
protected $config;
2727

28-
/**
29-
* Static storage of log entries.
30-
*
31-
* @var array
32-
*/
33-
protected $logs = [];
34-
3528
/**
3629
* Creates RulesLoggerChannel object.
3730
*
@@ -47,12 +40,6 @@ public function __construct(ConfigFactoryInterface $config_factory) {
4740
* {@inheritdoc}
4841
*/
4942
public function log($level, $message, array $context = []) {
50-
$this->logs[] = [
51-
'level' => $level,
52-
'message' => $message,
53-
'context' => $context,
54-
];
55-
5643
// Log message only if rules logging setting is enabled.
5744
if ($this->config->get('debug_log')) {
5845
if ($this->levelTranslation[$this->config->get('log_errors')] >= $this->levelTranslation[$level]) {
@@ -61,21 +48,4 @@ public function log($level, $message, array $context = []) {
6148
}
6249
}
6350

64-
/**
65-
* Returns the structured array of entries.
66-
*
67-
* @return array
68-
* Array of stored log entries.
69-
*/
70-
public function getLogs() {
71-
return $this->logs;
72-
}
73-
74-
/**
75-
* Clears the static logs entries cache.
76-
*/
77-
public function clearLogs() {
78-
$this->logs = [];
79-
}
80-
8151
}

src/RulesServiceProvider.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \Drupal\rules\WebProfiler\WebProfilerServiceProvider.
6+
*/
7+
8+
namespace Drupal\rules;
9+
10+
use Drupal\Core\Config\BootstrapConfigStorageFactory;
11+
use Drupal\Core\DependencyInjection\ContainerBuilder;
12+
use Drupal\Core\DependencyInjection\ServiceProviderBase;
13+
14+
/**
15+
* Defines a service profiler for the web profiler module.
16+
*/
17+
class RulesServiceProvider extends ServiceProviderBase {
18+
19+
const CONFIG_PREFIX = 'webprofiler.config';
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function register(ContainerBuilder $container) {
25+
if (FALSE !== $container->hasDefinition('logger.channel.rules') && $this->isRulesDebuggingEnabled()) {
26+
// Replace the regular logger.channel.rules service with a traceable one.
27+
$definition = $container->findDefinition('logger.channel.rules');
28+
$definition->setClass('Drupal\rules\WebProfiler\RulesChannelLoggerWrapper');
29+
}
30+
}
31+
32+
/**
33+
* Checks whether the site is multilingual.
34+
*
35+
* @return bool
36+
* TRUE if the site is multilingual, FALSE otherwise.
37+
*/
38+
protected function isRulesDebuggingEnabled() {
39+
$config_storage = BootstrapConfigStorageFactory::get();
40+
$config = $config_storage->read(static::CONFIG_PREFIX);
41+
return !empty($config['active_toolbar_items']['rules']);
42+
}
43+
44+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \Drupal\rules\Tests\RulesDrupalWebTestBase
6+
*/
7+
8+
namespace Drupal\rules\Tests;
9+
10+
use Drupal\simpletest\WebTestBase;
11+
12+
/**
13+
* Tests that the webprofile shows rules debug log and respects rules settings.
14+
*
15+
* @group block
16+
*/
17+
class RulesDrupalWebTestBase extends WebTestBase {
18+
19+
/**
20+
* Modules to install.
21+
*
22+
* @var array
23+
*/
24+
public static $modules = ['rules', 'rules_ui'];
25+
26+
/**
27+
* Authenticated user.
28+
*
29+
* @var \Drupal\user\Entity\User
30+
*/
31+
protected $user;
32+
33+
/**
34+
* User with administer rules permissions.
35+
*
36+
* @var \Drupal\user\Entity\User
37+
*/
38+
protected $adminUser;
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
protected function setUp() {
44+
parent::setUp();
45+
46+
$permissions = array('create page content', 'administer rules');
47+
$this->user = $this->drupalCreateUser();
48+
$this->adminUser = $this->drupalCreateUser($permissions);
49+
}
50+
51+
}

src/Tests/RulesWebProfilerTest.php

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \Drupal\rules\Tests\RulesWebProfilerTest
6+
*/
7+
8+
namespace Drupal\rules\Tests;
9+
10+
/**
11+
* Class RulesWebProfilerTest
12+
* @group Rules
13+
*/
14+
class RulesWebProfilerTest extends RulesDrupalWebTestBase {
15+
16+
/**
17+
* Authenticated user with access to web profiler.
18+
*
19+
* @var \Drupal\user\Entity\User
20+
*/
21+
protected $webProfilerUser;
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function setUp() {
27+
parent::setup();
28+
29+
$this->webProfilerUser = $this->drupalCreateUser(array(
30+
'access webprofiler',
31+
'view webprofiler toolbar',
32+
));
33+
34+
// Enables rules web debugging with web profiler.
35+
$this->config('webprofiler.config')->set('active_toolbar_items.rules', 'rules');
36+
$this->drupalLogin($this->webProfilerUser);
37+
}
38+
39+
/**
40+
* Tests does necessary information exist in WebProfiler toolbar.
41+
*/
42+
public function testWebProfilerToolbar() {
43+
$this->drupalGet('<front>', array(
44+
'log' => '1',
45+
'log-level' => 'info',
46+
'log-message' => 'info message',
47+
'log-amount' => 5,
48+
));
49+
50+
$this->assertText('Rules logs', 'Rules logs are visible in the toolbar.');
51+
$this->assertText('Info log entries 5', 'Additional rules logs info is visible in the toolbar.');
52+
53+
$this->drupalGet('<front>', array(
54+
'log' => '1',
55+
'log-level' => 'critical',
56+
'log-message' => 'critical message',
57+
'log-amount' => 3,
58+
));
59+
60+
$this->assertText('Rules logs', 'Rules logs are visible in the toolbar.');
61+
$this->assertText('Error log entries 3', 'Additional rules logs info is visible in the toolbar.');
62+
63+
$this->drupalGet('<front>', array(
64+
'log' => '0',
65+
'log-level' => 'debug',
66+
'log-message' => 'debug message',
67+
'log-amount' => 3,
68+
));
69+
70+
$this->assertText('Rules logs', 'Rules logs are visible in the toolbar.');
71+
$this->assertText('Debug log entries 0', 'Additional rules logs info is visible in the toolbar.');
72+
73+
$this->config('webprofiler.config')->set('active_toolbar_items.rules', '');
74+
75+
$this->drupalGet('<front>', array(
76+
'log' => '1',
77+
'log-level' => 'debug',
78+
'log-message' => 'debug message',
79+
'log-amount' => 3,
80+
));
81+
82+
$this->assertNoText('Rules logs', 'Rules logs are no visible in the toolbar.');
83+
$this->assertNoText('Debug log entries 3', 'Additional rules logs info is not visible in the toolbar.');
84+
}
85+
86+
/**
87+
* Goes to WebProfiler page using link from toolbar and check entries there.
88+
*/
89+
public function testWebProfilerPage() {
90+
$this->drupalGet('<front>', array(
91+
'log' => '1',
92+
'log-level' => 'info',
93+
'log-message' => 'info message',
94+
'log-amount' => 5,
95+
));
96+
97+
$links = $this->xpath('//div[@class="sf-toolbar-icon"]/a[@title="Rules"]');
98+
99+
$url = $this->getAbsoluteUrl($links[0]['href']);
100+
$this->drupalGet($url);
101+
$this->assertText('Rules logs', 'Rules logs table exists');
102+
$this->assertText('info message', 'Rules log entry exists');
103+
}
104+
105+
}

src/Webprofiler/DataCollector/RulesDataCollector.php

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,57 @@ public function getLogsCount() {
7777
return count($this->data['logs']);
7878
}
7979

80+
/**
81+
* Return amount of rules log entries with level higher then warning.
82+
*
83+
* @return int
84+
* Amount of error rules log entries.
85+
*/
86+
public function getErrorLogsCount() {
87+
$amount = 0;
88+
array_walk($this->data['logs'], function ($log) use ($amount) {
89+
if (in_array($log['level'], array('error', 'critical', 'alert', 'emergency'))) {
90+
$amount++;
91+
}
92+
});
93+
94+
return $amount;
95+
}
96+
97+
/**
98+
* Return amount of rules log entries with level notice or warning.
99+
*
100+
* @return int
101+
* Amount of error rules log entries.
102+
*/
103+
public function getNoticeLogsCount() {
104+
$amount = 0;
105+
array_walk($this->data['logs'], function ($log) use ($amount) {
106+
if (in_array($log['level'], array('warning', 'notice'))) {
107+
$amount++;
108+
}
109+
});
110+
111+
return $amount;
112+
}
113+
114+
/**
115+
* Return amount of rules info log entries.
116+
*
117+
* @return int
118+
* Amount of error rules log entries.
119+
*/
120+
public function getInfoLogsCount() {
121+
$amount = 0;
122+
array_walk($this->data['logs'], function ($log) use ($amount) {
123+
if (in_array($log['level'], array('debug', 'info'))) {
124+
$amount++;
125+
}
126+
});
127+
128+
return $amount;
129+
}
130+
80131
/**
81132
* {@inheritdoc}
82133
*/
@@ -96,7 +147,7 @@ public function getPanel() {
96147

97148
$rows = array_map(function ($log) {
98149
return [
99-
$log, $log['message'], implode(', ', array_keys($log['context']))
150+
$log['level'], $log['message'], implode(', ', array_keys($log['context']))
100151
];
101152
}, $this->data['logs']);
102153

src/Webprofiler/RulesChannelLoggerWrapper.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
namespace Drupal\rules\WebProfiler;
99

10-
use Drupal\rules\Logger\RulesChannelLogger;
10+
use Drupal\rules\Logger\RulesLoggerChannel;
1111

12-
class RulesChannelLoggerWrapper extends RulesChannelLogger {
12+
class RulesChannelLoggerWrapper extends RulesLoggerChannel {
1313

1414
/**
1515
* Static list of rules log entries.
@@ -24,11 +24,11 @@ class RulesChannelLoggerWrapper extends RulesChannelLogger {
2424
public function log($level, $message, array $context = array()) {
2525
parent::log($level, $message, $context);
2626

27-
$this->logs[] = array(
27+
$this->logs[] = [
2828
'level' => $level,
2929
'message' => $message,
3030
'context' => $context,
31-
);
31+
];
3232
}
3333

3434
/**

src/Webprofiler/WebprofilerServiceProvider.php

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)