Commit be0df7ff authored by Wolfgang Ziegler's avatar Wolfgang Ziegler
Browse files

Issue #2847646: Add test coverage for widgets.

parent 875ca868
Loading
Loading
Loading
Loading
+5 −1
Changes for src/Plugin/TypedDataFormWidget/TextInputWidget.php: 5 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -5,6 +5,8 @@ namespace Drupal\typed_data\Plugin\TypedDataFormWidget;
use Drupal\Core\Form\SubformStateInterface;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\Core\TypedData\Type\FloatInterface;
use Drupal\Core\TypedData\Type\IntegerInterface;
use Drupal\Core\TypedData\Type\StringInterface;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\typed_data\Form\SubformState;
@@ -39,7 +41,9 @@ class TextInputWidget extends FormWidgetBase {
   * {@inheritdoc}
   */
  public function isApplicable(DataDefinitionInterface $definition) {
    return is_subclass_of($definition->getClass(), StringInterface::class);
    return is_subclass_of($definition->getClass(), StringInterface::class) ||
      is_subclass_of($definition->getClass(), IntegerInterface::class) ||
      is_subclass_of($definition->getClass(), FloatInterface::class);
  }

  /**
+2 −1
Changes for src/Widget/FormWidgetInterface.php: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@
namespace Drupal\typed_data\Widget;

use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Form\SubformStateInterface;
use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\Core\TypedData\TypedDataInterface;
@@ -11,7 +12,7 @@ use Symfony\Component\Validator\ConstraintViolationListInterface;
/**
 * Interface definition for form widget plugins.
 */
interface FormWidgetInterface extends ConfigurablePluginInterface {
interface FormWidgetInterface extends ConfigurablePluginInterface, PluginInspectionInterface {

  /**
   * Returns if the widget can be used for the provided data.
+7 −6
Changes for tests/modules/typed_data_widget_test/src/FormWidgetExampleForm.php: 7 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -50,19 +50,20 @@ class FormWidgetExampleForm extends FormBase {
   */
  public function buildForm(array $form, FormStateInterface $form_state, $widget_id = NULL) {
    $widget = $this->getFormWidgetManager()->createInstance($widget_id);
    $context_definition = $this->getExampleContextDefinition($widget_id);
    $form_state->set('widget_id', $widget_id);
    $form_state->set('context_definition', $context_definition);


    // Read and write widget configuration into the state.
    // @todo: Create a StateTrait in core and use it here.
    $state = \Drupal::state();
    $value = $state->get('typed_data_wigets.' . $widget_id);
    // Allow tests to define a custom context definition.
    $context_definition = $state->get('typed_data_widgets.definition');
    $context_definition = $context_definition ?: $this->getExampleContextDefinition($widget_id);
    $form_state->set('widget_id', $widget_id);
    $form_state->set('context_definition', $context_definition);

    // Create a typed data object.
    $data = $this->getTypedDataManager()
      ->create($context_definition->getDataDefinition());
    $value = $state->get('typed_data_widgets.' . $widget_id);
    $value = isset($value) ? $value : $context_definition->getDefaultValue();
    $data->setValue($value);

@@ -113,7 +114,7 @@ class FormWidgetExampleForm extends FormBase {
    // Read and write widget configuration into the state.
    // @todo: Create a StateTrait in core and use it here.
    $state = \Drupal::state();
    $state->set('typed_data_wigets.' . $widget_id, $data->getValue());
    $state->set('typed_data_widgets.' . $widget_id, $data->getValue());
    drupal_set_message('Value saved');
  }
}
 No newline at end of file
+77 −0
Changes for tests/src/BrowserTestHelpersTrait.php: 77 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\typed_data;

/**
 * Provides some helpers for BrowserTestBase.
 */
trait BrowserTestHelpersTrait {

  /**
   * Finds link with specified locator.
   *
   * @param string $locator
   *   Link id, title, text or image alt.
   *
   * @return \Behat\Mink\Element\NodeElement|null
   *   The link node element.
   */
  public function findLink($locator) {
    return $this->getSession()->getPage()->findLink($locator);
  }

  /**
   * Finds field (input, textarea, select) with specified locator.
   *
   * @param string $locator
   *   Input id, name or label.
   *
   * @return \Behat\Mink\Element\NodeElement|null
   *   The input field element.
   */
  public function findField($locator) {
    return $this->getSession()->getPage()->findField($locator);
  }

  /**
   * Finds button with specified locator.
   *
   * @param string $locator
   *   Button id, value or alt.
   *
   * @return \Behat\Mink\Element\NodeElement|null
   *   The button node element.
   */
  public function findButton($locator) {
    return $this->getSession()->getPage()->findButton($locator);
  }

  /**
   * Presses button with specified locator.
   *
   * @param string $locator
   *   Button id, value or alt.
   *
   * @throws \Behat\Mink\Exception\ElementNotFoundException
   */
  public function pressButton($locator) {
    $this->getSession()->getPage()->pressButton($locator);
  }

  /**
   * Fills in field (input, textarea, select) with specified locator.
   *
   * @param string $locator
   *   Input id, name or label.
   * @param string $value
   *   Value.
   *
   * @throws \Behat\Mink\Exception\ElementNotFoundException
   *
   * @see \Behat\Mink\Element\NodeElement::setValue
   */
  public function fillField($locator, $value) {
    $this->getSession()->getPage()->fillField($locator, $value);
  }

}
 No newline at end of file
+99 −0
Changes for tests/src/Functional/TypedDataFormWidget/SelectWidgetTest.php: 99 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\typed_data\Functional\TypedDataFormWidget;

use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\TypedData\ListDataDefinition;
use Drupal\Core\TypedData\MapDataDefinition;
use Drupal\Core\TypedData\TypedDataTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\typed_data\BrowserTestHelpersTrait;
use Drupal\typed_data\Widget\FormWidgetManagerTrait;

/**
 * Class SelectWidgetTest.
 *
 * @group typed_data
 *
 * @coversDefaultClass \Drupal\typed_data\Plugin\TypedDataFormWidget\SelectWidget
 */
class SelectWidgetTest extends BrowserTestBase {

  use BrowserTestHelpersTrait;
  use FormWidgetManagerTrait;
  use TypedDataTrait;

  /**
   * The tested form widget.
   *
   * @var \Drupal\typed_data\Widget\FormWidgetInterface
   */
  protected $widget;

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = [
    'typed_data',
    'typed_data_widget_test',
    'text',
  ];

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();
    $this->widget = $this->getFormWidgetManager()->createInstance('select');
  }

  /**
   * @covers ::isApplicable
   */
  public function testIsApplicable() {
    $this->assertFalse($this->widget->isApplicable(DataDefinition::create('any')));
    $this->assertFalse($this->widget->isApplicable(DataDefinition::create('binary')));
    $this->assertFalse($this->widget->isApplicable(DataDefinition::create('boolean')));
    $this->assertFalse($this->widget->isApplicable(DataDefinition::create('datetime_iso8601')));;
    $this->assertFalse($this->widget->isApplicable(DataDefinition::create('duration_iso8601')));
    $this->assertFalse($this->widget->isApplicable(DataDefinition::create('email')));
    $this->assertFalse($this->widget->isApplicable(DataDefinition::create('float')));
    $this->assertFalse($this->widget->isApplicable(DataDefinition::create('integer')));
    $this->assertFalse($this->widget->isApplicable(DataDefinition::create('string')));
    $this->assertFalse($this->widget->isApplicable(DataDefinition::create('timespan')));
    $this->assertFalse($this->widget->isApplicable(DataDefinition::create('timestamp')));
    $this->assertFalse($this->widget->isApplicable(DataDefinition::create('uri')));
    $this->assertFalse($this->widget->isApplicable(ListDataDefinition::create('string')));
    $this->assertFalse($this->widget->isApplicable(MapDataDefinition::create()));
    $this->assertTrue($this->widget->isApplicable(DataDefinition::create('filter_format')));
  }

  /**
   * @covers ::form
   * @covers ::extractFormValues
   */
  public function testFormEditing() {
    $context_definition = ContextDefinition::create('filter_format')
      ->setLabel('Filter format')
      ->setDescription('Some example selection.');
    \Drupal::state()->set('typed_data_widgets.definition', $context_definition);

    $this->drupalLogin($this->createUser([], NULL, TRUE));
    $path = 'admin/config/user-interface/typed-data-widgets/' . $this->widget->getPluginId();
    $this->drupalGet($path);

    $this->assertSession()->elementTextContains('css', 'label[for=edit-data-value]', $context_definition->getLabel());
    $this->assertSession()->elementTextContains('css', 'div[id=edit-data-value--description]', $context_definition->getDescription());
    $this->assertSession()->fieldValueEquals('data[value]', $context_definition->getDefaultValue());

    $this->getSession()->getPage()->selectFieldOption('data[value]', 'plain_text');
    $this->pressButton('Submit');

    $this->drupalGet($path);
    $this->assertSession()->fieldValueEquals('data[value]', 'plain_text');
  }

}
Loading