report_options: Syntax fix
[ninja.git] / test / unit_test / tests / saved_reports_Test.php
bloba2b0db683d895a54bf16a8d19a12df52ce6bdc31
1 <?php
2 class Saved_reports_Test extends PHPUnit_Framework_TestCase
4 function setUp()
6 // sweep up leftover crap from other runs
7 $db = Database::instance();
8 $db->query('DELETE FROM saved_reports_objects');
9 $db->query('DELETE FROM saved_reports_options');
10 $db->query('DELETE FROM saved_reports');
12 // categorize all non-custom SLA periods, so we have easy-ish access to them from SLA tests
13 $opts = new Sla_options();
14 $props = $opts->properties();
15 $valid_periods = $props['report_period']['options'];
16 unset($valid_periods['custom']);
17 $valid_periods = array_keys($valid_periods);
18 sort($valid_periods);
20 $this->length = array(
21 1 => array('lastmonth'),
22 3 => array('last3months', 'lastquarter'),
23 6 => array('last6months'),
24 12 => array('lastyear', 'last12months'),
25 -1 => array('thisyear')
28 $lenghts = array();
29 foreach ($this->length as $items)
30 $lenghts = array_merge($lenghts, $items);
31 sort($lenghts);
32 $this->assertEquals($valid_periods, $lenghts, 'Expected all non-custom periods to be categorized in a length - did you add a new report period?');
34 $this->type = array(
35 'rotating' => array('lastmonth', 'last3months', 'last6months'),
36 'special_rotating' => array('lastquarter'),
37 'static' => array('thisyear', 'lastyear', 'last12months'),
39 $types = array();
40 foreach ($this->type as $items)
41 $types = array_merge($types, $items);
42 sort($types);
43 $this->assertEquals($valid_periods, $types, 'Expected all non-custom periods to be categorized in a type - did you add a new report period?');
46 function tearDown()
48 Report_options::$now = null;
51 function test_CRUD()
53 $the_opts = array(
54 'report_name' => 'TEST_REPORT',
55 'report_type' => 'hosts',
56 'objects' => array('monitor'),
57 'report_period' => 'custom',
58 'start_time' => time() - 3600,
59 'end_time' => time(),
61 $opts = Report_options::setup_options_obj('avail', $the_opts);
63 $this->assertSame(true, $opts->save($msg), "Failed to save report: $msg");
64 $orig_report_id = $opts['report_id'];
65 $this->assertGreaterThanOrEqual(0, $opts['report_id']);
66 $this->assertNotEquals(false, $opts['report_id']);
67 $res = Avail_options::get_all_saved();
68 $this->assertContains($opts['report_id'], array_keys($res), "Expected to find report id {$opts['report_id']} in " . var_export($res, true));
69 $this->assertSame($opts['report_name'], $res[$opts['report_id']]);
71 $loaded = Report_options::setup_options_obj('avail', array('report_id' => $opts['report_id']));
72 $loaded->expand();
73 $this->assertEquals($opts, $loaded);
74 $this->assertSame($orig_report_id, $loaded['report_id']);
76 $opts['report_name'] = 'TEST_REPORT2';
77 $this->assertNotEquals($opts, $loaded);
79 $this->assertSame($orig_report_id, $opts['report_id']);
80 $this->assertSame(true, $opts->save());
81 $loaded = Report_options::setup_options_obj('avail', array('report_id' => $opts['report_id']));
82 $this->assertSame($orig_report_id, $loaded['report_id']);
83 $loaded->expand();
84 $this->assertEquals($opts, $loaded);
86 $this->assertSame(true, $opts->delete());
87 $loaded = Report_options::setup_options_obj('avail', array('report_id' => $opts['report_id']));
88 $this->assertSame(false, $loaded['report_id'], "Expected report_id to be set to false, because loading should fail");
89 $res = Avail_options::get_all_saved();
90 $this->assertNotContains($opts['report_name'], $res);
93 /**
94 * Check for bugs like #6821
96 function test_add_object()
98 $the_opts = array(
99 'report_name' => 'TEST_REPORT',
100 'report_type' => 'hosts',
101 'objects' => array('monitor'),
102 'report_period' => 'custom',
103 'start_time' => time() - 3600,
104 'end_time' => time(),
106 $opts = Report_options::setup_options_obj('avail', $the_opts);
108 $this->assertSame(true, $opts->save());
109 $orig_report_id = $opts['report_id'];
110 $loaded = Report_options::setup_options_obj('avail', array('report_id' => $opts['report_id']));
111 $loaded->expand();
112 $this->assertEquals($opts, $loaded);
113 $opts->options['objects'][] = 'host_pending';
114 $this->assertSame(true, $opts->save());
115 $loaded = Report_options::setup_options_obj('avail', array('report_id' => $opts['report_id']));
116 $loaded->expand();
117 sort($opts->options['objects']);
118 $this->assertEquals($opts, $loaded);
119 $this->assertSame($orig_report_id, $loaded['report_id']);
120 $this->assertSame(true, $opts->delete());
124 * Relevant, as it's supposed to be an array that's stored serialized, which makes it a special case
126 * Yay, special case!
128 function test_host_filter_status()
130 $the_opts = array(
131 'report_name' => 'TEST_REPORT',
132 'report_type' => 'hosts',
133 'objects' => array('monitor'),
134 'report_period' => 'custom',
135 'start_time' => time() - 3600,
136 'end_time' => time(),
137 'host_filter_status' => array(Reports_Model::HOST_DOWN)
139 $opts = Report_options::setup_options_obj('avail', $the_opts);
141 $this->assertSame(true, $opts->save());
142 $orig_report_id = $opts['report_id'];
143 $loaded = Report_options::setup_options_obj('avail', array('report_id' => $opts['report_id']));
144 $loaded->expand();
145 $this->assertEquals($opts, $loaded);
146 $this->assertEquals($the_opts['host_filter_status'], $loaded['host_filter_status']);
147 $this->assertSame(true, $opts->delete());
150 function test_sla_months()
152 $the_opts = array(
153 'report_name' => 'TEST_REPORT',
154 'report_type' => 'hosts',
155 'objects' => array('monitor'),
156 'report_period' => 'custom',
157 'start_time' => time() - 3600,
158 'end_time' => time(),
159 'months' => array(date('n') => 99)
161 $opts = Report_options::setup_options_obj('sla', $the_opts);
163 $this->assertSame(true, $opts->save());
164 $orig_report_id = $opts['report_id'];
165 $loaded = Report_options::setup_options_obj('sla', array('report_id' => $opts['report_id']));
166 $this->assertNotEquals(null, $loaded);
167 $loaded->expand();
168 $this->assertEquals($opts, $loaded);
169 $this->assertEquals($the_opts['months'], $loaded['months']);
170 $this->assertSame(true, $opts->delete());
173 function test_wrong_type()
175 $the_opts = array(
176 'report_name' => 'TEST_REPORT',
177 'report_type' => 'hosts',
178 'objects' => array('monitor'),
179 'report_period' => 'custom',
180 'start_time' => time() - 3600,
181 'end_time' => time(),
182 'months' => array(1 => 99)
184 $opts = Report_options::setup_options_obj('sla', $the_opts);
186 $this->assertSame(true, $opts->save());
187 $orig_report_id = $opts['report_id'];
188 $loaded = Report_options::setup_options_obj('avail', array('report_id' => $opts['report_id']));
189 $this->assertEquals(false, $loaded['report_id']);
190 $this->assertSame(true, $opts->delete());
193 function test_overwrite()
195 $the_opts = array(
196 'report_name' => 'TEST_REPORT',
197 'report_type' => 'hosts',
198 'objects' => array('monitor'),
199 'report_period' => 'custom',
200 'start_time' => time() - 3600,
201 'end_time' => time(),
203 $opts = Report_options::setup_options_obj('avail', $the_opts);
205 $this->assertSame(true, $opts->save($msg), $msg);
206 unset($opts['report_id']);
207 $this->assertSame(false, $opts->save($msg), $msg);
211 * The following is intended to verify that saved reports that only covers part of
212 * the year will have defined SLA month values for all months - bug #7897
214 * In accordance with the old code, last12months will not rotate, which
215 * makes very little sense to me, but then the whole "rotation with varying
216 * SLA values" concept seems busted in the first place - why would it be OK
217 * in April to have 2% downtime in February, if it wasn't OK in Mars?
221 * This test *does* change indexes every month. That's awkward and
222 * annoying, but this is supposed to be the simple, "obvious" case
223 * that never fails - the special cases go below. Hence, if this fails
224 * only certain months, then those months belongs in the suite below.
226 function test_sla_create_now()
228 $thismonth = date('n');
229 foreach ($this->length as $months => $periods) {
230 foreach ($periods as $period) {
231 $the_opts = array(
232 'report_name' => 'TEST_REPORT',
233 'report_type' => 'hosts',
234 'objects' => array('monitor'),
235 'report_period' => $period,
237 $opts = Report_options::setup_options_obj('sla', $the_opts);
238 for ($i = -5; $i < 15; $i++) {
239 $ary = $opts['months'];
240 $ary[$i] = $i;
241 $opts['months'] = $ary;
243 if ($months == -1)
244 $months = (int)date('n');
245 $this->assertCount($months, $opts['months'], "Expected there to be $months elements for $period in ".var_export($opts['months'], true));
247 $this->assertTrue($opts->save());
249 $loaded = Report_options::setup_options_obj('sla', array('report_id' => $opts['report_id']));
250 $this->assertCount($months, $loaded['months'], "Expected there to be $months elements for $period in ".var_export($loaded['months'], true));
251 foreach($loaded['months'] as $key => $val) {
252 $this->assertEquals($key, $val);
254 $this->assertTrue($opts->delete());
259 function test_sla_create_special()
261 $constant_times = array(
262 'all the same year' => array(
263 'time' => strtotime('2013-05-01 08:00:00'),
264 'period' => 'last3months',
265 'expected' => array(2 => 2, 3 => 3, 4 => 4)
267 'wrapped across year boundary' => array(
268 'time' => strtotime('2013-02-01 08:00:00'),
269 'period' => 'last6months',
270 'expected' => array(8 => 8, 9 => 9, 10 => 10, 11 => 11, 12 => 12, 1 => 1)
273 foreach ($constant_times as $case) {
274 Report_options::$now = $case['time'];
275 $the_opts = array(
276 'report_name' => 'TEST_REPORT',
277 'report_type' => 'hosts',
278 'objects' => array('monitor'),
279 'report_period' => $case['period'],
281 $opts = Report_options::setup_options_obj('sla', $the_opts);
282 for ($i = -5; $i < 15; $i++) {
283 $ary = $opts['months'];
284 $ary[$i] = $i;
285 $opts['months'] = $ary;
287 $this->assertSame($case['expected'], $opts['months']);
289 $this->assertTrue($opts->save());
291 Report_options::$now = $case['time'];
292 $loaded = Report_options::setup_options_obj('sla', array('report_id' => $opts['report_id']));
293 $this->assertCount(count($case['expected']), $loaded['months'], "Expected there to be {$case['expected']} elements for {$case['period']} in ".var_export($loaded['months'], true));
294 foreach($loaded['months'] as $key => $val) {
295 $this->assertEquals($key, $val, "$key should have value $val in a {$case['period']} report loaded on " . date('c', $case['time']));
297 $this->assertTrue($opts->delete());
301 function test_sla_regular_rotation()
303 $rotation_cases = array(
304 'loaded after saved unwrapped' => array(
305 'save_time' => strtotime('2012-07-25 08:00:00'),
306 'load_time' => strtotime('2012-08-01 12:00:00'),
307 'converter' => function ($key) {
308 return $key - 1;
311 'loaded after saved, wrapped before, not wrapped after' => array(
312 'save_time' => strtotime('2012-01-25 08:00:00'),
313 'load_time' => strtotime('2012-08-01 12:00:00'),
314 'converter' => function ($key) {
315 $res = $key - 7;
316 if ($res < 1)
317 $res += 12;
318 return $res;
321 'loaded after saved, wrapped after, not wrapped before' => array(
322 'save_time' => strtotime('2012-07-25 08:00:00'),
323 'load_time' => strtotime('2013-02-01 12:00:00'),
324 'converter' => function ($key) {
325 $res = $key - 7;
326 if ($res < 1)
327 $res += 12;
328 return $res;
331 'loaded after saved, both wrapped' => array(
332 'save_time' => strtotime('2012-02-25 08:00:00'),
333 'load_time' => strtotime('2013-01-01 12:00:00'),
334 'converter' => function ($key) {
335 $res = $key - 11;
336 if ($res < 1)
337 $res += 12;
338 return $res;
342 foreach ($rotation_cases as $descr => $case) {
343 foreach ($this->type['rotating'] as $period) {
344 Report_options::$now = $case['save_time'];
345 $the_opts = array(
346 'report_name' => 'TEST_REPORT',
347 'report_type' => 'hosts',
348 'objects' => array('monitor'),
349 'report_period' => $period,
351 $opts = Report_options::setup_options_obj('sla', $the_opts);
352 for ($i = -5; $i < 15; $i++) {
353 $ary = $opts['months'];
354 $ary[$i] = (float)$i;
355 $opts['months'] = $ary;
358 $this->assertTrue($opts->save());
360 Report_options::$now = $case['load_time'];
361 $loaded = Report_options::setup_options_obj('sla', array('report_id' => $opts['report_id']));
362 $this->assertCount(count($opts['months']), $loaded['months']);
363 foreach ($loaded['months'] as $key => $val) {
364 $this->assertEquals(call_user_func($case['converter'], $key), $val, "The callback for '$descr' over '$period' disagrees with the loaded value for month $key, was $val. Months: ".var_export($loaded['months'], true));
366 $this->assertTrue($opts->delete());
371 function test_sla_no_rotation()
373 $rotation_cases = array(
374 'loaded after saved unwrapped' => array(
375 'save_time' => strtotime('2012-07-25 08:00:00'),
376 'load_time' => strtotime('2012-08-01 12:00:00'),
377 'converter' => function ($key) {
378 return $key - 1;
381 'loaded after saved, wrapped before, not wrapped after' => array(
382 'save_time' => strtotime('2012-01-25 08:00:00'),
383 'load_time' => strtotime('2012-08-01 12:00:00'),
384 'converter' => function ($key) {
385 $res = $key - 7;
386 if ($res < 1)
387 $res += 12;
388 return $res;
391 'loaded after saved, wrapped after, not wrapped before' => array(
392 'save_time' => strtotime('2012-07-25 08:00:00'),
393 'load_time' => strtotime('2013-02-01 12:00:00'),
394 'converter' => function ($key) {
395 $res = $key - 7;
396 if ($res < 1)
397 $res += 12;
398 return $res;
401 'loaded after saved, both wrapped' => array(
402 'save_time' => strtotime('2012-02-25 08:00:00'),
403 'load_time' => strtotime('2013-01-01 12:00:00'),
404 'converter' => function ($key) {
405 $res = $key - 11;
406 if ($res < 1)
407 $res += 12;
408 return $res;
412 foreach ($rotation_cases as $descr => $case) {
413 foreach ($this->type['static'] as $period) {
414 Report_options::$now = $case['save_time'];
415 $the_opts = array(
416 'report_name' => 'TEST_REPORT',
417 'report_type' => 'hosts',
418 'objects' => array('monitor'),
419 'report_period' => $period,
421 $opts = Report_options::setup_options_obj('sla', $the_opts);
422 for ($i = -5; $i < 15; $i++) {
423 $ary = $opts['months'];
424 $ary[$i] = (float)$i;
425 $opts['months'] = $ary;
428 $this->assertTrue($opts->save());
430 Report_options::$now = $case['load_time'];
431 $loaded = Report_options::setup_options_obj('sla', array('report_id' => $opts['report_id']));
432 if ($period == 'thisyear') {
433 foreach ($opts['months'] as $idx => $val) {
434 if (isset($loaded['months'][$idx]))
435 $this->assertEquals($val, $loaded['months'][$idx]);
437 } else {
438 $this->assertEquals($opts['months'], $loaded['months'], "Got unexpected array for test '$descr' over '$period'");
440 $this->assertTrue($opts->delete());
445 function test_sla_special_rotation()
447 $rotation_cases = array(
448 'one month, one quarter' => array(
449 'save_time' => strtotime('2012-03-25 08:00:00'),
450 'load_time' => strtotime('2012-04-01 12:00:00'),
451 'converter' => function ($key) {
452 return $key - 3 + 12;
455 'one month, zero quarters' => array(
456 'save_time' => strtotime('2012-02-25 08:00:00'),
457 'load_time' => strtotime('2012-03-01 12:00:00'),
458 'converter' => function ($key) {
459 return $key;
462 'two month, one quarter' => array(
463 'save_time' => strtotime('2012-02-25 08:00:00'),
464 'load_time' => strtotime('2012-04-01 12:00:00'),
465 'converter' => function ($key) {
466 return $key - 3 + 12;
469 'two month, zero quarters' => array(
470 'save_time' => strtotime('2012-01-25 08:00:00'),
471 'load_time' => strtotime('2012-03-01 12:00:00'),
472 'converter' => function ($key) {
473 return $key;
476 'eleven months, one quarter' => array(
477 'save_time' => strtotime('2012-04-25 08:00:00'),
478 'load_time' => strtotime('2012-03-01 12:00:00'),
479 'converter' => function ($key) {
480 return $key - 9;
483 'eleven months, zero quarters' => array(
484 'save_time' => strtotime('2012-03-25 08:00:00'),
485 'load_time' => strtotime('2012-02-01 12:00:00'),
486 'converter' => function ($key) {
487 return $key;
491 foreach ($rotation_cases as $descr => $case) {
492 foreach ($this->type['special_rotating'] as $period) {
493 Report_options::$now = $case['save_time'];
494 $the_opts = array(
495 'report_name' => 'TEST_REPORT',
496 'report_type' => 'hosts',
497 'objects' => array('monitor'),
498 'report_period' => $period,
500 $opts = Report_options::setup_options_obj('sla', $the_opts);
501 for ($i = -5; $i < 15; $i++) {
502 $ary = $opts['months'];
503 $ary[$i] = (float)$i;
504 $opts['months'] = $ary;
506 $this->assertCount(3, $opts['months'], "Failure in $descr");
508 $this->assertTrue($opts->save());
510 Report_options::$now = $case['load_time'];
511 $loaded = Report_options::setup_options_obj('sla', array('report_id' => $opts['report_id']));
512 $this->assertCount(3, $loaded['months']);
513 $this->assertCount(count($opts['months']), $loaded['months']);
514 foreach ($loaded['months'] as $key => $val) {
515 $this->assertEquals(call_user_func($case['converter'], $key), $val, "The callback for '$descr' over '$period' disagrees with the loaded value for month $key, was $val. Months: ".var_export($loaded['months'], true));
517 $this->assertTrue($opts->delete());