/**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     // Nest the element to make sure that works.
     $form['container']['line_item'] = array('#cardinality' => 4, '#default_value' => Generate::createPaymentLineItems(), '#type' => 'payment_line_items_input');
     $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
     return $form;
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     $key = 'payment_reference_element_prototype_payment';
     if ($form_state->has($key)) {
         $prototype_payment = $form_state->get($key);
         /** @var \Drupal\payment_reference\Plugin\Payment\Type\PaymentReference $payment_type */
         $payment_type = $prototype_payment->getPaymentType();
     } else {
         $entity_type_id = 'user';
         $bundle = 'user';
         $field_name = 'foobarbaz';
         /** @var \Drupal\payment\Entity\PaymentInterface $prototype_payment */
         $prototype_payment = entity_create('payment', array('bundle' => 'payment_reference'));
         $prototype_payment->setCurrencyCode('EUR')->setOwnerId(2)->setLineItems(Generate::createPaymentLineItems());
         /** @var \Drupal\payment_reference\Plugin\Payment\Type\PaymentReference $payment_type */
         $payment_type = $prototype_payment->getPaymentType();
         $payment_type->setEntityTypeId($entity_type_id);
         $payment_type->setBundle($bundle);
         $payment_type->setFieldName($field_name);
         $form_state->set($key, $prototype_payment);
     }
     $form['payment_reference'] = array('#plugin_selector_id' => 'payment_select_list', '#prototype_payment' => $prototype_payment, '#queue_category_id' => $payment_type->getEntityTypeId() . '.' . $payment_type->getBundle() . '.' . $payment_type->getFieldName(), '#queue_owner_id' => 2, '#required' => TRUE, '#title' => 'FooBarBaz', '#type' => 'payment_reference');
     $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
     return $form;
 }
 /**
  * Asserts a correct field value.
  */
 protected function assertFieldValue(EntityInterface $entity, $field_name)
 {
     $field = $entity->{$field_name};
     foreach (Generate::createPaymentLineItems() as $i => $line_item) {
         $this->assertTrue(is_string($field[$i]->plugin_id));
         $this->assertTrue(is_array($field[$i]->plugin_configuration));
     }
 }
 /**
  * Tests CRUD();
  */
 protected function testCRUD()
 {
     $database = \Drupal::database();
     $user = $this->drupalCreateUser();
     $payment_type_configuration = array($this->randomMachineName() => $this->randomMachineName());
     $payment_method = Payment::methodManager()->createInstance('payment_basic:no_payment_required');
     // Test creating a payment.
     $payment = Generate::createPayment($user->id(), $payment_method);
     $payment->getPaymentType()->setConfiguration($payment_type_configuration);
     $this->assertTrue($payment instanceof PaymentInterface);
     // @todo The ID should be an integer, but for some reason the entity field
     //   API returns a string.
     $this->assertTrue(is_numeric($payment->getOwnerId()));
     $this->assertEqual(count($payment->validate()), 0);
     $this->assertTrue($payment->getPaymentType() instanceof PaymentTypeInterface);
     // Test saving a payment.
     $this->assertFalse($payment->id());
     // Set an extra status, so we can test for status IDs later.
     $payment->setPaymentStatus(Payment::statusManager()->createInstance('payment_success'));
     $payment->save();
     // @todo The ID should be an integer, but for some reason the entity field
     //   API returns a string.
     $this->assertTrue(is_numeric($payment->id()));
     $this->assertTrue(strlen($payment->uuid()));
     // @todo The ID should be an integer, but for some reason the entity field
     //   API returns a string.
     $this->assertTrue(is_numeric($payment->getOwnerId()));
     // Check references to other tables.
     $payment_data = $database->select('payment', 'p')->fields('p', array('current_payment_status_delta'))->condition('id', $payment->id())->execute()->fetchAssoc();
     $this->assertEqual($payment_data['current_payment_status_delta'], 1);
     /** @var \Drupal\payment\Entity\PaymentInterface $payment_loaded */
     $payment_loaded = entity_load_unchanged('payment', $payment->id());
     $this->assertEqual(count($payment_loaded->getLineItems()), count($payment->getLineItems()));
     foreach ($payment_loaded->getLineItems() as $line_item) {
         $this->assertTrue($line_item instanceof PaymentLineItemInterface);
     }
     $this->assertEqual(count($payment_loaded->getPaymentStatuses()), count($payment->getPaymentStatuses()));
     foreach ($payment_loaded->getPaymentStatuses() as $status) {
         $this->assertTrue($status instanceof PaymentStatusInterface);
     }
     $this->assertEqual($payment_loaded->getPaymentMethod()->getConfiguration(), $payment->getPaymentMethod()->getConfiguration());
     $this->assertEqual($payment_loaded->getPaymentType()->getConfiguration(), $payment->getPaymentType()->getConfiguration());
 }
 /**
  * Tests the widget.
  */
 protected function testWidget()
 {
     // Create the field and field instance.
     $field_name = strtolower($this->randomMachineName());
     entity_create('field_storage_config', array('cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, 'entity_type' => 'user', 'field_name' => $field_name, 'type' => 'payment_reference'))->save();
     entity_create('field_config', array('bundle' => 'user', 'entity_type' => 'user', 'field_name' => $field_name, 'settings' => array('currency_code' => 'EUR', 'line_items' => [])))->save();
     entity_get_form_display('user', 'user', 'default')->setComponent($field_name, [])->save();
     $user = $this->drupalCreateUser(array('payment.payment.view.own'));
     $this->drupalLogin($user);
     $payment_method = Generate::createPaymentMethodConfiguration(mt_rand(), 'payment_basic');
     $payment_method->setPluginConfiguration(array('brand_label' => $this->randomMachineName(), 'execute_status_id' => 'payment_success', 'message_text' => $this->randomMachineName()));
     $payment_method->save();
     // Test the widget when editing an entity.
     $this->drupalGet('user/' . $user->id() . '/edit');
     $this->drupalPostForm(NULL, [], t('Re-check available payments'));
     $this->drupalPostForm(NULL, [], t('Pay'));
     $this->assertNoFieldByXPath('//input[@value="Pay"]');
     $this->assertLinkByHref('payment/1');
 }
 /**
  * Tests the element.
  */
 protected function testElement()
 {
     $state = \Drupal::state();
     $names = [];
     foreach (Generate::createPaymentLineItems() as $line_item) {
         $names[] = $line_item->getName();
     }
     $type = 'payment_basic';
     // Test the presence of default elements.
     $this->drupalGet('payment_test-element-payment-line-item');
     $this->assertLineItemElements($names);
     $this->assertAddMore(TRUE);
     // Add a line item through a regular submission.
     $this->drupalPostForm(NULL, array('line_item[add_more][type]' => $type), t('Add and configure a new line item'));
     $this->assertLineItemElements(array_merge($names, array($type)));
     $this->assertAddMore(FALSE);
     // Delete a line item through a regular submission.
     $this->drupalPostForm(NULL, [], t('Delete'));
     $this->assertLineItemElements($names);
     $elements = $this->xpath('//input[@name="line_item[line_items][' . $type . '][weight]"]');
     $this->assertFalse(isset($elements[0]));
     $this->assertAddMore(TRUE);
     // Change a line item's weight and test the element's value through a
     // regular submission.
     $name = 'line_item[line_items][' . reset($names) . '][weight]';
     $this->assertFieldByXPath('//select[@name="' . $name . '"]/option[@value="0" and @selected="selected"]');
     $this->drupalPostForm(NULL, array($name => count($names)), t('Submit'));
     $value = $state->get('payment_test_line_item_form_element');
     if ($this->assertTrue(is_array($value))) {
         /// We end up with one more line item than we originally had.
         $this->assertEqual(count($value), count($names));
         $line_items = [];
         foreach ($value as $line_item_data) {
             $this->assertTrue(isset($line_item_data['plugin_configuration']));
             $this->assertTrue(is_array($line_item_data['plugin_configuration']));
             $this->assertTrue(isset($line_item_data['plugin_id']));
             $line_items[] = Payment::lineItemManager()->createInstance($line_item_data['plugin_id'], $line_item_data['plugin_configuration']);
         }
         // Check that the first line item is now the last.
         $this->assertEqual(end($line_items)->getName(), reset($names));
     }
 }
 /**
  * Tests the field.
  */
 protected function testField()
 {
     // Create the field and field instance.
     $field_name = strtolower($this->randomMachineName());
     entity_create('field_storage_config', array('cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED, 'entity_type' => 'user', 'field_name' => $field_name, 'type' => 'payment_reference'))->save();
     entity_create('field_config', array('bundle' => 'user', 'entity_type' => 'user', 'field_name' => $field_name, 'settings' => array('currency_code' => 'EUR', 'line_items_data' => [])))->save();
     $payment = Generate::createPayment(mt_rand());
     $payment->setPaymentStatus(Payment::statusManager()->createInstance('payment_success'));
     $payment->save();
     PaymentReference::queue()->save('user.' . $field_name, $payment->id());
     $this->assertEqual(PaymentReference::queue()->loadPaymentIds('user.' . $field_name, $payment->getOwnerId()), array($payment->id()));
     // Set a field value on an entity and test getting it.
     /** @var \Drupal\user\UserInterface $user */
     $user = entity_create('user', array('name' => $this->randomString()));
     $user->get($field_name)->appendItem($payment->id());
     $this->assertEqual($user->get($field_name)->first()->entity->id(), $payment->id());
     // Save the entity, load it from storage and test getting the field value.
     $user->save();
     $user = entity_load_unchanged('user', $user->id());
     $this->assertEqual($user->{$field_name}[0]->target_id, $payment->id());
     $this->assertEqual(PaymentReference::queue()->loadPaymentIds('user.' . $field_name, $payment->getOwnerId()), []);
 }
 /**
  * {@inheritdoc
  */
 protected function setUp()
 {
     parent::setUp();
     $this->paymentStorage = \Drupal::entityManager()->getStorage('payment');
     /** @var \Drupal\currency\ConfigImporterInterface $config_importer */
     $config_importer = \Drupal::service('currency.config_importer');
     $config_importer->importCurrency('EUR');
     // Create the field and field instance.
     $field_name = strtolower($this->randomMachineName());
     entity_create('field_storage_config', ['cardinality' => FieldStorageConfigInterface::CARDINALITY_UNLIMITED, 'entity_type' => 'user', 'field_name' => $field_name, 'type' => 'payment_form'])->save();
     entity_create('field_config', ['bundle' => 'user', 'entity_type' => 'user', 'field_name' => $field_name, 'settings' => ['currency_code' => 'EUR']])->save();
     entity_get_display('user', 'user', 'default')->setComponent($field_name, ['type' => 'payment_form'])->save();
     // Create an entity.
     $this->user = entity_create('user', ['name' => $this->randomString(), 'status' => TRUE]);
     foreach (Generate::createPaymentLineItems() as $line_item) {
         $this->user->get($field_name)->appendItem(['plugin_id' => $line_item->getPluginId(), 'plugin_configuration' => $line_item->getConfiguration()]);
     }
     $this->user->save();
     // Create a payment method.
     $this->paymentMethod = Generate::createPaymentMethodConfiguration(2, 'payment_basic');
     $this->paymentMethod->setPluginConfiguration(['execute_status_id' => $this->executeStatusPluginId]);
     $this->paymentMethod->save();
 }
 /**
  * Tests queue service.
  */
 function testQueue()
 {
     $category_id_prefix = $this->randomMachineName();
     $category_id = $category_id_prefix . $this->randomMachineName();
     $payment = Generate::createPayment(2);
     $payment->setPaymentStatus($this->paymentStatusManager->createInstance('payment_success'));
     $payment->save();
     // Tests save().
     $this->queue->save($category_id, $payment->id());
     $payment_ids = $this->queue->loadPaymentIds($category_id, $payment->getOwnerId());
     $this->assertTrue(in_array($payment->id(), $payment_ids));
     // Tests claimPayment().
     $this->assertTrue(is_string($this->queue->claimPayment($payment->id())));
     $this->assertFalse($this->queue->claimPayment($payment->id()));
     $acquisition_code = $this->queue->claimPayment($payment->id());
     $this->assertTrue(is_string($acquisition_code));
     // Tests releaseClaim().
     $released = $this->queue->releaseClaim($payment->id(), $acquisition_code);
     $this->assertTrue($released);
     $acquisition_code = $this->queue->claimPayment($payment->id());
     $this->assertTrue(is_string($acquisition_code));
     // Tests acquirePayment().
     $acquired = $this->queue->acquirePayment($payment->id(), $acquisition_code);
     $this->assertTrue($acquired);
     $acquisition_code = $this->queue->claimPayment($payment->id());
     $this->assertFalse($acquisition_code);
     // Save another payment to the queue, because acquiring the previous one
     // deleted it.
     $payment = Generate::createPayment(2);
     $payment->setPaymentStatus($this->paymentStatusManager->createInstance('payment_success'));
     $payment->save();
     $this->queue->save($category_id, $payment->id());
     // Tests loadPaymentIds().
     $loaded_payment_ids = $this->queue->loadPaymentIds($category_id, $payment->getOwnerId());
     $this->assertEqual($loaded_payment_ids, array($payment->id()));
     // Tests deleteByPaymentId().
     $this->queue->deleteByPaymentId($payment->id());
     $payment_ids = $this->queue->loadPaymentIds($category_id, $payment->getOwnerId());
     $this->assertFalse(in_array($payment->id(), $payment_ids));
     // Tests deleteByCategoryIdPrefix().
     $this->queue->save($category_id, $payment->id());
     $this->queue->deleteByCategoryIdPrefix($category_id_prefix);
     $payment_ids = $this->queue->loadPaymentIds($category_id, $payment->getOwnerId());
     $this->assertFalse(in_array($payment->id(), $payment_ids));
     // Tests deleteByCategoryId().
     $this->queue->save($category_id, $payment->id());
     $this->queue->deleteByCategoryId($category_id);
     $payment_ids = $this->queue->loadPaymentIds($category_id, $payment->getOwnerId());
     $this->assertFalse(in_array($payment->id(), $payment_ids));
 }
 /**
  * Tests the payment UI.
  */
 protected function testPaymentUi()
 {
     $this->drupalPlaceBlock('local_tasks_block');
     $payment_method = Payment::methodManager()->createInstance('payment_test');
     // Create just enough payments for three pages
     $count_payments = 50 * 2 + 1;
     foreach (range(0, $count_payments) as $i) {
         $payment = Generate::createPayment(2, $payment_method);
         $payment->save();
         $payment = entity_load_unchanged('payment', $payment->id());
     }
     // View the administrative listing.
     $this->drupalLogin($this->drupalCreateUser(array('access administration pages')));
     $this->drupalGet('admin/content');
     $this->assertResponse('200');
     $this->assertNoLinkByHref('admin/content/payment');
     $this->drupalGet('admin/content/payment');
     $this->assertResponse('403');
     $this->drupalLogin($this->drupalCreateUser(array('access administration pages', 'payment.payment.view.any')));
     $this->drupalGet('admin/content');
     $this->clickLink(t('Payments'));
     if ($this->assertResponse('200')) {
         $this->assertTitle(t('Payments | Drupal'));
         $this->assertText(t('Last updated'));
         $this->assertText(t('Payment method'));
         $this->assertText(t('Enter a comma separated list of user names.'));
         $this->assertText(t('EUR 24.20'));
         $this->assertText($payment_method->getPluginLabel());
         $count_pages = ceil($count_payments / 50);
         if ($count_pages) {
             foreach (range(1, $count_pages - 1) as $page) {
                 $this->assertLinkByHref('&page=' . $page);
             }
             $this->assertNoLinkByHref('&page=' . ($page + 1));
         }
         $this->assertLinkByHref('payment/1');
         $this->clickLinkPartialName('Next');
         $this->assertUrl('admin/content/payment?changed_after=&changed_before=&=Apply&page=1');
     }
     $this->drupalLogout();
     // View the payment.
     $path = 'payment/' . $payment->id();
     $this->drupalGet($path);
     $this->assertResponse('403');
     $this->drupalLogin($this->drupalCreateUser(array('payment.payment.view.any')));
     $this->drupalGet($path);
     if ($this->assertResponse('200')) {
         $this->assertText(t('Payment method'));
         $this->assertText(t('Status'));
     }
     // Delete a payment.
     $path = 'payment/' . $payment->id() . '/delete';
     $this->drupalGet($path);
     $this->assertResponse('403');
     $this->drupalLogin($this->drupalCreateUser(array('payment.payment.delete.any', 'payment.payment.view.any')));
     $this->drupalGet($path);
     if ($this->assertResponse('200')) {
         $this->clickLink(t('Cancel'));
         $this->assertUrl('payment/' . $payment->id());
         $this->drupalGet($path);
         $this->drupalPostForm(NULL, [], t('Delete'));
         $this->assertResponse('200');
         $this->assertFalse((bool) \Drupal::entityManager()->getStorage('payment')->loadUnchanged($payment->id()));
     }
 }