/**
  * Tests listing.
  */
 protected function testList()
 {
     $payment_status_id = strtolower($this->randomMachineName());
     /** @var \Drupal\payment\Entity\PaymentStatusInterface $status */
     $status = $this->paymentStatusStorage->create([]);
     $status->setId($payment_status_id)->setLabel($this->randomMachineName())->save();
     $path = 'admin/config/services/payment/status';
     $this->drupalGet($path);
     $this->assertResponse(403);
     $this->drupalLogin($this->drupalCreateUser(array('payment.payment_status.administer')));
     $this->drupalGet($path);
     $this->assertResponse(200);
     // Assert that the "Add payment status" link is visible.
     $this->assertLinkByHref('admin/config/services/payment/status/add');
     // Assert that all plugins are visible.
     $manager = Payment::statusManager();
     foreach ($manager->getDefinitions() as $definition) {
         $this->assertText($definition['label']);
         if ($definition['description']) {
             $this->assertText($definition['description']);
         }
     }
     // Assert that all config entity operations are visible.
     $this->assertLinkByHref('admin/config/services/payment/status/edit/' . $payment_status_id);
     $this->assertLinkByHref('admin/config/services/payment/status/delete/' . $payment_status_id);
 }
 /**
  * Creates payment line items.
  *
  * @return \Drupal\payment\Plugin\Payment\LineItem\PaymentLineItemInterface[]
  */
 static function createPaymentLineItems()
 {
     $line_item_manager = Payment::lineItemManager();
     /** @var \Drupal\currency\ConfigImporterInterface $config_importer */
     $config_importer = \Drupal::service('currency.config_importer');
     $config_importer->importCurrency('NLG');
     $config_importer->importCurrency('JPY');
     $config_importer->importCurrency('MGA');
     $line_items = array($line_item_manager->createInstance('payment_basic', [])->setName('foo')->setAmount(9.9)->setCurrencyCode('NLG')->setDescription(static::getRandom()->string()), $line_item_manager->createInstance('payment_basic', [])->setName('bar')->setAmount(5.5)->setCurrencyCode('JPY')->setQuantity(2)->setDescription(static::getRandom()->string()), $line_item_manager->createInstance('payment_basic', [])->setName('baz')->setAmount(1.1)->setCurrencyCode('MGA')->setQuantity(3)->setDescription(static::getRandom()->string()));
     return $line_items;
 }
 /**
  * {@inheritdoc}
  */
 public function buildForm(array $form, FormStateInterface $form_state)
 {
     if ($form_state->has('payment_line_item')) {
         $line_item = $form_state->get('payment_line_item');
     } else {
         $line_item = Payment::lineItemManager()->createInstance('payment_basic');
         $form_state->set('payment_line_item', $line_item);
     }
     $form['line_item'] = $line_item->buildConfigurationForm([], $form_state);
     $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
     return $form;
 }
 /**
  * {@inheritdoc}
  */
 public function fieldSettingsForm(array $form, FormStateInterface $form_state)
 {
     /** @var \Drupal\currency\FormHelperInterface $form_helper */
     $form_helper = \Drupal::service('currency.form_helper');
     $element['#element_validate'] = [get_class() . '::fieldSettingsFormValidate'];
     $element['currency_code'] = ['#empty_value' => '', '#type' => 'select', '#title' => $this->t('Payment currency'), '#options' => $form_helper->getCurrencyOptions(), '#default_value' => $this->getSetting('currency_code'), '#required' => TRUE];
     $line_items = [];
     foreach ($this->getSetting('line_items_data') as $line_item_data) {
         $line_items[] = Payment::lineItemManager()->createInstance($line_item_data['plugin_id'], $line_item_data['plugin_configuration']);
     }
     $element['line_items'] = ['#type' => 'payment_line_items_input', '#title' => $this->t('Line items'), '#default_value' => $line_items, '#required' => TRUE, '#currency_code' => ''];
     return $element;
 }
 /**
  * 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 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()), []);
 }
 /**
  * @covers ::typeManager
  */
 public function testTypeManager()
 {
     $container = new Container();
     $type_manager = $this->getMock(PaymentTypeManagerInterface::class);
     $container->set('plugin.manager.payment.type', $type_manager);
     \Drupal::setContainer($container);
     $this->assertSame($type_manager, Payment::typeManager());
 }
 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     $this->lineItem = Payment::lineItemManager()->createInstance('payment_basic');
 }
 /**
  * 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()));
     }
 }
 /**
  * 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 selecting.
  */
 protected function doTestAddSelect()
 {
     $this->drupalLogout();
     $plugin_id = 'payment_basic';
     $this->drupalGet('admin/config/services/payment/method/configuration-add');
     $this->assertResponse(403);
     $this->drupalLogin($this->drupalCreateUser(array('payment.payment_method_configuration.create.' . $plugin_id)));
     $this->drupalGet('admin/config/services/payment/method/configuration-add');
     $this->assertResponse(200);
     $definition = Payment::methodConfigurationManager()->getDefinition($plugin_id);
     $this->assertText($definition['label']);
 }
 /**
  * Tests getPaymentMethod().
  */
 protected function testGetPaymentMethod()
 {
     $payment_method = Payment::methodManager()->createInstance('payment_basic');
     $this->payment->setPaymentMethod($payment_method);
     $this->assertTrue(spl_object_hash($this->payment->getPaymentMethod()), spl_object_hash($this->payment));
 }