Skip to content

Commit 3323a8a

Browse files
jrockowitzjrockowitz
jrockowitz
authored andcommitted
Issue #2961548 by jrockowitz: Add 'Test' to contextual links
1 parent 358e79e commit 3323a8a

7 files changed

+122
-8
lines changed

js/webform.contextual.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@
1212
// @see webform_contextual_links_view_alter()
1313
// @see Drupal.behaviors.contextual
1414
$(document).on('click', '.contextual', function() {
15-
$(this).find('a.webform-contextual').each(function() {
15+
$(this).find('a.webform-contextual').once('webform-contextual').each(function() {
1616
this.href = this.href.split('?')[0];
17+
18+
// Add ?_webform_test={webform} to the current page's URL.
19+
if (/webform\/([^\/]+)\/test/.test(this.href)) {
20+
this.href = window.location.pathname + '?_webform_test=' + RegExp.$1;
21+
}
1722
});
1823
});
1924

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Drupal\webform\Tests;
4+
5+
use Drupal\webform\Entity\Webform;
6+
use Drupal\webform\Entity\WebformSubmission;
7+
8+
/**
9+
* Tests for webform submission generator.
10+
*
11+
* @group Webform
12+
*/
13+
class WebformSubmissionGenerateTest extends WebformTestBase {
14+
15+
/**
16+
* Tests webform submission entity.
17+
*/
18+
public function testWebformSubmissionGenerate() {
19+
$this->drupalLogin($this->rootUser);
20+
21+
$webform = Webform::load('contact');
22+
23+
/**************************************************************************/
24+
// Test tab.
25+
/**************************************************************************/
26+
27+
// Check test form.
28+
$sid = $this->postSubmissionTest($webform);
29+
$webform_submission = WebformSubmission::load($sid);
30+
// Note that only 'message' and 'subject' have predefined #test values.
31+
$test_data = [
32+
'message' => 'Please ignore this email.',
33+
'subject' => 'Testing contact webform from Drupal',
34+
];
35+
$data = $webform_submission->getData();
36+
$this->assertEqual($data['message'], $test_data['message']);
37+
$this->assertEqual($data['subject'], $test_data['subject']);
38+
39+
// Check test form classes and values.
40+
$this->drupalGet('webform/contact/test');
41+
$this->assertCssSelect('.webform-submission-form.webform-submission-test-form.webform-submission-contact-form.webform-submission-contact-test-form');
42+
foreach ($test_data as $name => $value) {
43+
$this->assertFieldByName($name, $value);
44+
}
45+
46+
/**************************************************************************/
47+
// Test querystring parameter.
48+
/**************************************************************************/
49+
50+
// Check add form classes and empty values.
51+
$this->drupalGet('webform/contact');
52+
$this->assertCssSelect('.webform-submission-form.webform-submission-add-form.webform-submission-contact-form.webform-submission-contact-add-form');
53+
foreach ($test_data as $name => $value) {
54+
$this->assertNoFieldByName($name, $value);
55+
}
56+
57+
// Check add form classes and values with querystring parameter.
58+
$this->drupalGet('webform/contact', ['query' => ['_webform_test' => 'contact']]);
59+
$this->assertCssSelect('.webform-submission-form.webform-submission-test-form.webform-submission-contact-form.webform-submission-contact-test-form');
60+
foreach ($test_data as $name => $value) {
61+
$this->assertFieldByName($name, $value);
62+
}
63+
}
64+
65+
}

src/Tests/WebformTestBase.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,15 @@ protected function postSubmission(WebformInterface $webform, array $edit = [], $
365365
* Submission values.
366366
* @param string $submit
367367
* Value of the submit button whose click is to be emulated.
368+
* @param $options
369+
* Options to be forwarded to the url generator.
368370
*
369371
* @return int
370372
* The created test submission's sid.
371373
*/
372-
protected function postSubmissionTest(WebformInterface $webform, array $edit = [], $submit = NULL) {
374+
protected function postSubmissionTest(WebformInterface $webform, array $edit = [], $submit = NULL, $options = []) {
373375
$submit = $this->getWebformSubmitButtonLabel($webform, $submit);
374-
$this->drupalPostForm('webform/' . $webform->id() . '/test', $edit, $submit);
376+
$this->drupalPostForm('webform/' . $webform->id() . '/test', $edit, $submit, $options);
375377
return $this->getLastSubmissionId($webform);
376378
}
377379

src/WebformSubmissionForm.php

+30-2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ class WebformSubmissionForm extends ContentEntityForm {
110110
*/
111111
protected $webformEntityReferenceManager;
112112

113+
/**
114+
* The webform submission generation service.
115+
*
116+
* @var \Drupal\webform\WebformSubmissionGenerateInterface
117+
*/
118+
protected $generate;
119+
113120
/**
114121
* The webform settings.
115122
*
@@ -156,8 +163,10 @@ class WebformSubmissionForm extends ContentEntityForm {
156163
* The webform submission conditions (#states) validator.
157164
* @param \Drupal\webform\WebformEntityReferenceManagerInterface $webform_entity_reference_manager
158165
* The webform entity reference manager.
166+
* @param \Drupal\webform\WebformSubmissionGenerateInterface $submission_generate
167+
* The webform submission generation service.
159168
*/
160-
public function __construct(EntityManagerInterface $entity_manager, RendererInterface $renderer, AliasManagerInterface $alias_manager, PathValidatorInterface $path_validator, WebformRequestInterface $request_handler, WebformElementManagerInterface $element_manager, WebformThirdPartySettingsManagerInterface $third_party_settings_manager, WebformMessageManagerInterface $message_manager, WebformTokenManagerInterface $token_manager, WebformSubmissionConditionsValidator $conditions_validator, WebformEntityReferenceManagerInterface $webform_entity_reference_manager) {
169+
public function __construct(EntityManagerInterface $entity_manager, RendererInterface $renderer, AliasManagerInterface $alias_manager, PathValidatorInterface $path_validator, WebformRequestInterface $request_handler, WebformElementManagerInterface $element_manager, WebformThirdPartySettingsManagerInterface $third_party_settings_manager, WebformMessageManagerInterface $message_manager, WebformTokenManagerInterface $token_manager, WebformSubmissionConditionsValidator $conditions_validator, WebformEntityReferenceManagerInterface $webform_entity_reference_manager, WebformSubmissionGenerateInterface $submission_generate) {
161170
parent::__construct($entity_manager);
162171
$this->renderer = $renderer;
163172
$this->requestHandler = $request_handler;
@@ -170,6 +179,8 @@ public function __construct(EntityManagerInterface $entity_manager, RendererInte
170179
$this->tokenManager = $token_manager;
171180
$this->conditionsValidator = $conditions_validator;
172181
$this->webformEntityReferenceManager = $webform_entity_reference_manager;
182+
$this->generate = $submission_generate;
183+
173184
}
174185

175186
/**
@@ -187,7 +198,9 @@ public static function create(ContainerInterface $container) {
187198
$container->get('webform.message_manager'),
188199
$container->get('webform.token_manager'),
189200
$container->get('webform_submission.conditions_validator'),
190-
$container->get('webform.entity_reference_manager')
201+
$container->get('webform.entity_reference_manager'),
202+
$container->get('webform_submission.generate')
203+
191204
);
192205
}
193206

@@ -217,11 +230,26 @@ public function getFormId() {
217230

218231
/**
219232
* {@inheritdoc}
233+
*
234+
* This is the best place to override an entity form's default settings
235+
* because is is called immediately after the form object is initialized.
236+
*
237+
* @see \Drupal\Core\Entity\EntityFormBuilder::getForm
220238
*/
221239
public function setEntity(EntityInterface $entity) {
222240
/** @var \Drupal\webform\WebformSubmissionInterface $entity */
223241
$webform = $entity->getWebform();
224242

243+
// If ?_webform_test is defined for the current webform, override
244+
// the 'add' operation with 'test' operation and generate test data.
245+
if ($this->operation === 'add' &&
246+
$this->getRequest()->query->get('_webform_test') === $webform->id() &&
247+
$webform->access('test')
248+
) {
249+
$this->operation = 'test';
250+
$entity->setData($this->generate->getData($webform));
251+
}
252+
225253
// Get the source entity and allow webform submission to be used as a source
226254
// entity.
227255
$this->sourceEntity = $this->requestHandler->getCurrentSourceEntity(['webform']);

webform.libraries.yml

+2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ webform.contextual:
134134
version: VERSION
135135
js:
136136
js/webform.contextual.js: {}
137+
dependencies:
138+
- core/jquery.once
137139

138140
# Help.
139141

webform.links.contextual.yml

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1+
entity.webform.test_form:
2+
title: Test
3+
route_name: entity.webform.test_form
4+
group: webform
5+
weight: 0
6+
17
entity.webform.results_submissions:
28
title: Results
39
route_name: entity.webform.results_submissions
410
group: webform
5-
weight: 0
11+
weight: 10
612

713
entity.webform.edit_form:
814
title: Build
915
route_name: entity.webform.edit_form
1016
group: webform
11-
weight: 10
17+
weight: 20
1218

1319
entity.webform.settings:
1420
title: Settings
1521
route_name: entity.webform.settings
1622
group: webform
17-
weight: 20
23+
weight: 30

webform.module

+6
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,15 @@ function webform_file_download($uri) {
641641

642642
/**
643643
* Implements hook_contextual_links_view_alter().
644+
*
645+
* Add .webform-contextual class to all webform context links.
646+
*
647+
* @see webform.links.contextual.yml
648+
* @see js/webform.contextual.js
644649
*/
645650
function webform_contextual_links_view_alter(&$element, $items) {
646651
$links = [
652+
'entitywebformtest-form',
647653
'entitywebformresults-submissions',
648654
'entitywebformedit-form',
649655
'entitywebformsettings',

0 commit comments

Comments
 (0)