producttest.php 12.5 KB
Newer Older
1 2 3 4
<?php
/*
    Copyright (C) 2016 Volker Krause <vkrause@kde.org>

Volker Krause's avatar
Volker Krause committed
5 6 7 8 9 10 11
    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
    "Software"), to deal in the Software without restriction, including
    without limitation the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software, and to
    permit persons to whom the Software is furnished to do so, subject to
    the following conditions:
12

Volker Krause's avatar
Volker Krause committed
13 14
    The above copyright notice and this permission notice shall be included
    in all copies or substantial portions of the Software.
15

Volker Krause's avatar
Volker Krause committed
16 17 18 19 20 21 22
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 24
*/

25
require_once('abstractdatastoretest.php');
26
require_once('../src/server/shared/product.php');
27

28
class ProductTest extends AbstractDatastoreTest
29
{
30 31 32
    public function testTableName_data()
    {
        return [
33 34
            'normal' => [ 'foo', 'pd_foo' ],
            'dot' => [ 'org.kde.foo', 'pd_org_kde_foo' ]
35 36 37 38 39 40 41 42 43 44 45
        ];
    }

    /** @dataProvider testTableName_data */
    public function testTableName($input, $output)
    {
        $p = new Product;
        $p->name = $input;
        $this->assertEquals($output, $p->dataTableName());
    }

46 47 48 49 50 51 52 53 54 55 56 57 58
    public function testFromJson()
    {
        $p = Product::fromJson('{
            "name": "org.kde.product",
            "schema": [
                {
                    "name": "entry1",
                    "type": "scalar",
                    "elements" : [
                        { "name": "element1", "type": "int" },
                        { "name": "element2", "type": "bool" }
                    ]
                }
59
            ],
Volker Krause's avatar
Volker Krause committed
60
            "aggregation": [ { "type": "category", "name": "n1", "elements": [
61 62
                { "type": "value", "schemaEntry": "entry1", "schemaEntryElement": "element1" }
            ] } ]
63 64 65
        }');
        $this->assertEquals($p->name, 'org.kde.product');
        $this->assertEquals(1, count($p->schema));
66
        $this->assertEquals('pd_org_kde_product', $p->dataTableName());
67 68 69 70 71 72 73 74 75
        $entry = $p->schema[0];
        $this->assertEquals($entry->name, 'entry1');
        $this->assertEquals($entry->type, 'scalar');

        $this->assertEquals(2, count($entry->elements));
        $elem = $entry->elements[1];
        $this->assertInstanceOf(SchemaEntryElement::class, $elem);
        $this->assertEquals('element2', $elem->name);
        $this->assertEquals('bool', $elem->type);
76 77

        $this->assertCount(1, $p->aggregation);
78 79 80 81 82 83
    }

    public function testToJson()
    {
        $p = new Product();
        $p->name = 'org.kde.product';
84
        $entry = new SchemaEntry($p);
85 86
        $entry->name = 'entry1';
        $entry->type = 'list';
87
        $elem = new SchemaEntryElement($entry);
88 89 90 91
        $elem->name = 'element3';
        $elem->type = 'number';
        array_push($entry->elements, $elem);
        array_push($p->schema, $entry);
92 93
        $a = new Aggregation;
        $a->type = 'numeric';
Volker Krause's avatar
Volker Krause committed
94
        $a->name = 'n1';
95 96
        $a->elements = json_decode('[{ "type": "size", "schemaEntry": "entry1" }]');
        array_push($p->aggregation, $a);
97

98
        $this->assertJsonStringEqualsJsonString(json_encode($p), '{
99 100 101 102 103 104 105 106 107
            "name": "org.kde.product",
            "schema": [
                {
                    "name": "entry1",
                    "type": "list",
                    "elements" : [
                        { "name": "element3", "type": "number" }
                    ]
                }
108
            ],
Volker Krause's avatar
Volker Krause committed
109
            "aggregation": [ { "type": "numeric", "name": "n1", "elements": [
110 111
                { "type": "size", "schemaEntry": "entry1" }
            ] } ]
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
        }');
    }

    public function testAllProducts()
    {
        $ps = Product::allProducts(self::$db);
        $this->assertEquals(1, count($ps));
        $p = $ps[0];
        $this->assertInstanceOf(Product::class, $p);
        $this->assertEquals('org.kde.UnitTest', $p->name);
        $this->assertEquals(2, count($p->schema));

        $entry1 = $p->schema[0];
        $entry2 = $p->schema[1];
        if ($entry1->name > $entry2->name) {
            $entry1 = $p->schema[1];
            $entry2 = $p->schema[0];
        }

        $this->assertInstanceOf(SchemaEntry::class, $entry1);
        $this->assertEquals($entry1->name, 'entry1');
        $this->assertEquals($entry1->type, 'scalar');
        $this->assertEquals(2, count($entry1->elements));
        $elem = $entry1->elements[1];
        $this->assertInstanceOf(SchemaEntryElement::class, $elem);
        $this->assertEquals('element12', $elem->name);
        $this->assertEquals('bool', $elem->type);

        $this->assertInstanceOf(SchemaEntry::class, $entry2);
        $this->assertEquals($entry2->name, 'entry2');
        $this->assertEquals($entry2->type, 'list');
143 144

        $this->assertCount(2, $p->aggregation);
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
    }

    public function testProductByName()
    {
        $p = Product::productByName(self::$db, 'I don\'t exist');
        $this->assertNull($p);

        $p = Product::productByName(self::$db, 'org.kde.UnitTest');
        $this->assertInstanceOf(Product::class, $p);
        $this->assertEquals('org.kde.UnitTest', $p->name);
        $this->assertEquals(2, count($p->schema));

        $entry1 = $p->schema[0];
        $entry2 = $p->schema[1];
        if ($entry1->name > $entry2->name) {
            $entry1 = $p->schema[1];
            $entry2 = $p->schema[0];
        }

        $this->assertInstanceOf(SchemaEntry::class, $entry1);
        $this->assertEquals($entry1->name, 'entry1');
        $this->assertEquals($entry1->type, 'scalar');
        $elem = $entry1->elements[1];
        $this->assertInstanceOf(SchemaEntryElement::class, $elem);
        $this->assertEquals('element12', $elem->name);
        $this->assertEquals('bool', $elem->type);

        $this->assertInstanceOf(SchemaEntry::class, $entry2);
        $this->assertEquals($entry2->name, 'entry2');
        $this->assertEquals($entry2->type, 'list');
    }

    public function testProductInsert()
    {
        $json = '{
            "name": "org.kde.NewProduct",
            "schema": [
                {
                    "name": "entryA",
                    "type": "scalar",
                    "elements" : [
                        { "name": "elementA1", "type": "int" },
                        { "name": "elementA2", "type": "bool" }
                    ]
                },
                {
                    "name": "entryB",
                    "type": "map",
                    "elements" : [
                        { "name": "elementB1", "type": "number" },
                        { "name": "elementB2", "type": "string" }
                    ]
                }
198 199 200 201
            ],
            "aggregation": [
                {
                    "type": "category",
Volker Krause's avatar
Volker Krause committed
202
                    "name": "n1",
203 204 205 206 207 208
                    "elements": [
                        { "type": "value", "schemaEntry": "entryA", "schemaEntryElement": "elementA1" }
                    ]
                },
                {
                    "type": "ratio_set",
Volker Krause's avatar
Volker Krause committed
209
                    "name": "n2",
210 211 212 213
                    "elements": [
                        { "type": "value", "schemaEntry": "entryB", "schemaEntryElement": "elementB1" }
                    ]
                }
214 215 216 217 218 219 220
            ]
        }';

        $p = Product::fromJson($json);
        $p->insert(self::$db);

        $p = Product::productByName(self::$db, 'org.kde.NewProduct');
221
        $p->aggregation = Aggregation::aggregationsForProduct(self::$db, $p);
222
        $this->assertJsonStringEqualsJsonString($json, json_encode($p));
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    }

    public function testProductUpdate()
    {
        $jsonOld = '{
            "name": "org.kde.ProductToUpdate",
            "schema": [
                {
                    "name": "entryA",
                    "type": "scalar",
                    "elements" : [
                        { "name": "elementA1", "type": "int" },
                        { "name": "elementA2", "type": "bool" }
                    ]
                },
                {
                    "name": "entryB",
                    "type": "list",
                    "elements" : [
                        { "name": "elementB1", "type": "number" },
                        { "name": "elementB2", "type": "string" }
                    ]
                }
246 247 248 249
            ],
            "aggregation": [
                {
                    "type": "ratio_set",
Volker Krause's avatar
Volker Krause committed
250
                    "name": "n2",
251 252 253 254
                    "elements": [
                        { "type": "value", "schemaEntry": "entryB", "schemaEntryElement": "elementB1" }
                    ]
                }
255 256 257 258 259 260 261
            ]
        }';

        $p = Product::fromJson($jsonOld);
        $p->insert(self::$db);

        $p = Product::productByName(self::$db, 'org.kde.ProductToUpdate');
262
        $p->aggregation = Aggregation::aggregationsForProduct(self::$db, $p);
263
        $this->assertJsonStringEqualsJsonString($jsonOld, json_encode($p));
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282

        $jsonNew = '{
            "name": "org.kde.ProductToUpdate",
            "schema": [
                {
                    "name": "entryA",
                    "type": "scalar",
                    "elements" : [
                        { "name": "elementA1", "type": "int" }
                    ]
                },
                {
                    "name": "entryC",
                    "type": "map",
                    "elements" : [
                        { "name": "elementC1", "type": "number" },
                        { "name": "elementC2", "type": "number" }
                    ]
                }
283 284 285 286
            ],
            "aggregation": [
                {
                    "type": "category",
Volker Krause's avatar
Volker Krause committed
287
                    "name": "n1",
288 289 290 291 292 293
                    "elements": [
                        { "type": "value", "schemaEntry": "entryA", "schemaEntryElement": "elementA1" }
                    ]
                },
                {
                    "type": "xy",
Volker Krause's avatar
Volker Krause committed
294
                    "name": "n3",
295 296 297 298 299
                    "elements": [
                        { "type": "value", "schemaEntry": "entryC", "schemaEntryElement": "elementC1" },
                        { "type": "value", "schemaEntry": "entryC", "schemaEntryElement": "elementC2" }
                    ]
                }
300 301 302 303 304 305
            ]
        }';
        $newP = Product::fromJson($jsonNew);
        $p->update(self::$db, $newP);

        $p = Product::productByName(self::$db, 'org.kde.ProductToUpdate');
306
        $p->aggregation = Aggregation::aggregationsForProduct(self::$db, $p);
307
        $this->assertJsonStringEqualsJsonString($jsonNew, json_encode($p));
308 309 310 311 312 313 314 315 316 317 318 319 320 321
    }

    public function testProductDelete()
    {
        $json = '{
            "name": "org.kde.DeletedProduct",
            "schema": [
                {
                    "name": "entry1",
                    "type": "list",
                    "elements" : [
                        { "name": "element3", "type": "number" }
                    ]
                }
322 323 324 325
            ],
            "aggregation": [
                {
                    "type": "numeric",
Volker Krause's avatar
Volker Krause committed
326
                    "name": "n4",
327 328 329 330
                    "elements": [
                        { "type": "size", "schemaEntry": "entry1" }
                    ]
                }
331 332 333 334 335 336
            ]
        }';

        $p = Product::fromJson($json);
        $p->insert(self::$db);
        $p = Product::productByName(self::$db, 'org.kde.DeletedProduct');
337
        $p->aggregation = Aggregation::aggregationsForProduct(self::$db, $p);
338
        $this->assertJsonStringEqualsJsonString($json, json_encode($p));
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365

        $p->delete(self::$db);
        $p = Product::productByName(self::$db, 'org.kde.DeletedProduct');
        $this->assertNull($p);
    }

    public function testInvalidInput_data()
    {
        return [
            'empty' => [ '{}' ],
            'empty name' => [ '{ "name": "", "schema": [] }' ],
            'invalid name' => [ '{ "name": "123", "schema": [] }' ],
            'non-string name' => [ '{ "name": 123, "schema": [] }' ]
        ];
    }

    /**
     * @dataProvider testInvalidInput_data
     * @expectedException RESTException
     * @exceptedExceptionCode 400
     */
    public function testInvalidInput($json)
    {
        $p = Product::fromJson($json);
    }
}