Skip to content

Commit cc634f8

Browse files
authored
filter aggregate metadata in Printer_CartridgeInfo (#21523)
1 parent 0ea457b commit cc634f8

File tree

2 files changed

+283
-1
lines changed

2 files changed

+283
-1
lines changed

src/Printer_CartridgeInfo.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,12 @@ public static function getSpecificValueToDisplay($field, $values, array $options
243243
$printer = new Printer();
244244
if (str_starts_with($field, '_virtual_')) {
245245
$type = preg_match('/_virtual_(.*)_percent/', $field, $matches) ? $matches[1] : '';
246+
$raw_data = $options['raw_data']['Printer_' . $printer->getSearchOptionIDByField('field', $field)] ?? [];
247+
// Filter to keep only numeric keys (actual data entries, not metadata like 'count')
248+
$data_entries = array_filter($raw_data, static fn($key) => is_int($key), ARRAY_FILTER_USE_KEY);
246249
$badges = array_filter(array_map(
247250
static fn($data) => self::createCartridgeInformationBadge($data, $type),
248-
$options['raw_data']['Printer_' . $printer->getSearchOptionIDByField('field', $field)]
251+
$data_entries
249252
));
250253

251254
if ($badges) {
Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
<?php
2+
3+
/**
4+
* ---------------------------------------------------------------------
5+
*
6+
* GLPI - Gestionnaire Libre de Parc Informatique
7+
*
8+
* http://glpi-project.org
9+
*
10+
* @copyright 2015-2025 Teclib' and contributors.
11+
* @licence https://www.gnu.org/licenses/gpl-3.0.html
12+
*
13+
* ---------------------------------------------------------------------
14+
*
15+
* LICENSE
16+
*
17+
* This file is part of GLPI.
18+
*
19+
* This program is free software: you can redistribute it and/or modify
20+
* it under the terms of the GNU General Public License as published by
21+
* the Free Software Foundation, either version 3 of the License, or
22+
* (at your option) any later version.
23+
*
24+
* This program is distributed in the hope that it will be useful,
25+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
26+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27+
* GNU General Public License for more details.
28+
*
29+
* You should have received a copy of the GNU General Public License
30+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
31+
*
32+
* ---------------------------------------------------------------------
33+
*/
34+
35+
namespace tests\units;
36+
37+
use DbTestCase;
38+
39+
/* Test for inc/printer_cartridgeinfo.class.php */
40+
41+
class Printer_CartridgeInfoTest extends DbTestCase
42+
{
43+
public function testGetSpecificValueToDisplayWithAggregateData()
44+
{
45+
// Create printer
46+
$printers_id = $this->createItem(\Printer::class, [
47+
'name' => 'Test Printer CMYK',
48+
'entities_id' => $this->getTestRootEntity(true),
49+
])->getID();
50+
51+
// Add cartridge info
52+
$cartridge_info = new \Printer_CartridgeInfo();
53+
$cartridge_info->add([
54+
'printers_id' => $printers_id,
55+
'property' => 'tonerblack',
56+
'value' => '71',
57+
]);
58+
$cartridge_info->add([
59+
'printers_id' => $printers_id,
60+
'property' => 'tonercyan',
61+
'value' => '85',
62+
]);
63+
64+
// Simulate search engine raw_data structure with 'count' metadata
65+
$options = [
66+
'raw_data' => [
67+
'Printer_1400' => [
68+
'count' => 2,
69+
0 => [
70+
'property' => 'tonerblack',
71+
'value' => '71',
72+
],
73+
1 => [
74+
'property' => 'tonercyan',
75+
'value' => '85',
76+
],
77+
],
78+
],
79+
];
80+
81+
$result = \Printer_CartridgeInfo::getSpecificValueToDisplay(
82+
'_virtual_toner_percent',
83+
[],
84+
$options
85+
);
86+
87+
// Verify badges are displayed
88+
$this->assertNotEmpty($result);
89+
$this->assertStringContainsString('Black', $result);
90+
$this->assertStringContainsString('Cyan', $result);
91+
$this->assertStringContainsString('71%', $result);
92+
$this->assertStringContainsString('85%', $result);
93+
$this->assertStringContainsString('d-flex flex-wrap', $result);
94+
$this->assertStringContainsString('badge', $result);
95+
}
96+
97+
public function testGetSpecificValueToDisplayWithNoCartridges()
98+
{
99+
$options = [
100+
'raw_data' => [
101+
'Printer_1400' => [
102+
'count' => 0,
103+
],
104+
],
105+
];
106+
107+
$result = \Printer_CartridgeInfo::getSpecificValueToDisplay(
108+
'_virtual_toner_percent',
109+
[],
110+
$options
111+
);
112+
113+
$this->assertTrue(empty($result));
114+
}
115+
116+
public function testGetSpecificValueToDisplayWithTonersAndDrums()
117+
{
118+
$options_toner = [
119+
'raw_data' => [
120+
'Printer_1400' => [
121+
'count' => 2,
122+
0 => ['property' => 'tonerblack', 'value' => '55'],
123+
1 => ['property' => 'tonercyan', 'value' => '78'],
124+
],
125+
],
126+
];
127+
128+
$options_drum = [
129+
'raw_data' => [
130+
'Printer_1401' => [
131+
'count' => 2,
132+
0 => ['property' => 'drumblack', 'value' => '32'],
133+
1 => ['property' => 'drumcyan', 'value' => '45'],
134+
],
135+
],
136+
];
137+
138+
// Test toner column
139+
$result_toner = \Printer_CartridgeInfo::getSpecificValueToDisplay(
140+
'_virtual_toner_percent',
141+
[],
142+
$options_toner
143+
);
144+
145+
$this->assertStringContainsString('55%', $result_toner);
146+
$this->assertStringContainsString('78%', $result_toner);
147+
$this->assertStringNotContainsString('32%', $result_toner);
148+
149+
// Test drum column
150+
$result_drum = \Printer_CartridgeInfo::getSpecificValueToDisplay(
151+
'_virtual_drum_percent',
152+
[],
153+
$options_drum
154+
);
155+
156+
$this->assertStringContainsString('32%', $result_drum);
157+
$this->assertStringContainsString('45%', $result_drum);
158+
$this->assertStringNotContainsString('55%', $result_drum);
159+
}
160+
161+
public function testGetSpecificValueToDisplayWithoutCountMetadata()
162+
{
163+
$options = [
164+
'raw_data' => [
165+
'Printer_1400' => [
166+
0 => ['property' => 'tonerblack', 'value' => '90'],
167+
],
168+
],
169+
];
170+
171+
$result = \Printer_CartridgeInfo::getSpecificValueToDisplay(
172+
'_virtual_toner_percent',
173+
[],
174+
$options
175+
);
176+
177+
$this->assertStringContainsString('90%', $result);
178+
}
179+
180+
public function testGetSpecificValueToDisplayWithCMYK()
181+
{
182+
$options = [
183+
'raw_data' => [
184+
'Printer_1400' => [
185+
'count' => 4,
186+
0 => ['property' => 'tonercyan', 'value' => '85'],
187+
1 => ['property' => 'tonermagenta', 'value' => '62'],
188+
2 => ['property' => 'toneryellow', 'value' => '47'],
189+
3 => ['property' => 'tonerblack', 'value' => '93'],
190+
],
191+
],
192+
];
193+
194+
$result = \Printer_CartridgeInfo::getSpecificValueToDisplay(
195+
'_virtual_toner_percent',
196+
[],
197+
$options
198+
);
199+
200+
// Verify all colors are present
201+
$this->assertStringContainsString('Cyan', $result);
202+
$this->assertStringContainsString('Magenta', $result);
203+
$this->assertStringContainsString('Yellow', $result);
204+
$this->assertStringContainsString('Black', $result);
205+
$this->assertStringContainsString('85%', $result);
206+
$this->assertStringContainsString('62%', $result);
207+
$this->assertStringContainsString('47%', $result);
208+
$this->assertStringContainsString('93%', $result);
209+
}
210+
211+
public function testGetSpecificValueToDisplayWithMissingRawData()
212+
{
213+
$options = [];
214+
215+
$result = \Printer_CartridgeInfo::getSpecificValueToDisplay(
216+
'_virtual_toner_percent',
217+
[],
218+
$options
219+
);
220+
221+
$this->assertTrue(empty($result));
222+
}
223+
224+
public function testGetSpecificValueToDisplayWithSearchEngine()
225+
{
226+
$this->login();
227+
228+
// Create printer with cartridge info
229+
$printer = $this->createItem(\Printer::class, [
230+
'name' => 'Test Printer Search',
231+
'entities_id' => $this->getTestRootEntity(true),
232+
]);
233+
234+
$this->createItem(\Printer_CartridgeInfo::class, [
235+
'printers_id' => $printer->getID(),
236+
'property' => 'tonerblack',
237+
'value' => '65',
238+
]);
239+
$this->createItem(\Printer_CartridgeInfo::class, [
240+
'printers_id' => $printer->getID(),
241+
'property' => 'drumcyan',
242+
'value' => '42',
243+
]);
244+
245+
// Run search with toner and drum columns
246+
$params = [
247+
'criteria' => [
248+
[
249+
'field' => 1, // Name
250+
'searchtype' => 'contains',
251+
'value' => 'Test Printer Search',
252+
],
253+
],
254+
'reset' => 'reset',
255+
];
256+
$params = \Search::manageParams(\Printer::class, $params);
257+
$data = \Search::getDatas(\Printer::class, $params, [1, 1400, 1401]);
258+
259+
// Verify search executed successfully
260+
$this->assertGreaterThan(0, $data['data']['totalcount']);
261+
$this->assertCount(1, $data['data']['rows']);
262+
263+
$row = $data['data']['rows'][0];
264+
265+
// Verify toner column contains the badge
266+
$this->assertArrayHasKey('Printer_1400', $row);
267+
$this->assertArrayHasKey('displayname', $row['Printer_1400']);
268+
$toner_display = $row['Printer_1400']['displayname'];
269+
$this->assertStringContainsString('65%', $toner_display);
270+
$this->assertStringContainsString('Black', $toner_display);
271+
272+
// Verify drum column contains the badge
273+
$this->assertArrayHasKey('Printer_1401', $row);
274+
$this->assertArrayHasKey('displayname', $row['Printer_1401']);
275+
$drum_display = $row['Printer_1401']['displayname'];
276+
$this->assertStringContainsString('42%', $drum_display);
277+
$this->assertStringContainsString('Cyan', $drum_display);
278+
}
279+
}

0 commit comments

Comments
 (0)