WebformSettingsLimitsTest.php in Webform 8.5
File
tests/src/Functional/Settings/WebformSettingsLimitsTest.php
View source
<?php
namespace Drupal\Tests\webform\Functional\Settings;
use Drupal\user\Entity\Role;
use Drupal\webform\Entity\Webform;
use Drupal\Tests\webform\Functional\WebformBrowserTestBase;
class WebformSettingsLimitsTest extends WebformBrowserTestBase {
public static $modules = [
'webform',
'block',
];
protected static $testWebforms = [
'test_form_limit',
'test_form_limit_wait',
];
protected function setUp() {
parent::setUp();
$this
->placeWebformBlocks('webform_test_block_submission_limit');
}
public function testFormLimits() {
$own_submission_user = $this
->drupalCreateUser([
'view own webform submission',
'edit own webform submission',
'delete own webform submission',
'access webform submission user',
]);
$webform_limit = Webform::load('test_form_limit');
$this
->drupalGet('/webform/test_form_limit');
$this
->assertFieldByName('op', 'Submit');
$this
->assertRaw('0 user submission(s)');
$this
->assertRaw('1 user limit (every minute)');
$this
->assertRaw('0 webform submission(s)');
$this
->assertRaw('4 webform limit (every minute)');
$this
->assertRaw('limit:webform: 4');
$this
->assertRaw('remaining:webform: 4');
$this
->assertRaw('limit:user: 1');
$this
->assertRaw('remaining:user: 1');
$this
->drupalLogin($own_submission_user);
$this
->postSubmission($webform_limit, [], 'Save Draft');
$this
->drupalGet('/webform/test_form_limit');
$this
->assertFieldByName('op', 'Submit');
$this
->assertRaw('A partially-completed form was found. Please complete the remaining portions.');
$this
->assertNoRaw('You are only allowed to have 1 submission for this webform.');
$this
->assertRaw('0 user submission(s)');
$this
->assertRaw('0 webform submission(s)');
$sid = $this
->postSubmission($webform_limit);
$this
->drupalGet('/webform/test_form_limit');
$this
->assertNoFieldByName('op', 'Submit');
$this
->assertRaw('You are only allowed to have 1 submission for this webform.');
$this
->assertRaw('1 user submission(s)');
$this
->assertRaw('1 webform submission(s)');
$this
->drupalGet("admin/structure/webform/manage/test_form_limit/submission/{$sid}/edit");
$this
->assertNoRaw('You are only allowed to have 1 submission for this webform.');
$this
->assertFieldByName('op', 'Save');
$this
->drupalLogout();
$this
->drupalLogin($this->rootUser);
$sid = $this
->postSubmission($webform_limit);
$this
->drupalGet("admin/structure/webform/manage/test_form_limit/submission/{$sid}/edit");
$this
->assertFieldByName('op', 'Save');
$this
->assertNoRaw('No more submissions are permitted.');
$this
->assertRaw('remaining:webform: 2');
$this
->assertRaw('remaining:user: 0');
$this
->assertRaw('1 user submission(s)');
$this
->assertRaw('2 webform submission(s)');
$this
->drupalLogout();
$role = Role::load('anonymous');
$role
->grantPermission('edit own webform submission');
$role
->save();
$this
->drupalGet('/webform/test_form_limit');
$this
->assertFieldByName('op', 'Submit');
$this
->assertNoRaw('You are only allowed to have 1 submission for this webform.');
$sid = $this
->postSubmission($webform_limit);
$this
->assertRaw('1 user submission(s)');
$this
->assertRaw('3 webform submission(s)');
$this
->drupalGet('/webform/test_form_limit');
$this
->assertNoFieldByName('op', 'Submit');
$this
->assertRaw('You are only allowed to have 1 submission for this webform.');
$this
->drupalGet("admin/structure/webform/manage/test_form_limit/submission/{$sid}/edit");
$this
->assertNoRaw('You are only allowed to have 1 submission for this webform.');
$this
->assertFieldByName('op', 'Save');
$this
->drupalLogin($this->rootUser);
$this
->postSubmission($webform_limit);
$this
->drupalLogout();
$this
->drupalGet('/webform/test_form_limit');
$this
->assertNoFieldByName('op', 'Submit');
$this
->assertRaw('Only 4 submissions are allowed.');
$this
->assertNoRaw('You are only allowed to have 1 submission for this webform.');
$this
->assertRaw('0 user submission(s)');
$this
->assertRaw('4 webform submission(s)');
$this
->drupalLogin($this->rootUser);
$this
->drupalGet('/webform/test_form_limit');
$this
->assertFieldByName('op', 'Submit');
$this
->assertRaw('Only 4 submissions are allowed.');
$this
->assertRaw('Only submission administrators are allowed to access this webform and create new submissions.');
$this
->assertRaw('2 user submission(s)');
$this
->assertRaw('4 webform submission(s)');
\Drupal::database()
->query('UPDATE {webform_submission} SET completed = :completed', [
':completed' => strtotime('-1 minute'),
]);
$this
->drupalGet('/webform/test_form_limit');
$this
->assertRaw('0 user submission(s)');
$this
->assertRaw('0 webform submission(s)');
$webform_limit_wait = Webform::load('test_form_limit_wait');
$this
->postSubmission($webform_limit_wait);
$this
->drupalGet('/webform/test_form_limit_wait');
$this
->assertPattern('/webform_submission:interval:user:wait => \\d+ seconds/');
}
}