Skip to content

Commit

Permalink
bugfix - force ProductCategory Code to be unique (#371)
Browse files Browse the repository at this point in the history
fixes #339
  • Loading branch information
jsirish committed Dec 5, 2018
1 parent 8cd82bd commit 1a1c218
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 33 deletions.
47 changes: 17 additions & 30 deletions src/Model/ProductCategory.php
Expand Up @@ -86,7 +86,10 @@ class ProductCategory extends DataObject
* @var array
*/
private static $indexes = [
'Code' => true,
'Code' => [
'type' => 'unique',
'columns' => ['Code'],
],
];

/**
Expand Down Expand Up @@ -183,37 +186,21 @@ public function getCMSFields()
->setDescription('Enter a dollar amount here for the declared customs value for international
shipments. If you leave this blank, the sale price of the item will be used.');

/*
$fields = FieldList::create(
LiteralField::create(
'PCIntro',
_t(
'ProductCategory.PCIntro',
'<p>Categories must be created in your
<a href="https://admin.foxycart.com/admin.php?ThisAction=ManageProductCategories"
target="_blank">
FoxyCart Product Categories
</a>, and also manually created in FoxyStripe.
</p>'
)
),
TextField::create('Code')
->setTitle(_t('ProductCategory.Code', 'Category Code'))
->setDescription(_t('ProductCategory.CodeDescription', 'copy/paste from FoxyCart')),
TextField::create('Title')
->setTitle(_t('ProductCategory.Title', 'Category Title'))
->setDescription(_t('ProductCategory.TitleDescription', 'copy/paste from FoxyCart')),
DropdownField::create(
'DeliveryType',
'Delivery Type',
singleton('ProductCategory')->dbObject('DeliveryType')->enumValues()
)->setEmptyString('')
);
return $fields;
}

$this->extend('updateCMSFields', $fields);
*/
/**
* @return \SilverStripe\ORM\ValidationResult
*/
public function validate()
{
$result = parent::validate();

return $fields;
if (ProductCategory::get()->filter('Code', $this->Code)->exclude('ID', $this->ID)->first()) {
$result->addError('Code must be unique for each category.');
}

return $result;
}

/**
Expand Down
3 changes: 0 additions & 3 deletions tests/ProductPageTest.php
Expand Up @@ -107,7 +107,6 @@ public function testProductCategoryCreation()
{
$this->logInWithPermission('Product_CANCRUD');
$category = $this->objFromFixture(ProductCategory::class, 'apparel');
$category->write();
$categoryID = $category->ID;

$productCategory = ProductCategory::get()->filter(array('Code' => 'APPAREL'))->first();
Expand All @@ -120,12 +119,10 @@ public function testProductCategoryDeletion()
$this->logInWithPermission('Product_CANCRUD');

$category = $this->objFromFixture(ProductCategory::class, 'default');
$category->write();

$this->assertFalse($category->canDelete());

$category2 = $this->objFromFixture(ProductCategory::class, 'apparel');
$category2->write();
$category2ID = $category2->ID;

$this->assertTrue($category2->canDelete());
Expand Down

0 comments on commit 1a1c218

Please sign in to comment.