/**
  * @covers ::getDefinitions
  */
 public function testGetDefinitions()
 {
     $definitions = array('foo' => array('label' => $this->randomMachineName()));
     $this->discovery->expects($this->once())->method('getDefinitions')->willReturn($definitions);
     $this->moduleHandler->expects($this->once())->method('alter')->with('payment_line_item');
     $this->assertSame($definitions, $this->sut->getDefinitions());
 }
 /**
  * @covers ::render
  */
 public function testRender()
 {
     $plugin_id = $this->randomMachineName();
     $plugin_label = $this->randomMachineName();
     $plugin_definition = ['label' => $plugin_label];
     $this->paymentLineItemManager->expects($this->atLeastOnce())->method('getDefinition')->with($plugin_id)->willReturn($plugin_definition);
     $result_row = new ResultRow();
     $result_row->{$this->sut->field_alias} = $plugin_id;
     $this->assertSame($plugin_label, $this->sut->render($result_row));
 }
 /**
  * {@inheritdoc}
  */
 public function createPayment(FieldDefinitionInterface $field_definition)
 {
     /** @var \Drupal\payment\Entity\PaymentInterface $payment */
     $payment = $this->entityManager->getStorage('payment')->create(['bundle' => 'payment_reference']);
     /** @var \Drupal\payment_reference\Plugin\Payment\Type\PaymentReference $payment_type */
     $payment_type = $payment->getPaymentType();
     $payment_type->setEntityTypeId($field_definition->getFieldStorageDefinition()->getTargetEntityTypeId());
     $payment_type->setBundle($field_definition->getTargetBundle());
     $payment_type->setFieldName($field_definition->getName());
     $payment->setCurrencyCode($field_definition->getSetting('currency_code'));
     foreach ($field_definition->getSetting('line_items_data') as $line_item_data) {
         $line_item = $this->paymentLineItemManager->createInstance($line_item_data['plugin_id'], $line_item_data['plugin_configuration']);
         $payment->setLineItem($line_item);
     }
     return $payment;
 }
 /**
  * @covers ::createPayment
  */
 public function testCreatePayment()
 {
     $bundle = $this->randomMachineName();
     $currency_code = $this->randomMachineName();
     $entity_type_id = $this->randomMachineName();
     $field_name = $this->randomMachineName();
     $payment_type = $this->getMockBuilder(PaymentReference::class)->disableOriginalConstructor()->getMock();
     $payment_type->expects($this->once())->method('setBundle')->with($bundle);
     $payment_type->expects($this->once())->method('setEntityTypeId')->with($entity_type_id);
     $payment_type->expects($this->once())->method('setFieldName')->with($field_name);
     $payment = $this->getMock(PaymentInterface::class);
     $payment->expects($this->once())->method('setCurrencyCode')->with($currency_code);
     $payment->expects($this->once())->method('getPaymentType')->willReturn($payment_type);
     $payment_storage = $this->getMock(EntityStorageInterface::class);
     $payment_storage->expects($this->once())->method('create')->with(array('bundle' => 'payment_reference'))->willReturn($payment);
     $this->entityManager->expects($this->once())->method('getStorage')->with('payment')->willReturn($payment_storage);
     $line_item_a = $this->getMock(PaymentLineItemInterface::class);
     $line_item_plugin_id_a = $this->randomMachineName();
     $line_item_plugin_configuration_a = array('foo' => $this->randomMachineName());
     $line_item_b = $this->getMock(PaymentLineItemInterface::class);
     $line_item_plugin_id_b = $this->randomMachineName();
     $line_item_plugin_configuration_b = array('bar' => $this->randomMachineName());
     $field_storage_definition = $this->getMock(FieldStorageDefinitionInterface::class);
     $field_storage_definition->expects($this->once())->method('getTargetEntityTypeId')->willReturn($entity_type_id);
     $field_definition = $this->getMock(FieldDefinitionInterface::class);
     $field_definition->expects($this->once())->method('getTargetBundle')->willReturn($bundle);
     $field_definition->expects($this->once())->method('getFieldStorageDefinition')->willReturn($field_storage_definition);
     $field_definition->expects($this->once())->method('getName')->willreturn($field_name);
     $map = array(array('currency_code', $currency_code), array('line_items_data', array(array('plugin_configuration' => $line_item_plugin_configuration_a, 'plugin_id' => $line_item_plugin_id_a), array('plugin_configuration' => $line_item_plugin_configuration_b, 'plugin_id' => $line_item_plugin_id_b))));
     $field_definition->expects($this->exactly(2))->method('getSetting')->willReturnMap($map);
     $this->paymentLineItemManager->expects($this->at(0))->method('createInstance')->with($line_item_plugin_id_a, $line_item_plugin_configuration_a)->willReturn($line_item_a);
     $this->paymentLineItemManager->expects($this->at(1))->method('createInstance')->with($line_item_plugin_id_b, $line_item_plugin_configuration_b)->willReturn($line_item_b);
     $this->assertSame($payment, $this->sut->createPayment($field_definition));
 }
 /**
  * Implements form #submit callback.
  */
 public function addMoreSubmit(array &$form, FormStateInterface $form_state)
 {
     $triggering_element = $form_state->getTriggeringElement();
     $parents = array_slice($triggering_element['#array_parents'], 0, -2);
     $root_element = NestedArray::getValue($form, $parents);
     $values = $form_state->getValues();
     $values = NestedArray::getValue($values, array_slice($triggering_element['#parents'], 0, -2));
     $line_item = $this->paymentLineItemManager->createInstance($values['add_more']['type']);
     $line_item->setName(static::createLineItemName($root_element, $form_state, $values['add_more']['type']));
     $line_items = static::getLineItems($root_element, $form_state);
     $line_items[] = $line_item;
     static::setLineItems($root_element, $form_state, $line_items);
     $form_state->setRebuild();
 }
 /**
  * Tests getAmount().
  */
 protected function testGetAmount()
 {
     $amount = 3;
     $quantity = 2;
     for ($i = 0; $i < 2; $i++) {
         /** @var \Drupal\payment\Plugin\Payment\LineItem\Basic $line_item */
         $line_item = $this->lineItemManager->createInstance('payment_basic');
         $name = $this->randomMachineName();
         $line_item->setName($name);
         $line_item->setAmount($amount);
         $line_item->setQuantity($quantity);
         $this->payment->setLineItem($line_item);
     }
     $this->assertEqual($this->payment->getAmount(), 12);
 }
 /**
  * @covers ::addMoreSubmit
  */
 public function testAddMoreSubmit()
 {
     $plugin_id = $this->randomMachineName();
     $values = array('add_more' => array('type' => $plugin_id));
     $line_item = $this->getMock(PaymentLineItemInterface::class);
     $line_item->expects($this->once())->method('setName')->with($plugin_id);
     $this->paymentLineItemManager->expects($this->once())->method('createInstance')->with($plugin_id)->willReturn($line_item);
     $form_build = array('foo' => array('#name' => $this->randomMachineName(), 'add_more' => array('add' => array('#array_parents' => array('foo', 'add_more', 'add'), '#parents' => []))));
     $form_state = new FormState();
     $form_state->setTriggeringElement($form_build['foo']['add_more']['add']);
     $form_state->setValues($values);
     $this->sut->addMoreSubmit($form_build, $form_state);
     $this->assertTrue($form_state->isRebuilding());
     $element = $this->sut;
     $line_items = $element::getLineItems($form_build['foo'], $form_state);
     $this->assertTrue(in_array($line_item, $line_items, TRUE));
 }
 /**
  * @covers ::viewElements
  */
 public function testViewElements()
 {
     $entity_type_id = $this->randomMachineName();
     $bundle = $this->randomMachineName();
     $field_name = $this->randomMachineName();
     $destination_url = $this->randomMachineName();
     $currency_code = $this->randomMachineName();
     $plugin_id = $this->randomMachineName();
     $plugin_configuration = [$this->randomMachineName() => $this->randomMachineName()];
     $plugin_id_property = $this->getMock(TypedDataInterface::class);
     $plugin_id_property->expects($this->once())->method('getValue')->willReturn($plugin_id);
     $plugin_configuration_property = $this->getMock(TypedDataInterface::class);
     $plugin_configuration_property->expects($this->once())->method('getValue')->willReturn($plugin_configuration);
     $map = [['plugin_id', $plugin_id_property], ['plugin_configuration', $plugin_configuration_property]];
     $item = $this->getMockBuilder(PaymentFormFieldType::class)->disableOriginalConstructor()->getMock();
     $item->expects($this->exactly(2))->method('get')->willReturnMap($map);
     $entity = $this->getMock(EntityInterface::class);
     $entity->expects($this->atLeastOnce())->method('bundle')->willReturn($bundle);
     $entity->expects($this->atLeastOnce())->method('getEntityTypeId')->willReturn($entity_type_id);
     $iterator = new \ArrayIterator([$item]);
     $items = $this->getMockBuilder(FieldItemList::class)->disableOriginalConstructor()->setMethods(['getEntity', 'getIterator'])->getMock();
     $items->expects($this->atLeastOnce())->method('getEntity')->willReturn($entity);
     $items->expects($this->atLeastOnce())->method('getIterator')->willReturn($iterator);
     $this->fieldDefinition->expects($this->once())->method('getName')->willReturn($field_name);
     $this->fieldDefinition->expects($this->atLeastOnce())->method('getSetting')->with('currency_code')->willReturn($currency_code);
     $payment_type = $this->getMockBuilder(PaymentFormPaymentType::class)->disableOriginalConstructor()->getMock();
     $payment_type->expects($this->once())->method('setEntityTypeId')->with($entity_type_id);
     $payment_type->expects($this->once())->method('setBundle')->with($bundle);
     $payment_type->expects($this->once())->method('setFieldName')->with($field_name);
     $payment_type->expects($this->once())->method('setDestinationUrl')->with($destination_url);
     $payment = $this->getMock(PaymentInterface::class);
     $payment->expects($this->once())->method('setCurrencyCode')->with($currency_code);
     $payment->expects($this->once())->method('getPaymentType')->willReturn($payment_type);
     $payment_line_item = $this->getMock(PaymentLineItemInterface::class);
     $this->paymentLineItemManager->expects($this->once())->method('createInstance')->with($plugin_id, $plugin_configuration)->willReturn($payment_line_item);
     $this->paymentStorage->expects($this->once())->method('create')->with(['bundle' => 'payment_form'])->willReturn($payment);
     $this->request->expects($this->atLeastOnce())->method('getUri')->willReturn($destination_url);
     $form = ['#foo' => $this->randomMachineName()];
     $this->entityFormBuilder->expects($this->once())->method('getForm')->with($payment, 'payment_form')->willReturn($form);
     $this->assertSame($form, $this->sut->viewElements($items, 'en'));
 }
 /**
  * {@inheritdoc}
  */
 public function viewElements(FieldItemListInterface $items, $langcode)
 {
     $entity_type_id = $items->getEntity()->getEntityTypeId();
     $bundle = $items->getEntity()->bundle();
     $field_name = $this->fieldDefinition->getName();
     /** @var \Drupal\payment\Entity\PaymentInterface $payment */
     $payment = $this->paymentStorage->create(['bundle' => 'payment_form']);
     $payment->setCurrencyCode($this->fieldDefinition->getSetting('currency_code'));
     /** @var \Drupal\payment_form\Plugin\Payment\Type\PaymentForm $payment_type */
     $payment_type = $payment->getPaymentType();
     $payment_type->setDestinationUrl($this->requestStack->getCurrentRequest()->getUri());
     $payment_type->setEntityTypeId($entity_type_id);
     $payment_type->setBundle($bundle);
     $payment_type->setFieldName($field_name);
     foreach ($items as $item) {
         /** @var \Drupal\payment_form\Plugin\Field\FieldType\PaymentForm $item */
         $plugin_id = $item->get('plugin_id')->getValue();
         if ($plugin_id) {
             $payment->setLineItem($this->paymentLineItemManager->createInstance($plugin_id, $item->get('plugin_configuration')->getValue()));
         }
     }
     return $this->entityFormBuilder->getForm($payment, 'payment_form');
 }
 /**
  * {@inheritdoc}
  */
 public function render(ResultRow $values)
 {
     $plugin_id = $this->getValue($values);
     $plugin_definition = $this->paymentLineItemManager->getDefinition($plugin_id);
     return $plugin_definition['label'];
 }