Skip to content

Commit

Permalink
Enforce unique IDs when adding list and property items
Browse files Browse the repository at this point in the history
  • Loading branch information
aimeos committed Jun 21, 2019
1 parent 0fdd6cd commit 2a55955
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
10 changes: 9 additions & 1 deletion lib/mshoplib/src/MShop/Common/Item/ListRef/Traits.php
Expand Up @@ -76,7 +76,7 @@ public function addListItem( $domain, \Aimeos\MShop\Common\Item\Lists\Iface $lis
$this->listRefItems[$domain][$id] = $refItem;
}

$id = $listItem->getId() ?: '_' . $domain . '_' . $listItem->getType() . '_' . $listItem->getRefId();
$id = $listItem->getId() ?: '_' . $this->getId() . '_' . $domain . '_' . $listItem->getType() . '_' . $listItem->getRefId();
$this->listItems[$domain][$id] = $listItem->setDomain( $domain )->setRefItem( $refItem );

if( isset( $this->listMap[$domain] ) ) {
Expand Down Expand Up @@ -312,6 +312,14 @@ public function getName( $type = 'name' )
}


/**
* Returns the unique ID of the item.
*
* @return string|null ID of the item
*/
abstract public function getId();


/**
* Returns the item type
*
Expand Down
10 changes: 9 additions & 1 deletion lib/mshoplib/src/MShop/Common/Item/PropertyRef/Traits.php
Expand Up @@ -49,7 +49,7 @@ public function __clone()
*/
public function addPropertyItem( \Aimeos\MShop\Common\Item\Property\Iface $item )
{
$id = $item->getId() ?: '_' . $item->getType() . '_' . $item->getLanguageId() . '_' . $item->getValue();
$id = $item->getId() ?: '_' . $this->getId() . '_' . $item->getType() . '_' . $item->getLanguageId() . '_' . $item->getValue();
$this->propItems[$id] = $item;

return $this;
Expand Down Expand Up @@ -171,6 +171,14 @@ public function getPropertyItems( $type = null, $active = true )
}


/**
* Returns the unique ID of the item.
*
* @return string|null ID of the item
*/
abstract public function getId();


/**
* Sets the property items in the trait
*
Expand Down
9 changes: 7 additions & 2 deletions lib/mshoplib/tests/MShop/Common/Item/ListRef/TraitsTest.php
Expand Up @@ -19,14 +19,19 @@ public function __construct( array $listItems, array $refItems )
$this->initListItems( $listItems, $refItems );
}

public function getId()
{
return 'id';
}

public function getLabel()
{
return 'test label';
}

public function getResourceType()
{
return '';
return 'test';
}
}

Expand Down Expand Up @@ -102,7 +107,7 @@ public function testAddListItemNew()
{
$this->object->addListItem( 'test', $this->listItem1->setId( null ), $this->textItem1->setId( null ) );

$this->assertEquals( ['_test_test_#2' => $this->listItem1], $this->object->getListItems( 'test' ) );
$this->assertEquals( ['_id_test_test_#2' => $this->listItem1], $this->object->getListItems( 'test' ) );
$this->assertEquals( ['#2' => $this->textItem1], $this->object->getRefItems( 'test' ) );
}

Expand Down
Expand Up @@ -12,6 +12,11 @@
class TraitsClass
{
use \Aimeos\MShop\Common\Item\PropertyRef\Traits;

public function getId()
{
return 'id';
}
}


Expand Down Expand Up @@ -88,7 +93,7 @@ public function testAddPropertyItem()
$object = new TraitsClass();
$object->addPropertyItem( $this->propItem );

$this->assertEquals( ['_test__value' => $this->propItem], $object->getPropertyItems() );
$this->assertEquals( ['_id_test__value' => $this->propItem], $object->getPropertyItems() );
}


Expand Down

0 comments on commit 2a55955

Please sign in to comment.