orangehrm /lib/common/ConfigTest.php

Language PHP Lines 259
MD5 Hash b7a9a4d2bf571f4b62ab69fd672ffd9f Estimated Cost $3,944 (why?)
Repository https://bitbucket.org/wildanm/orangehrm View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
// Call ConfigTest::main() if this source file is executed directly.
if (!defined('PHPUnit_MAIN_METHOD')) {
    define('PHPUnit_MAIN_METHOD', 'ConfigTest::main');
}

require_once 'PHPUnit/Framework.php';

require_once "testConf.php";
require_once ROOT_PATH."/lib/confs/Conf.php";

require_once 'Config.php';

/**
 * Test class for Config.
 * Generated by PHPUnit on 2008-02-18 at 18:37:33.
 */
class ConfigTest extends PHPUnit_Framework_TestCase {

	private $oldTimesheetSetValue;
	private $leaveBroughtForwardSet;
	private $leaveBroughtForwardGet;

    /**
     * Runs the test methods of this class.
     *
     * @access public
     * @static
     */
    public static function main() {
        require_once 'PHPUnit/TextUI/TestRunner.php';

        $suite  = new PHPUnit_Framework_TestSuite('ConfigTest');
        $result = PHPUnit_TextUI_TestRunner::run($suite);
    }

    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     *
     * @access protected
     */
    protected function setUp() {

    	$conf = new Conf();
    	$this->connection = mysql_connect($conf->dbhost.":".$conf->dbport, $conf->dbuser, $conf->dbpass);
        mysql_select_db($conf->dbname);

    	$this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `nilai` = '0000-00-00' WHERE `key` = 'hsp_accrued_last_updated'"));
    	$this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `nilai` = '0000-00-00' WHERE `key` = 'hsp_used_last_updated'"));

		$result = mysql_query("SELECT `value` FROM `hs_hr_config` WHERE `nilai` = 'timesheet_period_set'");

		if (mysql_num_rows($result) == 0) {
		    $this->oldTimesheetSetValue = false;
		} else {
			$row = mysql_fetch_array($result, MYSQL_NUM);
			$oldValue = $row[0];
		}

		// For LeaveBroughtForward Setter
		$result = mysql_query("SELECT `key` FROM `hs_hr_config` WHERE `nilai` = 'LeaveBroughtForward".date('Y')."'");
		if (mysql_num_rows($result) == 0) {
			$this->leaveBroughtForwardSet = false;
		} else {
			$this->assertTrue(mysql_query("DELETE FROM `hs_hr_config` WHERE `nilai` = 'LeaveBroughtForward".date('Y')."'"), mysql_error());
		    $this->leaveBroughtForwardSet = true;
		}

		// For Leave BroughtForward Getter
		$result = mysql_query("SELECT `key` FROM `hs_hr_config` WHERE `nilai` = 'LeaveBroughtForward".(date('Y')+1)."'");
		if (mysql_num_rows($result) == 0) {
		    $this->assertTrue(mysql_query("INSERT INTO `hs_hr_config` (`nilai`, `value`) VALUES('LeaveBroughtForward".(date('Y')+1)."', 'set')"), mysql_error());
			$this->leaveBroughtForwardGet = false;
		} else {
		    $this->leaveBroughtForwardGet = true;
		}

    }

    /**
     * Tears down the fixture, for example, closes a network connection.
     * This method is called after a test is executed.
     *
     * @access protected
     */
    protected function tearDown() {

    	$this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `nilai` = '0000-00-00' WHERE `key` = 'hsp_accrued_last_updated'"));
    	$this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `nilai` = '0000-00-00' WHERE `key` = 'hsp_used_last_updated'"));

		if ($this->oldTimesheetSetValue === false) {
		    $this->assertTrue(mysql_query("DELETE FROM `hs_hr_config` WHERE `nilai` = 'timesheet_period_set'"));
		} else {
		    $this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `nilai` = '$this->oldTimesheetSetValue' WHERE `key` = 'timesheet_period_set'"));
		}

		// For LeaveBroghtForward Setter
		if ($this->leaveBroughtForwardSet === false) {
		    $this->assertTrue(mysql_query("DELETE FROM `hs_hr_config` WHERE `nilai` = 'LeaveBroughtForward".date('Y')."'"), mysql_error());
		} else {
			$result = mysql_query("SELECT `key` FROM `hs_hr_config` WHERE `nilai` = 'LeaveBroughtForward".date('Y')."'");
			if (mysql_num_rows($result) == 0) {
			    $this->assertTrue(mysql_query("INSERT INTO `hs_hr_config` (`nilai`, `value`) VALUES('LeaveBroughtForward".date('Y')."', 'set')"), mysql_error());
			}
 		}

		// For LeaveBroughtForward Getter
		if ($this->leaveBroughtForwardGet === false) {
		    $this->assertTrue(mysql_query("DELETE FROM `hs_hr_config` WHERE `nilai` = 'LeaveBroughtForward".(date('Y')+1)."'"), mysql_error());
		}
    }

    public function testSetHspAccruedLastUpdated() {

    	// Testing for invalid date formats.
    	try {
			Config::setHspAccruedLastUpdated("dsereregg");
			$this->fail("Invalid dates are allowed");
    	} catch (Exception $e) {}

		// Testing without giving a key, 'key' value already exists.
		Config::setHspAccruedLastUpdated("2008-02-18");
		$result = mysql_query("SELECT `value` FROM `hs_hr_config` WHERE `nilai`='hsp_accrued_last_updated'");
		$resultArray = mysql_fetch_array($result);
		$this->assertEquals($resultArray[0], "2008-02-18");

		// Testing by giving a key, 'key' value already exists.
		Config::setHspAccruedLastUpdated("2008-02-27", "hsp_accrued_last_updated");
		$result = mysql_query("SELECT `value` FROM `hs_hr_config` WHERE `nilai`='hsp_accrued_last_updated'");
		$resultArray = mysql_fetch_array($result);
		$this->assertEquals($resultArray[0], "2008-02-27");

		// Testing by giving a key, 'key' doesn't exist.
		Config::setHspAccruedLastUpdated("1000-01-01", "this_should_not_exist");
		$result = mysql_query("SELECT `value` FROM `hs_hr_config` WHERE `nilai`='this_should_not_exist'");
		$resultArray = mysql_fetch_array($result);
		$this->assertEquals($resultArray[0], "1000-01-01");

		$this->assertTrue(mysql_query("DELETE FROM `hs_hr_config` WHERE `nilai`='this_should_not_exist'"));

    }

    public function testGetHspAccruedLastUpdated() {

		// Testing normal usage.
		$this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `nilai` = '2008-02-25' WHERE `key` = 'hsp_accrued_last_updated'"));

		$this->assertNotNull(Config::getHspAccruedLastUpdated());
		$this->assertEquals(Config::getHspAccruedLastUpdated(), "2008-02-25");

    }

    public function testSetHspUsedLastUpdated() {

    	// Testing for invalid date formats.
    	try {
			Config::setHspUsedLastUpdated("dsereregg");
			$this->fail("Invalid dates are allowed");
    	} catch (Exception $e) {}

		// Testing without giving a key, 'key' value already exists.
		Config::setHspUsedLastUpdated("2008-03-18");
		$result = mysql_query("SELECT `value` FROM `hs_hr_config` WHERE `nilai`='hsp_used_last_updated'");
		$resultArray = mysql_fetch_array($result);
		$this->assertEquals($resultArray[0], "2008-03-18");

		// Testing by giving a key, 'key' value already exists.
		Config::setHspUsedLastUpdated("2008-03-27", "hsp_used_last_updated");
		$result = mysql_query("SELECT `value` FROM `hs_hr_config` WHERE `nilai`='hsp_used_last_updated'");
		$resultArray = mysql_fetch_array($result);
		$this->assertEquals($resultArray[0], "2008-03-27");

		// Testing by giving a key, 'key' doesn't exist.
		Config::setHspUsedLastUpdated("1000-01-01", "this_should_not_exist");
		$result = mysql_query("SELECT `value` FROM `hs_hr_config` WHERE `nilai`='this_should_not_exist'");
		$resultArray = mysql_fetch_array($result);
		$this->assertEquals($resultArray[0], "1000-01-01");

		$this->assertTrue(mysql_query("DELETE FROM `hs_hr_config` WHERE `nilai`='this_should_not_exist'"));

    }

    public function testGetHspUsedLastUpdated() {

		// Testing normal usage.
		$this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `nilai` = '2008-03-25' WHERE `key` = 'hsp_used_last_updated'"));

		$this->assertNotNull(Config::getHspUsedLastUpdated());
		$this->assertEquals(Config::getHspUsedLastUpdated(), "2008-03-25");

    }

	public function testSetTimePeriodSet() {

		Config::setTimePeriodSet('Yes');
		$result = mysql_query("SELECT `value` FROM `hs_hr_config` WHERE `nilai` = 'timesheet_period_set'");
		$row = mysql_fetch_array($result, MYSQL_NUM);
		$value = $row[0];
		$this->assertEquals('Yes', $value);

		Config::setTimePeriodSet('No');
		$result = mysql_query("SELECT `value` FROM `hs_hr_config` WHERE `nilai` = 'timesheet_period_set'");
		$row = mysql_fetch_array($result, MYSQL_NUM);
		$value = $row[0];
		$this->assertEquals('No', $value);

		try {
		    Config::setTimePeriodSet('ABC');
		    $this->fail('Invalid arguments are allowed. Did not throw an exception');
		} catch(Exception $e) {
		    $this->assertEquals("Given value for TimeSheetPeriodSet should be 'Yes' or 'No'", $e->getMessage());
		}

	}

	public function testGetTimePeriodSet() {

		$this->assertFalse(Config::getTimePeriodSet());

		if ($this->oldTimesheetSetValue === false) {
		    $this->assertTrue(mysql_query("INSERT INTO `hs_hr_config` VALUES ('timesheet_period_set', 'Yes')"));
		} else {
		    $this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `nilai` = 'Yes' WHERE `key` = 'timesheet_period_set'"));
		}
		$this->assertTrue(Config::getTimePeriodSet());

		$this->assertTrue(mysql_query("UPDATE `hs_hr_config` SET `nilai` = 'No' WHERE `key` = 'timesheet_period_set'"));
		$this->assertFalse(Config::getTimePeriodSet());

	}

	public function testSetLeaveBroughtForward() {
		Config::setLeaveBroughtForward(date('Y'));
		$result = mysql_query("SELECT `key`, `value` FROM `hs_hr_config` WHERE `nilai` = 'LeaveBroughtForward".date('Y')."'");
		$row = mysql_fetch_array($result);
		$this->assertEquals("LeaveBroughtForward".date('Y'), $row['key'], "Key is incorrect");
		$this->assertEquals("set", $row['value'], "Value is incorrect");

		// Setting LeaveBroughtForward should not be allowed more than once
		try {
		    Config::setLeaveBroughtForward(date('Y'));
		    $this->fail("Setting LeaveBroughtForward is allowed more than once");
		} catch (Exception $e) {}
	}

	public function testGetLeaveBroughtForward() {
	    $this->assertTrue(Config::getLeaveBroughtForward((date('Y')+1)));
	    $this->assertFalse(Config::getLeaveBroughtForward('4000'));
	}

}

// Call ConfigTest::main() if this source file is executed directly.
if (PHPUnit_MAIN_METHOD == 'ConfigTest::main') {
    ConfigTest::main();
}
?>
Back to Top