1<?php
2
3$base_dir = realpath(dirname(__FILE__) . '/..');
4$config['install_dir'] = $base_dir;
5
6// Base observium includes
7include(dirname(__FILE__) . '/../includes/defaults.inc.php');
8//include(dirname(__FILE__) . '/../config.php'); // Do not include user editable config here
9include(dirname(__FILE__) . '/../includes/functions.inc.php');
10include(dirname(__FILE__) . '/../includes/definitions.inc.php');
11//include(dirname(__FILE__) . '/data/test_definitions.inc.php'); // Fake definitions for testing
12
13class IncludesTemplatesTest extends \PHPUnit\Framework\TestCase
14{
15  /**
16  * @dataProvider providerSimpleTemplate
17  * @group simple_template
18  */
19  public function testSimpleTemplate($template, $keys, $result)
20  {
21    $this->assertSame($result, simple_template($template, $keys));
22  }
23
24  public function providerSimpleTemplate()
25  {
26    $return = array(
27      // One line php-style comments
28      array(
29        '<h1>{{title}}</h1>   // just something interesting... #or ^not...',
30        array('title' => 'A Comedy of Errors'),
31        '<h1>A Comedy of Errors</h1>'
32      ),
33      // Multiline php-style comments
34      array(
35        '/**
36          * just something interesting... #or ^not...
37          */
38        <h1>{{title}}</h1>
39        /**
40          * just something interesting... #or ^not...
41          */',
42        array('title' => 'A Comedy of Errors'),
43        '        <h1>A Comedy of Errors</h1>'.PHP_EOL
44      ),
45      // Var not exist
46      array(
47        '<h1>{{title}}</h1>',
48        array('non_exist' => 'A Comedy of Errors'),
49        '<h1></h1>'
50      ),
51    );
52
53    $templates_dir = dirname(__FILE__) . '/templates';
54    foreach (scandir($templates_dir) as $dir)
55    {
56      $json = $templates_dir.'/'.$dir.'/'.$dir.'.json';
57      if ($dir != '.' && $dir != '..' && is_dir($templates_dir.'/'.$dir) && is_file($json))
58      {
59        $template = $templates_dir.'/'.$dir.'/'.$dir.'.mustache';
60        $result   = $templates_dir.'/'.$dir.'/'.$dir.'.txt';
61
62        $return[] = array(
63                      file_get_contents($template),
64                      json_decode(file_get_contents($json), TRUE),
65                      file_get_contents($result)
66                    );
67      }
68    }
69
70    return $return;
71  }
72
73  /**
74  * @dataProvider providerArrayToXML
75  * @group templates
76  */
77  public function testArrayToXML($array, $result)
78  {
79    $xml = new SimpleXMLElement('<template/>');
80    array_to_xml($array, $xml);
81
82    $this->assertSame($result, $xml->asXML());
83  }
84
85  public function providerArrayToXML()
86  {
87    return array(
88      array(
89        // Array
90        array('entity' => 'sensor',
91              'name' => 'temp',
92              'message' => 'temp alarm',
93              'severity' => 'crit',
94              'suppress_recovery' => 0,
95              'delay' => 0,
96              'conditions_and' => 0,
97              'conditions' => array('sensor_value > 30',
98                                    'sensor_value < 1'),
99              'conditions_complex' => 'sensor_value > 30 OR sensor_value < 1',
100              'associations' => array(array('device' => array('hostname match timos*'),
101                                            'entity' => array('sensor_class is temperature')),
102                                      array('device' => array('hostname match cisco*'),
103                                            'entity' => array('sensor_class is temperature')))),
104        // XML
105        '<?xml version="1.0"?>
106<template><entity>sensor</entity><name>temp</name><message>temp alarm</message><severity>crit</severity><suppress_recovery>0</suppress_recovery><delay>0</delay><conditions_and>0</conditions_and><conditions>sensor_value &gt; 30</conditions><conditions>sensor_value &lt; 1</conditions><conditions_complex>sensor_value &gt; 30 OR sensor_value &lt; 1</conditions_complex><associations><device>hostname match timos*</device><entity>sensor_class is temperature</entity></associations><associations><device>hostname match cisco*</device><entity>sensor_class is temperature</entity></associations></template>
107'
108      ),
109    );
110  }
111
112  /**
113  * @dataProvider providerGenerateTemplate
114  * @group templates
115  */
116  public function testGenerateTemplate($type, $array, $result)
117  {
118    $template = generate_template($type, $array);
119    $template = preg_replace('/<template type="' . $type . '".+?>/', '<template>', $template);
120    $this->assertSame($result, $template);
121  }
122
123  public function providerGenerateTemplate()
124  {
125    $alert_array = array(
126      'alert_test_id' => '4',
127      'entity_type' => 'sensor',
128      'alert_name' => 'temp',
129      'alert_message' => 'temp alarm',
130      'conditions' => array(
131          array('value' => '30', 'condition' => '>', 'metric' => 'sensor_value'),
132          array('value' => '1',  'condition' => '<', 'metric' => 'sensor_value')),
133      'and' => '0',
134      'severity' => 'crit',
135      'delay' => '0',
136      'alerter' => 'default',
137      'enable' => '1',
138      'show_frontpage' => '1',
139      'suppress_recovery' => '0',
140      'ignore_until' => NULL,
141      'associations' => array(
142          array('entity_type' => 'sensor',
143                'entity_attribs' => array(
144                                      array('value' => 'temperature',
145                                            'condition' => 'is',
146                                            'attrib' => 'sensor_class')),
147                'device_attribs' => array(
148                                      array('value' => 'timos*',
149                                            'condition' => 'match',
150                                            'attrib' => 'hostname'))),
151          array('entity_type' => 'sensor',
152                'entity_attribs' => array(
153                                      array('value' => 'temperature',
154                                            'condition' => 'is',
155                                            'attrib' => 'sensor_class')),
156                'device_attribs' => array(
157                                      array('value' => 'cisco*',
158                                            'condition' => 'match',
159                                            'attrib' => 'hostname')))),
160      );
161    $alert_array1 = array(
162      'alert_test_id' => '7',
163      'entity_type' => 'storage',
164      'alert_name' => 'stor_perc_85',
165      'alert_message' => 'Storage exceeds 85% of disk capacity',
166      'conditions' => array(
167          array('value' => '85', 'condition' => 'ge', 'metric' => 'storage_perc')),
168      'and' => '1',
169      'severity' => 'crit',
170      'delay' => '0',
171      'alerter' => 'default',
172      'enable' => '1',
173      'show_frontpage' => '1',
174      'suppress_recovery' => '0',
175      'ignore_until' => NULL,
176      'associations' => array(
177          array('entity_type' => 'storage',
178                'entity_attribs' => array(
179                                      array('value' => 'hrStorageFixedDisk',
180                                            'condition' => 'equals',
181                                            'metric' => 'storage_perc',
182                                            'attrib' => 'storage_type')),
183                'device_attribs' => array(
184                                      array ('value' => NULL,
185                                             'condition' => NULL,
186                                             'metric' => 'storage_perc',
187                                             'attrib' => '*')))),
188      );
189    return array(
190      array(
191        'alert',
192        // Array
193        $alert_array,
194        // XML
195        '<?xml version="1.0"?>
196<template><entity_type>sensor</entity_type><name>temp</name><message>temp alarm</message><severity>crit</severity><suppress_recovery>0</suppress_recovery><delay>0</delay><conditions_and>0</conditions_and><conditions>sensor_value &gt; 30</conditions><conditions>sensor_value &lt; 1</conditions><conditions_complex>sensor_value &gt; 30 OR sensor_value &lt; 1</conditions_complex><associations><device>hostname match timos*</device><entity>sensor_class is temperature</entity></associations><associations><device>hostname match cisco*</device><entity>sensor_class is temperature</entity></associations></template>
197'
198      ),
199      array(
200        'alert',
201        // Array
202        $alert_array1,
203        // XML
204        '<?xml version="1.0"?>
205<template><entity_type>storage</entity_type><name>stor_perc_85</name><message>Storage exceeds 85% of disk capacity</message><severity>crit</severity><suppress_recovery>0</suppress_recovery><delay>0</delay><conditions_and>1</conditions_and><conditions>storage_perc ge 85</conditions><conditions_complex>storage_perc ge 85</conditions_complex><associations><device>*</device><entity>storage_type equals hrStorageFixedDisk</entity></associations></template>
206'
207      ),
208    );
209  }
210
211}
212
213// EOF
214