Skip to content

Commit

Permalink
BUGFIX check permissions before returning product list (#28)
Browse files Browse the repository at this point in the history
* BUGFIX check permissions before returning product list

* UPDATE tests

* UPDATE tests and skip

* UPDATE remove unused “$count” var
  • Loading branch information
muskie9 authored Dec 17, 2019
1 parent a5387db commit 52c5db2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/Page/ProductCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\NumericField;
use SilverStripe\Security\Security;

class ProductCategory extends \Page
{
Expand Down Expand Up @@ -67,6 +68,10 @@ public function getProductList()

$this->extend('updateProductList', $products, $categories);

$products = $products->filterByCallback(function ($page) {
return $page->canView(Security::getCurrentUser());
});

return $products;
}
}
28 changes: 28 additions & 0 deletions tests/Page/ProductCategoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Dynamic\Products\Page\ProductCategory;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\FieldList;
use SilverStripe\Security\Member;

class ProductCategoryTest extends SapphireTest
{
Expand All @@ -22,4 +23,31 @@ public function testGetCMSFields()
$fields = $object->getCMSFields();
$this->assertInstanceOf(FieldList::class, $fields);
}

/**
*
*/
public function testGetProductList()
{
$this->markTestSkipped('Currently doesn\'t seem to respect the groups/members in automated tests');

$this->logOut();
$member = $this->objFromFixture(Member::class, 'author');
$this->logInAs(Member::get()->byID($member->ID));
$categoryID = $this->objFromFixture(ProductCategory::class, 'restricted')->ID;
/** @var ProductCategory $category */
$category = ProductCategory::get()->byID($categoryID);

$this->assertEquals(2, $category->getProductList()->count());

$this->logOut();
$member = $this->objFromFixture(Member::class, 'default');
$this->logInAs(Member::get()->byID($member->ID));
/** @var ProductCategory $category */
$category = ProductCategory::get()->byID($categoryID);

$this->assertEquals(1, $category->getProductList()->count());

$this->logOut();
}
}
15 changes: 14 additions & 1 deletion tests/fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,26 @@ Dynamic\Products\Model\Brochure:
Dynamic\Products\Page\ProductCategory:
default:
Title: 'Default Category'
restricted:
Title: 'Restricted Children'
CanViewType: 'Inherit'

Dynamic\Products\Page\Product:
one:
Title: 'Product One'
Brochures: =>Dynamic\Products\Model\Brochure.one
restrictedproduct:
Title: 'Restricted Product'
CanViewType: 'OnlyTheseUsers'
ViewerGroups: =>SilverStripe\Security\Group.content_authors
Parent: =>Dynamic\Products\Page\ProductCategory.restricted
nonrestrictedproduct:
Title: 'Non Restricted Product'
Parent: =>Dynamic\Products\Page\ProductCategory.restricted
CanViewType: 'Inherit'


Dynamic\Products\Page\ProductFileCollection:
default:
Title: 'Brochures'
ManagedClass: 'Dynamic\Products\Model\Brochure'
ManagedClass: 'Dynamic\Products\Model\Brochure'

0 comments on commit 52c5db2

Please sign in to comment.