You are here

public function ProductTest::testProductNodeForm in Ubercart 8.4

Tests product node form.

File

uc_product/tests/src/Functional/ProductTest.php, line 49

Class

ProductTest
Tests the product content type.

Namespace

Drupal\Tests\uc_product\Functional

Code

public function testProductNodeForm() {

  /** @var \Drupal\Tests\WebAssert $assert */
  $assert = $this
    ->assertSession();
  $this
    ->drupalGet('node/add/product');
  $fields = [
    'model[0][value]',
    'price[0][value]',
    'shippable[value]',
    'weight[0][value]',
    'weight[0][units]',
    'dimensions[0][length]',
    'dimensions[0][width]',
    'dimensions[0][height]',
    'dimensions[0][units]',
    'files[uc_product_image_0][]',
  ];
  foreach ($fields as $field) {
    $assert
      ->fieldExists($field);
  }
  $title_key = 'title[0][value]';
  $body_key = 'body[0][value]';

  // Make a node with those fields.
  $edit = [
    $title_key => $this
      ->randomMachineName(32),
    $body_key => $this
      ->randomMachineName(64),
    'model[0][value]' => $this
      ->randomMachineName(8),
    'price[0][value]' => mt_rand(1, 150),
    'shippable[value]' => mt_rand(0, 1),
    'weight[0][value]' => mt_rand(1, 50),
    'weight[0][units]' => array_rand([
      'lb' => 'Pounds',
      'kg' => 'Kilograms',
      'oz' => 'Ounces',
      'g' => 'Grams',
    ]),
    'dimensions[0][length]' => mt_rand(1, 50),
    'dimensions[0][width]' => mt_rand(1, 50),
    'dimensions[0][height]' => mt_rand(1, 50),
    'dimensions[0][units]' => array_rand([
      'in' => 'Inches',
      'ft' => 'Feet',
      'cm' => 'Centimeters',
      'mm' => 'Millimeters',
    ]),
  ];
  $this
    ->drupalGet('node/add/product');
  $this
    ->submitForm($edit, 'Save');

  // Check for product created message, and check for the expected product
  // field values on the newly-created product page.
  $assert
    ->pageTextContains(format_string('Product @title has been created.', [
    '@title' => $edit[$title_key],
  ]));

  // Product body text.
  $assert
    ->pageTextContains($edit[$body_key]);

  // Product model (SKU) text.
  $assert
    ->pageTextContains($edit['model[0][value]']);

  // Product price text.
  $this
    ->assertNoUniqueText(uc_currency_format($edit['price[0][value]']));

  // Formatted product weight text.
  $assert
    ->pageTextContains(uc_weight_format($edit['weight[0][value]'], $edit['weight[0][units]']));

  // Formatted product dimensions text.
  $assert
    ->pageTextContains(uc_length_format($edit['dimensions[0][length]'], $edit['dimensions[0][units]']));
  $assert
    ->pageTextContains(uc_length_format($edit['dimensions[0][width]'], $edit['dimensions[0][units]']));
  $assert
    ->pageTextContains(uc_length_format($edit['dimensions[0][height]'], $edit['dimensions[0][units]']));
  $elements = $this
    ->xpath('//body[contains(@class, "uc-product-node")]');
  $this
    ->assertCount(1, $elements, 'Product page contains body CSS class.');

  // Update the node fields.
  $edit = [
    $title_key => $this
      ->randomMachineName(32),
    $body_key => $this
      ->randomMachineName(64),
    'model[0][value]' => $this
      ->randomMachineName(8),
    'price[0][value]' => mt_rand(1, 150),
    'shippable[value]' => mt_rand(0, 1),
    'weight[0][value]' => mt_rand(1, 50),
    'weight[0][units]' => array_rand([
      'lb' => 'Pounds',
      'kg' => 'Kilograms',
      'oz' => 'Ounces',
      'g' => 'Grams',
    ]),
    'dimensions[0][length]' => mt_rand(1, 50),
    'dimensions[0][width]' => mt_rand(1, 50),
    'dimensions[0][height]' => mt_rand(1, 50),
    'dimensions[0][units]' => array_rand([
      'in' => 'Inches',
      'ft' => 'Feet',
      'cm' => 'Centimeters',
      'mm' => 'Millimeters',
    ]),
  ];
  $this
    ->clickLink('Edit');
  $this
    ->submitForm($edit, 'Save');

  // Check for product updated message, and check for the expected product
  // field values on the updated product page.
  $assert
    ->pageTextContains(format_string('Product @title has been updated.', [
    '@title' => $edit[$title_key],
  ]));

  // Product body text.
  $assert
    ->pageTextContains($edit[$body_key]);

  // Product model (SKU) text.
  $assert
    ->pageTextContains($edit['model[0][value]']);

  // Product price text.
  $this
    ->assertNoUniqueText(uc_currency_format($edit['price[0][value]']));

  // Formatted product weight text.
  $assert
    ->pageTextContains(uc_weight_format($edit['weight[0][value]'], $edit['weight[0][units]']));

  // Formatted product dimensions text.
  $assert
    ->pageTextContains(uc_length_format($edit['dimensions[0][length]'], $edit['dimensions[0][units]']));
  $assert
    ->pageTextContains(uc_length_format($edit['dimensions[0][width]'], $edit['dimensions[0][units]']));
  $assert
    ->pageTextContains(uc_length_format($edit['dimensions[0][height]'], $edit['dimensions[0][units]']));
  $this
    ->clickLink('Delete');
  $this
    ->submitForm([], 'Delete');

  // Check for product deleted message.
  $assert
    ->pageTextContains(format_string('Product @title has been deleted.', [
    '@title' => $edit[$title_key],
  ]));
}