Commit 5668cc0b authored by bojanz's avatar bojanz

Merge the product type admin & api tests into one (taking after NodeTypeTest).

parent ec7b5efa
<?php
/**
* @file
* Definition of Drupal\commerce\Tests\ProductTypeTest
*/
namespace Drupal\commerce_product\Tests;
use Drupal\commerce_product\Entity\ProductType;
/**
* Ensure the product type works correctly.
*
* @group commerce
*/
class ProductTypeAdminTest extends CommerceProductTestBase {
/**
* Tests if the default Product Type was created.
*/
public function testDefaultProductTypeAdmin() {
$this->drupalGet('admin/commerce/config/product-types');
$productTypes = ProductType::loadMultiple();
$this->assertTrue(isset($productTypes['product']), 'Found the product type "Product"');
$productType = ProductType::load('product');
$this->assertEqual($productTypes['product'], $productType, 'The correct product type is loaded');
}
/**
* Tests if the correct number of Product Types are being listed.
*/
public function testListProductTypeAdmin() {
$title = strtolower($this->randomMachineName(8));
$tableSelector = 'table tbody tr';
// The product shows one default product type.
$this->drupalGet('admin/commerce/config/product-types');
$productTypes = $this->cssSelect($tableSelector);
$this->assertEqual(count($productTypes), 1, '1 Products types are correctly listed');
// Create a new product type entity and see if the list has two product types.
$this->createEntity('commerce_product_type', [
'id' => $title,
'label' => $title
]
);
$this->drupalGet('admin/commerce/config/product-types');
$productTypes = $this->cssSelect($tableSelector);
$this->assertEqual(count($productTypes), 2, '2 Products types are correctly listed');
}
/**
* Tests creating a Product Type programaticaly and through the create form.
*/
public function testCreateProductTypeAdmin() {
$title = strtolower($this->randomMachineName(8));
// Create a product type programmaticaly.
$type = $this->createEntity('commerce_product_type', [
'id' => $title,
'label' => $title,
]
);
$typeExists = (bool) ProductType::load($type->id());
$this->assertTrue($typeExists, 'The new product type has been created in the database.');
$this->drupalGet('admin/commerce/config/product-types/add');
// Create a product type through the form.
$edit = [
'label' => 'foo',
'id' => 'foo'
];
$this->drupalPostForm('admin/commerce/config/product-types/add', $edit, t('Save'));
$typeExists = (bool) ProductType::load($edit['label']);
$this->assertTrue($typeExists, 'The new product type has been created in the database.');
}
/**
* Tests updating a Product Type through the edit form.
*/
public function testUpdateProductTypeAdmin() {
// Create a new product type.
$productType = $this->createEntity('commerce_product_type', [
'id' => 'foo',
'label' => 'Label for foo'
]
);
// Only change the label.
$edit = [
'label' => $this->randomMachineName(8),
];
$this->drupalPostForm('admin/commerce/config/product-types/' . $productType->id() . '/edit', $edit, 'Save');
$productTypeChanged = ProductType::load($productType->id());
$this->assertNotEqual($productType->label(), $productTypeChanged->label(), 'The label of the product type has been changed.');
}
/**
* Tests deleting a Product Type through the form.
*/
public function testDeleteProductTypeAdmin() {
// Create a product type programmaticaly.
$type = $this->createEntity('commerce_product_type', [
'id' => 'foo',
'label' => 'foo'
]
);
// Create a product.
$values = [
'sku' => $this->randomMachineName(),
'title' => $this->randomMachineName(),
'type' => $type->id()
];
$product = $this->createEntity('commerce_product', $values);
// Try to delete the product type.
$this->drupalGet('admin/commerce/config/product-types/' . $type->id() . '/delete');
$this->assertRaw(
t('%type is used by 1 product on your site. You can not remove this product type until you have removed all of the %type products.', ['%type' => $type->label()]),
'The product type will not be deleted until all products of that type are deleted'
);
$this->assertNoText(t('This action cannot be undone.'), 'The product type deletion confirmation form is not available');
// Deleting the product type when its not being referenced by a product.
$product->delete();
$this->drupalGet('admin/commerce/config/product-types/' . $type->id() . '/delete');
$this->assertRaw(
t('Are you sure you want to delete the product type %type?', ['%type' => $type->label()]),
'The product type is available for deletion'
);
$this->assertText(t('This action cannot be undone.'), 'The product type deletion confirmation form is available');
$this->drupalPostForm(NULL, NULL, t('Delete'));
$typeExists = (bool) ProductType::load($type->id());
$this->assertFalse($typeExists, 'The new product type has been deleted from the database.');
}
}
......@@ -17,59 +17,98 @@ use Drupal\commerce_product\Entity\ProductType;
class ProductTypeTest extends CommerceProductTestBase {
/**
* Tests if the default Product Type was created.
* Tests whether the default product type was created.
*/
public function testDefaultProductType() {
$productTypes = ProductType::loadMultiple();
$this->assertTrue(isset($productTypes['product']), 'Product Type Product is available');
$productType = ProductType::load('default');
$this->assertTrue($productType, 'The default product type is available.');
$productType = ProductType::load('product');
$this->assertEqual($productTypes['product'], $productType, 'The correct Product Type is loaded');
$this->drupalGet('admin/commerce/config/product-types');
$rows = $this->cssSelect('table tbody tr');
$this->assertEqual(count($rows), 1, '1 product type is correctly listed.');
}
/**
* Tests creating a Product Type programaticaly and through the create form.
* Tests creating a product type programmatically and via a form.
*/
public function testCreateProductType() {
$title = strtolower($this->randomMachineName(8));
function testProductTypeCreation() {
$values = [
'id' => strtolower($this->randomMachineName(8)),
'label' => $this->randomMachineName(),
'description' => 'My random product type',
'variationType' => 'default',
];
$productType = $this->createEntity('commerce_product_type', $values);
$productType = ProductType::load($values['id']);
$this->assertEqual($productType->label(), $values['label'], 'The new product type has the correct label.');
$this->assertEqual($productType->getDescription(), $values['description'], 'The new product type has the correct label.');
$this->assertEqual($productType->getVariationType(), $values['variationType'], 'The new product type has the correct associated variation type.');
// Create a product type programmaticaly.
$type = $this->createEntity('commerce_product_type', [
'id' => $title,
'label' => $title,
]
);
$this->drupalGet('admin/commerce/products/add/' . $productType->id());
$this->assertResponse(200, 'The new product type can be accessed at admin/commerce/products/add.');
$typeExists = (bool) ProductType::load($type->id());
$this->assertTrue($typeExists, 'The new product type has been created in the database.');
$user = $this->drupalCreateUser(['administer product types']);
$this->drupalLogin($user);
$edit = [
'id' => strtolower($this->randomMachineName(8)),
'label' => $this->randomMachineName(),
'description' => 'My even more random product type',
'variationType' => 'default',
];
$this->drupalPostForm('admin/commerce/config/product-types/add', $edit, t('Save'));
$productType = ProductType::load($edit['id']);
$this->assertTrue($productType, 'The new product type has been created.');
$this->assertEqual($productType->label(), $edit['label'], 'The new product type has the correct label.');
$this->assertEqual($productType->getDescription(), $edit['description'], 'The new product type has the correct label.');
$this->assertEqual($productType->getVariationType(), $edit['variationType'], 'The new product type has the correct associated variation type.');
}
/**
* Tests deleting a Product Type through the form.
* Tests editing a product type using the UI.
*/
public function testDeleteProductType() {
// Create a product type programmaticaly.
$type = $this->createEntity('commerce_product_type', [
'id' => 'foo',
'label' => 'foo'
]
);
function testProductTypeEditing() {
$edit = [
'label' => 'Default2',
'description' => 'New description.',
];
$this->drupalPostForm('admin/commerce/config/product-types/default/edit', $edit, t('Save'));
$productType = ProductType::load('default');
$this->assertEqual($productType->label(), $edit['label'], 'The label of the product type has been changed.');
$this->assertEqual($productType->getDescription(), $edit['description'], 'The new product type has the correct label.');
}
// Create a product.
$values = [
'sku' => $this->randomMachineName(),
/**
* Tests deleting a product type via a form.
*/
public function testProductTypeDeletion() {
$productType = $this->createEntity('commerce_product_type', [
'id' => 'foo',
'label' => 'foo'
]);
$product = $this->createEntity('commerce_product', [
'type' => $productType->id(),
'title' => $this->randomMachineName(),
'type' => "product"
];
$product = $this->createEntity('commerce_product', $values);
]);
// Try to delete the product type.
$type->delete();
// @todo Make sure $productType->delete() also does nothing if there's
// a product of that type. Right now the check is done on the form level.
$this->drupalGet('admin/commerce/config/product-types/' . $productType->id() . '/delete');
$this->assertRaw(
t('%type is used by 1 product on your site. You can not remove this product type until you have removed all of the %type products.', ['%type' => $productType->label()]),
'The product type will not be deleted until all products of that type are deleted.'
);
$this->assertNoText(t('This action cannot be undone.'), 'The product type deletion confirmation form is not available');
// Deleting the product type when its not being referenced by a product.
$product->delete();
$typeExists = (bool) ProductType::load($type->id());
$this->assertFalse($typeExists, 'The new product type has been deleted from the database.');
$this->drupalGet('admin/commerce/config/product-types/' . $productType->id() . '/delete');
$this->assertRaw(
t('Are you sure you want to delete the product type %type?', ['%type' => $productType->label()]),
'The product type is available for deletion'
);
$this->assertText(t('This action cannot be undone.'), 'The product type deletion confirmation form is available');
$this->drupalPostForm(NULL, NULL, t('Delete'));
$exists = (bool) ProductType::load($productType->id());
$this->assertFalse($exists, 'The new product type has been deleted from the database.');
}
}
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