2021-07-16 19:09:17 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Drupal\Tests\farm_quick\Kernel;
|
|
|
|
|
2021-07-20 14:48:05 +02:00
|
|
|
use Drupal\Core\Form\FormState;
|
2023-03-13 16:50:38 +01:00
|
|
|
use Drupal\farm_quick\Form\ConfigureQuickForm;
|
2021-07-16 19:09:17 +02:00
|
|
|
use Drupal\KernelTests\KernelTestBase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for farmOS quick forms.
|
|
|
|
*
|
|
|
|
* @group farm
|
|
|
|
*/
|
|
|
|
class QuickFormTest extends KernelTestBase {
|
|
|
|
|
|
|
|
/**
|
2023-08-04 16:17:51 +02:00
|
|
|
* The quick form instance manager.
|
2021-07-16 19:09:17 +02:00
|
|
|
*
|
2023-08-04 16:17:51 +02:00
|
|
|
* @var \Drupal\farm_quick\QuickFormInstanceManagerInterface
|
2021-07-16 19:09:17 +02:00
|
|
|
*/
|
2023-08-04 16:17:51 +02:00
|
|
|
protected $quickFormInstanceManager;
|
2021-07-16 19:09:17 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected static $modules = [
|
2021-07-20 14:48:05 +02:00
|
|
|
'asset',
|
2022-11-22 12:46:05 +01:00
|
|
|
'entity_reference_revisions',
|
|
|
|
'farm_field',
|
|
|
|
'farm_log_quantity',
|
2021-07-16 19:09:17 +02:00
|
|
|
'farm_quick',
|
|
|
|
'farm_quick_test',
|
2021-07-20 14:48:05 +02:00
|
|
|
'farm_unit',
|
|
|
|
'fraction',
|
2021-07-16 19:09:17 +02:00
|
|
|
'log',
|
2021-07-20 14:48:05 +02:00
|
|
|
'options',
|
|
|
|
'quantity',
|
2021-07-16 19:09:17 +02:00
|
|
|
'state_machine',
|
2021-07-20 14:48:05 +02:00
|
|
|
'taxonomy',
|
|
|
|
'text',
|
2021-07-16 19:09:17 +02:00
|
|
|
'user',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
protected function setUp(): void {
|
|
|
|
parent::setUp();
|
2023-08-04 16:17:51 +02:00
|
|
|
$this->quickFormInstanceManager = \Drupal::service('quick_form.instance_manager');
|
2021-07-20 14:48:05 +02:00
|
|
|
$this->installEntitySchema('asset');
|
2021-07-16 19:09:17 +02:00
|
|
|
$this->installEntitySchema('log');
|
2021-07-20 14:48:05 +02:00
|
|
|
$this->installEntitySchema('taxonomy_term');
|
|
|
|
$this->installEntitySchema('quantity');
|
2021-07-16 19:09:17 +02:00
|
|
|
$this->installEntitySchema('user');
|
|
|
|
$this->installConfig([
|
|
|
|
'farm_quick_test',
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test quick form discovery.
|
|
|
|
*/
|
|
|
|
public function testQuickFormDiscovery() {
|
|
|
|
|
2023-08-04 16:17:51 +02:00
|
|
|
// Load quick forms.
|
2023-08-04 16:18:14 +02:00
|
|
|
/** @var \Drupal\farm_quick\Entity\QuickFormInstanceInterface[] $quick_forms */
|
2023-08-04 16:17:51 +02:00
|
|
|
$quick_forms = $this->quickFormInstanceManager->getInstances();
|
2021-07-16 19:09:17 +02:00
|
|
|
|
2023-03-13 16:50:38 +01:00
|
|
|
// Confirm that three quick forms were discovered.
|
|
|
|
$this->assertEquals(3, count($quick_forms));
|
2021-07-16 19:09:17 +02:00
|
|
|
|
2023-03-13 16:50:38 +01:00
|
|
|
// Confirm the label, description, helpText, and permissions of the test
|
|
|
|
// quick form.
|
2023-08-04 16:17:51 +02:00
|
|
|
$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());
|
2023-08-04 16:18:14 +02:00
|
|
|
$this->assertEquals(['create test log'], $quick_forms['test']->getPlugin()->getPermissions());
|
2023-03-13 16:50:38 +01:00
|
|
|
|
|
|
|
// 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());
|
2021-07-16 19:09:17 +02:00
|
|
|
}
|
|
|
|
|
2021-07-20 14:48:05 +02:00
|
|
|
/**
|
|
|
|
* Test quick form submission.
|
|
|
|
*/
|
|
|
|
public function testQuickFormSubmission() {
|
|
|
|
|
|
|
|
// Programmatically submit the test quick form.
|
|
|
|
$form_state = (new FormState())->setValues([
|
2023-08-15 23:19:43 +02:00
|
|
|
'test' => '12',
|
2021-07-20 14:48:05 +02:00
|
|
|
]);
|
|
|
|
\Drupal::formBuilder()->submitForm('\Drupal\farm_quick\Form\QuickForm', $form_state, 'test');
|
|
|
|
|
|
|
|
// Load the form state storage.
|
|
|
|
$storage = $form_state->getStorage();
|
|
|
|
|
|
|
|
// Confirm that an asset was created.
|
|
|
|
$this->assertNotEmpty($storage['assets'][0]->id());
|
|
|
|
|
2021-07-19 22:18:18 +02:00
|
|
|
// Confirm that the asset is linked to the quick form.
|
|
|
|
$this->assertEquals('test', $storage['assets'][0]->quick[0]);
|
|
|
|
|
2021-07-20 14:48:05 +02:00
|
|
|
// Confirm that a log was created.
|
|
|
|
$this->assertNotEmpty($storage['logs'][0]->id());
|
|
|
|
|
2021-07-19 22:18:18 +02:00
|
|
|
// Confirm that the log is linked to the quick form.
|
|
|
|
$this->assertEquals('test', $storage['logs'][0]->quick[0]);
|
|
|
|
|
2022-11-22 12:46:05 +01:00
|
|
|
// Confirm that the log's quantity type is test.
|
|
|
|
$this->assertEquals('test', $storage['logs'][0]->get('quantity')->referencedEntities()[0]->bundle());
|
|
|
|
|
|
|
|
// Confirm that a quantity was created and its type is test2.
|
2021-07-20 14:48:05 +02:00
|
|
|
$this->assertNotEmpty($storage['quantities'][0]->id());
|
2022-11-22 12:46:05 +01:00
|
|
|
$this->assertEquals('test2', $storage['quantities'][0]->bundle());
|
2021-07-20 14:48:05 +02:00
|
|
|
|
|
|
|
// Confirm that three terms were created or loaded.
|
|
|
|
$this->assertEquals(3, count($storage['terms']));
|
|
|
|
foreach ($storage['terms'] as $term) {
|
|
|
|
$this->assertNotEmpty($term->id());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Confirm that the second and third terms have the same ID.
|
|
|
|
$match = $storage['terms'][1]->id() == $storage['terms'][2]->id();
|
|
|
|
$this->assertTrue($match);
|
|
|
|
}
|
|
|
|
|
2023-03-13 16:50:38 +01:00
|
|
|
/**
|
|
|
|
* 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']);
|
|
|
|
}
|
|
|
|
|
2021-07-16 19:09:17 +02:00
|
|
|
}
|