View source
<?php
namespace Drupal\Tests\webform_group\Functional;
use Drupal\webform\Entity\WebformSubmission;
class WebformGroupTokensTest extends WebformGroupBrowserTestBase {
public function testWebformAccessTokens() {
$group = $this
->createGroup([
'type' => 'default',
]);
$node = $this
->createWebformNode('contact');
$group
->addContent($node, 'group_node:webform');
$member_user = $this
->createUser([], 'member', FALSE, [
'mail' => 'member@example.com',
]);
$group
->addMember($member_user);
$custom_user = $this
->createUser([], 'custom', FALSE, [
'mail' => 'custom@example.com',
]);
$group
->addMember($custom_user, [
'group_roles' => [
'default-custom',
],
]);
$owner_user = $group
->getOwner();
$group
->save();
$edit = [
'name' => 'name',
'email' => 'name@example.com',
'subject' => 'subject',
'message' => 'message',
];
$sid = $this
->postNodeSubmission($node, $edit);
$webform_submission = WebformSubmission::load($sid);
$token_manager = \Drupal::service('webform.token_manager');
$token_data['webform_group'] = $webform_submission;
\Drupal::configFactory()
->getEditable('webform_group.settings')
->set('mail.group_roles', [
'member',
'custom',
'outsider',
])
->set('mail.group_owner', TRUE)
->save();
\Drupal::token()
->resetInfo();
$result = $token_manager
->replace('[webform_group:role:member]', $webform_submission, $token_data);
$this
->assertEqual(implode(',', [
$owner_user
->getEmail(),
$member_user
->getEmail(),
$custom_user
->getEmail(),
]), $result);
$result = $token_manager
->replace('[webform_group:role:custom]', $webform_submission, $token_data);
$this
->assertEqual(implode(',', [
$custom_user
->getEmail(),
]), $result);
$result = $token_manager
->replace('[webform_group:role:outsider]', $webform_submission, $token_data);
$this
->assertEqual('', $result);
$result = $token_manager
->replace('[webform_group:owner:mail]', $webform_submission, $token_data);
$this
->assertEqual($owner_user
->getEmail(), $result);
}
}