2015-07-01 10:47:44 -04:00
|
|
|
<?php
|
|
|
|
|
|
2026-03-24 06:30:06 -04:00
|
|
|
// SPDX-FileCopyrightText: 2018 Icinga GmbH <https://icinga.com>
|
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
2016-02-26 06:01:00 -05:00
|
|
|
namespace Tests\Icinga\Module\Director\Objects;
|
2016-02-25 12:56:39 -05:00
|
|
|
|
2015-07-01 10:47:44 -04:00
|
|
|
use Icinga\Module\Director\Objects\IcingaTimePeriod;
|
2016-02-25 12:56:39 -05:00
|
|
|
use Icinga\Module\Director\Test\BaseTestCase;
|
2015-07-01 10:47:44 -04:00
|
|
|
|
2016-03-17 18:05:16 -04:00
|
|
|
class IcingaTimePeriodTest extends BaseTestCase
|
2015-07-01 10:47:44 -04:00
|
|
|
{
|
2016-03-17 18:05:16 -04:00
|
|
|
protected $testPeriodName = '___TEST___timeperiod';
|
2015-07-01 10:47:44 -04:00
|
|
|
|
2018-09-13 08:58:21 -04:00
|
|
|
protected $createdNames = [];
|
|
|
|
|
|
2016-03-17 18:05:16 -04:00
|
|
|
public function testWhetherUpdatedTimeperiodsAreCorrectlyStored()
|
2015-07-01 10:47:44 -04:00
|
|
|
{
|
2016-02-26 06:01:00 -05:00
|
|
|
if ($this->skipForMissingDb()) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-25 12:56:39 -05:00
|
|
|
$period = $this->createTestPeriod();
|
2015-07-01 10:47:44 -04:00
|
|
|
|
|
|
|
|
$newRanges = array(
|
|
|
|
|
'monday' => '00:00-24:00',
|
2016-02-25 12:56:39 -05:00
|
|
|
'tuesday' => '18:00-24:00',
|
2015-07-01 10:47:44 -04:00
|
|
|
'wednesday' => '00:00-24:00',
|
|
|
|
|
);
|
2016-03-17 18:05:16 -04:00
|
|
|
$period->ranges = $newRanges;
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'18:00-24:00',
|
|
|
|
|
$period->toPlainObject()->ranges->tuesday
|
|
|
|
|
);
|
|
|
|
|
|
2016-02-25 14:04:03 -05:00
|
|
|
$period->store();
|
2015-07-01 10:47:44 -04:00
|
|
|
|
2016-02-25 12:56:39 -05:00
|
|
|
$period = $this->loadTestPeriod();
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'18:00-24:00',
|
2016-09-08 11:25:36 -04:00
|
|
|
$period->ranges()->get('tuesday')->range_value
|
2016-02-25 12:56:39 -05:00
|
|
|
);
|
2016-03-17 18:05:16 -04:00
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'00:00-24:00',
|
|
|
|
|
$period->toPlainObject()->ranges->wednesday
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$period->ranges()->setRange('wednesday', '09:00-17:00');
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'09:00-17:00',
|
|
|
|
|
$period->toPlainObject()->ranges->wednesday
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
'00:00-24:00',
|
|
|
|
|
$period->getPlainUnmodifiedObject()->ranges->wednesday
|
|
|
|
|
);
|
2015-07-01 10:47:44 -04:00
|
|
|
}
|
|
|
|
|
|
2018-09-13 08:58:21 -04:00
|
|
|
protected function createTestPeriod($suffix = '', $testRanges = [])
|
2015-07-01 10:47:44 -04:00
|
|
|
{
|
|
|
|
|
$db = $this->getDb();
|
2018-09-13 08:58:21 -04:00
|
|
|
$name = $this->testPeriodName . $suffix;
|
|
|
|
|
|
|
|
|
|
$this->createdNames[] = $name;
|
2016-02-25 12:56:39 -05:00
|
|
|
$object = IcingaTimePeriod::create(
|
|
|
|
|
array(
|
2018-09-13 08:58:21 -04:00
|
|
|
'object_name' => $name,
|
2016-02-25 12:56:39 -05:00
|
|
|
'object_type' => 'object'
|
|
|
|
|
),
|
|
|
|
|
$db
|
|
|
|
|
);
|
|
|
|
|
$object->store();
|
2015-07-01 10:47:44 -04:00
|
|
|
$ranges = $object->ranges();
|
|
|
|
|
|
2018-09-13 08:58:21 -04:00
|
|
|
if (empty($testRanges)) {
|
|
|
|
|
$testRanges = array(
|
|
|
|
|
'monday' => '00:00-24:00',
|
|
|
|
|
'tuesday' => '00:00-24:00',
|
|
|
|
|
'wednesday' => '00:00-24:00',
|
|
|
|
|
);
|
|
|
|
|
}
|
2015-07-01 10:47:44 -04:00
|
|
|
|
2016-02-25 12:56:39 -05:00
|
|
|
$ranges->set($testRanges);
|
2016-02-25 14:04:03 -05:00
|
|
|
$object->store();
|
2015-07-01 10:47:44 -04:00
|
|
|
|
2016-02-25 12:56:39 -05:00
|
|
|
return $object;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-18 15:21:53 -04:00
|
|
|
public function testIsActiveWorksForWeekdayRanges()
|
|
|
|
|
{
|
|
|
|
|
$period = $this->createTestPeriod();
|
|
|
|
|
|
|
|
|
|
$newRanges = array(
|
|
|
|
|
'monday' => '00:00-24:00',
|
|
|
|
|
'tuesday' => '18:00-24:00',
|
|
|
|
|
'wednesday' => '00:00-24:00',
|
|
|
|
|
);
|
|
|
|
|
$period->ranges = $newRanges;
|
|
|
|
|
|
|
|
|
|
// Tuesday:
|
|
|
|
|
$this->assertFalse($period->isActive(strtotime('2016-05-17 10:00:00')));
|
|
|
|
|
// Wednesday:
|
|
|
|
|
$this->assertTrue($period->isActive(strtotime('2016-05-18 10:00:00')));
|
|
|
|
|
// Thursday:
|
|
|
|
|
$this->assertFalse($period->isActive(strtotime('2016-05-19 10:00:00')));
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-13 08:58:21 -04:00
|
|
|
public function testPeriodWithIncludes()
|
|
|
|
|
{
|
|
|
|
|
$period = $this->createTestPeriod();
|
|
|
|
|
$include = $this->createTestPeriod('include', ['thursday' => '00:00-24:00']);
|
|
|
|
|
|
|
|
|
|
$period->set('includes', $include->object_name);
|
|
|
|
|
$period->store();
|
|
|
|
|
|
|
|
|
|
// Wednesday:
|
|
|
|
|
$this->assertTrue($period->isActive(strtotime('2016-05-18 10:00:00')));
|
|
|
|
|
// Thursday:
|
|
|
|
|
$this->assertTrue($period->isActive(strtotime('2016-05-19 10:00:00')));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testPeriodWithExcludes()
|
|
|
|
|
{
|
|
|
|
|
$period = $this->createTestPeriod();
|
|
|
|
|
$exclude = $this->createTestPeriod('exclude', ['wednesday' => '00:00-24:00']);
|
|
|
|
|
|
|
|
|
|
$period->set('excludes', $exclude->object_name);
|
|
|
|
|
$period->store();
|
|
|
|
|
|
|
|
|
|
// Wednesday:
|
|
|
|
|
$this->assertFalse($period->isActive(strtotime('2016-05-18 10:00:00')));
|
|
|
|
|
// Thursday:
|
|
|
|
|
$this->assertFalse($period->isActive(strtotime('2016-05-19 10:00:00')));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testPeriodPreferingIncludes()
|
|
|
|
|
{
|
|
|
|
|
$period = $this->createTestPeriod();
|
|
|
|
|
$include = $this->createTestPeriod('include', ['thursday' => '00:00-24:00']);
|
|
|
|
|
$exclude = $this->createTestPeriod('exclude', ['thursday' => '00:00-24:00']);
|
|
|
|
|
|
|
|
|
|
$period->set('includes', $include->object_name);
|
|
|
|
|
$period->set('excludes', $exclude->object_name);
|
|
|
|
|
$period->store();
|
|
|
|
|
|
|
|
|
|
// Wednesday:
|
|
|
|
|
$this->assertTrue($period->isActive(strtotime('2016-05-18 10:00:00')));
|
|
|
|
|
// Thursday:
|
|
|
|
|
$this->assertTrue($period->isActive(strtotime('2016-05-19 10:00:00')));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testPeriodPreferingExcludes()
|
|
|
|
|
{
|
|
|
|
|
$period = $this->createTestPeriod();
|
|
|
|
|
$include = $this->createTestPeriod('include', ['thursday' => '00:00-24:00']);
|
|
|
|
|
$exclude = $this->createTestPeriod('exclude', ['thursday' => '00:00-24:00']);
|
|
|
|
|
|
|
|
|
|
$period->set('prefer_includes', false);
|
|
|
|
|
$period->set('includes', $include->object_name);
|
|
|
|
|
$period->set('excludes', $exclude->object_name);
|
|
|
|
|
$period->store();
|
|
|
|
|
|
|
|
|
|
// Wednesday:
|
|
|
|
|
$this->assertTrue($period->isActive(strtotime('2016-05-18 10:00:00')));
|
|
|
|
|
// Thursday:
|
|
|
|
|
$this->assertFalse($period->isActive(strtotime('2016-05-19 10:00:00')));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function loadTestPeriod($suffix = '')
|
2016-02-25 12:56:39 -05:00
|
|
|
{
|
2018-09-13 08:58:21 -04:00
|
|
|
return IcingaTimePeriod::load($this->testPeriodName . $suffix, $this->getDb());
|
2016-02-25 12:56:39 -05:00
|
|
|
}
|
2015-07-01 10:47:44 -04:00
|
|
|
|
2023-08-30 08:37:31 -04:00
|
|
|
public function tearDown(): void
|
2016-02-25 12:56:39 -05:00
|
|
|
{
|
|
|
|
|
$db = $this->getDb();
|
2018-09-13 08:58:21 -04:00
|
|
|
|
|
|
|
|
foreach ($this->createdNames as $name) {
|
|
|
|
|
if (IcingaTimePeriod::exists($name, $db)) {
|
|
|
|
|
IcingaTimePeriod::load($name, $db)->delete();
|
|
|
|
|
}
|
2016-02-25 12:56:39 -05:00
|
|
|
}
|
2024-01-15 05:03:33 -05:00
|
|
|
|
|
|
|
|
parent::tearDown();
|
2015-07-01 10:47:44 -04:00
|
|
|
}
|
|
|
|
|
}
|