Commit 3d47a21c authored by Volker Krause's avatar Volker Krause
Browse files

Integrate aggregation settings into product management

parent caca58ab
......@@ -16,6 +16,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
require_once('aggregation.php');
require_once('schemaentry.php');
require_once('utils.php');
......@@ -24,6 +25,7 @@ class Product
{
public $name;
public $schema = array();
public $aggregation = array();
private $productId = -1;
......@@ -53,6 +55,7 @@ class Product
$p->productId = $row['id'];
$p->name = $row['name'];
$p->schema = SchemaEntry::loadSchema($db, $p);
$p->aggregation = Aggregation::aggregationsForProduct($db, $p);
array_push($products, $p);
}
return $products;
......@@ -94,6 +97,9 @@ class Product
// create schema entries
foreach ($this->schema as $entry)
$entry->insert($db, $this->productId);
// store aggregation settings
Aggregation::update($db, $this, $this->aggregation);
}
/** Update an existing product in the database to match @p $newProduct. */
......@@ -118,11 +124,17 @@ class Product
// delete whatever is left
foreach($oldSchema as $entry)
$entry->delete($db, $this->productId);
// store aggregation settings
Aggregation::update($db, $this, $newProduct->aggregation);
}
/** Delete an existing product in the database. */
public function delete(Datastore $db)
{
// delete aggregation settings
Aggregation::delete($db, $this);
// delete schema entries
foreach ($this->schema as $entry)
$entry->delete($db, $this->productId);
......@@ -146,6 +158,8 @@ class Product
$p = new Product();
$p->name = $jsonObj->name;
$p->schema = SchemaEntry::fromJson($jsonObj->schema, $p);
if (property_exists($jsonObj, 'aggregation'))
$p->aggregation = Aggregation::fromJson($jsonObj->aggregation);
// verify
if (strlen($p->name) <= 0 || !is_string($p->name))
......
......@@ -71,7 +71,10 @@ class ProductTest extends PHPUnit_Extensions_Database_TestCase
{ "name": "element2", "type": "bool" }
]
}
]
],
"aggregation": [ { "type": "category", "elements": [
{ "type": "value", "schemaEntry": "entry1", "schemaEntryElement": "element1" }
] } ]
}');
$this->assertEquals($p->name, 'org.kde.product');
$this->assertEquals(1, count($p->schema));
......@@ -86,6 +89,8 @@ class ProductTest extends PHPUnit_Extensions_Database_TestCase
$this->assertInstanceOf(SchemaEntryElement::class, $elem);
$this->assertEquals('element2', $elem->name);
$this->assertEquals('bool', $elem->type);
$this->assertCount(1, $p->aggregation);
}
public function testToJson()
......@@ -101,6 +106,10 @@ class ProductTest extends PHPUnit_Extensions_Database_TestCase
$elem->type = 'number';
array_push($entry->elements, $elem);
array_push($p->schema, $entry);
$a = new Aggregation;
$a->type = 'numeric';
$a->elements = json_decode('[{ "type": "size", "schemaEntry": "entry1" }]');
array_push($p->aggregation, $a);
$this->assertJsonStringEqualsJsonString(json_encode($p), '{
"name": "org.kde.product",
......@@ -113,7 +122,10 @@ class ProductTest extends PHPUnit_Extensions_Database_TestCase
{ "name": "element3", "type": "number" }
]
}
]
],
"aggregation": [ { "type": "numeric", "elements": [
{ "type": "size", "schemaEntry": "entry1" }
] } ]
}');
}
......@@ -147,6 +159,8 @@ class ProductTest extends PHPUnit_Extensions_Database_TestCase
$this->assertEquals($entry2->name, 'entry2');
$this->assertEquals($entry2->type, 'list');
$this->assertEquals($entry2->aggregationType, 'xy');
$this->assertCount(2, $p->aggregation);
}
public function testProductByName()
......@@ -204,6 +218,20 @@ class ProductTest extends PHPUnit_Extensions_Database_TestCase
{ "name": "elementB2", "type": "string" }
]
}
],
"aggregation": [
{
"type": "category",
"elements": [
{ "type": "value", "schemaEntry": "entryA", "schemaEntryElement": "elementA1" }
]
},
{
"type": "ratio_set",
"elements": [
{ "type": "value", "schemaEntry": "entryB", "schemaEntryElement": "elementB1" }
]
}
]
}';
......@@ -211,6 +239,7 @@ class ProductTest extends PHPUnit_Extensions_Database_TestCase
$p->insert(self::$db);
$p = Product::productByName(self::$db, 'org.kde.NewProduct');
$p->aggregation = Aggregation::aggregationsForProduct(self::$db, $p);
$this->assertJsonStringEqualsJsonString($json, json_encode($p));
}
......@@ -237,6 +266,14 @@ class ProductTest extends PHPUnit_Extensions_Database_TestCase
{ "name": "elementB2", "type": "string" }
]
}
],
"aggregation": [
{
"type": "ratio_set",
"elements": [
{ "type": "value", "schemaEntry": "entryB", "schemaEntryElement": "elementB1" }
]
}
]
}';
......@@ -244,6 +281,7 @@ class ProductTest extends PHPUnit_Extensions_Database_TestCase
$p->insert(self::$db);
$p = Product::productByName(self::$db, 'org.kde.ProductToUpdate');
$p->aggregation = Aggregation::aggregationsForProduct(self::$db, $p);
$this->assertJsonStringEqualsJsonString($jsonOld, json_encode($p));
$jsonNew = '{
......@@ -266,12 +304,28 @@ class ProductTest extends PHPUnit_Extensions_Database_TestCase
{ "name": "elementC2", "type": "number" }
]
}
],
"aggregation": [
{
"type": "category",
"elements": [
{ "type": "value", "schemaEntry": "entryA", "schemaEntryElement": "elementA1" }
]
},
{
"type": "xy",
"elements": [
{ "type": "value", "schemaEntry": "entryC", "schemaEntryElement": "elementC1" },
{ "type": "value", "schemaEntry": "entryC", "schemaEntryElement": "elementC2" }
]
}
]
}';
$newP = Product::fromJson($jsonNew);
$p->update(self::$db, $newP);
$p = Product::productByName(self::$db, 'org.kde.ProductToUpdate');
$p->aggregation = Aggregation::aggregationsForProduct(self::$db, $p);
$this->assertJsonStringEqualsJsonString($jsonNew, json_encode($p));
}
......@@ -288,12 +342,21 @@ class ProductTest extends PHPUnit_Extensions_Database_TestCase
{ "name": "element3", "type": "number" }
]
}
],
"aggregation": [
{
"type": "numeric",
"elements": [
{ "type": "size", "schemaEntry": "entry1" }
]
}
]
}';
$p = Product::fromJson($json);
$p->insert(self::$db);
$p = Product::productByName(self::$db, 'org.kde.DeletedProduct');
$p->aggregation = Aggregation::aggregationsForProduct(self::$db, $p);
$this->assertJsonStringEqualsJsonString($json, json_encode($p));
$p->delete(self::$db);
......
Supports Markdown
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