View source
<?php
namespace Drupal\Tests\webform_node\Functional;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Entity\WebformSubmission;
use Drupal\webform\WebformInterface;
class WebformNodeTest extends WebformNodeBrowserTestBase {
public static $modules = [
'block',
'webform',
'webform_node',
];
protected static $testWebforms = [
'test_form_limit',
'test_confirmation_inline',
];
protected function setUp() {
parent::setUp();
$this
->placeWebformBlocks('webform_test_block_submission_limit');
}
public function testNode() {
global $base_path;
$webform = Webform::load('contact');
$node = $this
->createWebformNode('contact');
$entity_reference_manager = \Drupal::service('webform.entity_reference_manager');
$normal_user = $this
->drupalCreateUser();
$this
->assertEqual($entity_reference_manager
->getTableNames(), [
"node__webform" => 'webform',
"node_revision__webform" => 'webform',
]);
$this
->drupalGet('/node/' . $node
->id());
$this
->assertRaw('id="webform-submission-contact-node-' . $node
->id() . '-add-form"');
$this
->assertNoFieldByName('name', 'John Smith');
$node->webform->default_data = "name: 'John Smith'";
$node
->save();
$this
->drupalGet('/node/' . $node
->id());
$this
->assertFieldByName('name', 'John Smith');
$webform
->setStatus(WebformInterface::STATUS_CLOSED);
$webform
->save();
$this
->drupalGet('/node/' . $node
->id());
$this
->assertRaw('Sorry… This form is closed to new submissions.');
$this
->drupalLogin($this->rootUser);
$this
->drupalGet('/node/' . $node
->id() . '/edit');
$this
->assertRaw('The <em class="placeholder">Contact</em> webform is <a href="' . $base_path . 'admin/structure/webform/manage/contact/settings/form">closed</a>. The below status will be ignored.');
$webform
->setStatus(WebformInterface::STATUS_OPEN);
$webform
->save();
$this
->drupalLogout();
$node->webform->status = WebformInterface::STATUS_CLOSED;
$node
->save();
$this
->drupalGet('/node/' . $node
->id());
$this
->assertNoFieldByName('name', 'John Smith');
$this
->assertRaw('Sorry… This form is closed to new submissions.');
$node->webform->target_id = 'test_confirmation_inline';
$node->webform->default_data = '';
$node->webform->status = WebformInterface::STATUS_OPEN;
$node->webform->open = '';
$node->webform->close = '';
$node
->save();
$this
->postNodeSubmission($node);
$this
->assertRaw('This is a custom inline confirmation message.');
$node->webform->target_id = 'contact';
$node->webform->status = WebformInterface::STATUS_SCHEDULED;
$node->webform->open = date('Y-m-d\\TH:i:s', strtotime('today +1 day'));
$node->webform->close = '';
$node
->save();
$this
->drupalGet('/node/' . $node
->id());
$this
->assertRaw('This form has not yet been opened to submissions.');
$this
->assertNoFieldByName('name', 'John Smith');
$node->webform->target_id = 'contact';
$node->webform->status = WebformInterface::STATUS_SCHEDULED;
$node->webform->open = date('Y-m-d\\TH:i:s', strtotime('today -1 day'));
$node->webform->close = '';
$node
->save();
$this
->drupalGet('/node/' . $node
->id());
$this
->assertNoRaw('This form has not yet been opened to submissions.');
$this
->assertFieldByName('name');
$node->webform->target_id = 'contact';
$node->webform->status = WebformInterface::STATUS_SCHEDULED;
$node->webform->open = '';
$node->webform->close = date('Y-m-d\\TH:i:s', strtotime('today -1 day'));
$node
->save();
$this
->drupalGet('/node/' . $node
->id());
$this
->assertRaw('Sorry… This form is closed to new submissions.');
$this
->assertNoFieldByName('name');
$node->webform->target_id = 'contact';
$node->webform->status = WebformInterface::STATUS_SCHEDULED;
$node->webform->open = '';
$node->webform->close = '';
$node
->save();
$this
->drupalGet('/node/' . $node
->id());
$this
->assertNoRaw('Sorry… This form is closed to new submissions.');
$this
->assertFieldByName('name');
$node->webform->target_id = 'contact';
$node->webform->status = WebformInterface::STATUS_SCHEDULED;
$node->webform->open = '';
$node->webform->close = date('Y-m-d\\TH:i:s', strtotime('today -1 day'));
$node
->save();
$this
->drupalGet('/node/' . $node
->id());
\Drupal::configFactory()
->getEditable('webform.settings')
->set('settings.default_form_close_message', '{Custom closed message}')
->save();
$this
->drupalGet('/node/' . $node
->id());
$this
->assertRaw('{Custom closed message}');
$node->webform->target_id = 'test_form_limit';
$node->webform->default_data = '';
$node->webform->open = '';
$node->webform->close = '';
$node
->save();
$limit_form = Webform::load('test_form_limit');
$limit_form
->setSettings([
'limit_total' => NULL,
'limit_user' => NULL,
'entity_limit_total' => 3,
'entity_limit_user' => 1,
'limit_total_message' => 'Only 3 submissions are allowed.',
'limit_user_message' => 'You are only allowed to have 1 submission for this webform.',
]);
$limit_form
->save();
$this
->drupalGet('/node/' . $node
->id());
$this
->assertRaw('limit:webform:source_entity: 3');
$this
->assertRaw('remaining:webform:source_entity: 3');
$this
->assertRaw('limit:user:source_entity: 1');
$this
->assertRaw('remaining:user:source_entity: 1');
$this
->assertRaw('0 user + source entity submission(s)');
$this
->assertRaw('1 user + source entity limit');
$this
->assertRaw('0 webform + source entity submission(s)');
$this
->assertRaw('3 webform + source entity limit');
$this
->drupalLogin($normal_user);
$this
->postNodeSubmission($node);
$this
->drupalGet('/node/' . $node
->id());
$this
->assertNoFieldByName('op', 'Submit');
$this
->assertRaw('You are only allowed to have 1 submission for this webform.');
$this
->assertRaw('1 user + source entity submission(s)');
$this
->assertRaw('1 user + source entity limit');
$this
->assertRaw('1 webform + source entity submission(s)');
$this
->assertRaw('3 webform + source entity limit');
$this
->drupalLogin($this->rootUser);
$this
->drupalGet('/node/' . $node
->id());
$this
->assertRaw('remaining:webform:source_entity: 2');
$this
->assertRaw('remaining:user:source_entity: 1');
$this
->postNodeSubmission($node);
$this
->postNodeSubmission($node);
$this
->drupalGet('/node/' . $node
->id());
$this
->assertRaw('remaining:webform:source_entity: 0');
$this
->assertRaw('remaining:user:source_entity: 0');
$this
->drupalLogin($normal_user);
$this
->drupalGet('/node/' . $node
->id());
$this
->assertNoFieldByName('op', 'Submit');
$this
->assertRaw('Only 3 submissions are allowed.');
$this
->assertNoRaw('You are only allowed to have 1 submission for this webform.');
$this
->assertRaw('1 user + source entity submission(s)');
$this
->assertRaw('1 user + source entity limit');
$this
->assertRaw('3 webform + source entity submission(s)');
$this
->assertRaw('3 webform + source entity limit');
$webform_contact = Webform::load('contact');
$node->webform->target_id = 'contact';
$node->webform->status = WebformInterface::STATUS_OPEN;
$node->webform->default_data = "name: '{name}'";
$node->webform->open = '';
$node->webform->close = '';
$node
->save();
$source_entity_options = [
'query' => [
'source_entity_type' => 'node',
'source_entity_id' => $node
->id(),
],
];
$this
->drupalGet('/webform/contact', $source_entity_options);
$this
->assertFieldByName('name', '{name}');
$edit = [
'name' => 'name',
'email' => 'example@example.com',
'subject' => 'subject',
'message' => 'message',
];
$this
->drupalPostForm('/webform/contact', $edit, 'Send message', $source_entity_options);
$sid = $this
->getLastSubmissionId($webform_contact);
$submission = WebformSubmission::load($sid);
$this
->assertNotNull($submission
->getSourceEntity());
if ($submission
->getSourceEntity()) {
$this
->assertEqual($submission
->getSourceEntity()
->getEntityTypeId(), 'node');
$this
->assertEqual($submission
->getSourceEntity()
->id(), $node
->id());
}
$display_options = [
'type' => 'webform_entity_reference_link',
'settings' => [
'label' => 'Register',
],
];
$view_display = EntityViewDisplay::load('node.webform.default');
$view_display
->setComponent('webform', $display_options)
->save();
$node->webform->target_id = 'contact';
$node->webform->status = WebformInterface::STATUS_OPEN;
$node->webform->default_data = "name: '{name}'";
$node->webform->open = '';
$node->webform->close = '';
$node
->save();
$this
->drupalGet('/node/' . $node
->id());
$this
->assertLink('Register');
$this
->assertLinkByHref($webform_contact
->toUrl('canonical', $source_entity_options)
->toString());
}
}