-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAbstractTypeTest.php
More file actions
104 lines (96 loc) · 3.71 KB
/
AbstractTypeTest.php
File metadata and controls
104 lines (96 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/**
* -------------------------------------------------------------------------
* Carbon plugin for GLPI
*
* @copyright Copyright (C) 2024-2025 Teclib' and contributors.
* @license https://www.gnu.org/licenses/gpl-3.0.txt GPLv3+
* @link https://github.com/pluginsGLPI/carbon
*
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of Carbon plugin for GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* -------------------------------------------------------------------------
*/
namespace GlpiPlugin\Carbon\Tests;
use GlpiPlugin\Carbon\AbstractType;
use PHPUnit\Framework\Attributes\CoversClass;
use Session;
use Symfony\Component\DomCrawler\Crawler;
#[CoversClass(AbstractType::class)]
abstract class AbstractTypeTest extends DbTestCase
{
protected static string $glpi_type_itemtype;
/**
* @template T of AbstractType
* @var class-string<T>
*/
protected static string $type_itemtype;
public function testGetTypeName()
{
$this->assertEquals('Environmental impact', static::$type_itemtype::getTypeName(1));
$this->assertEquals('Environmental impact', static::$type_itemtype::getTypeName(Session::getPluralNumber()));
}
public function testShowForItemType()
{
$glpi_type = $this->createItem(static::$glpi_type_itemtype);
$glpi_type_fk = $glpi_type::getForeignKeyField();
/** @var T $type */
$type = $this->createItem(static::$type_itemtype, [
$glpi_type_fk => $glpi_type->getID(),
]);
$this->login('glpi', 'glpi');
ob_start(function ($buffer) {
return $buffer;
});
$type->showForItemType($glpi_type);
$output = ob_get_clean();
$crawler = new Crawler($output);
$power = $crawler->filter('input[name="power_consumption"]');
$this->assertEquals(1, $power->count());
$power->each(function (Crawler $node) {
$this->assertEquals(0, $node->attr('value'));
$this->assertEquals('number', $node->attr('type'));
});
$category = $crawler->filter('select[name="category"]');
$this->assertEquals(0, $category->count());
}
public function test_displayTabContentForItemtype_creates_type_extra_data_when_they_dont_exist()
{
$glpi_type = $this->createItem(static::$glpi_type_itemtype);
// Check that there is no type extra data object
$glpi_type_fk = getForeignKeyFieldForItemType(get_class($glpi_type));
$type = new static::$type_itemtype();
$type->getFromDBByCrit([
$glpi_type_fk => $glpi_type->getID(),
]);
$this->assertTrue($type->isNewItem());
// Test the method
$this->login('glpi', 'glpi');
ob_start();
$type->displayTabContentForItem($glpi_type);
ob_end_clean();
// Now, $type is expected to be populated
$type->getFromDBByCrit([
$glpi_type_fk => $glpi_type->getID(),
]);
$this->assertFalse($type->isNewItem());
$this->assertSame($type->fields[$glpi_type_fk], $glpi_type->getID());
}
}