View source
<?php
namespace Drupal\yamlform_ui\Tests;
use Drupal\yamlform\Tests\YamlFormTestBase;
use Drupal\yamlform\Entity\YamlForm;
class YamlFormUiElementTest extends YamlFormTestBase {
public static $modules = [
'system',
'filter',
'user',
'yamlform',
'yamlform_test',
'yamlform_examples',
'yamlform_ui',
];
public function testElements() {
global $base_path;
$this
->drupalLogin($this->adminFormUser);
$yamlform_contact = YamlForm::load('contact');
$this
->assertEqual([
'name',
'email',
'subject',
'message',
], array_keys($yamlform_contact
->getElementsDecodedAndFlattened()));
$edit = [
'elements_reordered[message][weight]' => 0,
'elements_reordered[subject][weight]' => 1,
'elements_reordered[email][weight]' => 2,
'elements_reordered[name][weight]' => 3,
];
$this
->drupalPostForm('admin/structure/yamlform/manage/contact', $edit, t('Save elements'));
\Drupal::entityTypeManager()
->getStorage('yamlform_submission')
->resetCache();
\Drupal::entityTypeManager()
->getStorage('yamlform')
->resetCache();
$yamlform_contact = YamlForm::load('contact');
$this
->assertEqual([
'message',
'subject',
'email',
'name',
], array_keys($yamlform_contact
->getElementsDecodedAndFlattened()));
$this
->drupalGet('admin/structure/yamlform/manage/contact');
$this
->assertFieldChecked('edit-elements-reordered-name-required');
$edit = [
'elements_reordered[name][required]' => FALSE,
];
$this
->drupalPostForm('admin/structure/yamlform/manage/contact', $edit, t('Save elements'));
$this
->assertNoFieldChecked('edit-elements-reordered-name-required');
$this
->drupalPostForm('admin/structure/yamlform/manage/contact/element/add/textfield', [
'key' => 'test',
'properties[title]' => 'Test',
], t('Save'));
$this
->drupalGet('yamlform/contact');
$this
->assertRaw('<label for="edit-test">Test</label>');
$this
->assertRaw('<input data-drupal-selector="edit-test" type="text" id="edit-test" name="test" value="" size="60" maxlength="255" class="form-text" />');
$this
->drupalPostForm('admin/structure/yamlform/manage/contact/element/test/edit', [
'properties[title]' => 'Test 123',
'properties[default_value]' => 'This is a default value',
], t('Save'));
$this
->drupalGet('yamlform/contact');
$this
->assertRaw('<label for="edit-test">Test 123</label>');
$this
->assertRaw('<input data-drupal-selector="edit-test" type="text" id="edit-test" name="test" value="This is a default value" size="60" maxlength="255" class="form-text" />');
$this
->drupalPostForm('yamlform/contact/test', [], t('Send message'));
$this
->assertEqual(1, db_query("SELECT COUNT(sid) FROM {yamlform_submission_data} WHERE yamlform_id='contact' AND name='test'")
->fetchField());
$this
->drupalPostForm('admin/structure/yamlform/manage/contact/element/test/delete', [], t('Delete'));
$this
->drupalGet('yamlform/contact');
$this
->assertNoRaw('<label for="edit-test">Test 123</label>');
$this
->assertNoRaw('<input data-drupal-selector="edit-test" type="text" id="edit-test" name="test" value="This is a default value" size="60" maxlength="255" class="form-text" />');
$this
->assertEqual(0, db_query("SELECT COUNT(sid) FROM {yamlform_submission_data} WHERE yamlform_id='contact' AND name='test'")
->fetchField());
$this
->drupalPostForm('admin/structure/yamlform/manage/contact/element/add/textfield', [
'key' => 'test',
'properties[title]' => 'Test',
], t('Save'));
$this
->drupalGet('admin/structure/yamlform/manage/contact/element/test/edit');
$this
->assertRaw('Text field<a href="' . $base_path . 'admin/structure/yamlform/manage/contact/element/test/change" class="button button--small use-ajax" data-dialog-type="modal" data-dialog-options="{"width":800}" data-drupal-selector="edit-change-type" id="edit-change-type">Change</a>');
$this
->assertRaw(t('A short description of the element used as help for the user when he/she uses the form.'));
$this
->drupalGet('admin/structure/yamlform/manage/contact/element/test/change');
$this
->assertRaw(t('Hidden'));
$this
->assertRaw('<a href="' . $base_path . 'admin/structure/yamlform/manage/contact/element/test/edit?type=hidden" class="use-ajax" data-dialog-type="modal" data-dialog-options="{"width":800}">Change</a>');
$this
->assertRaw(t('value'));
$this
->assertRaw('<a href="' . $base_path . 'admin/structure/yamlform/manage/contact/element/test/edit?type=value" class="use-ajax" data-dialog-type="modal" data-dialog-options="{"width":800}">Change</a>');
$this
->assertRaw(t('Search'));
$this
->assertRaw('<a href="' . $base_path . 'admin/structure/yamlform/manage/contact/element/test/edit?type=search" class="use-ajax" data-dialog-type="modal" data-dialog-options="{"width":800}">Change</a>');
$this
->assertRaw(t('Telephone'));
$this
->assertRaw('<a href="' . $base_path . 'admin/structure/yamlform/manage/contact/element/test/edit?type=tel" class="use-ajax" data-dialog-type="modal" data-dialog-options="{"width":800}">Change</a>');
$this
->assertRaw(t('URL'));
$this
->assertRaw('<a href="' . $base_path . 'admin/structure/yamlform/manage/contact/element/test/edit?type=url" class="use-ajax" data-dialog-type="modal" data-dialog-options="{"width":800}">Change</a>');
$this
->drupalGet('admin/structure/yamlform/manage/contact/element/test/edit', [
'query' => [
'type' => 'value',
],
]);
$this
->assertNoRaw(t('A short description of the element used as help for the user when he/she uses the form.'));
$this
->assertRaw('Value<a href="' . $base_path . 'admin/structure/yamlform/manage/contact/element/test/edit" class="button button--small use-ajax" data-dialog-type="modal" data-dialog-options="{"width":800}" data-drupal-selector="edit-cancel" id="edit-cancel">Cancel</a>');
$this
->assertRaw('(Changing from <em class="placeholder">Text field</em>)');
$this
->drupalPostForm('admin/structure/yamlform/manage/contact/element/test/edit', [], t('Save'), [
'query' => [
'type' => 'value',
],
]);
$this
->drupalGet('admin/structure/yamlform/manage/contact/element/test/edit');
$this
->assertRaw('Value<a href="' . $base_path . 'admin/structure/yamlform/manage/contact/element/test/change" class="button button--small use-ajax" data-dialog-type="modal" data-dialog-options="{"width":800}" data-drupal-selector="edit-change-type" id="edit-change-type">Change</a>');
$this
->drupalPostForm('admin/structure/yamlform/manage/contact/element/add/color', [
'key' => 'test_color',
'properties[title]' => 'Test color',
], t('Save'));
$this
->drupalGet('admin/structure/yamlform/manage/contact/element/test_color/change');
$this
->assertResponse(404);
$edit = [
'properties[default_value]' => 'not a valid date',
];
$this
->drupalPostForm('admin/structure/yamlform/manage/test_element_dates/element/date_min_max_dynamic/edit', $edit, t('Save'));
$this
->assertRaw('The Default value could not be interpreted in <a href="https://www.gnu.org/software/tar/manual/html_chapter/tar_7.html#Date-input-formats">GNU Date Input Format</a>.');
}
public function testPermissions() {
$yamlform = $this
->createYamlForm();
$account = $this
->drupalCreateUser([
'administer yamlform',
]);
$this
->drupalLogin($account);
$this
->drupalGet('admin/structure/yamlform/manage/' . $yamlform
->id() . '/source');
$this
->assertResponse(403);
$this
->drupalLogout();
$account = $this
->drupalCreateUser([
'edit yamlform source',
]);
$this
->drupalLogin($account);
$this
->drupalGet('admin/structure/yamlform/manage/' . $yamlform
->id() . '/source');
$this
->assertResponse(403);
$this
->drupalLogout();
$account = $this
->drupalCreateUser([
'administer yamlform',
'edit yamlform source',
]);
$this
->drupalLogin($account);
$this
->drupalGet('admin/structure/yamlform/manage/' . $yamlform
->id() . '/source');
$this
->assertResponse(200);
$this
->drupalLogout();
}
}