View source
<?php
namespace Drupal\Tests\uc_product\Functional;
use Drupal\Tests\uc_store\Functional\UbercartBrowserTestBase;
class ProductTest extends UbercartBrowserTestBase {
protected static $modules = [
'path',
'uc_product',
];
protected static $adminPermissions = [
'administer content types',
];
protected function setUp() {
parent::setUp();
$this
->drupalLogin($this->adminUser);
}
public function testProductAdmin() {
$assert = $this
->assertSession();
$this
->drupalGet('admin/store/products/view');
$assert
->pageTextContains('Title');
$assert
->pageTextContains($this->product
->getTitle());
$assert
->pageTextContains('Price');
$assert
->pageTextContains(uc_currency_format($this->product->price->value));
}
public function testProductNodeForm() {
$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]';
$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');
$assert
->pageTextContains(format_string('Product @title has been created.', [
'@title' => $edit[$title_key],
]));
$assert
->pageTextContains($edit[$body_key]);
$assert
->pageTextContains($edit['model[0][value]']);
$this
->assertNoUniqueText(uc_currency_format($edit['price[0][value]']));
$assert
->pageTextContains(uc_weight_format($edit['weight[0][value]'], $edit['weight[0][units]']));
$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.');
$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');
$assert
->pageTextContains(format_string('Product @title has been updated.', [
'@title' => $edit[$title_key],
]));
$assert
->pageTextContains($edit[$body_key]);
$assert
->pageTextContains($edit['model[0][value]']);
$this
->assertNoUniqueText(uc_currency_format($edit['price[0][value]']));
$assert
->pageTextContains(uc_weight_format($edit['weight[0][value]'], $edit['weight[0][units]']));
$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');
$assert
->pageTextContains(format_string('Product @title has been deleted.', [
'@title' => $edit[$title_key],
]));
}
public function testZeroProductWeightAndDimensions() {
$assert = $this
->assertSession();
$edit = [
'title[0][value]' => $this
->randomMachineName(32),
'model[0][value]' => $this
->randomMachineName(8),
'price[0][value]' => mt_rand(1, 150),
'shippable[value]' => mt_rand(0, 1),
'weight[0][value]' => 0,
'weight[0][units]' => array_rand([
'lb' => 'Pounds',
'kg' => 'Kilograms',
'oz' => 'Ounces',
'g' => 'Grams',
]),
'dimensions[0][length]' => 0,
'dimensions[0][width]' => 0,
'dimensions[0][height]' => 0,
'dimensions[0][units]' => array_rand([
'in' => 'Inches',
'ft' => 'Feet',
'cm' => 'Centimeters',
'mm' => 'Millimeters',
]),
];
$this
->drupalGet('node/add/product');
$this
->submitForm($edit, 'Save');
$assert
->pageTextContains(format_string('Product @title has been created.', [
'@title' => $edit['title[0][value]'],
]));
$assert
->pageTextNotContains('Weight');
$assert
->pageTextNotContains('Dimensions');
}
public function testProductClassForm() {
$assert = $this
->assertSession();
$class = strtolower($this
->randomMachineName(12));
$edit = [
'type' => $class,
'name' => $class,
'description' => $this
->randomMachineName(32),
'uc_product[product]' => 1,
];
$this
->drupalGet('admin/structure/types/add');
$this
->submitForm($edit, 'Save content type');
$this
->assertTrue(uc_product_is_product($class), 'The new content type is a product class.');
$type = $this
->drupalCreateContentType([
'description' => $this
->randomMachineName(),
]);
$edit = [
'uc_product[product]' => 1,
];
$this
->drupalGet('admin/structure/types/manage/' . $type
->getOriginalId());
$this
->submitForm($edit, 'Save content type');
$this
->assertTrue(uc_product_is_product($type
->getOriginalId()), 'The updated content type is a product class.');
$this
->drupalGet('admin/store/products/classes');
$assert
->pageTextContains($type
->getOriginalId());
$assert
->pageTextContains($type
->getDescription());
$assert
->linkByHrefExists('admin/structure/types/manage/' . $type
->getOriginalId(), 0);
$assert
->linkByHrefExists('admin/structure/types/manage/' . $type
->getOriginalId() . '/delete', 0);
$edit = [
'uc_product[product]' => FALSE,
];
$this
->drupalGet('admin/structure/types/manage/' . $class);
$this
->submitForm($edit, 'Save content type');
$this
->assertFalse(uc_product_is_product($class), 'The updated content type is no longer a product class.');
}
public function testProductQuantity() {
$assert = $this
->assertSession();
$edit = [
'uc_product_add_to_cart_qty' => TRUE,
];
$this
->drupalGet('admin/store/config/products');
$this
->submitForm($edit, 'Save configuration');
$this
->addToCart($this->product, [
'qty' => 0,
]);
$assert
->pageTextContains('The quantity cannot be zero.');
$this
->addToCart($this->product, [
'qty' => 'x',
]);
$assert
->pageTextContains('The quantity must be an integer.');
$this
->addToCart($this->product, [
'qty' => '1a',
]);
$assert
->pageTextContains('The quantity must be an integer.');
$this
->addToCart($this->product, [
'qty' => 1,
]);
$assert
->pageTextContains($this->product
->getTitle() . ' added to your shopping cart.');
$this
->addToCart($this->product, [
'qty' => 1,
]);
$assert
->pageTextContains('Your item(s) have been updated.');
}
}