Add automated tests for quick forms.

Test plugin discovery and access control.
This commit is contained in:
Michael Stenta 2021-07-16 13:09:17 -04:00
parent c407ff70ac
commit f14a6efd07
5 changed files with 191 additions and 0 deletions

View File

@ -0,0 +1,8 @@
langcode: en
status: true
id: test
label: Test
description: ''
name_pattern: 'Test log [log:id]'
workflow: log_default
new_revision: true

View File

@ -0,0 +1,8 @@
name: farmOS quick form tests
type: module
description: Support module for farmOS quick form testing.
package: Testing
core_version_requirement: ^8.8 || ^9
dependencies:
- farm:farm_quick
- log:log

View File

@ -0,0 +1,44 @@
<?php
namespace Drupal\farm_quick_test\QuickForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\farm_quick\QuickFormBase;
/**
* Test quick form.
*
* @QuickForm(
* id = "test",
* label = @Translation("Test quick form"),
* description = @Translation("Test quick form description."),
* helpText = @Translation("Test quick form help text."),
* permissions = {
* "create test log",
* }
* )
*/
class Test extends QuickFormBase {
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, string $id = NULL) {
// Test field.
$form['test'] = [
'#type' => 'number',
'#title' => $this->t('Test field'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}

View File

@ -0,0 +1,64 @@
<?php
namespace Drupal\Tests\farm_quick\Functional;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Tests\farm_test\Functional\FarmBrowserTestBase;
/**
* Tests the quick form framework.
*
* @group farm
*/
class QuickFormTest extends FarmBrowserTestBase {
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'farm_quick_test',
];
/**
* Test quick forms.
*/
public function testQuickForms() {
// Create and login a test user with no permissions.
$user = $this->createUser();
$this->drupalLogin($user);
// Go to the quick form index and confirm that access is denied.
$this->drupalGet('quick');
$this->assertSession()->statusCodeEquals(403);
// Create and login a test user with access to the quick form index.
$user = $this->createUser(['view quick forms index']);
$this->drupalLogin($user);
// Go to the quick form index and confirm that access is granted, but no
// quick forms are visible.
$this->drupalGet('quick');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains($this->t('You do not have any quick forms.'));
// Create and login a test user with access to the quick form index, and
// permission to create test logs.
$user = $this->createUser(['view quick forms index', '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.
$this->drupalGet('quick');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains($this->t('Test 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'));
}
}

View File

@ -0,0 +1,67 @@
<?php
namespace Drupal\Tests\farm_quick\Kernel;
use Drupal\KernelTests\KernelTestBase;
/**
* Tests for farmOS quick forms.
*
* @group farm
*/
class QuickFormTest extends KernelTestBase {
/**
* The quick form manager.
*
* @var \Drupal\farm_quick\QuickFormManager
*/
protected $quickFormManager;
/**
* {@inheritdoc}
*/
protected static $modules = [
'farm_quick',
'farm_quick_test',
'log',
'state_machine',
'user',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->quickFormManager = \Drupal::service('plugin.manager.quick_form');
$this->installEntitySchema('log');
$this->installEntitySchema('user');
$this->installConfig([
'farm_quick_test',
]);
}
/**
* Test quick form discovery.
*/
public function testQuickFormDiscovery() {
// Load quick form definitions.
$quick_forms = $this->quickFormManager->getDefinitions();
// Confirm that one quick form was discovered.
$this->assertEquals(1, count($quick_forms));
// Initialize the test quick form.
/** @var \Drupal\farm_quick\QuickFormInterface $test_quick_form */
$test_quick_form = $this->quickFormManager->createInstance('test');
// Confirm the label, description, helpText, and permissions.
$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());
}
}