Unverified Commit 8d8a9da9 authored by git's avatar git Committed by Jonathan Hedstrom
Browse files

Issue #2885074 by sukanya.ramakrishnan: Multiple grouping methods do not...

Issue #2885074 by sukanya.ramakrishnan: Multiple grouping methods do not identify recipients in a category correctly
parent 787571b8
Loading
Loading
Loading
Loading
+19 −0
Changes for src/Form/CategoryForm.php: 19 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -91,6 +91,25 @@ class CategoryForm extends EntityForm {
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $recipients_chosen = FALSE;
    $mass_contact_category = $this->entity;
    foreach ($this->groupingMethodManager->getDefinitions() as $definition) {
      if (!$plugin = $mass_contact_category->getGroupingCategories($definition['id'])) {
        $plugin = $this->groupingMethodManager->createInstance($definition['id'], []);
      }
      if ($form['recipients'][$plugin->getPluginId()]['categories']['#value']) {
        $recipients_chosen = TRUE;
      }
    }
    if (!($recipients_chosen)) {
      $form_state->setErrorByName('', $this->t('At least one recipient is required.'), 'error');
    }
  }

  /**
   * {@inheritdoc}
   */
+7 −1
Changes for src/MassContact.php: 7 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -231,11 +231,17 @@ class MassContact implements MassContactInterface {
    $recipients = [];
    if (!empty($categories)) {
      foreach ($categories as $category) {
        $category_recipients = [];
        foreach ($category->getRecipients() as $plugin_id => $config) {
          $grouping = $category->getGroupingCategories($plugin_id);
          $recipients += $grouping->getRecipients($config['categories']);
          if (!empty($config['categories'])) {
            // Only If values were chosen for this grouping in this category,
            // we gather recipients.
            $category_recipients[$plugin_id] = $grouping->getRecipients($config['categories']);
          }
        }
        $recipients += count($category_recipients) > 1 ? call_user_func_array('array_intersect', $category_recipients) : reset($category_recipients);
      }

      // Filter out users that have opted out only if sender has chosen to
      // respect opt outs.
+1 −1
Changes for src/Plugin/MassContact/GroupingMethod/Role.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -146,7 +146,7 @@ class Role extends GroupingBase implements ContainerFactoryPluginInterface {
        'OR' => $this->t('Any'),
        'AND' => $this->t('All'),
      ],
      '#description' => $this->t('Choose <em>any</em> to return recipients with any of the categories, choose <em>all</em> to return recipients matching all of the categories.'),
      '#description' => $this->t('Choose <em>any</em> to return recipients with any of the roles, choose <em>all</em> to return recipients with all of the roles.'),
      '#default_value' => $this->configuration['conjunction'],
    ];
  }
+20 −4
Changes for tests/src/Functional/Form/CategoryFormTest.php: 20 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -28,12 +28,15 @@ class CategoryFormTest extends MassContactTestBase {
    $this->assertSession()->addressEquals('/admin/config/mass-contact');

    $this->assertSession()->linkExists(t('Categories'));
    $this->assertSession()->linkByHrefExists('/admin/config/mass-contact/settings');
    $this->assertSession()
      ->linkByHrefExists('/admin/config/mass-contact/settings');
    $this->clickLink(t('Categories'));
    $this->assertSession()->addressEquals('/admin/config/mass-contact/category');
    $this->assertSession()
      ->addressEquals('/admin/config/mass-contact/category');
    $this->assertSession()->linkExists(t('Add category'));
    $this->clickLink(t('Add category'));
    $this->assertSession()->addressEquals('/admin/config/mass-contact/category/add');
    $this->assertSession()
      ->addressEquals('/admin/config/mass-contact/category/add');

    // Create a category via the UI.
    $edit = [
@@ -67,7 +70,9 @@ class CategoryFormTest extends MassContactTestBase {
    $edit['recipients[role][conjunction]'] = 'OR';
    $this->drupalPostForm(NULL, $edit, t('Save'));

    \Drupal::entityTypeManager()->getStorage('mass_contact_category')->resetCache();
    \Drupal::entityTypeManager()
      ->getStorage('mass_contact_category')
      ->resetCache();
    /** @var \Drupal\mass_contact\Entity\MassContactCategoryInterface $category */
    $category = MassContactCategory::load($edit['id']);
    $this->assertEquals($edit['label'], $category->label());
@@ -81,6 +86,17 @@ class CategoryFormTest extends MassContactTestBase {
      'conjunction' => 'OR',
    ];
    $this->assertEquals($expected, $category->getRecipients()['role']);

    // Test that when no recipients are selected, a validation error is thrown.
    $this->drupalGet('/admin/config/mass-contact/category/add');
    // Create a category via the UI.
    $edit = [
      'id' => Unicode::strtolower($this->randomMachineName()),
      'label' => $this->randomString(),
    ];
    $this->drupalPostForm(NULL, $edit, t('Save'));
    $this->assertSession()
      ->pageTextContains('At least one recipient is required.');
  }

}
+1 −0
Changes for tests/src/Kernel/MassContactTest.php: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\mass_contact\Kernel;

use Drupal\mass_contact\Entity\MassContactMessage;
use Drupal\simpletest\UserCreationTrait;