1<?php
2
3namespace Tests\Icinga\Module\Director\Objects;
4
5use Icinga\Module\Director\Objects\IcingaHost;
6use Icinga\Module\Director\Objects\IcingaService;
7use Icinga\Module\Director\Objects\IcingaNotification;
8use Icinga\Module\Director\Objects\IcingaUser;
9use Icinga\Module\Director\Objects\IcingaUsergroup;
10use Icinga\Module\Director\Test\BaseTestCase;
11
12class IcingaNotificationTest extends BaseTestCase
13{
14    protected $testUserName1 = '___TEST___user1';
15
16    protected $testUserName2 = '___TEST___user2';
17
18    protected $testNotificationName = '___TEST___notification';
19
20    public function testPropertiesCanBeSet()
21    {
22        $n = $this->notification();
23        $n->notification_interval = '10m';
24        $this->assertEquals(
25            $n->notification_interval,
26            600
27        );
28    }
29
30    public function testCanBeStoredAndDeletedWithRelatedUserPassedAsString()
31    {
32        if ($this->skipForMissingDb()) {
33            return;
34        }
35        $db = $this->getDb();
36
37        $user = $this->user1();
38        $user->store($db);
39
40        $n = $this->notification();
41        $n->users = $user->object_name;
42        $this->assertTrue($n->store($db));
43        $this->assertTrue($n->delete());
44        $user->delete();
45    }
46
47    public function testCanBeStoredAndDeletedWithMultipleRelatedUsers()
48    {
49        if ($this->skipForMissingDb()) {
50            return;
51        }
52        $db = $this->getDb();
53
54        $user1 = $this->user1();
55        $user1->store($db);
56
57        $user2 = $this->user2();
58        $user2->store($db);
59
60        $n = $this->notification();
61        $n->users = array($user1->object_name, $user2->object_name);
62        $this->assertTrue($n->store($db));
63        $this->assertTrue($n->delete());
64        $user1->delete();
65        $user2->delete();
66    }
67
68    public function testGivesPlainObjectWithRelatedUsers()
69    {
70        if ($this->skipForMissingDb()) {
71            return;
72        }
73        $db = $this->getDb();
74
75        $user1 = $this->user1();
76        $user1->store($db);
77
78        $user2 = $this->user2();
79        $user2->store($db);
80
81        $n = $this->notification();
82        $n->users = array($user1->object_name, $user2->object_name);
83        $n->store($db);
84        $this->assertEquals(
85            (object) array(
86                'object_name' => $this->testNotificationName,
87                'object_type' => 'object',
88                'users' => array(
89                    $user1->object_name,
90                    $user2->object_name
91                )
92            ),
93            $n->toPlainObject(false, true)
94        );
95
96        $n = IcingaNotification::load($n->object_name, $db);
97        $this->assertEquals(
98            (object) array(
99                'object_name' => $this->testNotificationName,
100                'object_type' => 'object',
101                'users' => array(
102                    $user1->object_name,
103                    $user2->object_name
104                )
105            ),
106            $n->toPlainObject(false, true)
107        );
108        $this->assertEquals(
109            array(),
110            $n->toPlainObject()->user_groups
111        );
112        $n->delete();
113
114        $user1->delete();
115        $user2->delete();
116    }
117
118    public function testHandlesChangesForStoredRelations()
119    {
120        if ($this->skipForMissingDb()) {
121            return;
122        }
123        $db = $this->getDb();
124
125        $user1 = $this->user1();
126        $user1->store($db);
127
128        $user2 = $this->user2();
129        $user2->store($db);
130
131        $n = $this->notification();
132        $n->users = array($user1->object_name, $user2->object_name);
133        $n->store($db);
134
135        $n = IcingaNotification::load($n->object_name, $db);
136        $this->assertFalse($n->hasBeenModified());
137
138        $n->users = array($user2->object_name);
139        $this->assertTrue($n->hasBeenModified());
140
141        $n->store();
142
143        $n = IcingaNotification::load($n->object_name, $db);
144        $this->assertEquals(
145            array($user2->object_name),
146            $n->users
147        );
148
149        $n->users = array();
150        $n->store();
151
152        $n = IcingaNotification::load($n->object_name, $db);
153        $this->assertEquals(
154            array(),
155            $n->users
156        );
157
158        // Should be fixed with lazy loading:
159        // $n->users = array($user1->object_name, $user2->object_name);
160        // $this->assertFalse($n->hasBeenModified());
161
162        $n->delete();
163
164        $user1->delete();
165        $user2->delete();
166    }
167
168    public function testRendersConfigurationWithRelatedUsers()
169    {
170        if ($this->skipForMissingDb()) {
171            return;
172        }
173        $db = $this->getDb();
174
175        $user1 = $this->user1();
176        $user1->store($db);
177
178        $user2 = $this->user2();
179        $user2->store($db);
180
181        $n = $this->notification();
182        $n->users = array($user1->object_name, $user2->object_name);
183
184        $this->assertEquals(
185            $this->loadRendered('notification1'),
186            (string) $n
187        );
188    }
189
190    public function testLazyUsersCanBeSet()
191    {
192        $this->markTestSkipped('Setting lazy properties not yet completed');
193
194        $n = $this->notification();
195        $n->users = 'bla';
196    }
197
198    protected function user1()
199    {
200        return IcingaUser::create(array(
201            'object_name' => $this->testUserName1,
202            'object_type' => 'object',
203            'email'       => 'nowhere@example.com',
204        ), $this->getDb());
205    }
206
207    protected function user2()
208    {
209        return IcingaUser::create(array(
210            'object_name' => $this->testUserName2,
211            'object_type' => 'object',
212            'email'       => 'nowhere.else@example.com',
213        ), $this->getDb());
214    }
215
216    protected function notification()
217    {
218        return IcingaNotification::create(array(
219            'object_name' => $this->testNotificationName,
220            'object_type' => 'object',
221        ), $this->getDb());
222    }
223
224    protected function loadRendered($name)
225    {
226        return file_get_contents(__DIR__ . '/rendered/' . $name . '.out');
227    }
228
229    public function tearDown()
230    {
231        if ($this->hasDb()) {
232            $db = $this->getDb();
233            $kill = array($this->testNotificationName);
234            foreach ($kill as $name) {
235                if (IcingaNotification::exists($name, $db)) {
236                    IcingaNotification::load($name, $db)->delete();
237                }
238            }
239
240            $kill = array($this->testUserName1, $this->testUserName2);
241            foreach ($kill as $name) {
242                if (IcingaUser::exists($name, $db)) {
243                    IcingaUser::load($name, $db)->delete();
244                }
245            }
246        }
247    }
248}
249