1<?php 2 3namespace dokuwiki\test\Search\Index; 4 5use dokuwiki\Search\Index\TupleOps; 6 7class TupleOpsTest extends \DokuWikiTest 8{ 9 /** 10 * @see testUpdateTuple 11 */ 12 public function provideUpdateTuple() 13 { 14 return [ 15 ['', 'foo', 3, 'foo*3'], 16 ['', 17, 3, '17*3'], 17 ['foo*2', 'foo', 3, 'foo*3'], 18 ['17*2', 17, 3, '17*3'], 19 ['bar*2:foo*2', 'foo', 3, 'foo*3:bar*2'], 20 ['18*2:17*2', 17, 3, '17*3:18*2'], 21 // Test tuples without explicit count (implicit count of 1) 22 ['foo', 'foo', 3, 'foo*3'], 23 ['17', 17, 3, '17*3'], 24 ['bar:foo', 'foo', 3, 'foo*3:bar'], 25 ['bar*2:foo', 'foo', 5, 'foo*5:bar*2'], 26 ['18:17', 17, 3, '17*3:18'], 27 ['18:17', 19, 1, '19:18:17'], 28 // numeric keys must only replace exact tuple IDs, not decimal prefixes 29 ['170*4:17*2:171*3', 17, 5, '17*5:170*4:171*3'], 30 ['170:17:171*3', 17, 0, '170:171*3'], 31 // existing 1 counts are not updated unless touched directly 32 ['foo*4:bar*1:baz*3', 'uff', 2, 'uff*2:foo*4:bar*1:baz*3'], 33 ['foo*4:bar*1:baz*3', 'bar', 1, 'bar:foo*4:baz*3'], 34 // 0 is a valid entity 35 ['', 0, 1, '0'], 36 ['0*7', 0, 6, '0*6'], 37 ['foo:bar*3', 0, 1, '0:foo:bar*3'], 38 // frequency of 0 deletes 39 ['', 7, 0, ''], 40 ['0', 0, 0, ''], 41 ['foo*6:bar*3:baz', 'bar', 0, 'foo*6:baz'], 42 ]; 43 } 44 45 /** 46 * @dataProvider provideUpdateTuple 47 */ 48 public function testUpdateTuple($record, $key, $count, $expect) 49 { 50 $result = TupleOps::updateTuple($record, $key, $count); 51 $this->assertEquals($result, $expect); 52 } 53 54 /** 55 * @see testAggregateTupleCounts 56 */ 57 public function provideAggregateTupleCounts() 58 { 59 return [ 60 ['5*10:foo*2:14*100::bar*7', 119], 61 // Test with tuples without explicit count (implicit count of 1) 62 ['5:foo:14::bar', 4], 63 ['5*10:foo:14*100', 111], 64 ['key1:key2:key3', 3], 65 ['', 0], 66 ['single', 1], 67 ]; 68 } 69 70 /** 71 * @dataProvider provideAggregateTupleCounts 72 */ 73 public function testAggregateTupleCounts($record, $expected) 74 { 75 $result = TupleOps::aggregateTupleCounts($record); 76 $this->assertEquals($expected, $result); 77 } 78 79 /** 80 * @see testParseTuples 81 */ 82 public function provideParseTuples() 83 { 84 return [ 85 // Original test case 86 [ 87 '5*10:foo*2:14*100::bar*7', 88 [5 => 'first', 'bar' => 'second', 'foo' => null], 89 ['first' => 10, 'second' => 7] 90 ], 91 // Test with tuples without explicit count (implicit count of 1) 92 [ 93 '5:foo:14::bar', 94 [5 => 'first', 'bar' => 'second', 'foo' => null], 95 ['first' => 1, 'second' => 1] 96 ], 97 // Mixed: some with count, some without 98 [ 99 '5*10:foo:14*100', 100 [5 => 'first', 'foo' => 'second', 14 => 'third'], 101 ['first' => 10, 'second' => 1, 'third' => 100] 102 ], 103 // No filter map (returns all with original keys) 104 [ 105 '5*10:foo*2:bar', 106 null, 107 [5 => 10, 'foo' => 2, 'bar' => 1] 108 ], 109 // Empty record 110 [ 111 '', 112 [5 => 'first'], 113 [] 114 ], 115 ]; 116 } 117 118 /** 119 * @dataProvider provideParseTuples 120 */ 121 public function testParseTuples($record, $keys, $expect) 122 { 123 $result = TupleOps::parseTuples($record, $keys); 124 $this->assertEquals($expect, $result); 125 } 126 127} 128