You are here

public function AttributeTest::testAttributeAddToCart in Ubercart 8.4

Tests that product in cart has the selected attribute option.

File

uc_attribute/tests/src/Functional/AttributeTest.php, line 867

Class

AttributeTest
Tests the product attribute API.

Namespace

Drupal\Tests\uc_attribute\Functional

Code

public function testAttributeAddToCart() {

  /** @var \Drupal\Tests\WebAssert $assert */
  $assert = $this
    ->assertSession();
  for ($display = 0; $display <= 3; ++$display) {

    // Set up an attribute.
    $attribute = $this
      ->createAttribute([
      'display' => $display,
    ]);
    if ($display) {

      // Give the attribute an option.
      $option = $this
        ->createAttributeOption([
        'aid' => $attribute->aid,
      ]);
    }
    $attribute = uc_attribute_load($attribute->aid);

    // Put the attribute on a product.
    $product = $this
      ->createProduct();
    uc_attribute_subject_save($attribute, 'product', $product
      ->id(), TRUE);

    // Add the product to the cart.
    if ($display == 3) {
      $edit = [
        "attributes[{$attribute->aid}][{$option->oid}]" => $option->oid,
      ];
    }
    elseif (isset($option)) {
      $edit = [
        "attributes[{$attribute->aid}]" => $option->oid,
      ];
    }
    else {
      $option = new \stdClass();
      $option->name = self::randomMachineName();
      $option->price = 0;
      $edit = [
        "attributes[{$attribute->aid}]" => $option->name,
      ];
    }
    $this
      ->addToCart($product, $edit);

    // Check that option is listed on the cart item.
    $assert
      ->pageTextContains("{$attribute->label}: {$option->name}", 'Option selected on cart item.');

    // Check that the price in the cart is correct for this option.
    $assert
      ->pageTextContains(uc_currency_format($product->price->value + $option->price));
  }
}