/**
  * {@inheritdoc}
  */
 public function getDerivativeDefinitions($base_plugin_definition)
 {
     /** @var \Drupal\payment\Entity\PaymentMethodConfigurationInterface[] $payment_methods */
     $payment_methods = $this->paymentMethodConfigurationStorage->loadMultiple();
     foreach ($payment_methods as $payment_method) {
         if ($payment_method->getPluginId() == 'payment_basic') {
             /** @var \Drupal\payment\Plugin\Payment\MethodConfiguration\Basic $configuration_plugin */
             $configuration_plugin = $this->paymentMethodConfigurationManager->createInstance($payment_method->getPluginId(), $payment_method->getPluginConfiguration());
             $this->derivatives[$payment_method->id()] = array('id' => $base_plugin_definition['id'] . ':' . $payment_method->id(), 'active' => $payment_method->status(), 'label' => $configuration_plugin->getBrandLabel() ? $configuration_plugin->getBrandLabel() : $payment_method->label(), 'message_text' => $configuration_plugin->getMessageText(), 'message_text_format' => $configuration_plugin->getMessageTextFormat(), 'execute_status_id' => $configuration_plugin->getExecuteStatusId(), 'capture' => $configuration_plugin->getCapture(), 'capture_status_id' => $configuration_plugin->getCaptureStatusId(), 'refund' => $configuration_plugin->getRefund(), 'refund_status_id' => $configuration_plugin->getRefundStatusId()) + $base_plugin_definition;
         }
     }
     return $this->derivatives;
 }
 /**
  * Checks access to self::select().
  *
  * @return \Drupal\Core\Access\AccessResultInterface
  *   The access result.
  */
 public function access()
 {
     $definitions = $this->paymentMethodConfigurationManager->getDefinitions();
     unset($definitions['payment_unavailable']);
     $access_controller = $this->entityManager->getAccessControlHandler('payment_method_configuration');
     $access_result = AccessResult::forbidden();
     foreach (array_keys($definitions) as $plugin_id) {
         $access_result = $access_controller->createAccess($plugin_id, $this->currentUser, [], TRUE);
         if ($access_result->isAllowed()) {
             return $access_result;
         }
     }
     return $access_result;
 }
 /**
  * {@inheritdoc}
  */
 public function buildRow(EntityInterface $entity)
 {
     /** @var \Drupal\payment\Entity\PaymentMethodConfigurationInterface $payment_method_configuration */
     $payment_method_configuration = $entity;
     $row['data']['label'] = $payment_method_configuration->label();
     $plugin_definition = $this->paymentMethodConfigurationManager->getDefinition($payment_method_configuration->getPluginId());
     $row['data']['plugin'] = isset($plugin_definition['label']) ? $plugin_definition['label'] : $this->t('Unknown');
     $row['data']['owner']['data'] = array('#theme' => 'username', '#account' => $payment_method_configuration->getOwner());
     $row['data']['status'] = $payment_method_configuration->status() ? $this->t('Enabled') : $this->t('Disabled');
     $operations = $this->buildOperations($entity);
     $row['data']['operations']['data'] = $operations;
     if (!$payment_method_configuration->status()) {
         $row['class'] = array('payment-method-configuration-disabled');
     }
     return $row;
 }
 /**
  * @covers ::form
  *
  * @dataProvider providerTestForm
  */
 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->getMock(UserInterface::class);
     $payment_method_configuration_plugin = $this->getMock(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->getMock(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);
 }
 /**
  * {@inheritdoc}
  */
 public function form(array $form, FormStateInterface $form_state)
 {
     /** @var \Drupal\payment\Entity\PaymentMethodConfigurationInterface $payment_method_configuration */
     $payment_method_configuration = $this->getEntity();
     $definition = $this->paymentMethodConfigurationManager->getDefinition($payment_method_configuration->getPluginId());
     $form['type'] = array('#type' => 'item', '#title' => $this->t('Type'), '#markup' => $definition['label']);
     $form['status'] = array('#type' => 'checkbox', '#title' => $this->t('Enabled'), '#default_value' => $payment_method_configuration->status());
     $form['label'] = array('#type' => 'textfield', '#title' => $this->t('Label'), '#default_value' => $payment_method_configuration->label(), '#maxlength' => 255, '#required' => TRUE);
     $form['id'] = array('#type' => 'machine_name', '#default_value' => $payment_method_configuration->id(), '#maxlength' => 255, '#required' => TRUE, '#machine_name' => array('source' => array('label'), 'exists' => array($this, 'paymentMethodConfigurationIdExists')), '#disabled' => !$payment_method_configuration->isNew());
     $form['owner'] = array('#target_type' => 'user', '#type' => 'entity_autocomplete', '#title' => $this->t('Owner'), '#default_value' => $payment_method_configuration->getOwner() ? $payment_method_configuration->getOwner() : $this->currentUser, '#required' => TRUE);
     if ($form_state->has('payment_method_configuration')) {
         $payment_method_configuration_plugin = $form_state->get('payment_method_configuration');
     } else {
         $payment_method_configuration_plugin = $this->paymentMethodConfigurationManager->createInstance($payment_method_configuration->getPluginId(), $payment_method_configuration->getPluginConfiguration());
         $form_state->set('payment_method_configuration', $payment_method_configuration_plugin);
     }
     $form['plugin_form'] = array('#tree' => TRUE) + $payment_method_configuration_plugin->buildConfigurationForm([], $form_state);
     return parent::form($form, $form_state);
 }
 /**
  * @covers ::access
  */
 public function testAccess()
 {
     $definitions = ['payment_unavailable' => [], 'foo' => ['description' => $this->randomMachineName(), 'label' => $this->randomMachineName()], 'bar' => ['description' => $this->randomMachineName(), 'label' => $this->randomMachineName()]];
     $this->paymentMethodConfigurationManager->expects($this->exactly(2))->method('getDefinitions')->willReturn($definitions);
     $access_control_handler = $this->getMock(EntityAccessControlHandlerInterface::class);
     $access_control_handler->expects($this->at(0))->method('createAccess')->with('foo', $this->currentUser, [], TRUE)->willReturn(AccessResult::allowed());
     $access_control_handler->expects($this->at(1))->method('createAccess')->with('foo', $this->currentUser, [], TRUE)->willReturn(AccessResult::forbidden());
     $access_control_handler->expects($this->at(2))->method('createAccess')->with('bar', $this->currentUser, [], TRUE)->willReturn(AccessResult::forbidden());
     $this->entityManager->expects($this->exactly(2))->method('getAccessControlHandler')->with('payment_method_configuration')->willReturn($access_control_handler);
     $this->assertTrue($this->sut->access()->isAllowed());
     $this->assertFalse($this->sut->access()->isAllowed());
 }
 /**
  * @covers ::buildRow
  */
 function testBuildRow()
 {
     $payment_method_configuration_entity_label = $this->randomMachineName();
     $payment_method_configuration_entity_status = FALSE;
     $payment_method_configuration_plugin_id = $this->randomMachineName();
     $payment_method_configuration_plugin_label = $this->randomMachineName();
     $payment_method_configuration_plugin_definition = array('label' => $payment_method_configuration_plugin_label);
     $this->paymentMethodConfigurationManager->expects($this->any())->method('getDefinition')->with($payment_method_configuration_plugin_id)->willReturn($payment_method_configuration_plugin_definition);
     $owner = $this->getMockBuilder(UserInterface::class);
     $payment_method_configuration = $this->getMock(PaymentMethodConfigurationInterface::class);
     $payment_method_configuration->expects($this->any())->method('getOwner')->willReturn($owner);
     $payment_method_configuration->expects($this->any())->method('getPluginId')->willReturn($payment_method_configuration_plugin_id);
     $payment_method_configuration->expects($this->any())->method('getPaymentStatus')->willReturn($payment_method_configuration_entity_status);
     $payment_method_configuration->expects($this->any())->method('label')->willReturn($payment_method_configuration_entity_label);
     $this->moduleHandler->expects($this->any())->method('invokeAll')->willReturn([]);
     $build = $this->sut->buildRow($payment_method_configuration);
     unset($build['data']['operations']['data']['#attached']);
     $expected_build = array('data' => array('label' => $payment_method_configuration_entity_label, 'plugin' => $payment_method_configuration_plugin_label, 'owner' => array('data' => array('#theme' => 'username', '#account' => $owner)), 'operations' => array('data' => array('#type' => 'operations', '#links' => []))), 'class' => array('payment-method-configuration-disabled'));
     $this->assertInstanceOf(TranslatableMarkup::class, $build['data']['status']);
     unset($build['data']['status']);
     $this->assertSame($expected_build, $build);
 }
 /**
  * @covers ::getDerivativeDefinitions
  */
 public function testGetDerivativeDefinitions()
 {
     $id_enabled_basic = $this->randomMachineName();
     $id_disabled_basic = $this->randomMachineName();
     $brand_label = $this->randomMachineName();
     $message_text = $this->randomMachineName();
     $message_text_format = $this->randomMachineName();
     $execute_status_id = $this->randomMachineName();
     $capture = TRUE;
     $capture_status_id = $this->randomMachineName();
     $refund = TRUE;
     $refund_status_id = $this->randomMachineName();
     $payment_method_enabled_basic = $this->getMock(PaymentMethodConfigurationInterface::class);
     $payment_method_enabled_basic->expects($this->any())->method('status')->willReturn(TRUE);
     $payment_method_enabled_basic->expects($this->any())->method('id')->willReturn($id_enabled_basic);
     $payment_method_enabled_basic->expects($this->any())->method('getPluginConfiguration')->willReturn(['brand_label' => $brand_label, 'message_text' => $message_text, 'message_text_format' => $message_text_format, 'execute_status_id' => $execute_status_id, 'capture' => $capture, 'capture_status_id' => $capture_status_id, 'refund' => $refund, 'refund_status_id' => $refund_status_id]);
     $payment_method_enabled_basic->expects($this->any())->method('getPluginId')->willReturn('payment_basic');
     $payment_method_disabled_basic = $this->getMock(PaymentMethodConfigurationInterface::class);
     $payment_method_disabled_basic->expects($this->any())->method('status')->willReturn(FALSE);
     $payment_method_disabled_basic->expects($this->any())->method('id')->willReturn($id_disabled_basic);
     $payment_method_disabled_basic->expects($this->any())->method('getPluginConfiguration')->willReturn(['brand_label' => $brand_label, 'message_text' => $message_text, 'message_text_format' => $message_text_format, 'execute_status_id' => $execute_status_id, 'capture' => $capture, 'capture_status_id' => $capture_status_id, 'refund' => $refund, 'refund_status_id' => $refund_status_id]);
     $payment_method_disabled_basic->expects($this->any())->method('getPluginId')->willReturn('payment_basic');
     $payment_method_enabled_no_basic = $this->getMock(PaymentMethodConfigurationInterface::class);
     $payment_method_enabled_no_basic->expects($this->any())->method('status')->willReturn(TRUE);
     $payment_method_enabled_no_basic->expects($this->any())->method('getPluginId')->willReturn($this->randomMachineName());
     $this->paymentMethodConfigurationStorage->expects($this->once())->method('loadMultiple')->willReturn(array($payment_method_enabled_basic, $payment_method_enabled_no_basic, $payment_method_disabled_basic));
     $payment_method_plugin = $this->getMockBuilder(Basic::class)->disableOriginalConstructor()->getMock();
     $payment_method_plugin->expects($this->any())->method('getBrandLabel')->willReturn($brand_label);
     $payment_method_plugin->expects($this->any())->method('getMessageText')->willReturn($message_text);
     $payment_method_plugin->expects($this->any())->method('getMessageTextFormat')->willReturn($message_text_format);
     $payment_method_plugin->expects($this->any())->method('getExecuteStatusId')->willReturn($execute_status_id);
     $payment_method_plugin->expects($this->any())->method('getCaptureStatusId')->willReturn($capture_status_id);
     $payment_method_plugin->expects($this->any())->method('getCapture')->willReturn($capture);
     $payment_method_plugin->expects($this->any())->method('getRefundStatusId')->willReturn($refund_status_id);
     $payment_method_plugin->expects($this->any())->method('getRefund')->willReturn($refund);
     $this->paymentMethodConfigurationManager->expects($this->any())->method('createInstance')->with('payment_basic')->willReturn($payment_method_plugin);
     $class = $this->randomMachineName();
     $derivatives = $this->sut->getDerivativeDefinitions(array('class' => $class, 'id' => $this->randomMachineName()));
     $this->assertInternalType('array', $derivatives);
     $this->assertCount(2, $derivatives);
     $map = array($id_enabled_basic => TRUE, $id_disabled_basic => FALSE);
     foreach ($map as $id => $active) {
         $this->assertArrayHasKey($id, $derivatives);
         $this->assertArrayHasKey('active', $derivatives[$id]);
         $this->assertSame($active, $derivatives[$id]['active']);
         $this->assertArrayHasKey('class', $derivatives[$id]);
         $this->assertSame($class, $derivatives[$id]['class']);
         $this->assertArrayHasKey('label', $derivatives[$id]);
         $this->assertSame($brand_label, $derivatives[$id]['label']);
         $this->assertArrayHasKey('message_text', $derivatives[$id]);
         $this->assertSame($message_text, $derivatives[$id]['message_text']);
         $this->assertArrayHasKey('message_text_format', $derivatives[$id]);
         $this->assertSame($message_text_format, $derivatives[$id]['message_text_format']);
         $this->assertArrayHasKey('execute_status_id', $derivatives[$id]);
         $this->assertSame($execute_status_id, $derivatives[$id]['execute_status_id']);
         $this->assertArrayHasKey('capture', $derivatives[$id]);
         $this->assertSame($capture, $derivatives[$id]['capture']);
         $this->assertArrayHasKey('capture_status_id', $derivatives[$id]);
         $this->assertSame($capture_status_id, $derivatives[$id]['capture_status_id']);
     }
 }
 /**
  * Returns the title for the payment method configuration add form.
  *
  * @param string $plugin_id
  *
  * @return string
  */
 public function title($plugin_id)
 {
     $plugin_definition = $this->paymentMethodConfigurationManager->getDefinition($plugin_id);
     return $this->t('Add %label payment method configuration', ['%label' => $plugin_definition['label']]);
 }
 /**
  * @covers ::title
  */
 public function testTitle()
 {
     $plugin_id = $this->randomMachineName();
     $this->paymentMethodConfigurationManager->expects($this->atLeastOnce())->method('getDefinition')->with($plugin_id)->willReturn(['label' => $this->randomMachineName()]);
     $this->assertInstanceOf(TranslatableMarkup::class, $this->sut->title($plugin_id));
 }