diff --git a/src/Form/CategoryForm.php b/src/Form/CategoryForm.php index d9effdc..78d6ffc 100644 --- a/src/Form/CategoryForm.php +++ b/src/Form/CategoryForm.php @@ -106,7 +106,7 @@ class CategoryForm extends EntityForm { } } if (!($recipients_chosen)) { - $form_state->setErrorByName('', $this->t('Atleast one recipient is required.'), 'error'); + $form_state->setErrorByName('', $this->t('At least one recipient is required.'), 'error'); } } diff --git a/src/MassContact.php b/src/MassContact.php index 86dfe9f..641dcd8 100644 --- a/src/MassContact.php +++ b/src/MassContact.php @@ -234,15 +234,9 @@ class MassContact implements MassContactInterface { $category_recipients = []; foreach ($category->getRecipients() as $plugin_id => $config) { $grouping = $category->getGroupingCategories($plugin_id); - if (empty($config['categories'])) { - // If no values were chosen for this grouping in this category, - // some other grouping should have been chosen to determine - // recipients. We choose users that satisfy requirements across - // groupings for a category, so return all the users so that the - // other grouping method(s) chosen will determine the recipients. - $category_recipients[$plugin_id] = $this->getAllActiveUsers(); - } - else { + 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']); } } @@ -291,13 +285,4 @@ class MassContact implements MassContactInterface { } } - /** - * Gets all active users on the site. - */ - public function getAllActiveUsers() { - $query = $this->entityTypeManager->getStorage('user')->getQuery(); - $query->condition('status', 1); - return $query->execute(); - } - } diff --git a/tests/src/Functional/Form/CategoryFormTest.php b/tests/src/Functional/Form/CategoryFormTest.php index ff61732..3535188 100644 --- a/tests/src/Functional/Form/CategoryFormTest.php +++ b/tests/src/Functional/Form/CategoryFormTest.php @@ -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.'); } -} +} \ No newline at end of file