Commit 4bfdcd6e authored by bojanz's avatar bojanz

Rework the product sku constraint.

- Shorten the error message.
- Revert the per-store validation, it's not feasible the planned product architecture.
- Use the Symfony 2.5+ API for adding violations, instead of the deprecated one.
parent 0f45a239
......@@ -10,7 +10,7 @@ namespace Drupal\commerce_product\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
/**
* Supports validating product skus.
* Ensures product SKU uniqueness.
*
* @Constraint(
* id = "ProductSku",
......@@ -19,6 +19,6 @@ use Symfony\Component\Validator\Constraint;
*/
class ProductSkuConstraint extends Constraint {
public $message = 'The SKU %sku is already in use and must be unique. Please supply another value.';
public $message = 'The SKU %sku is already in use and must be unique.';
}
......@@ -21,18 +21,18 @@ class ProductSkuConstraintValidator extends ConstraintValidator {
public function validate($items, Constraint $constraint) {
$sku = $items->first()->value;
if (isset($sku) && $sku !== '') {
$productId = $items->getEntity()->product_id->value;
$storeId = $items->getEntity()->getStoreId();
$productId = $items->getEntity()->id();
$skuExists = (bool) \Drupal::entityQuery('commerce_product')
->condition("sku", $sku)
->condition('product_id', (int) $productId, '<>')
->condition('store_id', $storeId)
->range(0, 1)
->count()
->execute();
if ($skuExists) {
$this->context->addViolation($constraint->message, ['%sku' => $sku]);
$this->context->buildViolation($constraint->message)
->setParameter('%sku', $this->formatValue($sku))
->addViolation();
}
}
}
......
......@@ -45,47 +45,6 @@ class ProductTest extends CommerceProductTestBase {
$this->assertNotEqual($violations->count(), 0, 'Validation fails when creating a product with the same SKU.');
}
/**.
* Ensure that changing the store ID of the product to that of another store
* that already contains the same SKU does not save.
*/
function testAddCommerceProductExistingSkuDifferentStore() {
$values = [
'sku' => $this->randomMachineName(),
'title' => $this->randomMachineName(),
'type' => 'product',
'store_id' => $this->commerce_store->id()
];
/* @var $product \Drupal\commerce_product\Entity\Product */
$product = $this->createEntity('commerce_product', $values);
$name = strtolower($this->randomMachineName(8));
$store_type = $this->createEntity('commerce_store_type', [
'id' => $this->randomMachineName(),
'label' => $this->randomMachineName(),
]
);
/* @var $store2 \Drupal\commerce_store\Entity\Store */
$store2 = $this->createEntity('commerce_store', [
'type' => $store_type->id(),
'name' => $name,
'mail' => \Drupal::currentUser()->getEmail(),
'default_currency' => 'EUR',
]
);
$values['store_id'] = $store2->id();
$this->createEntity('commerce_product', $values);
$valid = $product->setStore($store2)->sku->validate()->count();
$this->assertEqual($valid, 1, 'Validation fails when changing the store_id.');
}
/**
* Tests deleting a product.
*/
......
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