Automated tests of configurable quick forms.
This commit is contained in:
parent
e987d9450c
commit
8ae1c6dd1f
|
@ -0,0 +1,9 @@
|
|||
langcode: en
|
||||
status: true
|
||||
id: configurable_test2
|
||||
plugin: configurable_test
|
||||
label: Test configurable quick form 2
|
||||
description: Overridden description
|
||||
helpText: Overridden help text
|
||||
settings:
|
||||
test_default: 500
|
|
@ -0,0 +1,7 @@
|
|||
farm_quick.settings.configurable_test:
|
||||
type: quick_form_settings
|
||||
label: 'Test configurable quick form settings'
|
||||
mapping:
|
||||
test_default:
|
||||
type: integer
|
||||
label: 'The default test value.'
|
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\farm_quick_test\Plugin\QuickForm;
|
||||
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\farm_quick\Plugin\QuickForm\ConfigurableQuickFormInterface;
|
||||
use Drupal\farm_quick\Traits\ConfigurableQuickFormTrait;
|
||||
|
||||
/**
|
||||
* Test configurable quick form.
|
||||
*
|
||||
* @QuickForm(
|
||||
* id = "configurable_test",
|
||||
* label = @Translation("Test configurable quick form"),
|
||||
* description = @Translation("Test configurable quick form description."),
|
||||
* helpText = @Translation("Test configurable quick form help text."),
|
||||
* permissions = {
|
||||
* "create test log",
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
class ConfigurableTest extends Test implements ConfigurableQuickFormInterface {
|
||||
|
||||
use ConfigurableQuickFormTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function defaultConfiguration() {
|
||||
return [
|
||||
'test_default' => 100,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(array $form, FormStateInterface $form_state, string $id = NULL) {
|
||||
$form = parent::buildForm($form, $form_state, $id);
|
||||
|
||||
// Set a default value from configuration.
|
||||
$form['test']['#default_value'] = $this->configuration['test_default'];
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
|
||||
$form['test_default'] = [
|
||||
'#type' => 'number',
|
||||
'#title' => $this->t('Default value'),
|
||||
'#default_value' => $this->configuration['test_default'],
|
||||
];
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
|
||||
$this->configuration['test_default'] = $form_state->getValue('test_default');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\farm_quick_test\Plugin\QuickForm;
|
||||
|
||||
/**
|
||||
* Test quick form that requires a configuration entity.
|
||||
*
|
||||
* @QuickForm(
|
||||
* id = "requires_entity_test",
|
||||
* label = @Translation("Test requiresEntity quick form"),
|
||||
* description = @Translation("Test requiresEntity quick form description."),
|
||||
* helpText = @Translation("Test requiresEntity quick form help text."),
|
||||
* permissions = {
|
||||
* "create test log",
|
||||
* },
|
||||
* requiresEntity = True
|
||||
* )
|
||||
*/
|
||||
class RequiresEntityTest extends Test {
|
||||
|
||||
}
|
|
@ -3,6 +3,7 @@
|
|||
namespace Drupal\Tests\farm_quick\Functional;
|
||||
|
||||
use Drupal\Core\StringTranslation\StringTranslationTrait;
|
||||
use Drupal\farm_quick\Entity\QuickFormInstance;
|
||||
use Drupal\Tests\farm_test\Functional\FarmBrowserTestBase;
|
||||
|
||||
/**
|
||||
|
@ -53,16 +54,96 @@ class QuickFormTest extends FarmBrowserTestBase {
|
|||
$user = $this->createUser(['view quick_form', 'create test log']);
|
||||
$this->drupalLogin($user);
|
||||
|
||||
// Go to the quick form index and confirm that access is granted, and the
|
||||
// test quick form item is visible.
|
||||
// Go to the quick form index and confirm that:
|
||||
// 1. access is granted.
|
||||
// 2. the test quick form item is visible.
|
||||
// 3. the default configurable_test quick form item is visible.
|
||||
// 4. the second instance of configurable_test quick form item is visible.
|
||||
// 5. the requires_entity_test quick form item is NOT visible.
|
||||
$this->drupalGet('quick');
|
||||
$this->assertSession()->statusCodeEquals(200);
|
||||
$this->assertSession()->pageTextContains($this->t('Test quick form'));
|
||||
$this->assertSession()->pageTextContains($this->t('Test configurable quick form'));
|
||||
$this->assertSession()->pageTextContains($this->t('Test configurable quick form 2'));
|
||||
$this->assertSession()->pageTextNotContains($this->t('Test requiresEntity quick form'));
|
||||
|
||||
// Go to the test quick form and confirm that the test field is visible.
|
||||
$this->drupalGet('quick/test');
|
||||
$this->assertSession()->statusCodeEquals(200);
|
||||
$this->assertSession()->pageTextContains($this->t('Test field'));
|
||||
|
||||
// Go to the default configurable_test quick form and confirm access is
|
||||
// granted and the default value is 100.
|
||||
$this->drupalGet('quick/configurable_test');
|
||||
$this->assertSession()->statusCodeEquals(200);
|
||||
$this->assertSession()->responseContains('value="100"');
|
||||
|
||||
// Go to the test configuration form and confirm that access is denied.
|
||||
$this->drupalGet('quick/configurable_test/configure');
|
||||
$this->assertSession()->statusCodeEquals(403);
|
||||
|
||||
// Create and login a test user with permission to create test logs and
|
||||
// permission to update quick forms.
|
||||
$user = $this->createUser(['view quick_form', 'create test log', 'update quick_form']);
|
||||
$this->drupalLogin($user);
|
||||
|
||||
// Go to the default configurable_test quick form and confirm that the
|
||||
// default value field is visible and the default value is 100.
|
||||
$this->drupalGet('quick/configurable_test/configure');
|
||||
$this->assertSession()->statusCodeEquals(200);
|
||||
$this->assertSession()->pageTextContains($this->t('Default value'));
|
||||
$this->assertSession()->responseContains('value="100"');
|
||||
|
||||
// Go to the configurable_test2 quick form and confirm access is granted and
|
||||
// the default value is 500.
|
||||
$this->drupalGet('quick/configurable_test2');
|
||||
$this->assertSession()->statusCodeEquals(200);
|
||||
$this->assertSession()->responseContains('value="500"');
|
||||
|
||||
// Go to the configurable_test2 quick form and confirm that the default
|
||||
// value field is visible and the default value is 500.
|
||||
$this->drupalGet('quick/configurable_test2/configure');
|
||||
$this->assertSession()->statusCodeEquals(200);
|
||||
$this->assertSession()->pageTextContains($this->t('Default value'));
|
||||
$this->assertSession()->responseContains('value="500"');
|
||||
|
||||
// Save the configurable_test2 config entity to change the value and
|
||||
// confirm that it is updated in the quick form and configuration form.
|
||||
$config_entity = \Drupal::entityTypeManager()->getStorage('quick_form')->load('configurable_test2');
|
||||
$config_entity->set('settings', ['test_default' => 600]);
|
||||
$config_entity->save();
|
||||
$this->drupalGet('quick/configurable_test2');
|
||||
$this->assertSession()->responseContains('value="600"');
|
||||
$this->drupalGet('quick/configurable_test2/configure');
|
||||
$this->assertSession()->responseContains('value="600"');
|
||||
|
||||
// Attempt to load a configuration form for a non-existent quick form and
|
||||
// confirm 404 not found.
|
||||
$this->drupalGet('quick/foo/configure');
|
||||
$this->assertSession()->statusCodeEquals(404);
|
||||
|
||||
// Go to the requires_entity_test quick form and confirm 404 not found.
|
||||
$this->drupalGet('quick/requires_entity_test');
|
||||
$this->assertSession()->statusCodeEquals(404);
|
||||
|
||||
// Create a config entity for the requires_entity_test plugin.
|
||||
$config_entity = QuickFormInstance::create([
|
||||
'id' => 'requires_entity_test',
|
||||
'plugin' => 'requires_entity_test',
|
||||
]);
|
||||
$config_entity->save();
|
||||
|
||||
// Go to the quick form index and confirm that the requires_entity_test
|
||||
// quick form item is visible.
|
||||
$this->drupalGet('quick');
|
||||
$this->assertSession()->statusCodeEquals(200);
|
||||
$this->assertSession()->pageTextContains($this->t('Test requiresEntity quick form'));
|
||||
|
||||
// Go to the default requires_entity_test quick form and confirm access
|
||||
// granted and the default value is 100.
|
||||
$this->drupalGet('quick/requires_entity_test');
|
||||
$this->assertSession()->statusCodeEquals(200);
|
||||
$this->assertSession()->pageTextContains($this->t('Test field'));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
namespace Drupal\Tests\farm_quick\Kernel;
|
||||
|
||||
use Drupal\Core\Form\FormState;
|
||||
use Drupal\farm_quick\Form\ConfigureQuickForm;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
|
||||
/**
|
||||
|
@ -65,14 +66,34 @@ class QuickFormTest extends KernelTestBase {
|
|||
/** @var \Drupal\farm_quick\Entity\QuickFormInstanceInterface[] $quick_forms */
|
||||
$quick_forms = $this->quickFormInstanceManager->getInstances();
|
||||
|
||||
// Confirm that one quick form was discovered.
|
||||
$this->assertEquals(1, count($quick_forms));
|
||||
// Confirm that three quick forms were discovered.
|
||||
$this->assertEquals(3, count($quick_forms));
|
||||
|
||||
// Confirm the label, description, helpText, and permissions.
|
||||
// Confirm the label, description, helpText, and permissions of the test
|
||||
// quick form.
|
||||
$this->assertEquals('Test quick form', $quick_forms['test']->getLabel());
|
||||
$this->assertEquals('Test quick form description.', $quick_forms['test']->getDescription());
|
||||
$this->assertEquals('Test quick form help text.', $quick_forms['test']->getHelpText());
|
||||
$this->assertEquals(['create test log'], $quick_forms['test']->getPlugin()->getPermissions());
|
||||
|
||||
// Confirm the label, description, helpText, and permissions of the
|
||||
// configurable_test quick form.
|
||||
$this->assertEquals('Test configurable quick form', $quick_forms['configurable_test']->getLabel());
|
||||
$this->assertEquals('Test configurable quick form description.', $quick_forms['configurable_test']->getDescription());
|
||||
$this->assertEquals('Test configurable quick form help text.', $quick_forms['configurable_test']->getHelpText());
|
||||
$this->assertEquals(['create test log'], $quick_forms['configurable_test']->getPlugin()->getPermissions());
|
||||
|
||||
// Confirm default configuration.
|
||||
$this->assertEquals(['test_default' => 100], $quick_forms['configurable_test']->getPlugin()->defaultConfiguration());
|
||||
|
||||
// Confirm overridden label, description, and helpText of the
|
||||
// configurable_test2 quick form.
|
||||
$this->assertEquals('Test configurable quick form 2', $quick_forms['configurable_test2']->getLabel());
|
||||
$this->assertEquals('Overridden description', $quick_forms['configurable_test2']->getDescription());
|
||||
$this->assertEquals('Overridden help text', $quick_forms['configurable_test2']->getHelpText());
|
||||
|
||||
// Confirm configuration of configurable_test2 quick form.
|
||||
$this->assertEquals(['test_default' => 500], $quick_forms['configurable_test2']->getPlugin()->getConfiguration());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -119,4 +140,41 @@ class QuickFormTest extends KernelTestBase {
|
|||
$this->assertTrue($match);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test configurable quick forms.
|
||||
*/
|
||||
public function testConfigurableQuickForm() {
|
||||
|
||||
// Load the configurable_test quick form.
|
||||
/** @var \Drupal\farm_quick\Entity\QuickFormInstanceInterface $quick_form */
|
||||
$quick_form = \Drupal::service('quick_form.instance_manager')->getInstance('configurable_test');
|
||||
|
||||
// Confirm that the config entity for this quick form has not been saved.
|
||||
$this->assertTrue($quick_form->isNew());
|
||||
|
||||
// Programmatically submit the configurable_test config form.
|
||||
$form = ConfigureQuickForm::create(\Drupal::getContainer());
|
||||
$form->setModuleHandler(\Drupal::moduleHandler());
|
||||
$form->setEntity($quick_form);
|
||||
$form_state = (new FormState())->setValues([
|
||||
'settings' => [
|
||||
'test_default' => '101',
|
||||
],
|
||||
]);
|
||||
$form_state->setTriggeringElement(\Drupal::formBuilder()->getForm($form)['actions']['submit']);
|
||||
\Drupal::formBuilder()->submitForm($form, $form_state);
|
||||
|
||||
// Reload the configurable_test quick form.
|
||||
/** @var \Drupal\farm_quick\Entity\QuickFormInstanceInterface $quick_form */
|
||||
$quick_form = \Drupal::service('quick_form.instance_manager')->getInstance('configurable_test');
|
||||
|
||||
// Confirm that a config entity was saved with all the proper defaults and
|
||||
// the submitted configuration value.
|
||||
$this->assertNotTrue($quick_form->isNew());
|
||||
$this->assertEquals($quick_form->getPlugin()->getLabel(), $quick_form->get('label'));
|
||||
$this->assertEquals($quick_form->getPlugin()->getDescription(), $quick_form->get('description'));
|
||||
$this->assertEquals($quick_form->getPlugin()->getHelpText(), $quick_form->get('helpText'));
|
||||
$this->assertEquals('101', $quick_form->get('settings')['test_default']);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue