From 95aa5dd1fd9ebb5341bd0f3ff6a7300a8fff5049 Mon Sep 17 00:00:00 2001 From: yanniboi Date: Wed, 11 May 2016 11:20:18 -0400 Subject: [PATCH 1/4] Issue #2702127 by yanniboi: Exposed UI to enable and disable operations for reaction rules and updated event subscriber to only trigger active rules. --- rules.routing.yml | 16 +++++++ src/Controller/RulesReactionController.php | 43 +++++++++++++++++++ src/Entity/ReactionRuleConfig.php | 2 + .../GenericEventSubscriber.php | 6 ++- src/Form/RulesComponentFormBase.php | 6 +++ 5 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 src/Controller/RulesReactionController.php diff --git a/rules.routing.yml b/rules.routing.yml index ce2b86ce..8e0b3739 100755 --- a/rules.routing.yml +++ b/rules.routing.yml @@ -49,6 +49,22 @@ entity.rules_reaction_rule.delete_form: requirements: _permission: 'administer rules+administer rules reactions' +entity.rules_reaction_rule.enable: + path: '/admin/config/workflow/rules/reactions/enable/{rules_reaction_rule}' + defaults: + _controller: '\Drupal\rules\Controller\RulesReactionController::performReactionRuleOperation' + op: 'enable' + requirements: + _permission: 'administer rules+administer rules reactions' + +entity.rules_reaction_rule.disable: + path: '/admin/config/workflow/rules/reactions/disable/{rules_reaction_rule}' + defaults: + _controller: '\Drupal\rules\Controller\RulesReactionController::performReactionRuleOperation' + op: 'disable' + requirements: + _permission: 'administer rules+administer rules reactions' + ### Rules Components entity.rules_component.collection: path: '/admin/config/workflow/rules/components' diff --git a/src/Controller/RulesReactionController.php b/src/Controller/RulesReactionController.php new file mode 100644 index 00000000..c158ae5b --- /dev/null +++ b/src/Controller/RulesReactionController.php @@ -0,0 +1,43 @@ +$op()->save(); + + if ($op == 'enable') { + drupal_set_message($this->t('The %label rule has been enabled.', ['%label' => $rules_reaction_rule->label()])); + } + elseif ($op == 'disable') { + drupal_set_message($this->t('The %label rule has been disabled.', ['%label' => $rules_reaction_rule->label()])); + } + + return $this->redirect('entity.rules_reaction_rule.collection'); + } + +} diff --git a/src/Entity/ReactionRuleConfig.php b/src/Entity/ReactionRuleConfig.php index 2047564f..dfbf21d8 100644 --- a/src/Entity/ReactionRuleConfig.php +++ b/src/Entity/ReactionRuleConfig.php @@ -43,6 +43,8 @@ * "collection" = "/admin/config/workflow/rules", * "edit-form" = "/admin/config/workflow/rules/reactions/edit/{rules_reaction_rule}", * "delete-form" = "/admin/config/workflow/rules/reactions/delete/{rules_reaction_rule}", + * "enable" = "/admin/config/workflow/rules/reactions/enable/{rules_reaction_rule}", + * "disable" = "/admin/config/workflow/rules/reactions/disable/{rules_reaction_rule}", * "break-lock-form" = "/admin/config/workflow/rules/reactions/edit/break-lock/{rules_reaction_rule}" * } * ) diff --git a/src/EventSubscriber/GenericEventSubscriber.php b/src/EventSubscriber/GenericEventSubscriber.php index 290b3a9d..2476cb30 100644 --- a/src/EventSubscriber/GenericEventSubscriber.php +++ b/src/EventSubscriber/GenericEventSubscriber.php @@ -119,8 +119,10 @@ public function onRulesEvent(Event $event, $event_name) { // variables added by one rule are not interfering with the variables of // another rule. foreach ($triggered_events as $triggered_event) { - // @todo Only load active reaction rules here. - $configs = $storage->loadByProperties(['events.*.event_name' => $triggered_event]); + $configs = $storage->loadByProperties([ + 'events.*.event_name' => $triggered_event, + 'status' => 1, + ]); // Loop over all rules and execute them. foreach ($configs as $config) { diff --git a/src/Form/RulesComponentFormBase.php b/src/Form/RulesComponentFormBase.php index 590ea218..18c0210b 100644 --- a/src/Form/RulesComponentFormBase.php +++ b/src/Form/RulesComponentFormBase.php @@ -82,6 +82,12 @@ public function form(array $form, FormStateInterface $form_state) { '#title' => $this->t('Description'), ]; + $form['settings']['status'] = [ + '#type' => 'checkbox', + '#default_value' => $this->entity->status(), + '#title' => $this->t('Active'), + ]; + return parent::form($form, $form_state); } From 799bed24ae83a0674b44aad0d2db540de7f3ed8f Mon Sep 17 00:00:00 2001 From: yanniboi Date: Wed, 11 May 2016 11:42:09 -0400 Subject: [PATCH 2/4] Issue #2702127 by yanniboi: Fixed codesniffer errors. --- src/Controller/RulesReactionController.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Controller/RulesReactionController.php b/src/Controller/RulesReactionController.php index c158ae5b..265fc27c 100644 --- a/src/Controller/RulesReactionController.php +++ b/src/Controller/RulesReactionController.php @@ -1,16 +1,10 @@ Date: Wed, 11 May 2016 17:50:13 -0400 Subject: [PATCH 3/4] Issue #2702127 by yanniboi: Added tests for enabling/disabling rules. --- .../src/Functional/ConfigureAndExecuteTest.php | 10 ++++++++++ tests/src/Functional/UiPageTest.php | 17 +++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/tests/src/Functional/ConfigureAndExecuteTest.php b/tests/src/Functional/ConfigureAndExecuteTest.php index 2a99e062..89e8b203 100644 --- a/tests/src/Functional/ConfigureAndExecuteTest.php +++ b/tests/src/Functional/ConfigureAndExecuteTest.php @@ -92,6 +92,16 @@ public function testConfigureAndExecute() { $this->pressButton('Save'); $this->assertSession()->pageTextContains('Title matched "Test title"!'); + + // Disable rule and make sure it doesn't get triggered. + $this->drupalGet('admin/config/workflow/rules'); + $this->clickLink('Disable'); + + $this->drupalGet('node/add/article'); + $this->fillField('Title', 'Test title'); + $this->pressButton('Save'); + + $this->assertSession()->pageTextNotContains('Title matched "Test title"!'); } } diff --git a/tests/src/Functional/UiPageTest.php b/tests/src/Functional/UiPageTest.php index cde6852b..8244401a 100644 --- a/tests/src/Functional/UiPageTest.php +++ b/tests/src/Functional/UiPageTest.php @@ -95,6 +95,23 @@ public function testCancelExpressionInRule() { $this->assertEquals(1, preg_match('#/admin/config/workflow/rules$#', $this->getSession()->getCurrentUrl())); } + /** + * Tests that enabling and disabling a rule works. + */ + public function testRuleStatusOperations() { + // Setup an active rule. + $this->testCreateReactionRule(); + $this->drupalGet('admin/config/workflow/rules'); + + // Test disabling. + $this->clickLink('Disable'); + $this->assertSession()->pageTextContains('The Test rule rule has been disabled.'); + + // Test enabling. + $this->clickLink('Enable'); + $this->assertSession()->pageTextContains('The Test rule rule has been enabled.'); + } + /** * Tests that deleting an expression from a rule works. */ From f9d9419dace6b6e1bd368927c4ea19f405ef197e Mon Sep 17 00:00:00 2001 From: yanniboi Date: Fri, 10 Jun 2016 11:40:09 +0100 Subject: [PATCH 4/4] Issue #2702127 by yanniboi: Added CSRF tokens to enable and disable rule routes. --- rules.routing.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rules.routing.yml b/rules.routing.yml index 8e0b3739..8bff29fe 100755 --- a/rules.routing.yml +++ b/rules.routing.yml @@ -56,6 +56,7 @@ entity.rules_reaction_rule.enable: op: 'enable' requirements: _permission: 'administer rules+administer rules reactions' + _csrf_token: 'TRUE' entity.rules_reaction_rule.disable: path: '/admin/config/workflow/rules/reactions/disable/{rules_reaction_rule}' @@ -64,6 +65,7 @@ entity.rules_reaction_rule.disable: op: 'disable' requirements: _permission: 'administer rules+administer rules reactions' + _csrf_token: 'TRUE' ### Rules Components entity.rules_component.collection: