You are here

public function AttributeTest::testProductAttribute in Ubercart 8.4

Tests attributes applied to a product.

File

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

Class

AttributeTest
Tests the product attribute API.

Namespace

Drupal\Tests\uc_attribute\Functional

Code

public function testProductAttribute() {

  /** @var \Drupal\Tests\WebAssert $assert */
  $assert = $this
    ->assertSession();
  $product = $this
    ->createProduct();
  $attribute = $this
    ->createAttribute([
    'display' => 2,
    'required' => TRUE,
  ]);
  for ($i = 0; $i < 3; $i++) {
    $option = $this
      ->createAttributeOption([
      'aid' => $attribute->aid,
    ]);
    $adjustment = $this
      ->createProductAdjustment([
      'combination' => serialize([
        $attribute->aid => $option->oid,
      ]),
      'nid' => $product
        ->id(),
    ]);
    $option->model = $adjustment->model;
    $attribute->options[$option->oid] = $option;
  }
  uc_attribute_subject_save($attribute, 'product', $product
    ->id(), TRUE);

  // Product node display.
  $this
    ->drupalGet('node/' . $product
    ->id());

  // Check for attribute label and description.
  $assert
    ->pageTextContains($attribute->label);
  $assert
    ->pageTextContains($attribute->description);
  foreach ($attribute->options as $option) {

    // Check for option name and price adjustment.
    $assert
      ->pageTextContains($option->name);
    $assert
      ->pageTextContains(uc_currency_format($option->price));
  }

  // Test required attribute.
  $this
    ->addToCart($product);

  // Check for attribute required message.
  $assert
    ->pageTextContains($attribute->label . ' field is required');

  // Cart display.
  $price = uc_currency_format($product->price->value + $option->price);
  $this
    ->addToCart($product, [
    'attributes[' . $attribute->aid . ']' => $option->oid,
  ]);

  // Check for selected attribute, option, and adjusted price in the cart.
  $assert
    ->pageTextContains($attribute->label . ': ' . $option->name);
  $assert
    ->pageTextContains($price);

  // Checkout display.
  $this
    ->submitForm([], 'Checkout');

  // Check for selected attribute, option, and adjusted price at checkout.
  $assert
    ->pageTextContains($attribute->label . ': ' . $option->name);
  $assert
    ->pageTextContains($price);
  $this
    ->checkout();

  // Admin order display.
  $cost = uc_currency_format($product->cost->value + $option->cost);
  $this
    ->drupalGet('admin/store/orders/1');

  // Check for selected attribute, option, adjusted price, adjusted cost and
  // adjusted SKU on admin order page.
  $assert
    ->pageTextContains($attribute->label . ': ' . $option->name, '');
  $assert
    ->pageTextContains($cost);
  $assert
    ->pageTextContains($price);
  $assert
    ->pageTextContains($option->model);

  // Invoice display.
  $this
    ->drupalGet('admin/store/orders/1/invoice');

  // Check for selected attribute, option, adjusted price, and adjusted SKU
  // on order invoice.
  $assert
    ->pageTextContains($attribute->label . ': ' . $option->name);
  $assert
    ->pageTextContains($price);
  $assert
    ->pageTextContains('SKU: ' . $option->model);
}