Add automated tests for map form element.

This commit is contained in:
Michael Stenta 2022-06-16 10:58:37 -04:00
parent 0516346d23
commit 26c9746681
4 changed files with 147 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,10 @@
name: farmOS map tests
type: module
description: Support module for farmOS map testing.
package: Testing
core_version_requirement: ^8.8 || ^9
dependencies:
- farm:farm_location
- farm:farm_map
- farm:farm_quick
- log:log

View File

@ -0,0 +1,67 @@
<?php
namespace Drupal\farm_map_test\Plugin\QuickForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\farm_quick\Plugin\QuickForm\QuickFormBase;
use Drupal\farm_quick\Traits\QuickLogTrait;
/**
* 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 {
use QuickLogTrait;
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
// Geometry field with a default value and no raw data textfield.
$form['geometry1'] = [
'#type' => 'farm_map_wkt',
'#title' => $this->t('Geometry 1'),
'#display_raw_geometry' => FALSE,
'#default_value' => 'POINT(-42.689862437640826 32.621823310499934)',
];
// Geometry field without a default value and a raw data field.
$form['geometry2'] = [
'#type' => 'farm_map_wkt',
'#title' => $this->t('Geometry 2'),
'#display_raw_geometry' => TRUE,
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Create two logs.
$this->createLog([
'type' => 'test',
'name' => 'Test 1',
'geometry' => $form_state->getValue('geometry1'),
]);
$this->createLog([
'type' => 'test',
'name' => 'Test 2',
'geometry' => $form_state->getValue('geometry2'),
]);
}
}

View File

@ -0,0 +1,62 @@
<?php
namespace Drupal\Tests\farm_map\Functional;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Tests\farm_test\Functional\FarmBrowserTestBase;
/**
* Tests the farmOS map form element.
*
* @group farm
*/
class MapFormTest extends FarmBrowserTestBase {
use StringTranslationTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'farm_map_test',
];
/**
* Test the farmOS map form element.
*/
public function testMapForm() {
// Create and login a test user with permission to create test logs.
$user = $this->createUser(['create test log']);
$this->drupalLogin($user);
// Go to the test quick form and confirm that both of the geometry fields
// are visible.
$this->drupalGet('quick/test');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains($this->t('Geometry 1'));
$this->assertSession()->pageTextContains($this->t('Geometry 2'));
// Submit the form with a value for the second geometry.
$edit = ['geometry2[value]' => 'POINT(-45.967095060886315 32.77503850904169)'];
$this->submitForm($edit, 'Submit');
// Load logs.
$logs = \Drupal::entityTypeManager()->getStorage('log')->loadMultiple();
// Confirm that two logs were created.
$this->assertCount(2, $logs);
// Check that the first log's geometry was populated with the form field's
// default value.
$log = $logs[1];
$this->assertEquals('POINT(-42.689862437640826 32.621823310499934)', $log->get('geometry')->value);
// Check that the second log's geometry field was populated with the value
// entered into the form.
$log = $logs[2];
$this->assertEquals('POINT(-45.967095060886315 32.77503850904169)', $log->get('geometry')->value);
}
}