/**
  * @covers ::methodManager
  */
 public function testMethodManager()
 {
     $container = new Container();
     $method_manager = $this->getMock(PaymentMethodManagerInterface::class);
     $container->set('plugin.manager.payment.method', $method_manager);
     \Drupal::setContainer($container);
     $this->assertSame($method_manager, Payment::methodManager());
 }
 /**
  * Creates a payment.
  *
  * @param integer $uid
  *   The user ID of the payment's owner.
  * @param \Drupal\payment\Plugin\Payment\Method\PaymentMethodInterface|null $payment_method
  *
  * @return \Drupal\payment\Entity\PaymentInterface
  */
 static function createPayment($uid, PaymentMethodInterface $payment_method = NULL)
 {
     if (!$payment_method) {
         $payment_method = Payment::methodManager()->createInstance('payment_unavailable');
     }
     /** @var \Drupal\payment\Entity\PaymentInterface $payment */
     $payment = entity_create('payment', array('bundle' => 'payment_unavailable'));
     /** @var \Drupal\currency\ConfigImporterInterface $config_importer */
     $config_importer = \Drupal::service('currency.config_importer');
     $config_importer->importCurrency('EUR');
     $payment->setCurrencyCode('EUR')->setPaymentMethod($payment_method)->setOwnerId($uid)->setLineItems(static::createPaymentLineItems());
     return $payment;
 }
 /**
  * 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 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 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));
 }