You are here

public function WebformShareNodeTest::testShare in Webform 8.5

Same name and namespace in other branches
  1. 6.x modules/webform_share/tests/src/Functional/WebformShareNodeTest.php \Drupal\Tests\webform_share\Functional\WebformShareNodeTest::testShare()

Test share.

File

modules/webform_share/tests/src/Functional/WebformShareNodeTest.php, line 27

Class

WebformShareNodeTest
Webform share node test.

Namespace

Drupal\Tests\webform_share\Functional

Code

public function testShare() {
  global $base_url;
  $config = \Drupal::configFactory()
    ->getEditable('webform.settings');

  /** @var \Drupal\webform\WebformInterface $webform */
  $webform = Webform::load('contact');
  $node = $this
    ->createWebformNode('contact');
  $nid = $node
    ->id();

  /** @var \Drupal\Core\Render\RendererInterface $render */
  $renderer = \Drupal::service('renderer');
  $this
    ->drupalLogin($this->rootUser);

  /**************************************************************************/

  // Check share page access denied.
  $this
    ->drupalGet('/webform/contact/share');
  $this
    ->assertResponse(403);

  // Check webform node share page access denied.
  $this
    ->drupalGet("/node/{$nid}/webform/share");
  $this
    ->assertResponse(403);

  // Check webform node preview access denied.
  $this
    ->drupalGet("/node/{$nid}/webform/share/preview");
  $this
    ->assertResponse(403);

  // Enable enable share for all webform node.
  $config
    ->set('settings.default_share_node', TRUE)
    ->save();

  // Check share enabled for all webform nodes.
  $this
    ->drupalGet('/webform/contact/share');
  $this
    ->assertResponse(200);
  $this
    ->drupalGet("/node/{$nid}/webform/share");
  $this
    ->assertResponse(200);
  $this
    ->drupalGet("/node/{$nid}/webform/share/preview");
  $this
    ->assertResponse(200);

  // Enable disable share for all webform nodes.
  $config
    ->set('settings.default_share_node', FALSE)
    ->save();

  // Enable share for contact webform node.
  $webform
    ->setSetting('share_node', TRUE)
    ->save();

  // Check share enabled for a single webform.
  $this
    ->drupalGet('/webform/contact/share');
  $this
    ->assertResponse(200);
  $this
    ->drupalGet("/node/{$nid}/webform/share");
  $this
    ->assertResponse(200);
  $this
    ->drupalGet("/node/{$nid}/webform/share/preview");
  $this
    ->assertResponse(200);

  // Check webform node script tag.
  $build = [
    '#type' => 'webform_share_script',
    '#webform' => $webform,
    '#source_entity' => $node,
  ];
  $actual_script_tag = $renderer
    ->renderPlain($build);
  $src = $base_url . "/webform/contact/share.js?source_entity_type=node&source_entity_id={$nid}";
  $src = preg_replace('#^https?:#', '', $src);
  $expected_script_tag = '<script src="' . $src . '"></script>' . PHP_EOL;
  $this
    ->assertEqual($expected_script_tag, $actual_script_tag);
}