Commit 701b1816 authored by joshtaylor's avatar joshtaylor

Fix commerce product tests

parent 127287ed
......@@ -349,7 +349,7 @@ display:
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- 'url.query_args.pagers:0'
- url.query_args
- user.permissions
cacheable: false
page_1:
......@@ -364,6 +364,6 @@ display:
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- 'url.query_args.pagers:0'
- url.query_args
- user.permissions
cacheable: false
......@@ -384,7 +384,7 @@ display:
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- 'url.query_args.pagers:0'
- url.query_args
- user.permissions
cacheable: false
page_1:
......@@ -399,6 +399,6 @@ display:
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- 'url.query_args.pagers:0'
- url.query_args
- user.permissions
cacheable: false
......@@ -70,6 +70,7 @@ abstract class CommerceProductTestBase extends WebTestBase {
'administer product types',
'administer commerce_product fields',
'access administration pages',
'administer commerce_product_variation fields'
]
);
$this->drupalLogin($this->adminUser);
......
......@@ -8,6 +8,7 @@
namespace Drupal\commerce_product\Tests;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductVariation;
/**
* Create, view, edit, delete, and change products and product types.
......@@ -21,19 +22,26 @@ class ProductAdminTest extends CommerceProductTestBase {
*/
function testAddCommerceProductAdmin() {
$title = $this->randomMachineName();
$this->drupalGet('admin/commerce/products');
$this->clickLink('Add a new product');
$edit = [
$this->clickLink('Add product');
$productVariationValues = [
'variations[form][inline_entity_form][sku][0][value]' => $this->randomMachineName(),
'variations[form][inline_entity_form][status][value]' => 1
];
$this->drupalPostForm(NULL, $productVariationValues, t('Create entity'));
$productValues = [
'title[0][value]' => $title,
'sku[0][value]' => strtolower($this->randomMachineName()),
'store_id' => $this->commerce_store->id()
];
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->drupalPostForm(NULL, $productValues, t('Save'));
$product = \Drupal::entityQuery('commerce_product')
->condition("sku", $edit['sku[0][value]'])
->condition("title", $productValues['title[0][value]'])
->range(0, 1)
->execute();
$product = entity_load("commerce_product", current($product));
$product = Product::load(current($product));
$this->assertNotNull($product, 'The new product has been created in the database.');
$this->assertText(t("The product @title has been successfully saved.", ['@title' => $title]), "Commerce Product success text is showing");
......@@ -43,37 +51,15 @@ class ProductAdminTest extends CommerceProductTestBase {
$this->drupalGet('product/' . $product->id());
$this->assertResponse(200);
$this->assertText($product->getTitle(), "Commerce Product title exists");
}
/**
* Tests creating a product with an existing SKU.
*/
function testAddCommerceProductExistingSkuAdmin() {
$product = $this->createEntity(
'commerce_product', [
'sku' => $this->randomMachineName(),
'title' => $this->randomMachineName(),
'type' => 'product',
'store_id' => $this->commerce_store->id()
]
);
$this->drupalGet('admin/commerce/products');
$this->clickLink('Add a new product');
$edit = [
'title[0][value]' => $this->randomMachineName(),
'sku[0][value]' => $product->getSku(),
'store_id' => $this->commerce_store->id()
];
$this->drupalPostForm(NULL, $edit, t('Save'));
// Assert that two products with the same SKU exist.
$duplicateCommerceProductSkus = \Drupal::entityQuery('commerce_product')
->count()
// Test product variations
$productVariation = \Drupal::entityQuery('commerce_product_variation')
->condition("sku", $productVariationValues['variations[form][inline_entity_form][sku][0][value]'])
->range(0, 1)
->execute();
$this->assertEqual($duplicateCommerceProductSkus, 1, "Only one product exists");
$this->assertText("is already in use", "Commerce Product failure text is showing");
$productVariation = ProductVariation::load(current($productVariation));
$this->assertNotNull($productVariation, 'The new product variation has been created in the database.');
}
/**
......@@ -82,7 +68,6 @@ class ProductAdminTest extends CommerceProductTestBase {
function testDeleteCommerceProductAdmin() {
$product = $this->createEntity(
'commerce_product', [
'sku' => $this->randomMachineName(),
'title' => $this->randomMachineName(),
'type' => "product"
]
......@@ -96,64 +81,6 @@ class ProductAdminTest extends CommerceProductTestBase {
$this->assertFalse($productExists, 'The new product has been deleted from the database.');
}
/**
* Tests adding product attributes to a field with just the attribute field checked.
*/
function testProductAttributesAdmin() {
$productFields = $this->testAddCommerceProductFieldAdmin();
$edit = [
'attribute_field' => 1,
'attribute_widget_title' => $this->randomMachineName()
];
$this->drupalPostForm(NULL, $edit, t('Save settings'));
$this->drupalGet('/admin/commerce/config/product-types/product/edit/fields/commerce_product.product.field_' . $productFields["field_name"]);
$this->assertFieldChecked("edit-attribute-field", "Product attribute field is checked");
$this->assertFieldChecked("edit-attribute-widget-select", "Product attribute widget select list field is checked");
$this->assertField('attribute_widget_title', $edit['attribute_widget_title']);
}
/**
* Tests adding product attributes to a field with the attribute field checked, and changing the radios.
*/
function testAddProductAttributesFieldsAdmin() {
$attributeWidgets = ['select', 'radios'];
foreach ($attributeWidgets as $attributeWidget) {
$productFields = $this->testAddCommerceProductFieldAdmin();
$edit = [
'attribute_field' => 1,
'attribute_widget' => $attributeWidget,
'attribute_widget_title' => $this->randomMachineName()
];
$this->drupalPostForm(NULL, $edit, t('Save settings'));
// Go back to the URL by clicking "Edit"
$this->drupalGet('/admin/commerce/config/product-types/product/edit/fields/commerce_product.product.field_' . $productFields["field_name"]);
$this->assertFieldChecked("edit-attribute-field", "Product attribute field is checked");
$this->assertFieldChecked("edit-attribute-widget-" . $attributeWidget, "Product attribute widget select list field is checked");
$this->assertField('attribute_widget_title', $edit['attribute_widget_title']);
}
}
/**
* Tests adding product fields.
*/
protected function testAddCommerceProductFieldAdmin() {
$this->drupalGet('admin/commerce/config/product-types/product/edit/fields/add-field');
// Create a new field.
$fields = [
'label' => $label = $this->randomMachineName(),
'field_name' => $name = strtolower($this->randomMachineName()),
'new_storage_type' => 'list_string',
];
$this->drupalPostForm('admin/commerce/config/product-types/product/edit/fields/add-field', $fields, t('Save and continue'));
$edit = ['settings[allowed_values]' => '1|1\n2|2'];
$this->drupalPostForm(NULL, $edit, t('Save field settings'));
return $fields;
}
/**
* Tests that anonymous users cannot see the admin/commerce/products page.
*/
......@@ -162,7 +89,7 @@ class ProductAdminTest extends CommerceProductTestBase {
$this->drupalGet('admin/commerce/products');
$this->assertResponse(200);
$this->assertNoText("You are not authorized to access this page.");
$this->assertLink("Add a new product");
$this->assertLink("Add product");
// Logout and check that anonymous users cannot see the products page
// and receieve a 403 error code.
......@@ -171,7 +98,7 @@ class ProductAdminTest extends CommerceProductTestBase {
$this->drupalGet('admin/commerce/products');
$this->assertResponse(403);
$this->assertText("You are not authorized to access this page.");
$this->assertNoLink("Add a new product");
$this->assertNoLink("Add product");
}
}
......@@ -21,37 +21,17 @@ class ProductTest extends CommerceProductTestBase {
*/
function testAddCommerceProduct() {
$this->createEntity('commerce_product', [
'sku' => $this->randomMachineName(),
'title' => $this->randomMachineName(),
'type' => 'product'
]);
}
/**
* Tests creating a product with an existing SKU.
*/
function testAddCommerceProductExistingSku() {
$values = [
'sku' => $this->randomMachineName(),
'title' => $this->randomMachineName(),
'type' => 'product',
'store_id' => $this->commerce_store->id()
];
$this->createEntity('commerce_product', $values);
$productDuplicate = Product::create($values);
$violations = $productDuplicate->sku->validate();
$this->assertNotEqual($violations->count(), 0, 'Validation fails when creating a product with the same SKU.');
}
/**
* Tests deleting a product.
*/
function testDeleteProduct() {
// Create a new product.
$values = [
'sku' => $this->randomMachineName(),
'title' => $this->randomMachineName(),
'type' => "product"
];
......
......@@ -99,4 +99,83 @@ class ProductVariationTypeTest extends CommerceProductTestBase {
$this->assertFalse($exists, 'The new product variation type has been deleted from the database.');
}
/**
* Tests adding product fields.
*/
protected function testAddCommerceProductVariationFieldAdmin() {
$variationType = $this->createEntity('commerce_product_variation_type', [
'id' => 'foo',
'label' => 'foo'
]);
$this->drupalGet('admin/commerce/config/product-variation-types/foo/edit/fields/add-field');
// Create a new field.
$fields = [
'label' => $label = $this->randomMachineName(),
'field_name' => $name = strtolower($this->randomMachineName()),
'new_storage_type' => 'list_string',
];
$this->drupalPostForm('admin/commerce/config/product-variation-types/foo/edit/fields/add-field', $fields, t('Save and continue'));
$edit = ['settings[allowed_values]' => '1|1\n2|2'];
$this->drupalPostForm(NULL, $edit, t('Save field settings'));
return $fields;
}
/**
* Tests adding product attributes to a field with just the attribute field checked.
*/
function testProductVariationAttributesAdmin() {
$productFields = $this->testAddCommerceProductVariationFieldAdmin();
$edit = [
'attribute_field' => 1,
'attribute_widget_title' => $this->randomMachineName()
];
$this->drupalPostForm(NULL, $edit, t('Save settings'));
$this->drupalGet('/admin/commerce/config/product-variation-types/foo/edit/fields/commerce_product_variation.foo.field_' . $productFields["field_name"]);
$this->assertFieldChecked("edit-attribute-field", "Product attribute field is checked");
$this->assertFieldChecked("edit-attribute-widget-select", "Product attribute widget select list field is checked");
$this->assertField('attribute_widget_title', $edit['attribute_widget_title']);
}
/**
* Tests adding product attributes to a field with the attribute field checked, and changing the radios.
*/
function testAddProductVariationAttributesFieldsAdmin() {
$variationType = $this->createEntity('commerce_product_variation_type', [
'id' => 'foo',
'label' => 'foo'
]);
$attributeWidgets = ['select', 'radios'];
foreach ($attributeWidgets as $attributeWidget) {
$this->drupalGet('admin/commerce/config/product-variation-types/foo/edit/fields/add-field');
// Create a new field.
$productVariationfields = [
'label' => $label = $this->randomMachineName(),
'field_name' => $name = strtolower($this->randomMachineName()),
'new_storage_type' => 'list_string',
];
$this->drupalPostForm('admin/commerce/config/product-variation-types/foo/edit/fields/add-field', $productVariationfields, t('Save and continue'));
$edit = ['settings[allowed_values]' => '1|1\n2|2'];
$this->drupalPostForm(NULL, $edit, t('Save field settings'));
$edit = [
'attribute_field' => 1,
'attribute_widget' => $attributeWidget,
'attribute_widget_title' => $this->randomMachineName()
];
$this->drupalPostForm(NULL, $edit, t('Save settings'));
// Go back to the URL by clicking "Edit"
$this->drupalGet('/admin/commerce/config/product-variation-types/foo/edit/fields/commerce_product_variation.foo.field_' . $productVariationfields["field_name"]);
$this->assertFieldChecked("edit-attribute-field", "Product attribute field is checked");
$this->assertFieldChecked("edit-attribute-widget-" . $attributeWidget, "Product attribute widget select list field is checked");
$this->assertField('attribute_widget_title', $edit['attribute_widget_title']);
}
}
}
......@@ -457,7 +457,7 @@ display:
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- 'url.query_args.pagers:0'
- url.query_args
- user.permissions
cacheable: false
page_1:
......@@ -472,6 +472,6 @@ display:
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- 'url.query_args.pagers:0'
- url.query_args
- user.permissions
cacheable: false
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment