15bc00e11SAndreas Gohr<?php 25bc00e11SAndreas Gohr 35bc00e11SAndreas Gohrnamespace dokuwiki\plugin\struct\test; 45bc00e11SAndreas Gohr 55bc00e11SAndreas Gohruse dokuwiki\plugin\struct\meta\Column; 65bc00e11SAndreas Gohruse dokuwiki\plugin\struct\meta\NestedResult; 75bc00e11SAndreas Gohruse dokuwiki\plugin\struct\meta\Value; 85bc00e11SAndreas Gohruse dokuwiki\plugin\struct\types\Text; 95bc00e11SAndreas Gohr 10f6acb215SAndreas Gohr/** 11f6acb215SAndreas Gohr * Tests for the NestedResult class 12f6acb215SAndreas Gohr * 13f6acb215SAndreas Gohr * @group plugin_struct 14f6acb215SAndreas Gohr * @group plugins 15f6acb215SAndreas Gohr * 16f6acb215SAndreas Gohr */ 175bc00e11SAndreas Gohrclass NestedResultTest extends StructTest 185bc00e11SAndreas Gohr{ 195bc00e11SAndreas Gohr protected $simpleItems = [ 205bc00e11SAndreas Gohr ['car', 'audi', 'a80'], 215bc00e11SAndreas Gohr ['car', 'audi', 'a4'], 225bc00e11SAndreas Gohr ['car', 'audi', 'quattro'], 235bc00e11SAndreas Gohr ['car', 'bmw', 'i3'], 245bc00e11SAndreas Gohr ['car', 'bmw', 'mini'], 255bc00e11SAndreas Gohr ['car', 'bmw', 'z1'], 265bc00e11SAndreas Gohr ['laptop', 'apple', 'pro 16'], 275bc00e11SAndreas Gohr ['laptop', 'apple', 'air'], 285bc00e11SAndreas Gohr ['laptop', 'apple', 'm1'], 295bc00e11SAndreas Gohr ['laptop', 'dell', 'inspiron'], 305bc00e11SAndreas Gohr ['laptop', 'dell', 'latitude'], 31ae522a2dSAndreas Gohr ['laptop', 'bmw', 'latitude'], 325bc00e11SAndreas Gohr ]; 335bc00e11SAndreas Gohr 345bc00e11SAndreas Gohr protected $multiItems = [ 355bc00e11SAndreas Gohr [['green', 'yellow'], 'car', 'audi', 'a80'], 365bc00e11SAndreas Gohr [['yellow', 'blue'], 'car', 'audi', 'a4'], 375bc00e11SAndreas Gohr [['black', 'green'], 'car', 'audi', 'quattro'], 385bc00e11SAndreas Gohr [['red', 'black'], 'car', 'bmw', 'i3'], 395bc00e11SAndreas Gohr [['blue', 'gray'], 'car', 'bmw', 'mini'], 405bc00e11SAndreas Gohr [['red', 'black'], 'car', 'bmw', 'z1'], 415bc00e11SAndreas Gohr [['green', 'blue'], 'laptop', 'apple', 'pro 16'], 425bc00e11SAndreas Gohr [['red', 'blue'], 'laptop', 'apple', 'air'], 435bc00e11SAndreas Gohr [['black', 'red'], 'laptop', 'apple', 'm1'], 445bc00e11SAndreas Gohr [['gray', 'green'], 'laptop', 'dell', 'xps'], 455bc00e11SAndreas Gohr [['blue', 'yellow'], 'laptop', 'dell', 'inspiron'], 465bc00e11SAndreas Gohr [['gray', 'yellow'], 'laptop', 'dell', 'latitude'], 475bc00e11SAndreas Gohr ]; 485bc00e11SAndreas Gohr 497b7a9290SAndreas Gohr protected $multiHoleItems = [ 507b7a9290SAndreas Gohr [['green', 'yellow'], 'car', 'audi', 'a80'], 517b7a9290SAndreas Gohr [[], 'car', 'audi', 'a4'], 527b7a9290SAndreas Gohr [['black', 'green'], '', 'audi', 'quattro'], 537b7a9290SAndreas Gohr [['red', 'black'], 'car', 'bmw', 'i3'], 547b7a9290SAndreas Gohr [['blue', 'gray'], 'car', 'bmw', 'mini'], 557b7a9290SAndreas Gohr [['red', 'black'], 'car', 'bmw', 'z1'], 567b7a9290SAndreas Gohr [['green', 'blue'], 'laptop', 'apple', 'pro 16'], 577b7a9290SAndreas Gohr [['red', 'blue'], 'laptop', 'apple', 'air'], 587b7a9290SAndreas Gohr [['black', 'red'], 'laptop', 'apple', 'm1'], 597b7a9290SAndreas Gohr [[], 'laptop', 'dell', 'xps'], 607b7a9290SAndreas Gohr [['blue', 'yellow'], '', 'dell', 'inspiron'], 617b7a9290SAndreas Gohr [['gray', 'yellow'], 'laptop', 'dell', 'latitude'], 627b7a9290SAndreas Gohr ]; 637b7a9290SAndreas Gohr 641ee0a2dcSAndreas Gohr protected $multiMultiItems = [ 651ee0a2dcSAndreas Gohr [['metal', 'wood'], ['green', 'yellow'], 'car', 'audi', 'a80'], 661ee0a2dcSAndreas Gohr [['metal', 'wood', 'plastic'], ['yellow', 'blue'], 'car', 'audi', 'a4'], 671ee0a2dcSAndreas Gohr [['plastic', 'metal'], ['red', 'blue'], 'laptop', 'apple', 'pro 16'], 681ee0a2dcSAndreas Gohr [['metal', 'plastic'], ['black', 'red'], 'laptop', 'apple', 'air'], 691ee0a2dcSAndreas Gohr ]; 701ee0a2dcSAndreas Gohr 715bc00e11SAndreas Gohr /** 725bc00e11SAndreas Gohr * Don't nest at all 735bc00e11SAndreas Gohr */ 745bc00e11SAndreas Gohr public function testSimpleZeroLevel() 755bc00e11SAndreas Gohr { 76*f1812f0bSAndreas Gohr $result = $this->createAggregationResult($this->simpleItems); 775bc00e11SAndreas Gohr $nestedResult = new NestedResult($result); 785bc00e11SAndreas Gohr $root = $nestedResult->getRoot(0); 795bc00e11SAndreas Gohr 801f53b3d5SAndreas Gohr $this->assertCount(0, $root->getChildren(true), 'no children expected'); 815bc00e11SAndreas Gohr $this->assertCount(12, $root->getResultRows(), '12 result rows expected'); 825bc00e11SAndreas Gohr } 835bc00e11SAndreas Gohr 845bc00e11SAndreas Gohr /** 855bc00e11SAndreas Gohr * Nest by the first level, no multi values 865bc00e11SAndreas Gohr */ 875bc00e11SAndreas Gohr public function testSimpleOneLevel() 885bc00e11SAndreas Gohr { 89*f1812f0bSAndreas Gohr $result = $this->createAggregationResult($this->simpleItems); 905bc00e11SAndreas Gohr $nestedResult = new NestedResult($result); 911ee0a2dcSAndreas Gohr $root = $nestedResult->getRoot(1); 921f53b3d5SAndreas Gohr $tree = $root->getChildren(true); 935bc00e11SAndreas Gohr 945bc00e11SAndreas Gohr $this->assertCount(2, $tree, '2 root nodes expected'); 955bc00e11SAndreas Gohr $this->assertEquals('car', $tree[0]->getValueObject()->getValue()); 965bc00e11SAndreas Gohr $this->assertEquals('laptop', $tree[1]->getValueObject()->getValue()); 975bc00e11SAndreas Gohr 981f53b3d5SAndreas Gohr $this->assertCount(0, $tree[0]->getChildren(true), 'no children expected'); 991f53b3d5SAndreas Gohr $this->assertCount(0, $tree[1]->getChildren(true), 'no children expected'); 1005bc00e11SAndreas Gohr 1015bc00e11SAndreas Gohr $this->assertCount(6, $tree[0]->getResultRows(), 'result rows'); 1025bc00e11SAndreas Gohr $this->assertCount(6, $tree[1]->getResultRows(), 'result rows'); 1035bc00e11SAndreas Gohr 1045bc00e11SAndreas Gohr $this->assertEquals('a80', $tree[0]->getResultRows()[0][1]->getValue(), 'Audi 80 expected'); 1055bc00e11SAndreas Gohr $this->assertEquals('pro 16', $tree[1]->getResultRows()[0][1]->getValue(), 'Mac Pro 16 expected'); 1065bc00e11SAndreas Gohr } 1075bc00e11SAndreas Gohr 1085bc00e11SAndreas Gohr 1095bc00e11SAndreas Gohr /** 1105bc00e11SAndreas Gohr * Nest by two levels, no multi values 1115bc00e11SAndreas Gohr */ 1125bc00e11SAndreas Gohr public function testSimpleTwoLevels() 1135bc00e11SAndreas Gohr { 114*f1812f0bSAndreas Gohr $result = $this->createAggregationResult($this->simpleItems); 1155bc00e11SAndreas Gohr $nestedResult = new NestedResult($result); 1161ee0a2dcSAndreas Gohr $root = $nestedResult->getRoot(2); 1171f53b3d5SAndreas Gohr $tree = $root->getChildren(true); 1185bc00e11SAndreas Gohr 1195bc00e11SAndreas Gohr $this->assertCount(2, $tree, '2 root nodes expected'); 1205bc00e11SAndreas Gohr $this->assertEquals('car', $tree[0]->getValueObject()->getValue()); 1215bc00e11SAndreas Gohr $this->assertEquals('laptop', $tree[1]->getValueObject()->getValue()); 1225bc00e11SAndreas Gohr 123ae522a2dSAndreas Gohr $this->assertCount(2, $tree[0]->getChildren(true), '2 car brands expected'); 124ae522a2dSAndreas Gohr $this->assertCount(3, $tree[1]->getChildren(true), '3 laptop brands expected'); 1255bc00e11SAndreas Gohr 126ae522a2dSAndreas Gohr $this->assertCount(3, $tree[0]->getChildren(true)[0]->getResultRows(), '3 audis expected'); 127ae522a2dSAndreas Gohr $this->assertCount(3, $tree[0]->getChildren(true)[1]->getResultRows(), '3 bmw expected'); 128ae522a2dSAndreas Gohr $this->assertCount(3, $tree[1]->getChildren(true)[0]->getResultRows(), '3 apple expected'); 129ae522a2dSAndreas Gohr $this->assertCount(1, $tree[1]->getChildren(true)[1]->getResultRows(), '1 bmw expected'); 130ae522a2dSAndreas Gohr $this->assertCount(2, $tree[1]->getChildren(true)[2]->getResultRows(), '2 dell expected'); 1315bc00e11SAndreas Gohr 1325bc00e11SAndreas Gohr 1331f53b3d5SAndreas Gohr $this->assertEquals('a80', $tree[0]->getChildren(true)[0]->getResultRows()[0][0]->getValue(), 'Audi 80 expected'); 1341f53b3d5SAndreas Gohr $this->assertEquals('pro 16', $tree[1]->getChildren(true)[0]->getResultRows()[0][0]->getValue(), 'Mac Pro 16 expected'); 1355bc00e11SAndreas Gohr } 1365bc00e11SAndreas Gohr 1371ee0a2dcSAndreas Gohr /** 1381ee0a2dcSAndreas Gohr * Nest by three levels, the first one being multi-value 1391ee0a2dcSAndreas Gohr */ 1401ee0a2dcSAndreas Gohr public function testMultiThreeLevels() 1415bc00e11SAndreas Gohr { 142*f1812f0bSAndreas Gohr $result = $this->createAggregationResult($this->multiItems); 1435bc00e11SAndreas Gohr $nestedResult = new NestedResult($result); 1441ee0a2dcSAndreas Gohr $root = $nestedResult->getRoot(3); 1451f53b3d5SAndreas Gohr $tree = $root->getChildren(true); // nest: color, type, brand -> model 1465bc00e11SAndreas Gohr 1475bc00e11SAndreas Gohr $this->assertCount(6, $tree, '6 root nodes of colors expected'); 1485bc00e11SAndreas Gohr 1495bc00e11SAndreas Gohr // Values on the first level will be multi-values, thus returning arrays 1505bc00e11SAndreas Gohr $this->assertEquals('black', $tree[0]->getValueObject()->getValue()[0]); 1515bc00e11SAndreas Gohr $this->assertEquals('blue', $tree[1]->getValueObject()->getValue()[0]); 1525bc00e11SAndreas Gohr $this->assertEquals('gray', $tree[2]->getValueObject()->getValue()[0]); 1535bc00e11SAndreas Gohr $this->assertEquals('green', $tree[3]->getValueObject()->getValue()[0]); 1545bc00e11SAndreas Gohr $this->assertEquals('red', $tree[4]->getValueObject()->getValue()[0]); 1555bc00e11SAndreas Gohr $this->assertEquals('yellow', $tree[5]->getValueObject()->getValue()[0]); 1565bc00e11SAndreas Gohr 1575bc00e11SAndreas Gohr // Results should now show up under multiple top-level nodes 1585bc00e11SAndreas Gohr $this->assertEquals('a80', 1595bc00e11SAndreas Gohr $tree[3] // green 1601f53b3d5SAndreas Gohr ->getChildren(true)[0] // car 1611f53b3d5SAndreas Gohr ->getChildren(true)[0] // audi 1625bc00e11SAndreas Gohr ->getResultRows()[0][0] // a80 1635bc00e11SAndreas Gohr ->getValue(), 1645bc00e11SAndreas Gohr 'green car audi a80 expected' 1655bc00e11SAndreas Gohr ); 1665bc00e11SAndreas Gohr $this->assertEquals('a80', 1675bc00e11SAndreas Gohr $tree[5] // yellow 1681f53b3d5SAndreas Gohr ->getChildren(true)[0] // car 1691f53b3d5SAndreas Gohr ->getChildren(true)[0] // audi 1705bc00e11SAndreas Gohr ->getResultRows()[0][0] // a80 1715bc00e11SAndreas Gohr ->getValue(), 1725bc00e11SAndreas Gohr 'yellow car audi a80 expected' 1735bc00e11SAndreas Gohr ); 1745bc00e11SAndreas Gohr } 1755bc00e11SAndreas Gohr 1767b7a9290SAndreas Gohr public function testMultiHoles() 1777b7a9290SAndreas Gohr { 178*f1812f0bSAndreas Gohr $result = $this->createAggregationResult($this->multiHoleItems); 1797b7a9290SAndreas Gohr $nestedResult = new NestedResult($result); 1807b7a9290SAndreas Gohr $root = $nestedResult->getRoot(3); 1811f53b3d5SAndreas Gohr $tree = $root->getChildren(true); // nest: color, type, brand -> model 1827b7a9290SAndreas Gohr $this->assertCount(7, $tree, '6 root nodes of colors + 1 n/a expected'); // should have one n/a node 1831f53b3d5SAndreas Gohr $this->assertCount(2, $tree[6]->getChildren(true), 'top n/a node should have car, laptop'); 1841f53b3d5SAndreas Gohr $this->assertCount(3, $tree[0]->getChildren(true), 'black should have car,laptop,n/a'); 1857b7a9290SAndreas Gohr } 1867b7a9290SAndreas Gohr 1871ee0a2dcSAndreas Gohr /** 1881ee0a2dcSAndreas Gohr * Nest by two multi value levels 1891ee0a2dcSAndreas Gohr */ 1901ee0a2dcSAndreas Gohr public function testMultiMultiTwoLevels() 1911ee0a2dcSAndreas Gohr { 192*f1812f0bSAndreas Gohr $result = $this->createAggregationResult($this->multiMultiItems); 1931ee0a2dcSAndreas Gohr $nestedResult = new NestedResult($result); 1941ee0a2dcSAndreas Gohr $root = $nestedResult->getRoot(2); 1951f53b3d5SAndreas Gohr $tree = $root->getChildren(true); // nest: material, color, * 1961ee0a2dcSAndreas Gohr 1971ee0a2dcSAndreas Gohr $this->assertCount(3, $tree, '3 root nodes of material expected'); 1981f53b3d5SAndreas Gohr $this->assertCount(1, $tree[0]->getChildren(true)[0]->getResultRows(), '1 metal black row expected'); 1991ee0a2dcSAndreas Gohr } 200ce44c639SAndreas Gohr 201ce44c639SAndreas Gohr /** 202ce44c639SAndreas Gohr * Nest by two multi value levels with indexing 203ce44c639SAndreas Gohr */ 204ce44c639SAndreas Gohr public function testMultiMultiTwoLevelsIndex() 205ce44c639SAndreas Gohr { 206*f1812f0bSAndreas Gohr $result = $this->createAggregationResult($this->multiMultiItems); 207ce44c639SAndreas Gohr $nestedResult = new NestedResult($result); 208ce44c639SAndreas Gohr $root = $nestedResult->getRoot(2, 1); 2091f53b3d5SAndreas Gohr $tree = $root->getChildren(true); // nest: index, material, color, * 210ce44c639SAndreas Gohr 211ce44c639SAndreas Gohr $this->assertCount(3, $tree, '3 root index nodes expected'); 212ce44c639SAndreas Gohr $this->assertEquals('M', $tree[0]->getValueObject()->getValue(), 'M expected'); 2131f53b3d5SAndreas Gohr $this->assertCount(1, $tree[0]->getChildren(true), '1 metal sub node under M expected'); 214ce44c639SAndreas Gohr } 215ce44c639SAndreas Gohr 216ce44c639SAndreas Gohr /** 217ce44c639SAndreas Gohr * Index a flat result with no multi values 218ce44c639SAndreas Gohr */ 219ce44c639SAndreas Gohr public function testSimpleIndex() 220ce44c639SAndreas Gohr { 221*f1812f0bSAndreas Gohr $result = $this->createAggregationResult($this->simpleItems); 222ce44c639SAndreas Gohr $nestedResult = new NestedResult($result); 223ce44c639SAndreas Gohr $root = $nestedResult->getRoot(0, 2); 2241f53b3d5SAndreas Gohr $tree = $root->getChildren(true); 225ce44c639SAndreas Gohr 226ce44c639SAndreas Gohr $this->assertCount(2, $tree, '2 root index nodes expected'); 227ce44c639SAndreas Gohr $this->assertEquals('CA', $tree[0]->getValueObject()->getValue(), 'CA(r) index expected'); 228ce44c639SAndreas Gohr $this->assertEquals('LA', $tree[1]->getValueObject()->getValue(), 'LA(ptop) index expected'); 229ce44c639SAndreas Gohr 230ce44c639SAndreas Gohr $this->assertCount(6, $tree[0]->getResultRows(), '6 rows under CA expected'); 231ce44c639SAndreas Gohr } 232ce44c639SAndreas Gohr 233ce44c639SAndreas Gohr 234ce44c639SAndreas Gohr /** 235ce44c639SAndreas Gohr * Index a flat result with multi values 236ce44c639SAndreas Gohr */ 237ce44c639SAndreas Gohr public function testMultiIndex() 238ce44c639SAndreas Gohr { 239*f1812f0bSAndreas Gohr $result = $this->createAggregationResult($this->multiItems); 240ce44c639SAndreas Gohr $nestedResult = new NestedResult($result); 241ce44c639SAndreas Gohr $root = $nestedResult->getRoot(0, 2); 2421f53b3d5SAndreas Gohr $tree = $root->getChildren(true); 243ce44c639SAndreas Gohr 244ce44c639SAndreas Gohr $this->assertCount(4, $tree, '4 root index nodes expected'); 245ce44c639SAndreas Gohr $this->assertEquals('BL', $tree[0]->getValueObject()->getValue(), 'BL(ack|blue) index expected'); 246ce44c639SAndreas Gohr 247ce44c639SAndreas Gohr $this->assertCount(4, $tree[0]->getResultRows(), '4 rows under BL expected'); 248ce44c639SAndreas Gohr } 2495bc00e11SAndreas Gohr} 250