Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix - force ProductCategory Code to be unique #371

Merged
merged 3 commits into from
Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
47 changes: 17 additions & 30 deletions src/Model/ProductCategory.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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