You are here

public function RoleCheckoutTest::testCheckoutRoleAssignment in Ubercart 8.4

Tests that roles are properly assigned after checkout.

File

uc_role/tests/src/Functional/RoleCheckoutTest.php, line 46

Class

RoleCheckoutTest
Tests role assignment upon checkout.

Namespace

Drupal\Tests\uc_role\Functional

Code

public function testCheckoutRoleAssignment() {
  $this
    ->drupalLogin($this->adminUser);
  $method = $this
    ->createPaymentMethod('other');

  // Add role assignment to the test product.
  $rid = $this
    ->drupalCreateRole([
    'access content',
  ]);
  $this
    ->drupalGet('node/' . $this->product
    ->id() . '/edit/features');
  $this
    ->submitForm([
    'feature' => 'role',
  ], 'Add');
  $this
    ->submitForm([
    'role' => $rid,
  ], 'Save feature');

  // Process an anonymous, shippable order.
  $order = $this
    ->createOrder([
    'uid' => 0,
    'payment_method' => $method['id'],
  ]);
  $order->products[1]->data->shippable = 1;
  $order
    ->save();
  uc_payment_enter($order
    ->id(), 'other', $order
    ->getTotal());

  // Find the order uid.
  $uid = \Drupal::database()
    ->query('SELECT uid FROM {uc_orders} ORDER BY order_id DESC')
    ->fetchField();
  $account = User::load($uid);

  // @todo Re-enable when Rules is available.
  // $this->assertTrue($account->hasRole($rid), 'New user was granted role.');
  $order = Order::load($order
    ->id());
  $this
    ->assertEquals('payment_received', $order
    ->getStatusId(), 'Shippable order was set to payment received.');

  // 4 e-mails: new account, customer invoice, admin invoice, role assignment.
  $this
    ->assertMailString('subject', 'Account details', 4, 'New account email was sent');
  $this
    ->assertMailString('subject', 'Your Order at Ubercart', 4, 'Customer invoice was sent');
  $this
    ->assertMailString('subject', 'New Order at Ubercart', 4, 'Admin notification was sent');

  // @todo Re-enable when Rules is available.
  // $this->assertMailString('subject', 'role granted', 4, 'Role assignment notification was sent');
  \Drupal::state()
    ->set('system.test_mail_collector', []);

  // Test again with an existing authenticated user and a non-shippable order.
  $order = $this
    ->createOrder([
    'uid' => 0,
    'primary_email' => $this->customer
      ->getEmail(),
    'payment_method' => $method['id'],
  ]);
  $order->products[2]->data->shippable = 0;
  $order
    ->save();
  uc_payment_enter($order
    ->id(), 'other', $order
    ->getTotal());
  $account = User::load($this->customer
    ->id());

  // @todo Re-enable when Rules is available.

  //$this->assertTrue($account->hasRole($rid), 'Existing user was granted role.');
  $order = Order::load($order
    ->id());
  $this
    ->assertEquals('completed', $order
    ->getStatusId(), 'Non-shippable order was set to completed.');

  // 3 e-mails: customer invoice, admin invoice, role assignment.
  $this
    ->assertNoMailString('subject', 'Account details', 4, 'New account email was sent');
  $this
    ->assertMailString('subject', 'Your Order at Ubercart', 4, 'Customer invoice was sent');
  $this
    ->assertMailString('subject', 'New Order at Ubercart', 4, 'Admin notification was sent');

  // @todo Re-enable when Rules is available.
  // $this->assertMailString('subject', 'role granted', 4, 'Role assignment notification was sent');
}