View source
<?php
namespace Drupal\Tests\webform\Functional\Settings;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
use Drupal\Tests\webform\Functional\WebformBrowserTestBase;
class WebformSettingsPrepopulateTest extends WebformBrowserTestBase {
public static $modules = [
'block',
'webform',
];
protected static $testWebforms = [
'test_form_prepopulate',
];
protected function setUp() {
parent::setUp();
$this
->placeBlocks();
}
public function testPrepopulate() {
$webform_prepopulate = Webform::load('test_form_prepopulate');
$this
->drupalGet('/webform/test_form_prepopulate', [
'query' => [
'name' => 'John',
'colors' => [
'red',
'white',
],
],
]);
$this
->assertFieldByName('name', 'John');
$this
->assertFieldChecked('edit-colors-red');
$this
->assertFieldChecked('edit-colors-white');
$this
->assertNoFieldChecked('edit-colors-blue');
$this
->drupalGet('/webform/test_form_prepopulate', [
'query' => [
'name' => 'John',
'colors' => 'red',
],
]);
$this
->assertFieldByName('name', 'John');
$this
->assertFieldChecked('edit-colors-red');
$this
->assertNoFieldChecked('edit-colors-white');
$this
->assertNoFieldChecked('edit-colors-blue');
$webform_prepopulate
->setSetting('form_prepopulate', FALSE);
$webform_prepopulate
->save();
$this
->drupalGet('/webform/test_form_prepopulate', [
'query' => [
'name' => 'John',
],
]);
$this
->assertFieldByName('name', '');
$this
->drupalPostForm('/webform/test_form_prepopulate', [], 'Submit', [
'query' => [
'source_entity_type' => 'webform',
'source_entity_id' => 'contact',
],
]);
$sid = $this
->getLastSubmissionId($webform_prepopulate);
$webform_submission = WebformSubmission::load($sid);
$this
->assertNotNull($webform_submission
->getSourceEntity());
if ($webform_submission
->getSourceEntity()) {
$this
->assertEqual($webform_submission
->getSourceEntity()
->getEntityTypeId(), 'webform');
$this
->assertEqual($webform_submission
->getSourceEntity()
->id(), 'contact');
}
$webform_prepopulate
->setSetting('form_prepopulate_source_entity', FALSE);
$webform_prepopulate
->save();
$this
->drupalPostForm('/webform/test_form_prepopulate', [], 'Submit', [
'query' => [
'source_entity_type' => 'webform',
'source_entity_id' => 'contact',
],
]);
$sid = $this
->getLastSubmissionId($webform_prepopulate);
$webform_submission = WebformSubmission::load($sid);
$this
->assertNull($webform_submission
->getSourceEntity());
$webform_prepopulate
->setSetting('form_prepopulate_source_entity', TRUE);
$webform_prepopulate
->setSetting('form_prepopulate_source_entity_required', TRUE);
$webform_prepopulate
->save();
$this
->drupalGet('/webform/test_form_prepopulate');
$this
->assertRaw('This webform is not available. Please contact the site administrator.');
$this
->drupalGet('/webform/test_form_prepopulate', [
'query' => [
'source_entity_type' => 'webform',
'source_entity_id' => 'DOES_NOT_EXIST',
],
]);
$this
->assertRaw('This webform is not available. Please contact the site administrator.');
$this
->drupalGet('/webform/test_form_prepopulate', [
'query' => [
'source_entity_type' => 'webform',
'source_entity_id' => 'contact',
],
]);
$this
->assertNoRaw('This webform is not available. Please contact the site administrator.');
$this
->drupalLogin($this->rootUser);
$sid = $this
->postSubmission($webform_prepopulate, [], 'Submit', [
'query' => [
'source_entity_type' => 'webform',
'source_entity_id' => 'contact',
],
]);
$this
->drupalGet("/admin/structure/webform/manage/test_form_prepopulate/submission/{$sid}/edit");
$this
->assertNoRaw('This webform is not available. Please contact the site administrator.');
$this
->drupalLogout();
$webform_prepopulate
->setSetting('form_prepopulate_source_entity_type', 'user');
$webform_prepopulate
->save();
$this
->drupalGet('/webform/test_form_prepopulate', [
'query' => [
'source_entity_type' => 'webform',
'source_entity_id' => 'contact',
],
]);
$this
->assertRaw('This webform is not available. Please contact the site administrator.');
$webform_prepopulate
->setSetting('form_prepopulate_source_entity_type', 'webform');
$webform_prepopulate
->save();
$this
->drupalGet('/webform/test_form_prepopulate', [
'query' => [
'source_entity_type' => 'webform',
'source_entity_id' => 'contact',
],
]);
$this
->assertNoRaw('This webform is not available. Please contact the site administrator.');
$this
->drupalLogin($this->rootUser);
$route_options = [
'query' => [
'source_entity_type' => 'webform',
'source_entity_id' => 'contact',
],
];
$this
->drupalGet('/webform/test_form_prepopulate', $route_options);
$this
->assertLinkByHref($webform_prepopulate
->toUrl('canonical', $route_options)
->toString());
}
}