View source
<?php
namespace Drupal\Tests\webform\Kernel\Entity;
use Drupal\KernelTests\KernelTestBase;
use Drupal\webform\Entity\Webform;
use Drupal\webform\Utility\WebformYaml;
use Drupal\webform\WebformException;
use Drupal\webform\WebformInterface;
class WebformEntityTest extends KernelTestBase {
public static $modules = [
'system',
'path',
'path_alias',
'webform',
'user',
'field',
];
public function testWebformMethods() {
$this
->installEntitySchema('path_alias');
$this
->installSchema('webform', [
'webform',
]);
$this
->installConfig('webform');
$webform = Webform::create([
'id' => 'webform_test',
]);
$webform
->save();
$this
->assertEquals('webform_test', $webform
->id());
$this
->assertFalse($webform
->isTemplate());
$this
->assertTrue($webform
->isOpen());
try {
$webform
->setOverride(TRUE);
$webform
->save();
$this
->fail('Not possible to save webform with override = TRUE.');
} catch (WebformException $e) {
$this
->pass('Not possible to save webform with override = TRUE.');
}
try {
$webform
->setOverride(FALSE);
$webform
->save();
$this
->pass('Possible to save webform with override = FALSE.');
} catch (WebformException $e) {
$this
->fail('Possible to save webform with override = FALSE.');
}
$webform
->setStatus(FALSE);
$this
->assertFalse($webform
->isOpen());
$this
->assertEquals($webform
->get('status'), WebformInterface::STATUS_CLOSED);
$this
->assertFalse($webform
->isScheduled());
$webform
->setStatus(TRUE);
$this
->assertTrue($webform
->isOpen());
$this
->assertEquals($webform
->get('status'), WebformInterface::STATUS_OPEN);
$webform
->setStatus(NULL);
$this
->assertTrue($webform
->isOpen());
$this
->assertEquals($webform
->get('status'), WebformInterface::STATUS_SCHEDULED);
$webform
->setStatus(WebformInterface::STATUS_CLOSED);
$this
->assertFalse($webform
->isOpen());
$webform
->setStatus(WebformInterface::STATUS_OPEN);
$this
->assertTrue($webform
->isOpen());
$webform
->setStatus(WebformInterface::STATUS_SCHEDULED);
$this
->assertTrue($webform
->isOpen());
$this
->assertTrue($webform
->isScheduled());
$webform
->setStatus(WebformInterface::STATUS_SCHEDULED);
$webform
->set('open', date('Y-m-d\\TH:i:s', strtotime('today -1 days')));
$webform
->set('close', NULL);
$this
->assertTrue($webform
->isOpen());
$webform
->set('open', date('Y-m-d\\TH:i:s', strtotime('today +1 day')));
$webform
->set('close', NULL);
$this
->assertFalse($webform
->isOpen());
$webform
->set('open', NULL);
$webform
->set('close', date('Y-m-d\\TH:i:s', strtotime('today -1 day')));
$this
->assertFalse($webform
->isOpen());
$webform
->set('open', NULL);
$webform
->set('close', date('Y-m-d\\TH:i:s', strtotime('today +1 day')));
$this
->assertTrue($webform
->isOpen());
$webform
->set('open', date('Y-m-d\\TH:i:s', strtotime('today +1 day')));
$webform
->set('close', date('Y-m-d\\TH:i:s', strtotime('today +10 days')));
$this
->assertFalse($webform
->isOpen());
$this
->assertTrue($webform
->isOpening());
$webform
->set('open', date('Y-m-d\\TH:i:s', strtotime('today -1 day')));
$webform
->set('close', date('Y-m-d\\TH:i:s', strtotime('today +10 days')));
$this
->assertTrue($webform
->isOpen());
$webform
->set('open', date('Y-m-d\\TH:i:s', strtotime('today -1 day')));
$webform
->set('close', date('Y-m-d\\TH:i:s', strtotime('today -10 days')));
$this
->assertFalse($webform
->isOpen());
$this
->assertFalse($webform
->isOpening());
$webform
->setStatus(TRUE);
$webform
->set('open', date('Y-m-d\\TH:i:s', strtotime('today -1 day')));
$webform
->set('close', date('Y-m-d\\TH:i:s', strtotime('today -10 days')));
$this
->assertTrue($webform
->isOpen());
$webform
->setStatus(FALSE);
$webform
->set('open', date('Y-m-d\\TH:i:s', strtotime('today +1 day')));
$webform
->set('close', date('Y-m-d\\TH:i:s', strtotime('today -10 days')));
$this
->assertFalse($webform
->isOpen());
$webform
->set('open', date('Y-m-d\\TH:i:s', strtotime('today +1 day')));
$webform
->set('close', date('Y-m-d\\TH:i:s', strtotime('today -10 days')));
$this
->assertNotNull($webform
->get('open'));
$this
->assertNotNull($webform
->get('close'));
$webform
->setStatus(TRUE);
$this
->assertNull($webform
->get('open'));
$this
->assertNull($webform
->get('close'));
$webform
->set('template', TRUE)
->save();
$this
->assertTrue($webform
->isTemplate());
$this
->assertFalse($webform
->isOpen());
$elements = [
'root' => [
'#type' => 'textfield',
'#title' => 'root',
],
'container' => [
'#type' => 'container',
'#title' => 'container',
'child' => [
'#type' => 'textfield',
'#title' => 'child',
'#default_value' => '{default value}',
],
],
];
$webform
->setElements($elements);
$this
->assertEquals($webform
->getElementsRaw(), WebformYaml::encode($elements));
$flattened_elements = [
'root' => [
'#type' => 'textfield',
'#title' => 'root',
],
'container' => [
'#type' => 'container',
'#title' => 'container',
],
'child' => [
'#type' => 'textfield',
'#title' => 'child',
'#default_value' => '{default value}',
],
];
$this
->assertEquals($webform
->getElementsDecodedAndFlattened(), $flattened_elements);
$elements_initialized_and_flattened = [
'root' => [
'#type' => 'textfield',
'#title' => 'root',
'#webform' => 'webform_test',
'#webform_id' => 'webform_test--root',
'#webform_key' => 'root',
'#webform_parent_key' => '',
'#webform_parent_flexbox' => FALSE,
'#webform_depth' => 0,
'#webform_children' => [],
'#webform_multiple' => FALSE,
'#webform_composite' => FALSE,
'#webform_parents' => [
'root',
],
'#webform_plugin_id' => 'textfield',
'#admin_title' => 'root',
],
'container' => [
'#type' => 'container',
'#title' => 'container',
'#webform' => 'webform_test',
'#webform_id' => 'webform_test--container',
'#webform_key' => 'container',
'#webform_parent_key' => '',
'#webform_parent_flexbox' => FALSE,
'#webform_depth' => 0,
'#webform_children' => [
'child' => 'child',
],
'#webform_multiple' => FALSE,
'#webform_composite' => FALSE,
'#webform_parents' => [
'container',
],
'#webform_plugin_id' => 'container',
'#admin_title' => 'container',
],
'child' => [
'#type' => 'textfield',
'#title' => 'child',
'#default_value' => '{default value}',
'#webform' => 'webform_test',
'#webform_id' => 'webform_test--child',
'#webform_key' => 'child',
'#webform_parent_key' => 'container',
'#webform_parent_flexbox' => FALSE,
'#webform_depth' => 1,
'#webform_children' => [],
'#webform_multiple' => FALSE,
'#webform_composite' => FALSE,
'#webform_parents' => [
'container',
'child',
],
'#webform_plugin_id' => 'textfield',
'#admin_title' => 'child',
],
];
$this
->assertEquals($webform
->getElementsInitializedAndFlattened(), $elements_initialized_and_flattened);
$elements_initialized_flattened_and_has_value = $elements_initialized_and_flattened;
unset($elements_initialized_flattened_and_has_value['container']);
$this
->assertEquals($webform
->getElementsInitializedFlattenedAndHasValue(), $elements_initialized_flattened_and_has_value);
$this
->assertEquals($webform
->getElementsDefaultData(), [
'child' => '{default value}',
]);
$webform
->set('elements', 'invalid')
->save();
$this
->assertEquals([], $webform
->getElementsInitialized());
$this
->assertEquals($webform
->getPages(), []);
$wizard_elements = [
'page_1' => [
'#type' => 'webform_wizard_page',
'#title' => 'Page 1',
],
'page_2' => [
'#type' => 'webform_wizard_page',
'#title' => 'Page 2',
],
'page_3' => [
'#type' => 'webform_wizard_page',
'#title' => 'Page 3',
],
];
$webform
->set('elements', $wizard_elements)
->save();
$wizard_pages = [
'page_1' => [
'#title' => 'Page 1',
'#type' => 'page',
'#access' => TRUE,
],
'page_2' => [
'#title' => 'Page 2',
'#type' => 'page',
'#access' => TRUE,
],
'page_3' => [
'#title' => 'Page 3',
'#type' => 'page',
'#access' => TRUE,
],
'webform_confirmation' => [
'#title' => 'Complete',
'#type' => 'page',
'#access' => TRUE,
],
];
$this
->assertEquals($webform
->getPages(), $wizard_pages);
$webform
->setSetting('preview', TRUE)
->save();
$wizard_pages = [
'page_1' => [
'#title' => 'Page 1',
'#type' => 'page',
'#access' => TRUE,
],
'page_2' => [
'#title' => 'Page 2',
'#type' => 'page',
'#access' => TRUE,
],
'page_3' => [
'#title' => 'Page 3',
'#type' => 'page',
'#access' => TRUE,
],
'webform_preview' => [
'#title' => 'Preview',
'#type' => 'page',
'#access' => TRUE,
],
'webform_confirmation' => [
'#title' => 'Complete',
'#type' => 'page',
'#access' => TRUE,
],
];
$this
->assertEquals($webform
->getPages(), $wizard_pages);
$webform
->setSetting('preview', TRUE)
->save();
$wizard_pages = [
'webform_start' => [
'#title' => 'Start',
'#type' => 'page',
'#access' => TRUE,
],
'webform_preview' => [
'#title' => 'Preview',
'#type' => 'page',
'#access' => TRUE,
],
'webform_confirmation' => [
'#title' => 'Complete',
'#type' => 'page',
'#access' => TRUE,
],
];
$this
->assertEquals($webform
->getPages(TRUE), $wizard_pages);
}
public function testPaths() {
$this
->installEntitySchema('path_alias');
$this
->installSchema('webform', [
'webform',
]);
$this
->installConfig('webform');
$webform = Webform::create([
'id' => 'webform_test',
]);
$webform
->save();
$aliases = \Drupal::database()
->query('SELECT path, alias FROM {path_alias}')
->fetchAllKeyed();
$this
->assertEquals($aliases['/webform/webform_test'], '/form/webform-test');
$this
->assertEquals($aliases['/webform/webform_test/confirmation'], '/form/webform-test/confirmation');
$this
->assertEquals($aliases['/webform/webform_test/submissions'], '/form/webform-test/submissions');
}
public function testElementsCrud() {
$this
->installEntitySchema('path_alias');
$this
->installSchema('webform', [
'webform',
]);
$this
->installEntitySchema('webform_submission');
$webform = Webform::create([
'id' => 'webform_test',
]);
$webform
->save();
$elements = [
'root' => [
'#type' => 'container',
'#title' => 'root',
],
];
$webform
->setElementProperties('root', $elements['root']);
$this
->assertEquals($webform
->getElementsRaw(), WebformYaml::encode($elements));
$elements['root']['container'] = [
'#type' => 'container',
'#title' => 'container',
];
$webform
->setElementProperties('container', $elements['root']['container'], 'root');
$this
->assertEquals($webform
->getElementsRaw(), WebformYaml::encode($elements));
$elements['root']['container']['element'] = [
'#type' => 'textfield',
'#title' => 'element',
];
$webform
->setElementProperties('element', $elements['root']['container']['element'], 'container');
$this
->assertEquals($webform
->getElementsRaw(), WebformYaml::encode($elements));
$elements = [
'root' => [
'#type' => 'container',
'#title' => 'root',
],
];
$webform
->deleteElement('container');
$this
->assertEquals($webform
->getElementsRaw(), WebformYaml::encode($elements));
}
}