summaryrefslogtreecommitdiffstats
path: root/test/php/library/Director/Form/IcingaObjectFieldFormTest.php
blob: 8a5e0cc1399f3803b33488ceb4766a7990c146e5 (plain)
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php

namespace Tests\Icinga\Module\Director\Field;

use Icinga\Module\Director\Forms\IcingaObjectFieldForm;
use Icinga\Module\Director\Objects\DirectorDatafield;
use Icinga\Module\Director\Objects\IcingaHost;
use Icinga\Module\Director\Test\BaseTestCase;
use Icinga\Module\Director\Objects\IcingaCommand;

class IcingaObjectFieldFormTest extends BaseTestCase
{
    /** @var string */
    protected const COMMAND_NAME = 'icingaTestCommand';

    /** @var string */
    protected const DATAFIELD_NAME = 'dataFieldTest';

    /** @var string */
    protected const HOST_NAME = 'testHost';

    /** @var ?DirectorDatafield */
    protected $testDatafield = null;

    /** @var ?IcingaCommand */
    protected $testIcingaCommand = null;

    /** @var IcingaHost */
    private $testHost;

    public function setUp(): void
    {
        parent::setUp();
        if ($this->hasDb()) {
            $db = $this->getDb();
            $this->testDatafield = DirectorDatafield::create([
                'varname'       => self::DATAFIELD_NAME,
                'caption'       => 'Blah',
                'description'   => '',
                'datatype'      => 'Icinga\Module\Director\DataType\DataTypeArray',
                'format'        => 'json'
            ]);
            $this->testDatafield->store($db);

            $this->testIcingaCommand = IcingaCommand::create(
                [
                    'object_name' => self::COMMAND_NAME,
                    'object_type' => 'object'
                ],
                $db
            );
            $this->testIcingaCommand->store($db);
            
            $this->testHost = IcingaHost::create([
                'object_name'  => self::HOST_NAME,
                'object_type'  => 'object',
                'display_name' => 'Whatever'
            ], $this->getDb());
        }
    }

    public function testFieldSuggestionsWithoutCheckCommand()
    {
        if ($this->skipForMissingDb()) {
            return;
        }

        $db = $this->getDb();

        $icingaHostFieldForm = (new IcingaObjectFieldForm())
            ->setDb($db)
            ->setIcingaObject($this->testHost);

        $icingaHostFieldForm->setup();
        /** @var array<string> $suggestions */
        $suggestions = $icingaHostFieldForm->getElement('datafield_id')
            ->getAttrib('options');

        array_shift($suggestions);

        $this->assertEquals(
            [
                'Other available fields' => [
                    $this->testDatafield->get('id') => "Blah (dataFieldTest)"
                ]
            ],
            $suggestions
        );
    }

    public function testFieldSuggestionsWithCheckCommand()
    {
        if ($this->skipForMissingDb()) {
            return;
        }

        $db = $this->getDb();

        $this->testHost->check_command = self::COMMAND_NAME;
        $icingaHostFieldForm = (new IcingaObjectFieldForm())
            ->setDb($db)
            ->setIcingaObject($this->testHost);

        $icingaHostFieldForm->setup();

        /** @var array<string> $suggestions */
        $suggestions = $icingaHostFieldForm->getElement('datafield_id')
            ->getAttrib('options');

        array_shift($suggestions);
        $this->assertEquals(
            [
                'Other available fields' => [
                    $this->testDatafield->get('id') => "Blah (dataFieldTest)"
                ]
            ],
            $suggestions
        );
    }

    public function tearDown(): void
    {
        if ($this->hasDb()) {
            $db = $this->getDb();
            $this->deleteDatafields();

            if (IcingaHost::exists(self::HOST_NAME, $db)) {
                IcingaHost::load(self::HOST_NAME, $db)->delete();
            }

            if (IcingaCommand::exists(self::COMMAND_NAME, $db)) {
                IcingaCommand::load(self::COMMAND_NAME, $db)->delete();
            }
        }

        parent::tearDown();
    }

    protected function deleteDatafields()
    {
        $db = $this->getDb();
        $dbAdapter = $db->getDbAdapter();

        $query = $dbAdapter->select()
            ->from('director_datafield')
            ->where('varname = ?', self::DATAFIELD_NAME);
        foreach (DirectorDatafield::loadAll($db, $query, 'id') as $datafield) {
            $datafield->delete();
        }
    }
}