SmsBlastBrowserTest.php in SMS Framework 8
File
modules/sms_blast/tests/src/Functional/SmsBlastBrowserTest.php
View source
<?php
namespace Drupal\Tests\sms_blast\Functional;
use Drupal\Tests\sms\Functional\SmsFrameworkBrowserTestBase;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\Entity\FieldConfig;
use Drupal\Component\Utility\Unicode;
use Drupal\sms\Entity\PhoneNumberSettings;
class SmsBlastBrowserTest extends SmsFrameworkBrowserTestBase {
public static $modules = [
'sms',
'user',
'sms_blast',
];
protected $phoneNumberSettings;
protected function setUp() {
parent::setUp();
$this
->drupalLogin($this
->drupalCreateUser([
'Send SMS Blast',
]));
$this->gateway = $this
->createMemoryGateway([
'skip_queue' => TRUE,
]);
$this
->setFallbackGateway($this->gateway);
$phone_field = FieldStorageConfig::create([
'entity_type' => 'user',
'field_name' => Unicode::strtolower($this
->randomMachineName()),
'type' => 'telephone',
]);
$phone_field
->save();
FieldConfig::create([
'entity_type' => 'user',
'bundle' => 'user',
'field_name' => $phone_field
->getName(),
])
->save();
$this->phoneNumberSettings = PhoneNumberSettings::create();
$this->phoneNumberSettings
->setPhoneNumberEntityTypeId('user')
->setPhoneNumberBundle('user')
->setFieldName('phone_number', $phone_field
->getName())
->setVerificationMessage($this
->randomString())
->save();
}
public function testSendBlast() {
$phone_numbers = $this
->randomPhoneNumbers();
$entities = [];
for ($i = 0; $i < 6; $i++) {
$user = $this
->createEntityWithPhoneNumber($this->phoneNumberSettings, $phone_numbers);
$user
->activate()
->save();
$entities[] = $user;
}
$numbers = range(0, count($entities) - 1);
shuffle($numbers);
foreach (array_slice($numbers, 0, 3) as $i) {
$this
->verifyPhoneNumber($entities[$i], $phone_numbers[0]);
}
$this
->resetTestMessages();
$edit['message'] = $this
->randomString();
$this
->drupalPostForm('sms_blast', $edit, t('Send'));
$this
->assertResponse(200);
$this
->assertText('Message sent to 3 users.');
$this
->assertEqual(3, count($this
->getTestMessages($this->gateway)), 'Sent three messages.');
}
}