Skip to content

Commit 8885ca7

Browse files
cconard96cedric-anne
authored andcommitted
Custom asset api integration
Add support for computed sql fields
1 parent 48e2e8f commit 8885ca7

File tree

7 files changed

+812
-60
lines changed

7 files changed

+812
-60
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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+
* @copyright 2003-2014 by the INDEPNET Development Team.
12+
* @licence https://www.gnu.org/licenses/gpl-3.0.html
13+
*
14+
* ---------------------------------------------------------------------
15+
*
16+
* LICENSE
17+
*
18+
* This file is part of GLPI.
19+
*
20+
* This program is free software: you can redistribute it and/or modify
21+
* it under the terms of the GNU General Public License as published by
22+
* the Free Software Foundation, either version 3 of the License, or
23+
* (at your option) any later version.
24+
*
25+
* This program is distributed in the hope that it will be useful,
26+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
27+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28+
* GNU General Public License for more details.
29+
*
30+
* You should have received a copy of the GNU General Public License
31+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
32+
*
33+
* ---------------------------------------------------------------------
34+
*/
35+
36+
namespace tests\units\Glpi\Api\HL\Controller;
37+
38+
use Glpi\Asset\AssetDefinitionManager;
39+
use Glpi\Http\Request;
40+
use HLAPICallAsserter;
41+
use HLAPITestCase;
42+
use PHPUnit\Framework\Attributes\DataProvider;
43+
44+
class CustomAssetControllerTest extends HLAPITestCase
45+
{
46+
public function testIndex(): void
47+
{
48+
$definitions = AssetDefinitionManager::getInstance()->getDefinitions();
49+
$this->assertNotEmpty($definitions);
50+
$types = array_map(static fn($d) => $d->fields['system_name'], $definitions);
51+
52+
$this->login();
53+
$this->api->call(new Request('GET', '/Assets/Custom'), function ($call) use ($types) {
54+
/** @var HLAPICallAsserter $call */
55+
$call->response
56+
->isOK()
57+
->jsonContent(function ($content) use ($types) {
58+
$this->assertGreaterThanOrEqual(count($types), count($content));
59+
foreach ($content as $asset) {
60+
$this->assertNotEmpty($asset['itemtype']);
61+
$this->assertNotEmpty($asset['name']);
62+
$this->assertEquals('/Assets/Custom/' . $asset['itemtype'], $asset['href']);
63+
}
64+
});
65+
});
66+
}
67+
68+
public static function searchProvider()
69+
{
70+
return [
71+
['schema' => 'Test01', 'filters' => [], 'expected' => ['count' => ['>=', 2]]],
72+
['schema' => 'Test01', 'filters' => ['name==Test0'], 'expected' => ['count' => ['=', 0]]],
73+
['schema' => 'Test01', 'filters' => ['name==TestA'], 'expected' => ['count' => ['=', 1]]],
74+
['schema' => 'Test01', 'filters' => ['name=like=Test*'], 'expected' => ['count' => ['>=', 2]]],
75+
['schema' => 'Test01', 'filters' => ['custom_fields.teststring=="Test String A"'], 'expected' => ['count' => ['=', 1]]],
76+
];
77+
}
78+
79+
#[DataProvider('searchProvider')]
80+
public function testSearch(string $schema, array $filters, array $expected): void
81+
{
82+
$this->login();
83+
$request = new Request('GET', '/Assets/Custom/' . $schema);
84+
$request->setParameter('filter', $filters);
85+
$this->api->call($request, function ($call) use ($expected) {
86+
/** @var \HLAPICallAsserter $call */
87+
$call->response
88+
->isOK()
89+
->jsonContent(function ($content) use ($expected) {
90+
$this->checkSimpleContentExpect($content, $expected);
91+
});
92+
});
93+
}
94+
95+
public function testCRUD(): void
96+
{
97+
$this->api->autoTestCRUD('/Assets/Custom/Test01', [
98+
'custom_fields' => ['teststring' => 'Test String A'],
99+
], [
100+
'custom_fields' => ['teststring' => 'Test String A'],
101+
]);
102+
}
103+
}

phpunit/functional/Glpi/Api/HL/RSQL/ParserTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public function testParse(array $tokens, string $expected)
130130
$search_class->getProperty('flattened_properties')->setValue($search, Schema::flattenProperties($schema['properties']));
131131
$search_class->getProperty('joins')->setValue($search, Schema::getJoins($schema['properties']));
132132
$parser = new Parser($search);
133-
$this->assertEquals($expected, (string) $parser->parse($tokens)->getSQLCriteria());
133+
$this->assertEquals($expected, (string) $parser->parse($tokens)->getSQLWhereCriteria());
134134
}
135135

136136
/**
@@ -159,21 +159,21 @@ public function testIgnoreInvalidProperties()
159159
$parser = new Parser($search);
160160

161161
$result = $parser->parse([[5, 'test'], [6, '=='], [7, 'test']]);
162-
$this->assertEquals('1', (string) $result->getSQLCriteria());
162+
$this->assertEquals('1', (string) $result->getSQLWhereCriteria());
163163
$this->assertEquals(Error::UNKNOWN_PROPERTY, $result->getInvalidFilters()['test']);
164164
// Test an invalid filter with a valid one
165165
$result = $parser->parse([[5, 'test'], [6, '=='], [7, 'test'], [1, ';'], [5, 'name'], [6, '=='], [7, 'test']]);
166-
$this->assertEquals("(`_`.`name` = 'test')", (string) $result->getSQLCriteria());
166+
$this->assertEquals("(`_`.`name` = 'test')", (string) $result->getSQLWhereCriteria());
167167
$this->assertEquals(Error::UNKNOWN_PROPERTY, $result->getInvalidFilters()['test']);
168168

169169
// Test invalid operator
170170
$result = $parser->parse([[5, 'name'], [6, '=f='], [7, 'test']]);
171-
$this->assertEquals('1', (string) $result->getSQLCriteria());
171+
$this->assertEquals('1', (string) $result->getSQLWhereCriteria());
172172
$this->assertEquals(Error::UNKNOWN_OPERATOR, $result->getInvalidFilters()['name']);
173173

174174
// Mapped properties should be ignored
175175
$result = $parser->parse([[5, 'mapped'], [6, '=='], [7, 'test']]);
176-
$this->assertEquals('1', (string) $result->getSQLCriteria());
176+
$this->assertEquals('1', (string) $result->getSQLWhereCriteria());
177177
$this->assertEquals(Error::MAPPED_PROPERTY, $result->getInvalidFilters()['mapped']);
178178
}
179179
}

0 commit comments

Comments
 (0)