You are here

public function UserPointsTransactionTest::testUserPointsTransactionExecution in User Points 8

Tests user points transaction execution.

@covers \Drupal\userpoints\Plugin\Transaction\UserPointsTransactor

File

tests/src/Kernel/UserPointsTransactionTest.php, line 168

Class

UserPointsTransactionTest
Tests the user points transactor.

Namespace

Drupal\Tests\userpoints\Kernel

Code

public function testUserPointsTransactionExecution() {
  $transaction = $this->transaction;

  // Set an initial balance.
  $transaction
    ->set('field_balance', 10);
  $transaction
    ->set('field_amount', 10);
  $this
    ->assertTrue($transaction
    ->execute());

  // Checks the transaction status after its execution.
  $this
    ->assertEquals($transaction
    ->id(), $this->targetEntity
    ->get('field_last_transaction')->target_id);
  $this
    ->assertGreaterThan(0, $transaction
    ->getResultCode());
  $this
    ->assertEquals('Transaction executed successfully.', $transaction
    ->getResultMessage());
  $this
    ->assertEquals('Points credit', trim($transaction
    ->getDescription()));

  // Checks the result balance.
  $this
    ->assertEquals(20, $transaction
    ->get('field_balance')->value);
  $this
    ->assertEquals(20, $this->targetEntity
    ->get('field_balance')->value);
  $second_transaction = Transaction::create([
    'type' => $this->transactionType
      ->id(),
    'target_entity' => $this->targetEntity,
    'field_log_message' => $this->logMessage,
    // Initial balance must be ignored when at least one transactions was
    // previously executed.
    'field_balance' => 50,
    'field_amount' => -10,
  ]);
  $this->targetEntity
    ->set('field_balance', 50);
  $this
    ->assertTrue($second_transaction
    ->execute());

  // Checks the new transaction status.
  $this
    ->assertEquals('Points debit', trim($second_transaction
    ->getDescription()));

  // Checks the result balance.
  $this
    ->assertEquals(10, $second_transaction
    ->get('field_balance')->value);
  $this
    ->assertEquals(10, $this->targetEntity
    ->get('field_balance')->value);
  $this
    ->assertEquals($second_transaction
    ->id(), $this->targetEntity
    ->get('field_last_transaction')->target_id);
}