View source
<?php
namespace Drupal\Tests\mass_contact\Kernel;
use Drupal\mass_contact\MassContactInterface;
use Drupal\simpletest\UserCreationTrait;
class OptOutTest extends MassContactTestBase {
use CategoryCreationTrait;
use UserCreationTrait;
public static $modules = [
'field',
'options',
];
protected $categories;
protected $recipients;
protected $globalOptOut;
protected $categoryOptOut1;
protected $categoryOptOut2;
protected function setUp() {
parent::setUp();
$this
->installSchema('system', [
'sequences',
]);
$this
->installEntitySchema('user');
$this
->installConfig('mass_contact');
foreach (range(1, 3) as $i) {
$this->categories[$i] = $this
->createCategory();
}
foreach (range(1, 20) as $i) {
$account = $this
->createUser();
if (in_array($i, [
2,
3,
5,
])) {
$account->{MassContactInterface::OPT_OUT_FIELD_ID} = 1;
$account
->save();
$this->globalOptOut[$account
->id()] = $account;
}
elseif (in_array($i, [
4,
19,
])) {
$account->{MassContactInterface::OPT_OUT_FIELD_ID} = $this->categories[1]
->id();
$account
->save();
$this->categoryOptOut1[$account
->id()] = $account;
}
elseif (in_array($i, [
8,
16,
])) {
$account->{MassContactInterface::OPT_OUT_FIELD_ID} = $this->categories[2]
->id();
$account
->save();
$this->categoryOptOut2[$account
->id()] = $account;
}
else {
$this->recipients[$account
->id()] = $account;
}
}
}
public function testOptOutDisabled() {
$this
->config('mass_contact.settings')
->set('optout_enabled', MassContactInterface::OPT_OUT_DISABLED)
->save();
$opt_out = \Drupal::service('mass_contact.opt_out');
$this
->assertEmpty($opt_out
->getOptOutAccounts($this->categories));
}
public function testOptOutGlobal() {
$this
->config('mass_contact.settings')
->set('optout_enabled', MassContactInterface::OPT_OUT_GLOBAL)
->save();
$opt_out = \Drupal::service('mass_contact.opt_out');
$expected = array_merge(array_keys($this->globalOptOut), array_keys($this->categoryOptOut1), array_keys($this->categoryOptOut2));
$expected = array_combine($expected, $expected);
$this
->assertEquals($expected, $opt_out
->getOptOutAccounts($this->categories));
}
public function testOptOutCategory() {
$this
->config('mass_contact.settings')
->set('optout_enabled', MassContactInterface::OPT_OUT_CATEGORY)
->save();
$opt_out = \Drupal::service('mass_contact.opt_out');
$expected = array_merge(array_keys($this->globalOptOut), array_keys($this->categoryOptOut1));
$expected = array_combine($expected, $expected);
$this
->assertEquals($expected, $opt_out
->getOptOutAccounts([
$this->categories[1],
$this->categories[3],
]));
}
}