WebformSubmissionTokenOperationsTest.php in Webform 8.5
File
tests/src/Functional/WebformSubmissionTokenOperationsTest.php
View source
<?php
namespace Drupal\Tests\webform\Functional;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
class WebformSubmissionTokenOperationsTest extends WebformBrowserTestBase {
public static $modules = [
'token',
];
protected static $testWebforms = [
'test_token_operations',
];
public function testTokenOperationsTest() {
$normal_user = $this
->drupalCreateUser();
$webform = Webform::load('test_token_operations');
$token_operations = [
'view',
'update',
'delete',
];
$sid = $this
->postSubmission($webform, [
'textfield' => 'test',
]);
$webform_submission = WebformSubmission::load($sid);
foreach ($token_operations as $token_operation) {
$token_url = $webform_submission
->getTokenUrl($token_operation);
$link_label = $token_url
->setAbsolute(FALSE)
->toString();
$link_url = $token_url
->setAbsolute(TRUE)
->toString();
$this
->assertRaw('<a href="' . $link_url . '">' . $link_label . '</a>');
}
$this
->drupalLogin($normal_user);
$this
->drupalGet($webform_submission
->getTokenUrl('view'));
$this
->assertResponse(200);
$this
->assertRaw('Submission information');
$this
->assertRaw('<label>textfield</label>');
$this
->assertLinkByHref($webform_submission
->getTokenUrl('delete')
->setAbsolute(FALSE)
->toString());
$webform
->setSetting('token_view', FALSE)
->save();
$this
->drupalLogin($normal_user);
$this
->drupalGet($webform_submission
->getTokenUrl('view'));
$this
->assertResponse(403);
$this
->assertNoRaw('Submission information');
$this
->assertNoRaw('<label>textfield</label>');
$this
->drupalLogin($normal_user);
$this
->drupalGet($webform_submission
->getTokenUrl('update'));
$this
->assertResponse(200);
$this
->assertRaw('Submission information');
$this
->assertFieldByName('textfield', $webform_submission
->getElementData('textfield'));
$webform
->setSetting('token_update', FALSE)
->save();
$this
->drupalLogin($normal_user);
$this
->drupalGet($webform_submission
->getTokenUrl('update'));
$this
->assertResponse(200);
$this
->assertNoRaw('Submission information');
$this
->assertNoFieldByName('textfield', $webform_submission
->getElementData('textfield'));
$this
->drupalLogin($normal_user);
$this
->drupalGet($webform_submission
->getTokenUrl('delete'));
$this
->assertResponse(200);
$webform
->setSetting('token_delete', FALSE)
->save();
$this
->drupalLogin($normal_user);
$this
->drupalGet($webform_submission
->getTokenUrl('delete'));
$this
->assertResponse(403);
$this
->drupalLogout();
$access = $webform
->getAccessRules();
$access['create']['roles'] = [
'authenticated',
];
$webform
->setAccessRules($access);
$webform
->setSetting('token_view', TRUE)
->setSetting('token_update', TRUE)
->setSetting('token_delete', TRUE)
->save();
$this
->drupalGet('/webform/test_token_operations');
$this
->assertResponse(403);
foreach ($token_operations as $token_operation) {
$this
->drupalGet($webform_submission
->getTokenUrl($token_operation));
$this
->assertResponse(200);
}
}
}