View source  
  <?php
namespace Drupal\Tests\webform\Functional;
class WebformLibrariesTest extends WebformBrowserTestBase {
  
  public static $modules = [
    'webform_ui',
  ];
  
  protected static $testWebforms = [
    'test_libraries_optional',
  ];
  
  public function testLibraries() {
    $optional_properties = [
      'input_mask' => 'properties[input_mask][select]',
      'international_telephone' => 'properties[international]',
      'international_telephone_composite' => 'properties[phone__international]',
      'word_counter' => 'properties[counter_type]',
      'select2' => 'properties[select2]',
    ];
    $this
      ->drupalLogin($this->rootUser);
    
    $edit = [
      'excluded_libraries[choices]' => TRUE,
      'excluded_libraries[jquery.chosen]' => TRUE,
    ];
    $this
      ->drupalPostForm('/admin/structure/webform/config/libraries', $edit, 'Save configuration');
    
    $this
      ->drupalGet('/webform/test_libraries_optional');
    $this
      ->assertRaw('/select2.min.js');
    $this
      ->assertRaw('/choices.min.js');
    $this
      ->assertRaw('/chosen.jquery.min.js');
    $this
      ->assertRaw('/textcounter.min.js');
    $this
      ->assertRaw('/intlTelInput.min.js');
    $this
      ->assertRaw('/jquery.inputmask.min.js');
    $this
      ->assertRaw('/codemirror.js');
    $this
      ->assertRaw('/jquery.timepicker.min.js');
    
    foreach ($optional_properties as $element_name => $input_name) {
      $this
        ->drupalGet("/admin/structure/webform/manage/test_libraries_optional/element/{$element_name}/edit");
      $this
        ->assertFieldByName($input_name);
    }
    
    $edit = [
      'excluded_libraries[ckeditor.fakeobjects]' => FALSE,
      'excluded_libraries[ckeditor.image]' => FALSE,
      'excluded_libraries[ckeditor.link]' => FALSE,
      'excluded_libraries[codemirror]' => FALSE,
      'excluded_libraries[choices]' => FALSE,
      'excluded_libraries[jquery.inputmask]' => FALSE,
      'excluded_libraries[jquery.intl-tel-input]' => FALSE,
      'excluded_libraries[jquery.select2]' => FALSE,
      'excluded_libraries[jquery.chosen]' => FALSE,
      'excluded_libraries[jquery.timepicker]' => FALSE,
      'excluded_libraries[jquery.textcounter]' => FALSE,
    ];
    $this
      ->drupalPostForm('/admin/structure/webform/config/libraries', $edit, 'Save configuration');
    
    $this
      ->drupalGet('/webform/test_libraries_optional');
    $this
      ->assertNoRaw('/select2.min.js');
    $this
      ->assertNoRaw('/choices.min.js');
    $this
      ->assertNoRaw('/chosen.jquery.min.js');
    $this
      ->assertNoRaw('/textcounter.min.js');
    $this
      ->assertNoRaw('/intlTelInput.min.js');
    $this
      ->assertNoRaw('/jquery.inputmask.min.js');
    $this
      ->assertNoRaw('/codemirror.js');
    $this
      ->assertNoRaw('/jquery.timepicker.min.js');
    
    foreach ($optional_properties as $element_name => $input_name) {
      $this
        ->drupalGet("admin/structure/webform/manage/test_libraries_optional/element/{$element_name}/edit");
      $this
        ->assertNoFieldByName($input_name);
    }
    
    $this
      ->drupalGet('/admin/reports/status');
    $this
      ->assertNoText('CKEditor: Fakeobjects library ');
    $this
      ->assertNoText('CKEditor: Image library ');
    $this
      ->assertNoText('CKEditor: Link library ');
    $this
      ->assertNoText('Code Mirror library ');
    $this
      ->assertNoText('jQuery: iCheck library ');
    $this
      ->assertNoText('jQuery: Input Mask library ');
    $this
      ->assertNoText('jQuery: Select2 library ');
    $this
      ->assertNoText('jQuery: Choices library ');
    $this
      ->assertNoText('jQuery: Chosen library ');
    $this
      ->assertNoText('jQuery: Timepicker library ');
    $this
      ->assertNoText('jQuery: Text Counter library ');
    
    
    
    $edit = [
      'excluded_libraries[jquery.select2]' => TRUE,
    ];
    $this
      ->drupalPostForm('/admin/structure/webform/config/libraries', $edit, 'Save configuration');
    $this
      ->drupalGet('/webform/test_libraries_optional');
    
    $this
      ->assertRaw('https://cdnjs.cloudflare.com/ajax/libs/select2');
    
    \Drupal::service('module_installer')
      ->install([
      'select2',
    ]);
    drupal_flush_all_caches();
    
    $this
      ->drupalGet('/webform/test_libraries_optional');
    
    $this
      ->assertNoRaw('https://cdnjs.cloudflare.com/ajax/libs/select2');
    
  }
}