WebformShareTest.php in Webform 8.5
File
modules/webform_share/tests/src/Functional/WebformShareTest.php
View source
<?php
namespace Drupal\Tests\webform_share\Functional;
use Drupal\Tests\webform\Functional\WebformBrowserTestBase;
use Drupal\webform\Entity\Webform;
use Drupal\webform_share\Element\WebformShareIframe;
class WebformShareTest extends WebformBrowserTestBase {
public static $modules = [
'webform',
'webform_share',
];
public function testShare() {
global $base_url;
$library = WebformShareIframe::LIBRARY;
$version = WebformShareIframe::VERSION;
$config = \Drupal::configFactory()
->getEditable('webform.settings');
$webform = Webform::load('contact');
$renderer = \Drupal::service('renderer');
$this
->drupalLogin($this->rootUser);
$this
->drupalGet('/webform/contact/share');
$this
->assertResponse(403);
$this
->drupalGet('/webform/contact/share.js');
$this
->assertResponse(403);
$this
->drupalGet("/webform/contact/share/{$library}/{$version}");
$this
->assertResponse(403);
$this
->drupalGet('/admin/structure/webform/manage/contact/share/preview');
$this
->assertResponse(403);
$config
->set('settings.default_share', TRUE)
->save();
$this
->drupalGet('/webform/contact/share');
$this
->assertResponse(200);
$this
->drupalGet('/webform/contact/share.js');
$this
->assertResponse(200);
$this
->drupalGet("/webform/contact/share/{$library}/{$version}");
$this
->assertResponse(200);
$this
->drupalGet('/admin/structure/webform/manage/contact/share/preview');
$this
->assertResponse(200);
$config
->set('settings.default_share', FALSE)
->save();
$webform
->setSetting('share', TRUE)
->save();
$this
->drupalGet('/webform/contact/share');
$this
->assertResponse(200);
$this
->drupalGet('/webform/contact/share.js');
$this
->assertResponse(200);
$this
->drupalGet("/webform/contact/share/{$library}/{$version}");
$this
->assertResponse(200);
$this
->drupalGet('/admin/structure/webform/manage/contact/share/preview');
$this
->assertResponse(200);
$this
->drupalGet('/admin/structure/webform/manage/contact/share', [
'query' => [
'test' => '123',
],
]);
$this
->assertRaw("/webform/contact/share?test=123");
$this
->assertRaw('/webform/contact/share.js?test=123');
$this
->assertRaw("/webform/contact/share/{$library}/{$version}?test=123");
$this
->drupalGet('/webform/contact/share');
$this
->assertRaw('"theme":"' . \Drupal::config('system.theme')
->get('default') . '"');
\Drupal::service('theme_installer')
->install([
'bartik',
]);
$webform
->setSetting('share_theme_name', 'bartik')
->save();
$this
->drupalGet('/webform/contact/share');
$this
->assertRaw('"theme":"bartik"');
$this
->drupalGet('/webform/contact/share');
$this
->assertCssSelect('html.webform-share-page-html');
$this
->assertCssSelect('body.webform-share-page-body');
$this
->assertRaw('<h1 class="title page-title">Contact</h1>');
$webform
->setSetting('share_theme_name', '')
->setSetting('share_page_body_attributes', [
'class' => [
'my-custom-class',
],
])
->save();
$this
->drupalGet('/webform/contact/share');
$this
->assertCssSelect('body.my-custom-class');
$webform
->setSetting('share_page_body_attributes', [])
->setSetting('share_title', FALSE)
->save();
$this
->assertNoRaw('<h1 class="title page-title">Contact</h1>');
$this
->drupalGet("/webform/contact/share/{$library}/{$version}");
$this
->assertRaw('<script src="//cdn.jsdelivr.net/gh/davidjbradshaw/' . $library . '@v' . $version . '/js/iframeResizer.contentWindow.min.js"></script>');
$this
->drupalGet("/webform/contact/share.js");
$this
->assertRaw('document.write("');
$build = [
'#type' => 'webform_share_script',
'#webform' => $webform,
];
$actual_script_tag = $renderer
->renderPlain($build);
$src = $base_url . "/webform/contact/share.js";
$src = preg_replace('#^https?:#', '', $src);
$expected_script_tag = '<script src="' . $src . '"></script>' . PHP_EOL;
$this
->assertEqual($expected_script_tag, $actual_script_tag);
}
}