You are here

public function ProductAdminTest::testCreateProduct in Commerce Core 8.2

Tests creating a product.

File

modules/product/tests/src/Functional/ProductAdminTest.php, line 76

Class

ProductAdminTest
Create, view, edit, delete, and change products.

Namespace

Drupal\Tests\commerce_product\Functional

Code

public function testCreateProduct() {
  $this
    ->drupalGet('admin/commerce/products');
  $this
    ->getSession()
    ->getPage()
    ->clickLink('Add product');
  $store_ids = EntityHelper::extractIds($this->stores);
  $title = $this
    ->randomMachineName();
  $edit = [
    'title[0][value]' => $title,
  ];
  foreach ($store_ids as $store_id) {
    $edit['stores[target_id][value][' . $store_id . ']'] = $store_id;
  }
  $this
    ->submitForm($edit, 'Save');
  $result = \Drupal::entityTypeManager()
    ->getStorage('commerce_product')
    ->getQuery()
    ->condition("title", $edit['title[0][value]'])
    ->accessCheck(FALSE)
    ->range(0, 1)
    ->execute();
  $product_id = reset($result);
  $product = Product::load($product_id);
  $this
    ->assertNotNull($product, 'The new product has been created.');
  $this
    ->assertSession()
    ->pageTextContains(t('The product @title has been successfully saved', [
    '@title' => $title,
  ]));
  $this
    ->assertSession()
    ->pageTextContains($title);
  $this
    ->assertFieldValues($product
    ->getStores(), $this->stores, 'Created product has the correct associated stores.');
  $this
    ->assertFieldValues($product
    ->getStoreIds(), $store_ids, 'Created product has the correct associated store ids.');
  $this
    ->drupalGet($product
    ->toUrl('canonical'));
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->pageTextContains($product
    ->getTitle());
}