From ebe7e92cc437801b0787e173d182cc9bf80edeb5 Mon Sep 17 00:00:00 2001 From: Artyom Miroshnik Date: Fri, 17 Apr 2015 10:29:00 +0200 Subject: [PATCH 01/11] Init commit for webprofiler integration --- .travis.yml | 2 +- rules.info.yml | 2 + rules.services.yml | 5 + .../DataCollector/RulesDataCollector.php | 118 ++++++++++++++++++ src/Webprofiler/RulesChannelLoggerWrapper.php | 44 +++++++ .../WebprofilerServiceProvider.php | 27 ++++ templates/Collector/rules.html.twig | 32 +++++ 7 files changed, 229 insertions(+), 1 deletion(-) create mode 100644 src/Webprofiler/DataCollector/RulesDataCollector.php create mode 100644 src/Webprofiler/RulesChannelLoggerWrapper.php create mode 100644 src/Webprofiler/WebprofilerServiceProvider.php create mode 100644 templates/Collector/rules.html.twig diff --git a/.travis.yml b/.travis.yml index 61ef216c..5fda57a8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -52,7 +52,7 @@ before_script: # Reference and enable rules in build site. - ln -s $TESTDIR modules/rules - - drush --yes pm-enable simpletest rules + - 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 diff --git a/rules.info.yml b/rules.info.yml index 563cb89a..b63baa93 100644 --- a/rules.info.yml +++ b/rules.info.yml @@ -3,3 +3,5 @@ type: module description: 'React on events and conditionally evaluate actions.' package: Rules core: 8.x +test_dependency: + - webprofiler \ No newline at end of file diff --git a/rules.services.yml b/rules.services.yml index 36e7ab12..6457b7c6 100644 --- a/rules.services.yml +++ b/rules.services.yml @@ -8,3 +8,8 @@ services: logger.channel.rules: class: Drupal\rules\Logger\RulesLoggerChannel arguments: ['@config.factory'] + webprofiler.rules: + class: Drupal\rules\Webprofiler\DataCollector\RulesDataCollector + arguments: ['@logger.channel.rules'] + tags: + - { name: data_collector, template:'@rules/Collector/rules.html.twig', id:'rules', title:'Rules', priority: 200} diff --git a/src/Webprofiler/DataCollector/RulesDataCollector.php b/src/Webprofiler/DataCollector/RulesDataCollector.php new file mode 100644 index 00000000..0464ec7c --- /dev/null +++ b/src/Webprofiler/DataCollector/RulesDataCollector.php @@ -0,0 +1,118 @@ +rulesLogger = $rulesLogger; + } + + /** + * {@inheritdoc} + */ + public function collect(Request $request, Response $response, \Exception $exception = NULL) { + $this->data['logs'] = $this->rulesLogger->getLogs(); + } + + /** + * {@inheritdoc} + */ + public function getName() { + return 'rules'; + } + + /** + * {@inheritdoc} + */ + public function getTitle() { + return $this->t('Rules'); + } + + /** + * {@inheritdoc} + */ + public function getPanelSummary() { + return $this->t('Total rules log entries: @count', array('@count' => $this->getLogsCount())); + } + + /** + * Return amount of rules log entries. + * + * @return int + * Amount of rules log entries. + */ + public function getLogsCount() { + return count($this->data['logs']); + } + + /** + * {@inheritdoc} + */ + public function getPanel() { + $build = array(); + + $build['table_title'] = array( + '#type' => 'inline_template', + '#template' => '

Rules logs

', + ); + + $cssHeader = array( + 'level', + 'message', + 'passed_context', + ); + + $rows = array(); + foreach ($this->data['logs'] as $log) { + $row = array(); + + $row[] = $log['level']; + $row[] = $log['message']; + $row[] = implode(', ', array_keys($log['context'])); + + $rows[] = $row; + } + + $build['logs_table'] = array( + '#type' => 'table', + '#rows' => $rows, + '#header' => $cssHeader, + '#sticky' => TRUE, + ); + + return $build; + } + +} diff --git a/src/Webprofiler/RulesChannelLoggerWrapper.php b/src/Webprofiler/RulesChannelLoggerWrapper.php new file mode 100644 index 00000000..53079fa9 --- /dev/null +++ b/src/Webprofiler/RulesChannelLoggerWrapper.php @@ -0,0 +1,44 @@ +logs[] = array( + 'level' => $level, + 'message' => $message, + 'context' => $context, + ); + } + + /** + * Return list of rules log entries. + * + * @return array + * List of rules log entries. + */ + public function getLogs() { + return $this->logs; + } + +} diff --git a/src/Webprofiler/WebprofilerServiceProvider.php b/src/Webprofiler/WebprofilerServiceProvider.php new file mode 100644 index 00000000..1f111d24 --- /dev/null +++ b/src/Webprofiler/WebprofilerServiceProvider.php @@ -0,0 +1,27 @@ +findDefinition('logger.channel.rules'); + $definition->setClass('Drupal\rules\Webprofiler\RulesChannelLoggerWrapper'); + } +} diff --git a/templates/Collector/rules.html.twig b/templates/Collector/rules.html.twig new file mode 100644 index 00000000..9917b6d8 --- /dev/null +++ b/templates/Collector/rules.html.twig @@ -0,0 +1,32 @@ +{% block toolbar %} + {% set icon %} + + Asset + {{ collector.getlogscount }} + + {% endset %} + {% set text %} + +
+ Rules logs + {{ collector.getlogscount }} +
+ + {% endset %} + +
+
{{ icon|default('') }}
+
{{ text|default('') }}
+
+{% endblock %} + +{% block panel %} +
+

{{ 'Rules'|t }}

+
+ +
+ {{ content }} +
+{% endblock %} From 239886084241b979ec66d5c8a9b971af5cc9b0ae Mon Sep 17 00:00:00 2001 From: Artyom Miroshnik Date: Fri, 17 Apr 2015 11:11:20 +0200 Subject: [PATCH 02/11] Get rid of Assets entries after copy\paste --- rules.services.yml | 4 +-- .../DataCollector/RulesDataCollector.php | 27 ++++++++----------- src/Webprofiler/RulesChannelLoggerWrapper.php | 6 ++--- .../WebprofilerServiceProvider.php | 13 +++++---- templates/Collector/rules.html.twig | 4 +-- 5 files changed, 24 insertions(+), 30 deletions(-) diff --git a/rules.services.yml b/rules.services.yml index 6457b7c6..a15a1d2d 100644 --- a/rules.services.yml +++ b/rules.services.yml @@ -9,7 +9,7 @@ services: class: Drupal\rules\Logger\RulesLoggerChannel arguments: ['@config.factory'] webprofiler.rules: - class: Drupal\rules\Webprofiler\DataCollector\RulesDataCollector + class: Drupal\rules\WebProfiler\DataCollector\RulesDataCollector arguments: ['@logger.channel.rules'] tags: - - { name: data_collector, template:'@rules/Collector/rules.html.twig', id:'rules', title:'Rules', priority: 200} + - { name: data_collector, template: '@rules/Collector/rules.html.twig', id: 'rules', title: 'Rules', priority: 200} diff --git a/src/Webprofiler/DataCollector/RulesDataCollector.php b/src/Webprofiler/DataCollector/RulesDataCollector.php index 0464ec7c..249ce39a 100644 --- a/src/Webprofiler/DataCollector/RulesDataCollector.php +++ b/src/Webprofiler/DataCollector/RulesDataCollector.php @@ -2,10 +2,10 @@ /** * @file - * Contains \Drupal\webprofiler\DataCollector\AssetDataCollector. + * Contains \Drupal\rules\WebProfiler\DataCollector\RulesDataCollector. */ -namespace Drupal\rules\Webprofiler\DataCollector; +namespace Drupal\rules\WebProfiler\DataCollector; use Drupal\webprofiler\DataCollector\DrupalDataCollectorTrait; use Drupal\webprofiler\DrupalDataCollectorInterface; @@ -13,10 +13,10 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\DataCollector\DataCollector; -use Drupal\rules\Webprofiler\RulesChannelLoggerWrapper; +use Drupal\rules\WebProfiler\RulesChannelLoggerWrapper; /** - * Collects data about the used assets (CSS/JS). + * Collects Rules module log entries. */ class RulesDataCollector extends DataCollector implements DrupalDataCollectorInterface { @@ -30,7 +30,7 @@ class RulesDataCollector extends DataCollector implements DrupalDataCollectorInt private $rulesLogger; /** - * Constructs a AssetDataCollector object. + * Constructs a RulesDataCollector object. * * @param RulesChannelLoggerWrapper $rulesLogger * The rules logger channel. @@ -85,7 +85,7 @@ public function getPanel() { $build['table_title'] = array( '#type' => 'inline_template', - '#template' => '

Rules logs

', + '#template' => '

{{ "Rules logs"|t }}

', ); $cssHeader = array( @@ -94,16 +94,11 @@ public function getPanel() { 'passed_context', ); - $rows = array(); - foreach ($this->data['logs'] as $log) { - $row = array(); - - $row[] = $log['level']; - $row[] = $log['message']; - $row[] = implode(', ', array_keys($log['context'])); - - $rows[] = $row; - } + $rows = array_map(function ($log) { + return [ + $log, $log['message'], implode(', ', array_keys($log['context'])) + ]; + }, $this->data['logs']); $build['logs_table'] = array( '#type' => 'table', diff --git a/src/Webprofiler/RulesChannelLoggerWrapper.php b/src/Webprofiler/RulesChannelLoggerWrapper.php index 53079fa9..d0e61bc9 100644 --- a/src/Webprofiler/RulesChannelLoggerWrapper.php +++ b/src/Webprofiler/RulesChannelLoggerWrapper.php @@ -2,10 +2,10 @@ /** * @file - * Contains Drupal\rules\Webprofiler\RulesChannelLoggerWrapper + * Contains Drupal\rules\WebProfiler\RulesChannelLoggerWrapper */ -namespace Drupal\rules\Webprofiler; +namespace Drupal\rules\WebProfiler; use Drupal\rules\Logger\RulesChannelLogger; @@ -32,7 +32,7 @@ public function log($level, $message, array $context = array()) { } /** - * Return list of rules log entries. + * Return a list of rules log entries. * * @return array * List of rules log entries. diff --git a/src/Webprofiler/WebprofilerServiceProvider.php b/src/Webprofiler/WebprofilerServiceProvider.php index 1f111d24..65df505e 100644 --- a/src/Webprofiler/WebprofilerServiceProvider.php +++ b/src/Webprofiler/WebprofilerServiceProvider.php @@ -2,26 +2,25 @@ /** * @file - * Contains \Drupal\rules\Webprofiler\WebprofilerServiceProvider. + * Contains \Drupal\rules\WebProfiler\WebprofilerServiceProvider. */ -namespace Drupal\rules\Webprofiler; +namespace Drupal\rules\WebProfiler; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ServiceProviderBase; /** - * Defines a service profiler for the webprofiler module. + * Defines a service profiler for the web profiler module. */ -class WebprofilerServiceProvider extends ServiceProviderBase { +class WebProfilerServiceProvider extends ServiceProviderBase { /** * {@inheritdoc} */ public function register(ContainerBuilder $container) { - // Replace the regular logger.channel.rules service - // with a traceable one. + // Replace the regular logger.channel.rules service with a traceable one. $definition = $container->findDefinition('logger.channel.rules'); - $definition->setClass('Drupal\rules\Webprofiler\RulesChannelLoggerWrapper'); + $definition->setClass('Drupal\rules\WebProfiler\RulesChannelLoggerWrapper'); } } diff --git a/templates/Collector/rules.html.twig b/templates/Collector/rules.html.twig index 9917b6d8..98112f2b 100644 --- a/templates/Collector/rules.html.twig +++ b/templates/Collector/rules.html.twig @@ -1,7 +1,7 @@ {% block toolbar %} {% set icon %} - Asset {{ collector.getlogscount }} @@ -9,7 +9,7 @@ {% set text %}
- Rules logs + {{ 'Rules logs'|t }} {{ collector.getlogscount }}
From 41c021edfc9c08a871bb4d4e41424153cb18408f Mon Sep 17 00:00:00 2001 From: Artyom Miroshnik Date: Fri, 17 Apr 2015 16:15:11 +0200 Subject: [PATCH 03/11] Finish integration --- rules.info.yml | 2 +- src/Logger/RulesLoggerChannel.php | 30 ----- src/RulesServiceProvider.php | 44 ++++++++ src/Tests/RulesDrupalWebTestBase.php | 51 +++++++++ src/Tests/RulesWebProfilerTest.php | 105 ++++++++++++++++++ .../DataCollector/RulesDataCollector.php | 53 ++++++++- src/Webprofiler/RulesChannelLoggerWrapper.php | 8 +- .../WebprofilerServiceProvider.php | 26 ----- templates/Collector/rules.html.twig | 16 ++- .../rules_webprofiler_test.info.yml | 9 ++ .../rules_webprofiler_test.module | 17 +++ .../Plugin/Action/TestWebProfilerAction.php | 91 +++++++++++++++ 12 files changed, 387 insertions(+), 65 deletions(-) create mode 100644 src/RulesServiceProvider.php create mode 100644 src/Tests/RulesDrupalWebTestBase.php create mode 100644 src/Tests/RulesWebProfilerTest.php delete mode 100644 src/Webprofiler/WebprofilerServiceProvider.php create mode 100644 tests/modules/rules_webprofiler_test/rules_webprofiler_test.info.yml create mode 100644 tests/modules/rules_webprofiler_test/rules_webprofiler_test.module create mode 100644 tests/modules/rules_webprofiler_test/src/Plugin/Action/TestWebProfilerAction.php diff --git a/rules.info.yml b/rules.info.yml index b63baa93..467770c3 100644 --- a/rules.info.yml +++ b/rules.info.yml @@ -3,5 +3,5 @@ type: module description: 'React on events and conditionally evaluate actions.' package: Rules core: 8.x -test_dependency: +test_dependencies: - webprofiler \ No newline at end of file diff --git a/src/Logger/RulesLoggerChannel.php b/src/Logger/RulesLoggerChannel.php index 967a2fdb..55ac97ee 100644 --- a/src/Logger/RulesLoggerChannel.php +++ b/src/Logger/RulesLoggerChannel.php @@ -25,13 +25,6 @@ class RulesLoggerChannel extends LoggerChannel { */ protected $config; - /** - * Static storage of log entries. - * - * @var array - */ - protected $logs = []; - /** * Creates RulesLoggerChannel object. * @@ -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]) { @@ -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 = []; - } - } diff --git a/src/RulesServiceProvider.php b/src/RulesServiceProvider.php new file mode 100644 index 00000000..cf88c1b7 --- /dev/null +++ b/src/RulesServiceProvider.php @@ -0,0 +1,44 @@ +hasDefinition('logger.channel.rules') && $this->isRulesDebuggingEnabled()) { + // 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']); + } + +} diff --git a/src/Tests/RulesDrupalWebTestBase.php b/src/Tests/RulesDrupalWebTestBase.php new file mode 100644 index 00000000..5d54a5cd --- /dev/null +++ b/src/Tests/RulesDrupalWebTestBase.php @@ -0,0 +1,51 @@ +user = $this->drupalCreateUser(); + $this->adminUser = $this->drupalCreateUser($permissions); + } + +} diff --git a/src/Tests/RulesWebProfilerTest.php b/src/Tests/RulesWebProfilerTest.php new file mode 100644 index 00000000..c7b9ebc4 --- /dev/null +++ b/src/Tests/RulesWebProfilerTest.php @@ -0,0 +1,105 @@ +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('', 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('', 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('', 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('', 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('', 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'); + } + +} diff --git a/src/Webprofiler/DataCollector/RulesDataCollector.php b/src/Webprofiler/DataCollector/RulesDataCollector.php index 249ce39a..b0dd3f4b 100644 --- a/src/Webprofiler/DataCollector/RulesDataCollector.php +++ b/src/Webprofiler/DataCollector/RulesDataCollector.php @@ -77,6 +77,57 @@ public function getLogsCount() { return count($this->data['logs']); } + /** + * Return amount of rules log entries with level higher then warning. + * + * @return int + * Amount of error rules log entries. + */ + public function getErrorLogsCount() { + $amount = 0; + array_walk($this->data['logs'], function ($log) use ($amount) { + if (in_array($log['level'], array('error', 'critical', 'alert', 'emergency'))) { + $amount++; + } + }); + + return $amount; + } + + /** + * Return amount of rules log entries with level notice or warning. + * + * @return int + * Amount of error rules log entries. + */ + public function getNoticeLogsCount() { + $amount = 0; + array_walk($this->data['logs'], function ($log) use ($amount) { + if (in_array($log['level'], array('warning', 'notice'))) { + $amount++; + } + }); + + return $amount; + } + + /** + * Return amount of rules info log entries. + * + * @return int + * Amount of error rules log entries. + */ + public function getInfoLogsCount() { + $amount = 0; + array_walk($this->data['logs'], function ($log) use ($amount) { + if (in_array($log['level'], array('debug', 'info'))) { + $amount++; + } + }); + + return $amount; + } + /** * {@inheritdoc} */ @@ -96,7 +147,7 @@ public function getPanel() { $rows = array_map(function ($log) { return [ - $log, $log['message'], implode(', ', array_keys($log['context'])) + $log['level'], $log['message'], implode(', ', array_keys($log['context'])) ]; }, $this->data['logs']); diff --git a/src/Webprofiler/RulesChannelLoggerWrapper.php b/src/Webprofiler/RulesChannelLoggerWrapper.php index d0e61bc9..5c13edc3 100644 --- a/src/Webprofiler/RulesChannelLoggerWrapper.php +++ b/src/Webprofiler/RulesChannelLoggerWrapper.php @@ -7,9 +7,9 @@ namespace Drupal\rules\WebProfiler; -use Drupal\rules\Logger\RulesChannelLogger; +use Drupal\rules\Logger\RulesLoggerChannel; -class RulesChannelLoggerWrapper extends RulesChannelLogger { +class RulesChannelLoggerWrapper extends RulesLoggerChannel { /** * Static list of rules log entries. @@ -24,11 +24,11 @@ class RulesChannelLoggerWrapper extends RulesChannelLogger { public function log($level, $message, array $context = array()) { parent::log($level, $message, $context); - $this->logs[] = array( + $this->logs[] = [ 'level' => $level, 'message' => $message, 'context' => $context, - ); + ]; } /** diff --git a/src/Webprofiler/WebprofilerServiceProvider.php b/src/Webprofiler/WebprofilerServiceProvider.php deleted file mode 100644 index 65df505e..00000000 --- a/src/Webprofiler/WebprofilerServiceProvider.php +++ /dev/null @@ -1,26 +0,0 @@ -findDefinition('logger.channel.rules'); - $definition->setClass('Drupal\rules\WebProfiler\RulesChannelLoggerWrapper'); - } -} diff --git a/templates/Collector/rules.html.twig b/templates/Collector/rules.html.twig index 98112f2b..7678c8b5 100644 --- a/templates/Collector/rules.html.twig +++ b/templates/Collector/rules.html.twig @@ -1,7 +1,7 @@ {% block toolbar %} {% set icon %} - {{ 'Rules'|t }} {{ collector.getlogscount }} @@ -9,8 +9,18 @@ {% set text %}
- {{ 'Rules logs'|t }} - {{ collector.getlogscount }} + {{ 'Error log entries'|t }} + {{ collector.geterrorlogscount }} +
+ +
+ {{ 'Notice log entries'|t }} + {{ collector.getnoticelogscount }} +
+ +
+ {{ 'Info log entries'|t }} + {{ collector.getinfologscount }}
{% endset %} diff --git a/tests/modules/rules_webprofiler_test/rules_webprofiler_test.info.yml b/tests/modules/rules_webprofiler_test/rules_webprofiler_test.info.yml new file mode 100644 index 00000000..c28c0351 --- /dev/null +++ b/tests/modules/rules_webprofiler_test/rules_webprofiler_test.info.yml @@ -0,0 +1,9 @@ +name: Rules Webprofiler test +type: module +description: 'Support module for the Rules Webprofiler integration tests.' +package: Rules +core: 8.x +hidden: true +dependencies: + - rules + - webprofiler diff --git a/tests/modules/rules_webprofiler_test/rules_webprofiler_test.module b/tests/modules/rules_webprofiler_test/rules_webprofiler_test.module new file mode 100644 index 00000000..98bd2a66 --- /dev/null +++ b/tests/modules/rules_webprofiler_test/rules_webprofiler_test.module @@ -0,0 +1,17 @@ +createAction('rules_test_webprofiler_log'); + $action->execute(); +} diff --git a/tests/modules/rules_webprofiler_test/src/Plugin/Action/TestWebProfilerAction.php b/tests/modules/rules_webprofiler_test/src/Plugin/Action/TestWebProfilerAction.php new file mode 100644 index 00000000..4f583e04 --- /dev/null +++ b/tests/modules/rules_webprofiler_test/src/Plugin/Action/TestWebProfilerAction.php @@ -0,0 +1,91 @@ +logger = $logger; + $this->request = $request_stack->getCurrentRequest(); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('logger.channel.rules'), + $container->get('request_stack') + ); + } + + /** + * {@inheritdoc} + */ + public function execute() { + // Get level passed via GET param. + $log = $this->request->get('log', 'enabled'); + if ($log) { + $level = $this->request->get('log-level', 'debug'); + $amount = $this->request->get('log-amount', 1); + $message = $this->request->get('log-message', 'debug message'); + $contexts = $this->request->get('log-contexts', ''); + for ($i = 0; $i < $amount; $i++) { + $this->logger->log($level, $message . '#' . $i, array_flip(explode('|', $contexts))); + } + } + } + +} From 657cfefb3c149762741c29f39b0d0ab559f9d555 Mon Sep 17 00:00:00 2001 From: Artyom Miroshnik Date: Fri, 17 Apr 2015 16:30:20 +0200 Subject: [PATCH 04/11] Fix couple of problems and put test data to check patch with --- rules.services.yml | 5 ----- src/RulesServiceProvider.php | 9 +++++++++ .../DataCollector/RulesDataCollector.php | 6 +++--- src/Webprofiler/RulesChannelLoggerWrapper.php | 13 ++++++++++++- 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/rules.services.yml b/rules.services.yml index a15a1d2d..36e7ab12 100644 --- a/rules.services.yml +++ b/rules.services.yml @@ -8,8 +8,3 @@ services: logger.channel.rules: class: Drupal\rules\Logger\RulesLoggerChannel arguments: ['@config.factory'] - webprofiler.rules: - class: Drupal\rules\WebProfiler\DataCollector\RulesDataCollector - arguments: ['@logger.channel.rules'] - tags: - - { name: data_collector, template: '@rules/Collector/rules.html.twig', id: 'rules', title: 'Rules', priority: 200} diff --git a/src/RulesServiceProvider.php b/src/RulesServiceProvider.php index cf88c1b7..e9988e58 100644 --- a/src/RulesServiceProvider.php +++ b/src/RulesServiceProvider.php @@ -10,6 +10,7 @@ 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. @@ -23,6 +24,14 @@ class RulesServiceProvider extends ServiceProviderBase { */ 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'); diff --git a/src/Webprofiler/DataCollector/RulesDataCollector.php b/src/Webprofiler/DataCollector/RulesDataCollector.php index b0dd3f4b..bfcc907c 100644 --- a/src/Webprofiler/DataCollector/RulesDataCollector.php +++ b/src/Webprofiler/DataCollector/RulesDataCollector.php @@ -85,7 +85,7 @@ public function getLogsCount() { */ public function getErrorLogsCount() { $amount = 0; - array_walk($this->data['logs'], function ($log) use ($amount) { + array_walk($this->data['logs'], function ($log) use (&$amount) { if (in_array($log['level'], array('error', 'critical', 'alert', 'emergency'))) { $amount++; } @@ -102,7 +102,7 @@ public function getErrorLogsCount() { */ public function getNoticeLogsCount() { $amount = 0; - array_walk($this->data['logs'], function ($log) use ($amount) { + array_walk($this->data['logs'], function ($log) use (&$amount) { if (in_array($log['level'], array('warning', 'notice'))) { $amount++; } @@ -119,7 +119,7 @@ public function getNoticeLogsCount() { */ public function getInfoLogsCount() { $amount = 0; - array_walk($this->data['logs'], function ($log) use ($amount) { + array_walk($this->data['logs'], function ($log) use (&$amount) { if (in_array($log['level'], array('debug', 'info'))) { $amount++; } diff --git a/src/Webprofiler/RulesChannelLoggerWrapper.php b/src/Webprofiler/RulesChannelLoggerWrapper.php index 5c13edc3..0a02e46c 100644 --- a/src/Webprofiler/RulesChannelLoggerWrapper.php +++ b/src/Webprofiler/RulesChannelLoggerWrapper.php @@ -16,7 +16,18 @@ class RulesChannelLoggerWrapper extends RulesLoggerChannel { * * @var array */ - protected $logs; + protected $logs = [ + [ + 'level' => 'info', + 'message' => 'Hello', + 'context' => [] + ], + [ + 'level' => 'critical', + 'message' => 'Take care!', + 'context' => [] + ] + ]; /** * {@inheritdoc} From 0b6dca70ba7b1815c63227d8036c12a620f53bd4 Mon Sep 17 00:00:00 2001 From: Artyom Miroshnik Date: Fri, 17 Apr 2015 16:51:44 +0200 Subject: [PATCH 05/11] Remove redundant tests and try to fix travis --- .travis.yml | 1 + src/Tests/ConfigEntityTest.php | 6 ------ src/Tests/RulesDrupalTestBase.php | 27 --------------------------- src/Tests/RulesEngineTest.php | 9 --------- 4 files changed, 1 insertion(+), 42 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5fda57a8..3a1668d2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -52,6 +52,7 @@ before_script: # Reference and enable rules in build site. - ln -s $TESTDIR modules/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 diff --git a/src/Tests/ConfigEntityTest.php b/src/Tests/ConfigEntityTest.php index 64d05d21..25cf97d3 100644 --- a/src/Tests/ConfigEntityTest.php +++ b/src/Tests/ConfigEntityTest.php @@ -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'); } /** @@ -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'); } /** diff --git a/src/Tests/RulesDrupalTestBase.php b/src/Tests/RulesDrupalTestBase.php index 9ebb97b6..fdab8121 100644 --- a/src/Tests/RulesDrupalTestBase.php +++ b/src/Tests/RulesDrupalTestBase.php @@ -35,14 +35,6 @@ abstract class RulesDrupalTestBase extends KernelTestBase { */ protected $typedDataManager; - - /** - * Rules logger. - * - * @var \Drupal\rules\Logger\RulesLoggerChannel - */ - protected $logger; - /** * Modules to enable. * @@ -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'); @@ -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); - } - } diff --git a/src/Tests/RulesEngineTest.php b/src/Tests/RulesEngineTest.php index f9b9baa9..685da3a7 100644 --- a/src/Tests/RulesEngineTest.php +++ b/src/Tests/RulesEngineTest.php @@ -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'); } /** @@ -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'); } /** @@ -99,9 +93,6 @@ public function testProvidedVariables() { $rule->addAction('rules_test_log'); $rule->execute(); - - // Test that the action logged something. - $this->assertRulesLogEntryExists('action called'); } /** From a0e0a6f5e76268c5f3cb00e95401e75079845fc8 Mon Sep 17 00:00:00 2001 From: Artyom Miroshnik Date: Fri, 17 Apr 2015 19:25:35 +0200 Subject: [PATCH 06/11] Tests rule --- config/install/rules.settings.yml | 1 - config/schema/rules.schema.yml | 7 +-- src/Logger/RulesStubLogger.php | 54 +++++++++++++++++++ src/RulesServiceProvider.php | 8 ++- src/Tests/ConfigEntityTest.php | 5 ++ src/Tests/RulesDrupalTestBase.php | 25 +++++++++ src/Tests/RulesDrupalWebTestBase.php | 11 ++-- src/Tests/RulesEngineTest.php | 7 +++ src/Tests/RulesWebProfilerTest.php | 11 ++-- .../DataCollector/RulesDataCollector.php | 53 +++++++++--------- .../src/Plugin/Action/TestLogAction.php | 15 +++--- .../rules_webprofiler_test.info.yml | 4 +- .../Plugin/Action/TestWebProfilerAction.php | 20 +++---- 13 files changed, 156 insertions(+), 65 deletions(-) create mode 100644 src/Logger/RulesStubLogger.php diff --git a/config/install/rules.settings.yml b/config/install/rules.settings.yml index 64f5e97a..f3193f8b 100644 --- a/config/install/rules.settings.yml +++ b/config/install/rules.settings.yml @@ -1,4 +1,3 @@ log_errors: warning debug_log: false -debug: false log_level: info diff --git a/config/schema/rules.schema.yml b/config/schema/rules.schema.yml index 6a7b1482..5b26c4b1 100644 --- a/config/schema/rules.schema.yml +++ b/config/schema/rules.schema.yml @@ -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' diff --git a/src/Logger/RulesStubLogger.php b/src/Logger/RulesStubLogger.php new file mode 100644 index 00000000..943c5d3e --- /dev/null +++ b/src/Logger/RulesStubLogger.php @@ -0,0 +1,54 @@ +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; + } + +} diff --git a/src/RulesServiceProvider.php b/src/RulesServiceProvider.php index e9988e58..8d1eedf3 100644 --- a/src/RulesServiceProvider.php +++ b/src/RulesServiceProvider.php @@ -2,7 +2,7 @@ /** * @file - * Contains \Drupal\rules\WebProfiler\WebProfilerServiceProvider. + * Contains \Drupal\rules\RulesServiceProvider. */ namespace Drupal\rules; @@ -13,12 +13,10 @@ use Symfony\Component\DependencyInjection\Reference; /** - * Defines a service profiler for the web profiler module. + * Defines a service profiler for the WebProfiler module. */ class RulesServiceProvider extends ServiceProviderBase { - const CONFIG_PREFIX = 'webprofiler.config'; - /** * {@inheritdoc} */ @@ -46,7 +44,7 @@ public function register(ContainerBuilder $container) { */ protected function isRulesDebuggingEnabled() { $config_storage = BootstrapConfigStorageFactory::get(); - $config = $config_storage->read(static::CONFIG_PREFIX); + $config = $config_storage->read('webprofiler.config'); return !empty($config['active_toolbar_items']['rules']); } diff --git a/src/Tests/ConfigEntityTest.php b/src/Tests/ConfigEntityTest.php index 25cf97d3..15213df9 100644 --- a/src/Tests/ConfigEntityTest.php +++ b/src/Tests/ConfigEntityTest.php @@ -63,6 +63,9 @@ 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'); } /** @@ -86,6 +89,8 @@ 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'); } /** diff --git a/src/Tests/RulesDrupalTestBase.php b/src/Tests/RulesDrupalTestBase.php index fdab8121..576b9135 100644 --- a/src/Tests/RulesDrupalTestBase.php +++ b/src/Tests/RulesDrupalTestBase.php @@ -7,6 +7,7 @@ namespace Drupal\rules\Tests; +use Drupal\rules\Logger\RulesStubLogger; use Drupal\simpletest\KernelTestBase; /** @@ -48,6 +49,16 @@ abstract class RulesDrupalTestBase extends KernelTestBase { public function setUp() { parent::setUp(); + // 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'); $this->typedDataManager = $this->container->get('typed_data_manager'); @@ -69,4 +80,18 @@ 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->container->get('logger')->getLogs(); + $this->assertEqual($logs[$log_item_index]['message'], $message); + } + } diff --git a/src/Tests/RulesDrupalWebTestBase.php b/src/Tests/RulesDrupalWebTestBase.php index 5d54a5cd..5d16c084 100644 --- a/src/Tests/RulesDrupalWebTestBase.php +++ b/src/Tests/RulesDrupalWebTestBase.php @@ -10,18 +10,18 @@ use Drupal\simpletest\WebTestBase; /** - * Tests that the webprofile shows rules debug log and respects rules settings. + * Rules base web test. * - * @group block + * @group rules */ -class RulesDrupalWebTestBase extends WebTestBase { +abstract class RulesDrupalWebTestBase extends WebTestBase { /** * Modules to install. * * @var array */ - public static $modules = ['rules', 'rules_ui']; + public static $modules = ['rules']; /** * Authenticated user. @@ -45,7 +45,8 @@ protected function setUp() { $permissions = array('create page content', 'administer rules'); $this->user = $this->drupalCreateUser(); - $this->adminUser = $this->drupalCreateUser($permissions); + // @todo uncomment it when patch with permission comes. + // $this->adminUser = $this->drupalCreateUser($permissions); } } diff --git a/src/Tests/RulesEngineTest.php b/src/Tests/RulesEngineTest.php index 685da3a7..239c7694 100644 --- a/src/Tests/RulesEngineTest.php +++ b/src/Tests/RulesEngineTest.php @@ -54,6 +54,9 @@ 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'); } /** @@ -76,6 +79,8 @@ 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'); } /** @@ -93,6 +98,8 @@ public function testProvidedVariables() { $rule->addAction('rules_test_log'); $rule->execute(); + // Test that the action logged something. + $this->assertRulesLogEntryExists('action called'); } /** diff --git a/src/Tests/RulesWebProfilerTest.php b/src/Tests/RulesWebProfilerTest.php index c7b9ebc4..da5f2d83 100644 --- a/src/Tests/RulesWebProfilerTest.php +++ b/src/Tests/RulesWebProfilerTest.php @@ -9,17 +9,22 @@ /** * Class RulesWebProfilerTest - * @group Rules + * + * Tests integration Rules logging with WebProfiler module. + * + * @group rules */ class RulesWebProfilerTest extends RulesDrupalWebTestBase { /** - * Authenticated user with access to web profiler. + * Authenticated user with access to WebProfiler. * * @var \Drupal\user\Entity\User */ protected $webProfilerUser; + public static $modules = ['rules', 'webprofiler']; + /** * {@inheritdoc} */ @@ -31,7 +36,7 @@ public function setUp() { 'view webprofiler toolbar', )); - // Enables rules web debugging with web profiler. + // Enables rules web debugging with WebProfiler. $this->config('webprofiler.config')->set('active_toolbar_items.rules', 'rules'); $this->drupalLogin($this->webProfilerUser); } diff --git a/src/Webprofiler/DataCollector/RulesDataCollector.php b/src/Webprofiler/DataCollector/RulesDataCollector.php index bfcc907c..0afca5b5 100644 --- a/src/Webprofiler/DataCollector/RulesDataCollector.php +++ b/src/Webprofiler/DataCollector/RulesDataCollector.php @@ -10,6 +10,7 @@ use Drupal\webprofiler\DataCollector\DrupalDataCollectorTrait; use Drupal\webprofiler\DrupalDataCollectorInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; +use Psr\Log\LogLevel; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\DataCollector\DataCollector; @@ -23,7 +24,7 @@ class RulesDataCollector extends DataCollector implements DrupalDataCollectorInt use StringTranslationTrait, DrupalDataCollectorTrait; /** - * The rules logger wrapper. + * The Rules logger wrapper. * * @var RulesChannelLoggerWrapper */ @@ -33,7 +34,7 @@ class RulesDataCollector extends DataCollector implements DrupalDataCollectorInt * Constructs a RulesDataCollector object. * * @param RulesChannelLoggerWrapper $rulesLogger - * The rules logger channel. + * The Rules logger channel. */ public function __construct(RulesChannelLoggerWrapper $rulesLogger) { $this->rulesLogger = $rulesLogger; @@ -78,54 +79,50 @@ public function getLogsCount() { } /** - * Return amount of rules log entries with level higher then warning. + * Return amount of Rules log entries with level higher then warning. * * @return int - * Amount of error rules log entries. + * Amount of error Rules log entries. */ public function getErrorLogsCount() { - $amount = 0; - array_walk($this->data['logs'], function ($log) use (&$amount) { - if (in_array($log['level'], array('error', 'critical', 'alert', 'emergency'))) { - $amount++; - } + $logs = array_filter($this->data['logs'], function ($log) { + return in_array($log['level'], array( + LogLevel::ERROR, + LogLevel::CRITICAL, + LogLevel::ALERT, + LogLevel::EMERGENCY + )); }); - return $amount; + return count($logs); } /** - * Return amount of rules log entries with level notice or warning. + * Return amount of Rules log entries with level notice or warning. * * @return int - * Amount of error rules log entries. + * Amount of error Rules log entries. */ public function getNoticeLogsCount() { - $amount = 0; - array_walk($this->data['logs'], function ($log) use (&$amount) { - if (in_array($log['level'], array('warning', 'notice'))) { - $amount++; - } + $logs = array_filter($this->data['logs'], function ($log) { + return in_array($log['level'], array(LogLevel::WARNING, LogLevel::NOTICE)); }); - return $amount; + return count($logs); } /** - * Return amount of rules info log entries. + * Return amount of Rules info log entries. * * @return int - * Amount of error rules log entries. + * Amount of error Rules log entries. */ public function getInfoLogsCount() { - $amount = 0; - array_walk($this->data['logs'], function ($log) use (&$amount) { - if (in_array($log['level'], array('debug', 'info'))) { - $amount++; - } + $logs = array_filter($this->data['logs'], function ($log) { + return in_array($log['level'], array(LogLevel::DEBUG, LogLevel::INFO)); }); - return $amount; + return count($logs); } /** @@ -139,7 +136,7 @@ public function getPanel() { '#template' => '

{{ "Rules logs"|t }}

', ); - $cssHeader = array( + $css_header = array( 'level', 'message', 'passed_context', @@ -154,7 +151,7 @@ public function getPanel() { $build['logs_table'] = array( '#type' => 'table', '#rows' => $rows, - '#header' => $cssHeader, + '#header' => $css_header, '#sticky' => TRUE, ); diff --git a/tests/modules/rules_test/src/Plugin/Action/TestLogAction.php b/tests/modules/rules_test/src/Plugin/Action/TestLogAction.php index a14a7261..67352a81 100644 --- a/tests/modules/rules_test/src/Plugin/Action/TestLogAction.php +++ b/tests/modules/rules_test/src/Plugin/Action/TestLogAction.php @@ -10,6 +10,7 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\rules\Core\RulesActionBase; use Drupal\rules\Logger\RulesLoggerChannel; +use Psr\Log\LoggerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -27,7 +28,7 @@ class TestLogAction extends RulesActionBase implements ContainerFactoryPluginInt * * @var \Drupal\rules\Logger\RulesLoggerChannel */ - protected $logger; + protected $loggerChannel; /** * Constructs a TestLogAction object. @@ -38,12 +39,13 @@ class TestLogAction extends RulesActionBase implements ContainerFactoryPluginInt * The plugin ID for the plugin instance. * @param mixed $plugin_definition * The plugin implementation definition. - * @param \Drupal\rules\Logger\RulesLoggerChannel $logger + * @param \Drupal\rules\Logger\RulesLoggerChannel $loggerChannel * Rules logger object. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, RulesLoggerChannel $logger) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, RulesLoggerChannel $loggerChannel, LoggerInterface $logger) { parent::__construct($configuration, $plugin_id, $plugin_definition); - $this->logger = $logger; + $this->loggerChannel = $loggerChannel; + $this->loggerChannel->addLogger($logger); } /** @@ -54,7 +56,8 @@ public static function create(ContainerInterface $container, array $configuratio $configuration, $plugin_id, $plugin_definition, - $container->get('logger.channel.rules') + $container->get('logger.channel.rules'), + $container->get('logger') ); } @@ -62,7 +65,7 @@ public static function create(ContainerInterface $container, array $configuratio * {@inheritdoc} */ public function execute() { - $this->logger->info('action called'); + $this->loggerChannel->critical('action called'); } } diff --git a/tests/modules/rules_webprofiler_test/rules_webprofiler_test.info.yml b/tests/modules/rules_webprofiler_test/rules_webprofiler_test.info.yml index c28c0351..94c461c0 100644 --- a/tests/modules/rules_webprofiler_test/rules_webprofiler_test.info.yml +++ b/tests/modules/rules_webprofiler_test/rules_webprofiler_test.info.yml @@ -1,6 +1,6 @@ -name: Rules Webprofiler test +name: Rules Web Profiler test type: module -description: 'Support module for the Rules Webprofiler integration tests.' +description: 'Support module for the Rules Web Profiler integration tests.' package: Rules core: 8.x hidden: true diff --git a/tests/modules/rules_webprofiler_test/src/Plugin/Action/TestWebProfilerAction.php b/tests/modules/rules_webprofiler_test/src/Plugin/Action/TestWebProfilerAction.php index 4f583e04..3a3a8954 100644 --- a/tests/modules/rules_webprofiler_test/src/Plugin/Action/TestWebProfilerAction.php +++ b/tests/modules/rules_webprofiler_test/src/Plugin/Action/TestWebProfilerAction.php @@ -22,7 +22,7 @@ * label = @Translation("Test webprofiler action.") * ) */ -class TestLogAction extends RulesActionBase implements ContainerFactoryPluginInterface { +class TestWebProfilerAction extends RulesActionBase implements ContainerFactoryPluginInterface { /** * Rules logger instance. @@ -76,15 +76,15 @@ public static function create(ContainerInterface $container, array $configuratio */ public function execute() { // Get level passed via GET param. - $log = $this->request->get('log', 'enabled'); - if ($log) { - $level = $this->request->get('log-level', 'debug'); - $amount = $this->request->get('log-amount', 1); - $message = $this->request->get('log-message', 'debug message'); - $contexts = $this->request->get('log-contexts', ''); - for ($i = 0; $i < $amount; $i++) { - $this->logger->log($level, $message . '#' . $i, array_flip(explode('|', $contexts))); - } + if (!$log = $this->request->get('log', 'enabled')) { + return; + } + $level = $this->request->get('log-level', 'debug'); + $amount = $this->request->get('log-amount', 1); + $message = $this->request->get('log-message', 'debug message'); + $contexts = $this->request->get('log-contexts', ''); + for ($i = 0; $i < $amount; $i++) { + $this->logger->log($level, $message . '#' . $i, array_flip(explode('|', $contexts))); } } From 46ccda62bd04da21c79c692873ed102ae586304e Mon Sep 17 00:00:00 2001 From: Artyom Miroshnik Date: Sat, 18 Apr 2015 17:12:20 +0200 Subject: [PATCH 07/11] Fix webprofiler webtest --- src/RulesServiceProvider.php | 14 +--- src/Tests/RulesWebProfilerTest.php | 84 ++++++------------- src/Webprofiler/RulesChannelLoggerWrapper.php | 13 +-- .../rules_webprofiler_test.services.yml | 5 ++ .../RulesWebProfilerTestSubscriber.php | 30 +++++++ 5 files changed, 62 insertions(+), 84 deletions(-) create mode 100644 tests/modules/rules_webprofiler_test/rules_webprofiler_test.services.yml create mode 100644 tests/modules/rules_webprofiler_test/src/EventSubscriber/RulesWebProfilerTestSubscriber.php diff --git a/src/RulesServiceProvider.php b/src/RulesServiceProvider.php index 8d1eedf3..038b2847 100644 --- a/src/RulesServiceProvider.php +++ b/src/RulesServiceProvider.php @@ -21,7 +21,7 @@ class RulesServiceProvider extends ServiceProviderBase { * {@inheritdoc} */ public function register(ContainerBuilder $container) { - if (FALSE !== $container->hasDefinition('logger.channel.rules') && $this->isRulesDebuggingEnabled()) { + 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( @@ -36,16 +36,4 @@ public function register(ContainerBuilder $container) { } } - /** - * 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('webprofiler.config'); - return !empty($config['active_toolbar_items']['rules']); - } - } diff --git a/src/Tests/RulesWebProfilerTest.php b/src/Tests/RulesWebProfilerTest.php index da5f2d83..0c16a078 100644 --- a/src/Tests/RulesWebProfilerTest.php +++ b/src/Tests/RulesWebProfilerTest.php @@ -23,7 +23,7 @@ class RulesWebProfilerTest extends RulesDrupalWebTestBase { */ protected $webProfilerUser; - public static $modules = ['rules', 'webprofiler']; + public static $modules = ['rules', 'webprofiler', 'block']; /** * {@inheritdoc} @@ -31,80 +31,46 @@ class RulesWebProfilerTest extends RulesDrupalWebTestBase { 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->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('', 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('', 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->container->get('config.factory') + ->getEditable('webprofiler.config') + ->set('active_toolbar_items.rules', 'rules') + ->save(); - $this->drupalGet('', 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('', 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.'); + $this->drupalLogin($this->webProfilerUser); } /** * Goes to WebProfiler page using link from toolbar and check entries there. */ public function testWebProfilerPage() { - $this->drupalGet('', array( - 'log' => '1', - 'log-level' => 'info', - 'log-message' => 'info message', - 'log-amount' => 5, - )); - - $links = $this->xpath('//div[@class="sf-toolbar-icon"]/a[@title="Rules"]'); - + $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('info message', 'Rules log entry exists'); + $this->assertText('critical message', 'Rules log entry exists'); } } diff --git a/src/Webprofiler/RulesChannelLoggerWrapper.php b/src/Webprofiler/RulesChannelLoggerWrapper.php index 0a02e46c..5c13edc3 100644 --- a/src/Webprofiler/RulesChannelLoggerWrapper.php +++ b/src/Webprofiler/RulesChannelLoggerWrapper.php @@ -16,18 +16,7 @@ class RulesChannelLoggerWrapper extends RulesLoggerChannel { * * @var array */ - protected $logs = [ - [ - 'level' => 'info', - 'message' => 'Hello', - 'context' => [] - ], - [ - 'level' => 'critical', - 'message' => 'Take care!', - 'context' => [] - ] - ]; + protected $logs; /** * {@inheritdoc} diff --git a/tests/modules/rules_webprofiler_test/rules_webprofiler_test.services.yml b/tests/modules/rules_webprofiler_test/rules_webprofiler_test.services.yml new file mode 100644 index 00000000..79b4970a --- /dev/null +++ b/tests/modules/rules_webprofiler_test/rules_webprofiler_test.services.yml @@ -0,0 +1,5 @@ +services: + rules_webprofiler_test_event_subscriber: + class: Drupal\rules_webprofiler_test\EventSubscriber\RulesWebProfilerTestSubscriber + tags: + - {name: event_subscriber} \ No newline at end of file diff --git a/tests/modules/rules_webprofiler_test/src/EventSubscriber/RulesWebProfilerTestSubscriber.php b/tests/modules/rules_webprofiler_test/src/EventSubscriber/RulesWebProfilerTestSubscriber.php new file mode 100644 index 00000000..e9140952 --- /dev/null +++ b/tests/modules/rules_webprofiler_test/src/EventSubscriber/RulesWebProfilerTestSubscriber.php @@ -0,0 +1,30 @@ +createAction('rules_test_webprofiler_log'); + $action->execute(); + } + + /** + * {@inheritdoc} + */ + static function getSubscribedEvents() { + $events[KernelEvents::REQUEST][] = array('executeRulesAction'); + return $events; + } + +} From dcab46a4fe75fdcc28ff2014871d5ae4ab36de69 Mon Sep 17 00:00:00 2001 From: Artyom Miroshnik Date: Sat, 18 Apr 2015 17:31:57 +0200 Subject: [PATCH 08/11] Try to fix travic build with drush tricks --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 3a1668d2..89a5489e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -53,7 +53,8 @@ before_script: # Reference and enable rules in build site. - ln -s $TESTDIR modules/rules - drush --yes dl webprofiler - - drush --yes pm-enable simpletest rules webprofiler + - drush --yes pm-enable simpletest rules + - 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 From e0518adf7b493720b9d73148e6312e2a29d56c1f Mon Sep 17 00:00:00 2001 From: Artyom Miroshnik Date: Sat, 18 Apr 2015 17:59:00 +0200 Subject: [PATCH 09/11] Try to fix travic build with drush tricks #2 --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 89a5489e..6e6a0328 100644 --- a/.travis.yml +++ b/.travis.yml @@ -54,6 +54,7 @@ before_script: - 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 From ed002ffb51ac37f9f48566f6f4ebb1f223471edc Mon Sep 17 00:00:00 2001 From: Artyom Miroshnik Date: Sat, 18 Apr 2015 19:14:05 +0200 Subject: [PATCH 10/11] Lets do a trick --- .../DataCollector/RulesDataCollector.php | 161 ------------------ src/Webprofiler/RulesChannelLoggerWrapper.php | 44 ----- 2 files changed, 205 deletions(-) delete mode 100644 src/Webprofiler/DataCollector/RulesDataCollector.php delete mode 100644 src/Webprofiler/RulesChannelLoggerWrapper.php diff --git a/src/Webprofiler/DataCollector/RulesDataCollector.php b/src/Webprofiler/DataCollector/RulesDataCollector.php deleted file mode 100644 index 0afca5b5..00000000 --- a/src/Webprofiler/DataCollector/RulesDataCollector.php +++ /dev/null @@ -1,161 +0,0 @@ -rulesLogger = $rulesLogger; - } - - /** - * {@inheritdoc} - */ - public function collect(Request $request, Response $response, \Exception $exception = NULL) { - $this->data['logs'] = $this->rulesLogger->getLogs(); - } - - /** - * {@inheritdoc} - */ - public function getName() { - return 'rules'; - } - - /** - * {@inheritdoc} - */ - public function getTitle() { - return $this->t('Rules'); - } - - /** - * {@inheritdoc} - */ - public function getPanelSummary() { - return $this->t('Total rules log entries: @count', array('@count' => $this->getLogsCount())); - } - - /** - * Return amount of rules log entries. - * - * @return int - * Amount of rules log entries. - */ - public function getLogsCount() { - return count($this->data['logs']); - } - - /** - * Return amount of Rules log entries with level higher then warning. - * - * @return int - * Amount of error Rules log entries. - */ - public function getErrorLogsCount() { - $logs = array_filter($this->data['logs'], function ($log) { - return in_array($log['level'], array( - LogLevel::ERROR, - LogLevel::CRITICAL, - LogLevel::ALERT, - LogLevel::EMERGENCY - )); - }); - - return count($logs); - } - - /** - * Return amount of Rules log entries with level notice or warning. - * - * @return int - * Amount of error Rules log entries. - */ - public function getNoticeLogsCount() { - $logs = array_filter($this->data['logs'], function ($log) { - return in_array($log['level'], array(LogLevel::WARNING, LogLevel::NOTICE)); - }); - - return count($logs); - } - - /** - * Return amount of Rules info log entries. - * - * @return int - * Amount of error Rules log entries. - */ - public function getInfoLogsCount() { - $logs = array_filter($this->data['logs'], function ($log) { - return in_array($log['level'], array(LogLevel::DEBUG, LogLevel::INFO)); - }); - - return count($logs); - } - - /** - * {@inheritdoc} - */ - public function getPanel() { - $build = array(); - - $build['table_title'] = array( - '#type' => 'inline_template', - '#template' => '

{{ "Rules logs"|t }}

', - ); - - $css_header = array( - 'level', - 'message', - 'passed_context', - ); - - $rows = array_map(function ($log) { - return [ - $log['level'], $log['message'], implode(', ', array_keys($log['context'])) - ]; - }, $this->data['logs']); - - $build['logs_table'] = array( - '#type' => 'table', - '#rows' => $rows, - '#header' => $css_header, - '#sticky' => TRUE, - ); - - return $build; - } - -} diff --git a/src/Webprofiler/RulesChannelLoggerWrapper.php b/src/Webprofiler/RulesChannelLoggerWrapper.php deleted file mode 100644 index 5c13edc3..00000000 --- a/src/Webprofiler/RulesChannelLoggerWrapper.php +++ /dev/null @@ -1,44 +0,0 @@ -logs[] = [ - 'level' => $level, - 'message' => $message, - 'context' => $context, - ]; - } - - /** - * Return a list of rules log entries. - * - * @return array - * List of rules log entries. - */ - public function getLogs() { - return $this->logs; - } - -} From 1d3dbab381166f3210441396f2033b86a62b005a Mon Sep 17 00:00:00 2001 From: Artyom Miroshnik Date: Sat, 18 Apr 2015 19:15:05 +0200 Subject: [PATCH 11/11] And finish the trick with camelCase directory name --- .../DataCollector/RulesDataCollector.php | 161 ++++++++++++++++++ src/WebProfiler/RulesChannelLoggerWrapper.php | 44 +++++ 2 files changed, 205 insertions(+) create mode 100644 src/WebProfiler/DataCollector/RulesDataCollector.php create mode 100644 src/WebProfiler/RulesChannelLoggerWrapper.php diff --git a/src/WebProfiler/DataCollector/RulesDataCollector.php b/src/WebProfiler/DataCollector/RulesDataCollector.php new file mode 100644 index 00000000..0afca5b5 --- /dev/null +++ b/src/WebProfiler/DataCollector/RulesDataCollector.php @@ -0,0 +1,161 @@ +rulesLogger = $rulesLogger; + } + + /** + * {@inheritdoc} + */ + public function collect(Request $request, Response $response, \Exception $exception = NULL) { + $this->data['logs'] = $this->rulesLogger->getLogs(); + } + + /** + * {@inheritdoc} + */ + public function getName() { + return 'rules'; + } + + /** + * {@inheritdoc} + */ + public function getTitle() { + return $this->t('Rules'); + } + + /** + * {@inheritdoc} + */ + public function getPanelSummary() { + return $this->t('Total rules log entries: @count', array('@count' => $this->getLogsCount())); + } + + /** + * Return amount of rules log entries. + * + * @return int + * Amount of rules log entries. + */ + public function getLogsCount() { + return count($this->data['logs']); + } + + /** + * Return amount of Rules log entries with level higher then warning. + * + * @return int + * Amount of error Rules log entries. + */ + public function getErrorLogsCount() { + $logs = array_filter($this->data['logs'], function ($log) { + return in_array($log['level'], array( + LogLevel::ERROR, + LogLevel::CRITICAL, + LogLevel::ALERT, + LogLevel::EMERGENCY + )); + }); + + return count($logs); + } + + /** + * Return amount of Rules log entries with level notice or warning. + * + * @return int + * Amount of error Rules log entries. + */ + public function getNoticeLogsCount() { + $logs = array_filter($this->data['logs'], function ($log) { + return in_array($log['level'], array(LogLevel::WARNING, LogLevel::NOTICE)); + }); + + return count($logs); + } + + /** + * Return amount of Rules info log entries. + * + * @return int + * Amount of error Rules log entries. + */ + public function getInfoLogsCount() { + $logs = array_filter($this->data['logs'], function ($log) { + return in_array($log['level'], array(LogLevel::DEBUG, LogLevel::INFO)); + }); + + return count($logs); + } + + /** + * {@inheritdoc} + */ + public function getPanel() { + $build = array(); + + $build['table_title'] = array( + '#type' => 'inline_template', + '#template' => '

{{ "Rules logs"|t }}

', + ); + + $css_header = array( + 'level', + 'message', + 'passed_context', + ); + + $rows = array_map(function ($log) { + return [ + $log['level'], $log['message'], implode(', ', array_keys($log['context'])) + ]; + }, $this->data['logs']); + + $build['logs_table'] = array( + '#type' => 'table', + '#rows' => $rows, + '#header' => $css_header, + '#sticky' => TRUE, + ); + + return $build; + } + +} diff --git a/src/WebProfiler/RulesChannelLoggerWrapper.php b/src/WebProfiler/RulesChannelLoggerWrapper.php new file mode 100644 index 00000000..5c13edc3 --- /dev/null +++ b/src/WebProfiler/RulesChannelLoggerWrapper.php @@ -0,0 +1,44 @@ +logs[] = [ + 'level' => $level, + 'message' => $message, + 'context' => $context, + ]; + } + + /** + * Return a list of rules log entries. + * + * @return array + * List of rules log entries. + */ + public function getLogs() { + return $this->logs; + } + +}