From e0ea03a3575f9e4ce7e4486ac4440b85a9485130 Mon Sep 17 00:00:00 2001
From: Stephane Chapron <schapron@centreon.com>
Date: Tue, 10 Apr 2018 17:23:17 +0200
Subject: [PATCH] fix(downtime): fix recurrent downtimes on hostGroups when no
 serviceGroup exist

---
 behat.yml                                     |   4 +
 features/DowntimeRecurrent.feature            |  14 ++
 .../bootstrap/DowntimeRecurrentContext.php    | 126 ++++++++++++++++++
 www/class/centreonDowntime.class.php          |   2 +-
 4 files changed, 145 insertions(+), 1 deletion(-)
 create mode 100644 features/DowntimeRecurrent.feature
 create mode 100644 features/bootstrap/DowntimeRecurrentContext.php

diff --git a/behat.yml b/behat.yml
index 28d1509a20..590842dc7b 100644
--- a/behat.yml
+++ b/behat.yml
@@ -105,6 +105,10 @@ default:
       paths: [ %paths.base%/features/DowntimeDST.feature ]
       contexts: [ DowntimeDSTContext ]
 
+    downtime_recurrent:
+      paths: [ %paths.base%/features/DowntimeRecurrent.feature ]
+      contexts: [ DowntimeRecurrentContext ]
+
     command_arguments:
       paths: [ %paths.base%/features/CommandArguments.feature ]
       contexts: [ CommandArgumentsContext ]
diff --git a/features/DowntimeRecurrent.feature b/features/DowntimeRecurrent.feature
new file mode 100644
index 0000000000..6fdc7440be
--- /dev/null
+++ b/features/DowntimeRecurrent.feature
@@ -0,0 +1,14 @@
+Feature: Testing a recurrent Downtime
+  As a Centreon user
+  I want to be certain that the recurrent downtimes work correctly
+  To release quality products
+
+  Background:
+    Given I am logged in a Centreon server
+
+  @critical
+  Scenario: Testing a recurrent Downtime on a HostGroup without any ServiceGroup created (Bugfix)
+    Given a hostGroup is configured
+    And a recurrent downtime on a hostGroup
+    When this one gives a downtime
+    Then the recurrent downtime started
diff --git a/features/bootstrap/DowntimeRecurrentContext.php b/features/bootstrap/DowntimeRecurrentContext.php
new file mode 100644
index 0000000000..4b11fc1085
--- /dev/null
+++ b/features/bootstrap/DowntimeRecurrentContext.php
@@ -0,0 +1,126 @@
+<?php
+
+use Centreon\Test\Behat\CentreonContext;
+use Centreon\Test\Behat\Configuration\ServiceConfigurationPage;
+use Centreon\Test\Behat\Configuration\DowntimeConfigurationListingPage;
+use Centreon\Test\Behat\Configuration\HostConfigurationPage;
+use Centreon\Test\Behat\Configuration\HostGroupConfigurationPage;
+use Centreon\Test\Behat\Configuration\RecurrentDowntimeConfigurationPage;
+
+/**
+ * Defines application features from the specific context.
+ */
+class DowntimeRecurrentContext extends CentreonContext
+{
+    protected $currentPage;
+    protected $startDate;
+    protected $endDate;
+
+    protected $host = array(
+        'name' => 'host',
+        'alias' => 'host',
+        'address' => 'host2@localhost',
+        'check_command' => 'check_centreon_dummy'
+    );
+
+    protected $hostGroup = array(
+        'name' => 'hostGroupName',
+        'alias' => 'hostGroupAlias',
+        'hosts' => 'host',
+        'enabled' => 1
+    );
+
+    protected $service = array(
+        'hosts' => 'host',
+        'description' => 'service',
+        'templates' => 'generic-service',
+        'check_command' => 'check_centreon_dummy',
+        'check_period' => '24x7',
+        'max_check_attempts' => 1,
+        'normal_check_interval' => 1,
+        'retry_check_interval' => 1,
+        'active_checks_enabled' => 1,
+        'passive_checks_enabled' => 0,
+        'notifications_enabled' => 1,
+        'notify_on_recovery' => 1,
+        'notify_on_critical' => 1,
+        'recovery_notification_delay' => 1,
+        'cs' => 'admin_admin'
+    );
+
+    /**
+     * @Given a hostGroup is configured
+     */
+    public function aHostGroupIsConfigured()
+    {
+        $this->currentPage = new HostConfigurationPage($this);
+        $this->currentPage->setproperties($this->host);
+        $this->currentPage->save();
+        $this->currentPage = new HostGroupConfigurationPage($this);
+        $this->currentPage->setProperties($this->hostGroup);
+        $this->currentPage->save();
+        $this->currentPage = new ServiceConfigurationPage($this);
+        $this->currentPage->setProperties($this->service);
+        $this->currentPage->save();
+        $this->reloadAllPollers();
+    }
+
+    /**
+     * @Given a recurrent downtime on a hostGroup
+     */
+    public function aRecurrentDowntime()
+    {
+        $this->startDate = new DateTime('now');
+
+        $this->endDate = new DateTime('now');
+        $this->endDate->add(new DateInterval('PT360M'));
+
+        $this->currentPage = new RecurrentDowntimeConfigurationPage($this);
+        $this->currentPage->setProperties(array(
+            'name' => 'test_DT',
+            'alias' => 'recurrent_DT',
+            'days' => array(7, 1, 2, 3, 4, 5, 6),
+            'start' => $this->startDate->format('H:i'),
+            'end' => $this->endDate->format('H:i'),
+            'hostgroup_relation' => $this->hostGroup['name']
+        ));
+
+        $this->currentPage->save();
+    }
+
+    /**
+     * @When this one gives a downtime
+     */
+    public function thisOneGivesADowntime()
+    {
+        /* faking cron's launchtime. 2 min sooner */
+        $this->container->execute(
+            "faketime -f '-120s' php /usr/share/centreon/cron/downtimeManager.php",
+            'web'
+        );
+    }
+
+    /**
+     * @Then the recurrent downtime started
+     */
+    public function aRecurrentDowntimeIsStarted()
+    {
+        /* checking for results */
+        $this->spin(
+            function ($context) {
+                $found = false;
+                $this->currentPage = new DowntimeConfigurationListingPage($context);
+                $this->currentPage->displayDowntimeCycle();
+                foreach ($this->currentPage->getEntries() as $entry) {
+                    if ($entry['host'] == $context->host['name'] &&
+                        $entry['service'] == $context->service['description'] &&
+                        $entry['started'] == true
+                    ) {
+                        $found = true;
+                    }
+                }
+                return $found;
+            }
+        );
+    }
+}
\ No newline at end of file
diff --git a/www/class/centreonDowntime.class.php b/www/class/centreonDowntime.class.php
index 7059f9c1d6..887d9dea43 100644
--- a/www/class/centreonDowntime.class.php
+++ b/www/class/centreonDowntime.class.php
@@ -360,7 +360,7 @@ class CentreonDowntime
             . 'dtp.dtp_month_cycle, dtp.dtp_day_of_month, dtp.dtp_fixed, dtp.dtp_duration, '
             . 'h.host_id, h.host_name, NULL as service_id, NULL as service_description '
             . 'FROM downtime_period dtp, downtime dt, '
-            . 'downtime_hostgroup_relation dhr, servicegroup sg, '
+            . 'downtime_hostgroup_relation dhr, '
             . 'host h, hostgroup_relation hgr '
             . 'WHERE dtp.dt_id = dhr.dt_id '
             . 'AND dtp.dt_id = dt.dt_id '
-- 
GitLab