View source
<?php
namespace Drupal\Tests\userpoints\Kernel;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\transaction\Kernel\KernelTransactionTestBase;
use Drupal\transaction\Entity\Transaction;
use Drupal\transaction\Entity\TransactionType;
use Drupal\user\Entity\User;
class UserPointsTransactionTest extends KernelTransactionTestBase {
public static $modules = [
'userpoints',
];
protected function prepareTargetEntity() {
$this
->installSchema('system', 'sequences');
$this->targetEntity = User::create([
'name' => 'admin',
]);
$this->targetEntity
->save();
}
protected function prepareTransactionType() {
$this->transactionType = TransactionType::create([
'id' => 'test_userpoints',
'label' => 'Test user points',
'target_entity_type' => 'user',
'transactor' => [
'id' => 'userpoints',
'settings' => [
'last_transaction' => 'field_last_transaction',
'log_message' => 'field_log_message',
'amount' => 'field_amount',
'balance' => 'field_balance',
'target_balance' => 'field_balance',
],
],
]);
$this->transactionType
->save();
$this
->prepareTransactionAmountField();
$this
->prepareTransactionBalanceField();
$this
->prepareTargetEntityBalanceField();
$this
->addTransactionLogMessageField();
}
protected function prepareTransactionAmountField() {
$field_storage = FieldStorageConfig::create([
'field_name' => 'field_amount',
'type' => 'decimal',
'entity_type' => 'transaction',
]);
$field_storage
->save();
FieldConfig::create([
'label' => 'Points amount',
'field_storage' => $field_storage,
'bundle' => $this->transactionType
->id(),
])
->save();
}
protected function prepareTransactionBalanceField() {
$field_storage = FieldStorageConfig::create([
'field_name' => 'field_balance',
'type' => 'decimal',
'entity_type' => 'transaction',
]);
$field_storage
->save();
FieldConfig::create([
'label' => 'Points balance',
'field_storage' => $field_storage,
'bundle' => $this->transactionType
->id(),
])
->save();
}
protected function prepareTargetEntityBalanceField() {
$field_storage = FieldStorageConfig::create([
'field_name' => 'field_balance',
'type' => 'decimal',
'entity_type' => 'user',
]);
$field_storage
->save();
FieldConfig::create([
'label' => 'Points',
'field_storage' => $field_storage,
'bundle' => 'user',
])
->save();
}
protected function prepareTargetEntityLastTransactionField() {
$field_storage = FieldStorageConfig::create([
'field_name' => 'field_last_transaction',
'type' => 'entity_reference',
'entity_type' => 'user',
'settings' => [
'target_type' => 'transaction',
],
]);
$field_storage
->save();
FieldConfig::create([
'label' => 'Last transaction',
'field_storage' => $field_storage,
'bundle' => 'user',
])
->save();
}
public function testUserPointsTransactionCreation() {
$transaction = $this->transaction;
$transactor = $transaction
->getType()
->getPlugin();
$this
->assertEquals('Zero points transaction (pending)', $transaction
->getDescription());
$this
->assertEquals('The current user points balance will not be altered.', $transactor
->getExecutionIndications($transaction));
$this
->assertEquals([
$this->logMessage,
], $transaction
->getDetails());
$transaction
->set('field_amount', -10);
$this
->assertEquals('Points debit (pending)', $transaction
->getDescription(TRUE));
$this
->assertEquals('The user will loss 10 points.', $transactor
->getExecutionIndications($transaction));
$transaction
->set('field_amount', 10);
$this
->assertEquals('Points credit (pending)', $transaction
->getDescription(TRUE));
$this
->assertEquals('The user will gain 10 points.', $transactor
->getExecutionIndications($transaction));
}
public function testUserPointsTransactionExecution() {
$transaction = $this->transaction;
$transaction
->set('field_balance', 10);
$transaction
->set('field_amount', 10);
$this
->assertTrue($transaction
->execute());
$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()));
$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,
'field_balance' => 50,
'field_amount' => -10,
]);
$this->targetEntity
->set('field_balance', 50);
$this
->assertTrue($second_transaction
->execute());
$this
->assertEquals('Points debit', trim($second_transaction
->getDescription()));
$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);
}
}