View source
<?php
namespace Drupal\Tests\uc_attribute\Functional;
use Drupal\Tests\uc_store\Functional\UbercartBrowserTestBase;
class AttributeTest extends UbercartBrowserTestBase {
protected static $modules = [
'uc_attribute',
];
protected static $adminPermissions = [
'administer attributes',
'administer product attributes',
'administer product options',
];
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('page_title_block');
$this
->drupalLogin($this->adminUser);
}
public function testAttributeApi() {
$attribute = $this
->createAttribute();
$loaded_attribute = uc_attribute_load($attribute->aid);
foreach ($this
->attributeFieldsToTest() as $field) {
if ($loaded_attribute->{$field} != $attribute->{$field}) {
$this
->fail('Attribute integrity check failed.');
break;
}
}
$product = $this
->createProduct();
uc_attribute_subject_save($attribute, 'product', $product
->id());
$this
->assertEquals($attribute->aid, \Drupal::database()
->query('SELECT aid FROM {uc_product_attributes} WHERE nid = :nid', [
':nid' => $product
->id(),
])
->fetchField(), 'Attribute was attached to a product properly.');
$this
->assertTrue(uc_attribute_subject_exists($attribute->aid, 'product', $product
->id()));
$loaded_attribute = uc_attribute_load($attribute->aid, $product
->id(), 'product');
foreach ($this
->attributeFieldsToTest('product') as $field) {
if ($loaded_attribute->{$field} != $attribute->{$field}) {
$this
->fail('Attribute integrity check failed.');
break;
}
}
uc_attribute_subject_delete($attribute->aid, 'product', $product
->id());
$this
->assertFalse(\Drupal::database()
->query('SELECT aid FROM {uc_product_attributes} WHERE nid = :nid', [
':nid' => $product
->id(),
])
->fetchField(), 'Attribute was detached from a product properly.');
$this
->assertFalse(uc_attribute_subject_exists($attribute->aid, 'product', $product
->id()));
$product_class = $this
->createProductClass();
uc_attribute_subject_save($attribute, 'class', $product_class
->id());
$this
->assertEquals($attribute->aid, \Drupal::database()
->query('SELECT aid FROM {uc_class_attributes} WHERE pcid = :pcid', [
':pcid' => $product_class
->id(),
])
->fetchField(), 'Attribute was attached to a product class properly.');
$this
->assertTrue(uc_attribute_subject_exists($attribute->aid, 'class', $product_class
->id()));
$loaded_attribute = uc_attribute_load($attribute->aid, $product_class
->id(), 'class');
foreach ($this
->attributeFieldsToTest('class') as $field) {
if ($loaded_attribute->{$field} != $attribute->{$field}) {
$this
->fail('Attribute integrity check failed.');
break;
}
}
uc_attribute_subject_delete($attribute->aid, 'class', $product_class
->id());
$this
->assertFalse(\Drupal::database()
->query('SELECT aid FROM {uc_class_attributes} WHERE pcid = :pcid', [
':pcid' => $product_class
->id(),
])
->fetchField(), 'Attribute was detached from a product class properly.');
$this
->assertFalse(uc_attribute_subject_exists($attribute->aid, 'class', $product_class
->id()));
for ($i = 0; $i < 5; $i++) {
$a = $this
->createAttribute();
$attributes[$a->aid] = $a;
}
$attribute_aids = array_keys($attributes);
$all_options = [];
foreach ($attribute_aids as $aid) {
for ($i = 0; $i < 3; $i++) {
$option = $this
->createAttributeOption([
'aid' => $aid,
]);
$all_options[$option->aid][$option->oid] = $option;
}
}
for ($i = 0; $i < 3; $i++) {
$option = $this
->createAttributeOption([
'aid' => $aid,
]);
$all_options[$option->aid][$option->oid] = $option;
}
$attribute = uc_attribute_load($attribute->aid);
$attributes_with_options = uc_attribute_load_multiple();
foreach ($all_options as $aid => $options) {
foreach ($options as $oid => $option) {
foreach ($this
->attributeOptionFieldsToTest() as $field) {
if ($option->{$field} != $attributes_with_options[$aid]->options[$oid]->{$field}) {
$this
->fail('Option integrity check failed.');
break;
}
}
}
}
$aids = array_rand($attributes, 3);
$aids = array_combine($aids, $aids);
$loaded_attributes = uc_attribute_load_multiple($aids);
$this
->assertSameSize($aids, $loaded_attributes, 'Verifying attribute result.');
$this
->assertSameSize($aids, array_intersect_key($aids, $loaded_attributes), 'Verifying attribute result.');
foreach ($loaded_attributes as $aid => $loaded_attribute) {
foreach ($this
->attributeFieldsToTest() as $field) {
if ($attributes[$aid]->{$field} != $loaded_attributes[$aid]->{$field}) {
$this
->fail('Attribute integrity check failed.');
break;
}
}
}
foreach ($loaded_attributes as $loaded_attribute) {
uc_attribute_subject_save($loaded_attribute, 'product', $product
->id(), TRUE);
}
$loaded_product_attributes = uc_attribute_load_multiple([], 'product', $product
->id());
$product_attributes = $loaded_attributes;
$this
->assertSameSize($loaded_product_attributes, $product_attributes, 'Verifying attribute result.');
$this
->assertSameSize($loaded_product_attributes, array_intersect_key($loaded_product_attributes, $product_attributes), 'Verifying attribute result.');
foreach ($loaded_product_attributes as $aid => $loaded_product_attribute) {
foreach ($this
->attributeFieldsToTest('product') as $field) {
if ($loaded_product_attributes[$aid]->{$field} != $product_attributes[$aid]->{$field}) {
$this
->fail('Attribute integrity check failed.');
break;
}
}
}
foreach ($all_options as $aid => $options) {
foreach ($options as $oid => $option) {
if (empty($loaded_product_attributes[$aid]) || empty($loaded_product_attributes[$aid]->options[$oid])) {
continue;
}
foreach ($this
->attributeOptionFieldsToTest() as $field) {
if ($option->{$field} != $loaded_product_attributes[$aid]->options[$oid]->{$field}) {
$this
->fail('Option integrity check failed.');
break;
}
}
}
}
foreach ($loaded_attributes as $loaded_attribute) {
uc_attribute_subject_save($loaded_attribute, 'class', $product_class
->id(), TRUE);
}
$loaded_class_attributes = uc_attribute_load_multiple([], 'class', $product_class
->id());
$class_attributes = $loaded_attributes;
$this
->assertSameSize($loaded_class_attributes, $class_attributes, 'Verifying attribute result.');
$this
->assertSameSize($loaded_class_attributes, array_intersect_key($loaded_class_attributes, $class_attributes), 'Verifying attribute result.');
foreach ($loaded_class_attributes as $aid => $loaded_class_attribute) {
foreach ($this
->attributeFieldsToTest('class') as $field) {
if ($loaded_class_attributes[$aid]->{$field} != $class_attributes[$aid]->{$field}) {
$this
->fail('Attribute integrity check failed.');
break;
}
}
}
foreach ($all_options as $aid => $options) {
foreach ($options as $oid => $option) {
if (empty($loaded_class_attributes[$aid]) || empty($loaded_class_attributes[$aid]->options[$oid])) {
continue;
}
foreach ($this
->attributeOptionFieldsToTest() as $field) {
if ($option->{$field} != $loaded_class_attributes[$aid]->options[$oid]->{$field}) {
$this
->fail('Option integrity check failed.');
break;
}
}
}
}
$options = $attribute->options;
uc_attribute_delete($attribute->aid);
$this
->assertFalse(uc_attribute_load($attribute->aid), 'Attribute was deleted properly.');
$this
->assertFalse(\Drupal::database()
->query('SELECT aid FROM {uc_attributes} WHERE aid = :aid', [
':aid' => $attribute->aid,
])
->fetchField(), 'Attribute was seriously deleted properly!');
foreach ($options as $option) {
$this
->assertFalse(\Drupal::database()
->query('SELECT oid FROM {uc_attribute_options} WHERE oid = :oid', [
':oid' => $option->oid,
])
->fetchField(), 'Make sure options are deleted properly.');
}
$loaded_product_attributes = uc_attribute_load_multiple([], 'product', $product
->id());
$product_attributes = $loaded_attributes;
$this
->assertSameSize($loaded_product_attributes, $product_attributes, 'Verifying attribute result.');
$this
->assertSameSize($loaded_product_attributes, array_intersect_key($loaded_product_attributes, $product_attributes), 'Verifying attribute result.');
$loaded_class_attributes = uc_attribute_load_multiple([], 'class', $product_class
->id());
$class_attributes = $loaded_attributes;
$this
->assertSameSize($loaded_class_attributes, $class_attributes, 'Verifying attribute result.');
$this
->assertSameSize($loaded_class_attributes, array_intersect_key($loaded_class_attributes, $class_attributes), 'Verifying attribute result.');
$this
->createProductAdjustment([
'combination' => 'a:1:{i:1;s:1:"1";}',
'nid' => 1,
]);
$this
->createProductAdjustment([
'combination' => 'a:1:{i:1;s:1:"2";}',
'nid' => 1,
]);
$this
->createProductAdjustment([
'combination' => 'a:1:{i:1;s:1:"3";}',
'nid' => 1,
]);
$this
->createProductAdjustment([
'combination' => 'a:1:{i:2;s:1:"1";}',
'nid' => 2,
]);
$this
->createProductAdjustment([
'combination' => 'a:1:{i:3;s:1:"1";}',
'nid' => 2,
]);
$this
->createProductAdjustment([
'combination' => 'a:1:{i:1;s:1:"2";}',
'nid' => 3,
]);
$this
->createProductAdjustment([
'combination' => 'a:1:{i:1;s:1:"3";}',
'nid' => 3,
]);
$this
->createProductAdjustment([
'combination' => 'a:1:{i:3;s:1:"2";}',
'nid' => 3,
]);
$this
->createProductAdjustment([
'combination' => 'a:1:{i:3;s:1:"3";}',
'nid' => 4,
]);
uc_attribute_adjustments_delete([
'nid' => 1,
]);
$this
->assertEquals(6, \Drupal::database()
->query('SELECT COUNT(*) FROM {uc_product_adjustments}')
->fetchField());
uc_attribute_adjustments_delete([
'aid' => 2,
]);
$this
->assertEquals(5, \Drupal::database()
->query('SELECT COUNT(*) FROM {uc_product_adjustments}')
->fetchField());
uc_attribute_adjustments_delete([
'oid' => 2,
]);
$this
->assertEquals(3, \Drupal::database()
->query('SELECT COUNT(*) FROM {uc_product_adjustments}')
->fetchField());
uc_attribute_adjustments_delete([
'aid' => 1,
'oid' => 3,
]);
$this
->assertEquals(2, \Drupal::database()
->query('SELECT COUNT(*) FROM {uc_product_adjustments}')
->fetchField());
}
public function testAttributeUiAddAttribute() {
$assert = $this
->assertSession();
$this
->drupalGet('admin/store/products/attributes/add');
$assert
->pageTextContains('The name of the attribute used in administrative forms');
$edit = (array) $this
->createAttribute([], FALSE);
$this
->submitForm($edit, 'Submit');
if ($edit['display'] != 0) {
$assert
->pageTextContains('Options for ' . $edit['name']);
$assert
->pageTextContains('No options for this attribute have been added yet.');
}
else {
$assert
->pageTextContains($edit['name']);
$assert
->pageTextContains($edit['label']);
}
$this
->drupalGet('admin/store/products/attributes');
$assert
->responseContains('<td>' . $edit['name'] . '</td>');
$assert
->responseContains('<td>' . $edit['label'] . '</td>');
$assert
->responseContains('<td>' . ($edit['required'] ? 'Yes' : 'No') . '</td>');
$assert
->responseContains('<td>' . $edit['ordering'] . '</td>');
$types = _uc_attribute_display_types();
$assert
->responseContains('<td>' . $types[$edit['display']] . '</td>');
$aid = \Drupal::database()
->query('SELECT aid FROM {uc_attributes} WHERE name = :name', [
':name' => $edit['name'],
])
->fetchField();
$this
->assertTrue($aid, 'Attribute was created.');
$attribute = uc_attribute_load($aid);
$fields_ok = TRUE;
foreach ($edit as $field => $value) {
if ($attribute->{$field} != $value) {
$this
->showVar($attribute);
$this
->showVar($edit);
$fields_ok = FALSE;
break;
}
}
$this
->assertTrue($fields_ok, 'Attribute created properly.');
}
public function testAttributeUiSettings() {
$assert = $this
->assertSession();
$product = $this
->createProduct();
$attribute = $this
->createAttribute([
'display' => 1,
]);
$option = $this
->createAttributeOption([
'aid' => $attribute->aid,
'price' => 30,
]);
$attribute->options[$option->oid] = $option;
uc_attribute_subject_save($attribute, 'product', $product
->id(), TRUE);
$qty = $product->default_qty->value;
if (!$qty) {
$qty = 1;
}
$adjust_price = uc_currency_format($option->price * $qty);
$total_price = uc_currency_format(($product->price->value + $option->price) * $qty);
$raw = [
'none' => $option->name . '</option>',
'adjustment' => $option->name . ', +' . $adjust_price . '</option>',
'total' => $total_price . '</option>',
];
foreach ([
'none',
'adjustment',
'total',
] as $type) {
$edit['uc_attribute_option_price_format'] = $type;
$this
->drupalGet('admin/store/config/products');
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet('node/' . $product
->id());
$assert
->responseContains($raw[$type]);
}
}
public function testAttributeUiEditAttribute() {
$assert = $this
->assertSession();
$attribute = $this
->createAttribute();
$this
->drupalGet('admin/store/products/attributes/' . $attribute->aid . '/edit');
$assert
->pageTextContains('Edit attribute: ' . $attribute->name);
$edit = (array) $this
->createAttribute([], FALSE);
$this
->submitForm($edit, 'Submit');
$attribute = uc_attribute_load($attribute->aid);
$fields_ok = TRUE;
foreach ($edit as $field => $value) {
if ($attribute->{$field} != $value) {
$this
->showVar($attribute);
$this
->showVar($edit);
$fields_ok = FALSE;
break;
}
}
$this
->assertTrue($fields_ok, 'Attribute edited properly.');
}
public function testAttributeUiDeleteAttribute() {
$assert = $this
->assertSession();
$attribute = $this
->createAttribute();
$this
->drupalGet('admin/store/products/attributes/' . $attribute->aid . '/delete');
$assert
->pageTextContains('Are you sure you want to delete the attribute ' . $attribute->name . '?');
$this
->submitForm([], 'Delete');
$assert
->pageTextContains('Product attribute deleted.');
}
public function testAttributeUiAttributeOptions() {
$assert = $this
->assertSession();
$attribute = $this
->createAttribute();
$option = $this
->createAttributeOption([
'aid' => $attribute->aid,
]);
uc_attribute_option_save($option);
$this
->drupalGet('admin/store/products/attributes/' . $attribute->aid . '/options');
$assert
->pageTextContains('Options for ' . $attribute->name);
}
public function testAttributeUiAttributeOptionsAdd() {
$assert = $this
->assertSession();
$attribute = $this
->createAttribute();
$this
->drupalGet('admin/store/products/attributes/' . $attribute->aid . '/options/add');
$assert
->pageTextContains('Options for ' . $attribute->name);
$edit = (array) $this
->createAttributeOption([
'aid' => $attribute->aid,
], FALSE);
unset($edit['aid']);
$this
->drupalGet('admin/store/products/attributes/' . $attribute->aid . '/options/add');
$this
->submitForm($edit, 'Submit');
$option = \Drupal::database()
->query('SELECT * FROM {uc_attribute_options} WHERE aid = :aid', [
':aid' => $attribute->aid,
])
->fetchObject();
$fields_ok = TRUE;
foreach ($edit as $field => $value) {
if ($option->{$field} != $value) {
$this
->showVar($option);
$this
->showVar($edit);
$fields_ok = FALSE;
break;
}
}
$this
->assertTrue($fields_ok, 'Attribute option added successfully by form.');
}
public function testAttributeUiAttributeOptionsEdit() {
$assert = $this
->assertSession();
$attribute = $this
->createAttribute();
$option = $this
->createAttributeOption([
'aid' => $attribute->aid,
]);
uc_attribute_option_save($option);
$this
->drupalGet('admin/store/products/attributes/' . $attribute->aid . '/options/' . $option->oid . '/edit');
$assert
->pageTextContains('Edit option: ' . $option->name);
$edit = (array) $this
->createAttributeOption([
'aid' => $attribute->aid,
], FALSE);
unset($edit['aid']);
$this
->drupalGet('admin/store/products/attributes/' . $attribute->aid . '/options/' . $option->oid . '/edit');
$this
->submitForm($edit, 'Submit');
$option = uc_attribute_option_load($option->oid);
$fields_ok = TRUE;
foreach ($edit as $field => $value) {
if ($option->{$field} != $value) {
$this
->showVar($option);
$this
->showVar($edit);
$fields_ok = FALSE;
break;
}
}
$this
->assertTrue($fields_ok, 'Attribute option edited successfully by form.');
}
public function testAttributeUiAttributeOptionsDelete() {
$assert = $this
->assertSession();
$attribute = $this
->createAttribute();
$option = $this
->createAttributeOption([
'aid' => $attribute->aid,
]);
uc_attribute_option_save($option);
$this
->drupalGet('admin/store/products/attributes/' . $attribute->aid . '/options/' . $option->oid . '/delete');
$assert
->pageTextContains('Are you sure you want to delete the option ' . $option->name . '?');
$this
->submitForm([], 'Delete');
$option = uc_attribute_option_load($option->oid);
$this
->assertFalse($option, 'Attribute option deleted successfully by form');
}
public function testAttributeUiClassAttributeOverview() {
$assert = $this
->assertSession();
$class = $this
->createProductClass();
$attribute = $this
->createAttribute();
$this
->drupalGet('admin/structure/types/manage/' . $class
->id() . '/attributes');
$assert
->pageTextContains('No attributes available.');
uc_attribute_subject_save($attribute, 'class', $class
->id());
$this
->drupalGet('admin/structure/types/manage/' . $class
->id() . '/attributes');
$assert
->pageTextNotContains('No attributes available.');
$a = (array) $this
->createAttribute([], FALSE);
unset($a['name'], $a['description']);
foreach ($a as $field => $value) {
$edit["attributes[{$attribute->aid}][{$field}]"] = $value;
}
$this
->showVar($edit);
$this
->drupalGet('admin/structure/types/manage/' . $class
->id() . '/attributes');
$this
->submitForm($edit, 'Save changes');
$attribute = uc_attribute_load($attribute->aid, $class
->id(), 'class');
$fields_ok = TRUE;
foreach ($a as $field => $value) {
if ($attribute->{$field} != $value) {
$this
->showVar($attribute);
$this
->showVar($a);
$fields_ok = FALSE;
break;
}
}
$this
->assertTrue($fields_ok, 'Class attribute edited successfully by form.');
$edit = [];
$edit["attributes[{$attribute->aid}][remove]"] = TRUE;
$this
->drupalGet('admin/structure/types/manage/' . $class
->id() . '/attributes');
$this
->submitForm($edit, 'Save changes');
$assert
->pageTextContains('No attributes available.');
}
public function testAttributeUiClassAttributeAdd() {
$assert = $this
->assertSession();
$class = $this
->createProductClass();
$attribute = $this
->createAttribute();
$this
->drupalGet('admin/structure/types/manage/' . $class
->id() . '/attributes/add');
$assert
->responseContains($attribute->name . '</label>');
$edit['add_attributes[' . $attribute->aid . ']'] = 1;
$this
->drupalGet('admin/structure/types/manage/' . $class
->id() . '/attributes/add');
$this
->submitForm($edit, 'Add attributes');
$assert
->pageTextNotContains('No attributes available.');
}
public function testAttributeUiClassAttributeOptionOverview() {
$assert = $this
->assertSession();
$class = $this
->createProductClass();
$attribute = $this
->createAttribute();
$option = $this
->createAttributeOption([
'aid' => $attribute->aid,
]);
uc_attribute_subject_save($attribute, 'class', $class
->id());
$this
->drupalGet('admin/structure/types/manage/' . $class
->id() . '/options');
$assert
->responseContains($option->name . '</label>');
$o = (array) $this
->createAttributeOption([
'aid' => $attribute->aid,
], FALSE);
unset($o['name'], $o['aid']);
$o['select'] = TRUE;
foreach ($o as $field => $value) {
$edit["attributes[{$attribute->aid}][options][{$option->oid}][{$field}]"] = $value;
}
unset($o['select']);
$edit["attributes[{$attribute->aid}][default]"] = $option->oid;
$this
->showVar($edit);
$this
->drupalGet('admin/structure/types/manage/' . $class
->id() . '/options');
$this
->submitForm($edit, 'Save changes');
$assert
->pageTextContains('The changes have been saved.');
$this
->showVar($option);
$option = uc_attribute_subject_option_load($option->oid, 'class', $class
->id());
$fields_ok = TRUE;
foreach ($o as $field => $value) {
if ($option->{$field} != $value) {
$this
->showVar($option);
$this
->showVar($o);
$fields_ok = FALSE;
break;
}
}
$this
->assertTrue($fields_ok, 'Class attribute option edited successfully by form.');
}
public function testAttributeUiProductAttributes() {
$assert = $this
->assertSession();
$product = $this
->createProduct();
$attribute = $this
->createAttribute([
'display' => 1,
]);
$option = $this
->createAttributeOption([
'aid' => $attribute->aid,
]);
$this
->drupalGet('node/' . $product
->id() . '/edit/attributes');
$assert
->pageTextContains('No attributes available.');
$this
->clickLink('Add existing attribute');
$assert
->pageTextContains($attribute->name);
$this
->submitForm([
'add_attributes[' . $attribute->aid . ']' => 1,
], 'Add attributes');
$assert
->pageTextContains('1 attribute has been added.');
$assert
->pageTextContains($attribute->name);
$assert
->fieldValueEquals('attributes[' . $attribute->aid . '][label]', $attribute->label);
$assert
->pageTextContains($option->name);
$assert
->pageTextContains(uc_currency_format($option->price));
$assert
->fieldValueEquals('attributes[' . $attribute->aid . '][display]', $attribute->display);
$this
->drupalGet('node/' . $product
->id() . '/edit/attributes/add');
$assert
->pageTextNotContains($attribute->name);
$assert
->pageTextContains('No attributes left to add.');
$edit = [
'attributes[' . $attribute->aid . '][remove]' => 1,
];
$this
->drupalGet('node/' . $product
->id() . '/edit/attributes');
$this
->submitForm($edit, 'Save changes');
$assert
->pageTextContains('No attributes available.');
}
public function testAttributeUiProductOptions() {
$assert = $this
->assertSession();
$product = $this
->createProduct();
$attribute = $this
->createAttribute([
'display' => 1,
]);
for ($i = 0; $i < 3; $i++) {
$option = $this
->createAttributeOption([
'aid' => $attribute->aid,
]);
$attribute->options[$option->oid] = $option;
}
uc_attribute_subject_save($attribute, 'product', $product
->id(), TRUE);
$this
->drupalGet('node/' . $product
->id() . '/edit/options');
$assert
->pageTextContains($attribute->name);
foreach ($attribute->options as $option) {
$assert
->pageTextContains($option->name);
$assert
->fieldValueEquals('attributes[' . $attribute->aid . '][options][' . $option->oid . '][cost]', uc_currency_format($option->cost, FALSE));
$assert
->fieldValueEquals('attributes[' . $attribute->aid . '][options][' . $option->oid . '][price]', uc_currency_format($option->price, FALSE));
$assert
->fieldValueEquals('attributes[' . $attribute->aid . '][options][' . $option->oid . '][weight]', $option->weight);
}
}
public function testAttributeUiProductAdjustments() {
$assert = $this
->assertSession();
$product = $this
->createProduct();
$attribute = $this
->createAttribute([
'display' => 1,
]);
for ($i = 0; $i < 3; $i++) {
$option = $this
->createAttributeOption([
'aid' => $attribute->aid,
]);
$adjustment = $this
->createProductAdjustment([
'combination' => serialize([
$attribute->aid => $option->oid,
]),
'nid' => $product
->id(),
]);
$option->model = $adjustment->model;
$attribute->options[$option->oid] = $option;
}
uc_attribute_subject_save($attribute, 'product', $product
->id(), TRUE);
$this
->drupalGet('node/' . $product
->id() . '/edit/adjustments');
$assert
->pageTextContains('Default product SKU: ' . $product->model->value);
$assert
->responseContains('<th>' . $attribute->name . '</th>');
foreach ($attribute->options as $option) {
$assert
->responseContains('<td>' . $option->name . '</td>');
$assert
->responseContains($option->model);
}
}
public function testProductAttribute() {
$assert = $this
->assertSession();
$product = $this
->createProduct();
$attribute = $this
->createAttribute([
'display' => 2,
'required' => TRUE,
]);
for ($i = 0; $i < 3; $i++) {
$option = $this
->createAttributeOption([
'aid' => $attribute->aid,
]);
$adjustment = $this
->createProductAdjustment([
'combination' => serialize([
$attribute->aid => $option->oid,
]),
'nid' => $product
->id(),
]);
$option->model = $adjustment->model;
$attribute->options[$option->oid] = $option;
}
uc_attribute_subject_save($attribute, 'product', $product
->id(), TRUE);
$this
->drupalGet('node/' . $product
->id());
$assert
->pageTextContains($attribute->label);
$assert
->pageTextContains($attribute->description);
foreach ($attribute->options as $option) {
$assert
->pageTextContains($option->name);
$assert
->pageTextContains(uc_currency_format($option->price));
}
$this
->addToCart($product);
$assert
->pageTextContains($attribute->label . ' field is required');
$price = uc_currency_format($product->price->value + $option->price);
$this
->addToCart($product, [
'attributes[' . $attribute->aid . ']' => $option->oid,
]);
$assert
->pageTextContains($attribute->label . ': ' . $option->name);
$assert
->pageTextContains($price);
$this
->submitForm([], 'Checkout');
$assert
->pageTextContains($attribute->label . ': ' . $option->name);
$assert
->pageTextContains($price);
$this
->checkout();
$cost = uc_currency_format($product->cost->value + $option->cost);
$this
->drupalGet('admin/store/orders/1');
$assert
->pageTextContains($attribute->label . ': ' . $option->name, '');
$assert
->pageTextContains($cost);
$assert
->pageTextContains($price);
$assert
->pageTextContains($option->model);
$this
->drupalGet('admin/store/orders/1/invoice');
$assert
->pageTextContains($attribute->label . ': ' . $option->name);
$assert
->pageTextContains($price);
$assert
->pageTextContains('SKU: ' . $option->model);
}
public function testAttributeAddToCart() {
$assert = $this
->assertSession();
for ($display = 0; $display <= 3; ++$display) {
$attribute = $this
->createAttribute([
'display' => $display,
]);
if ($display) {
$option = $this
->createAttributeOption([
'aid' => $attribute->aid,
]);
}
$attribute = uc_attribute_load($attribute->aid);
$product = $this
->createProduct();
uc_attribute_subject_save($attribute, 'product', $product
->id(), TRUE);
if ($display == 3) {
$edit = [
"attributes[{$attribute->aid}][{$option->oid}]" => $option->oid,
];
}
elseif (isset($option)) {
$edit = [
"attributes[{$attribute->aid}]" => $option->oid,
];
}
else {
$option = new \stdClass();
$option->name = self::randomMachineName();
$option->price = 0;
$edit = [
"attributes[{$attribute->aid}]" => $option->name,
];
}
$this
->addToCart($product, $edit);
$assert
->pageTextContains("{$attribute->label}: {$option->name}", 'Option selected on cart item.');
$assert
->pageTextContains(uc_currency_format($product->price->value + $option->price));
}
}
public function createProductAdjustment(array $data) {
$adjustment = $data + [
'model' => $this
->randomMachineName(8),
];
\Drupal::database()
->insert('uc_product_adjustments')
->fields($adjustment)
->execute();
return (object) $adjustment;
}
protected function attributeFieldsToTest($type = '') {
$fields = [
'aid',
'name',
'ordering',
'required',
'display',
'description',
'label',
];
switch ($type) {
case 'product':
case 'class':
$info = uc_attribute_type_info($type);
$fields = array_merge($fields, [
$info['id'],
]);
break;
}
return $fields;
}
protected function attributeOptionFieldsToTest($type = '') {
$fields = [
'aid',
'oid',
'name',
'cost',
'price',
'weight',
'ordering',
];
switch ($type) {
case 'product':
case 'class':
$info = uc_attribute_type_info($type);
$fields = array_merge($fields, [
$info['id'],
]);
break;
}
return $fields;
}
protected function showVar($var) {
$this
->assertTrue(TRUE, '<pre>' . print_r($var, TRUE) . '</pre>');
}
}