View source
<?php
namespace Drupal\Tests\farm_quick\Kernel;
use Drupal\Core\Form\FormState;
use Drupal\KernelTests\KernelTestBase;
class QuickFormTest extends KernelTestBase {
protected $quickFormManager;
protected static $modules = [
'asset',
'farm_quantity_standard',
'farm_quick',
'farm_quick_test',
'farm_unit',
'fraction',
'log',
'options',
'quantity',
'state_machine',
'taxonomy',
'text',
'user',
];
protected function setUp() : void {
parent::setUp();
$this->quickFormManager = \Drupal::service('plugin.manager.quick_form');
$this
->installEntitySchema('asset');
$this
->installEntitySchema('log');
$this
->installEntitySchema('taxonomy_term');
$this
->installEntitySchema('quantity');
$this
->installEntitySchema('user');
$this
->installConfig([
'farm_quick_test',
]);
}
public function testQuickFormDiscovery() {
$quick_forms = $this->quickFormManager
->getDefinitions();
$this
->assertEquals(1, count($quick_forms));
$test_quick_form = $this->quickFormManager
->createInstance('test');
$this
->assertEquals('Test quick form', $test_quick_form
->getLabel());
$this
->assertEquals('Test quick form description.', $test_quick_form
->getDescription());
$this
->assertEquals('Test quick form help text.', $test_quick_form
->getHelpText());
$this
->assertEquals([
'create test log',
], $test_quick_form
->getPermissions());
}
public function testQuickFormSubmission() {
$form_state = (new FormState())
->setValues([
'count' => '12',
]);
\Drupal::formBuilder()
->submitForm('\\Drupal\\farm_quick\\Form\\QuickForm', $form_state, 'test');
$storage = $form_state
->getStorage();
$this
->assertNotEmpty($storage['assets'][0]
->id());
$this
->assertEquals('test', $storage['assets'][0]->quick[0]);
$this
->assertNotEmpty($storage['logs'][0]
->id());
$this
->assertEquals('test', $storage['logs'][0]->quick[0]);
$this
->assertNotEmpty($storage['quantities'][0]
->id());
$this
->assertEquals(3, count($storage['terms']));
foreach ($storage['terms'] as $term) {
$this
->assertNotEmpty($term
->id());
}
$match = $storage['terms'][1]
->id() == $storage['terms'][2]
->id();
$this
->assertTrue($match);
}
}