You are here

public function PaymentMethodConfigurationFormTest::testForm in Payment 8.2

@covers ::form

@dataProvider providerTestForm

File

tests/src/Unit/Entity/PaymentMethodConfiguration/PaymentMethodConfigurationFormTest.php, line 147

Class

PaymentMethodConfigurationFormTest
@coversDefaultClass \Drupal\payment\Entity\PaymentMethodConfiguration\PaymentMethodConfigurationForm

Namespace

Drupal\Tests\payment\Unit\Entity\PaymentMethodConfiguration

Code

public function testForm($has_owner) {
  $payment_method_configuration_entity_id = $this
    ->randomMachineName();
  $payment_method_configuration_entity_is_new = FALSE;
  $payment_method_configuration_entity_label = $this
    ->randomMachineName();
  $payment_method_configuration_entity_status = TRUE;
  $payment_method_configuration_plugin_form = array(
    '#type' => $this
      ->randomMachineName(),
  );
  $payment_method_configuration_plugin_id = $this
    ->randomMachineName();
  $payment_method_configuration_plugin_configuration = array(
    'foo' => $this
      ->randomMachineName(),
  );
  $payment_method_configuration_plugin_label = $this
    ->randomMachineName();
  $payment_method_configuration_plugin_definition = array(
    'label' => $payment_method_configuration_plugin_label,
  );
  $owner = $this
    ->createMock(UserInterface::class);
  $payment_method_configuration_plugin = $this
    ->createMock(PaymentMethodConfigurationInterfacePlugin::class);
  $form = array(
    'plugin_form' => [],
  );
  $form_state = new FormState();
  $payment_method_configuration_plugin
    ->expects($this
    ->atLeastOnce())
    ->method('buildConfigurationForm')
    ->with([], $form_state)
    ->willReturn($payment_method_configuration_plugin_form);
  $this->paymentMethodConfigurationManager
    ->expects($this
    ->atLeastOnce())
    ->method('getDefinition')
    ->willReturn($payment_method_configuration_plugin_definition);
  $language = $this
    ->createMock(LanguageInterface::class);
  $this->paymentMethodConfiguration
    ->expects($this
    ->atLeastOnce())
    ->method('getOwner')
    ->willReturn($has_owner ? $owner : NULL);
  $this->paymentMethodConfiguration
    ->expects($this
    ->atLeastOnce())
    ->method('getPluginConfiguration')
    ->willReturn($payment_method_configuration_plugin_configuration);
  $this->paymentMethodConfiguration
    ->expects($this
    ->any())
    ->method('getPluginId')
    ->willReturn($payment_method_configuration_plugin_id);
  $this->paymentMethodConfiguration
    ->expects($this
    ->any())
    ->method('id')
    ->willReturn($payment_method_configuration_entity_id);
  $this->paymentMethodConfiguration
    ->expects($this
    ->any())
    ->method('label')
    ->willReturn($payment_method_configuration_entity_label);
  $this->paymentMethodConfiguration
    ->expects($this
    ->any())
    ->method('language')
    ->willReturn($language);
  $this->paymentMethodConfiguration
    ->expects($this
    ->any())
    ->method('status')
    ->willReturn($payment_method_configuration_entity_status);
  $this->paymentMethodConfigurationManager
    ->expects($this
    ->once())
    ->method('createInstance')
    ->with($payment_method_configuration_plugin_id, $payment_method_configuration_plugin_configuration)
    ->willReturn($payment_method_configuration_plugin);
  $build = $this->sut
    ->form($form, $form_state);

  // Make sure the payment method configuration plugin is instantiated only
  // once by building the form twice.
  $this->sut
    ->form($form, $form_state);
  unset($build['#process']);
  unset($build['langcode']);
  $expected_build = array(
    'type' => array(
      '#type' => 'item',
      '#title' => 'Type',
      '#markup' => $payment_method_configuration_plugin_label,
    ),
    'status' => array(
      '#type' => 'checkbox',
      '#title' => 'Enabled',
      '#default_value' => $payment_method_configuration_entity_status,
    ),
    'label' => array(
      '#type' => 'textfield',
      '#title' => 'Label',
      '#default_value' => $payment_method_configuration_entity_label,
      '#maxlength' => 255,
      '#required' => TRUE,
    ),
    'id' => array(
      '#type' => 'machine_name',
      '#default_value' => $payment_method_configuration_entity_id,
      '#maxlength' => 255,
      '#required' => TRUE,
      '#machine_name' => array(
        'source' => array(
          'label',
        ),
        'exists' => array(
          $this->sut,
          'paymentMethodConfigurationIdExists',
        ),
      ),
      '#disabled' => !$payment_method_configuration_entity_is_new,
    ),
    'owner' => array(
      '#target_type' => 'user',
      '#type' => 'entity_autocomplete',
      '#title' => 'Owner',
      '#default_value' => $has_owner ? $owner : $this->currentUser,
      '#required' => TRUE,
    ),
    'plugin_form' => array(
      '#tree' => TRUE,
    ) + $payment_method_configuration_plugin_form,
    '#after_build' => [
      '::afterBuild',
    ],
  );
  $this
    ->assertEquals($expected_build, $build);
}