| 1 | <?php |
| 2 | |
| 3 | require_once 'PHPUnit.php'; |
| 4 | require_once 'diogenes/diogenes.logger-view.inc.php'; |
| 5 | require_once 'diogenes/diogenes.core.globals.inc.php'; |
| 6 | require_once 'TestHelpers.php'; |
| 7 | |
| 8 | class DiogenesLoggerViewTest extends PHPUnit_TestCase |
| 9 | { |
| 10 | function setUp() |
| 11 | { |
| 12 | global $globals; |
| 13 | |
| 14 | $globals = new DiogenesCoreGlobals; |
| 15 | |
| 16 | $globals->database_error_fatal = true; |
| 17 | $globals->db = new DiogenesDatabase('test', 'localhost', 'test', ''); |
| 18 | $globals->db->_fatal = true; |
| 19 | |
| 20 | if ($globals->db->err()) |
| 21 | { |
| 22 | echo "DB err: " . $globals->db->error() . "\n"; |
| 23 | } |
| 24 | |
| 25 | $this->o = new DiogenesLoggerView(); |
| 26 | } |
| 27 | |
| 28 | function testConstructor() |
| 29 | { |
| 30 | global $globals; |
| 31 | |
| 32 | $this->assertEquals($this->o->dbh, $globals->db); |
| 33 | $this->assertTrue(is_a($this->o->dbh, 'diogenesdatabase')); |
| 34 | } |
| 35 | |
| 36 | function test_getAuths() |
| 37 | { |
| 38 | global $globals; |
| 39 | $globals->table_log_sessions = 'diogenes_logsessions'; |
| 40 | |
| 41 | $this->assertEquals(array(0 => __('all'), 'native' => 'native', 'external' => 'external'), $this->o->_getAuths()); |
| 42 | |
| 43 | $globals->db->query("CREATE TABLE logsesh (auth enum('x', 'y', 'z', 'heh'))"); |
| 44 | $this->assertFalse($globals->db->err()); |
| 45 | |
| 46 | $globals->table_log_sessions = 'logsesh'; |
| 47 | $this->assertEquals(array(0 => __('all'), 'x' => 'x', 'y' => 'y', 'z' => 'z', 'heh' => 'heh'), $this->o->_getAuths()); |
| 48 | } |
| 49 | |
| 50 | function test_getDays() |
| 51 | { |
| 52 | global $globals; |
| 53 | |
| 54 | $rows[] = "DELETE FROM diogenes_logsessions"; |
| 55 | $rows[] = "INSERT INTO diogenes_logsessions (start) |
| 56 | VALUES (20040215000000)"; |
| 57 | $rows[] = "INSERT INTO diogenes_logsessions (start) |
| 58 | VALUES (20040315000000)"; |
| 59 | $rows[] = "INSERT INTO diogenes_logsessions (start) |
| 60 | VALUES (20040415000000)"; |
| 61 | BulkQueries($rows); |
| 62 | |
| 63 | $globals->table_log_sessions = 'diogenes_logsessions'; |
| 64 | |
| 65 | $febdays[0] = __('all'); |
| 66 | $mardays[0] = __('all'); |
| 67 | $aprdays[0] = __('all'); |
| 68 | |
| 69 | // Febdays should be 16 to 29 (leap year), mardays should be |
| 70 | // all month, and aprdays should be up to and including |
| 71 | // the 15th. |
| 72 | |
| 73 | for ($i = 1; $i < 16; $i++) |
| 74 | { |
| 75 | $aprdays[$i] = $mardays[$i] = $i; |
| 76 | } |
| 77 | |
| 78 | for ($i = 15; $i < 30; $i++) |
| 79 | { |
| 80 | $mardays[$i] = $febdays[$i] = $i; |
| 81 | } |
| 82 | |
| 83 | $mardays[30] = 30; |
| 84 | $mardays[31] = 31; |
| 85 | |
| 86 | $this->assertTrue(checkdate(2,29,2004), "Checkdate giving dodgy results"); |
| 87 | $this->assertEquals(array(), $this->o->_getDays(2003, 1)); |
| 88 | $this->assertEquals(array(), $this->o->_getDays(2003, 3)); |
| 89 | $this->assertEquals(array(), $this->o->_getDays(2003, 6)); |
| 90 | $this->assertEquals(array(), $this->o->_getDays(2004, 1)); |
| 91 | $this->assertEquals($febdays, $this->o->_getDays(2004, 2)); |
| 92 | $this->assertEquals($mardays, $this->o->_getDays(2004, 3)); |
| 93 | $this->assertEquals($aprdays, $this->o->_getDays(2004, 4)); |
| 94 | $this->assertEquals(array(), $this->o->_getDays(2004, 6)); |
| 95 | $this->assertEquals(array(), $this->o->_getDays(2005, 1)); |
| 96 | $this->assertEquals(array(), $this->o->_getDays(2005, 3)); |
| 97 | $this->assertEquals(array(), $this->o->_getDays(2005, 8)); |
| 98 | } |
| 99 | |
| 100 | function test_getMonths() |
| 101 | { |
| 102 | global $globals; |
| 103 | |
| 104 | $rows[] = "DELETE FROM diogenes_logsessions"; |
| 105 | $rows[] = "INSERT INTO diogenes_logsessions (start) |
| 106 | VALUES (20020401000000)"; |
| 107 | $rows[] = "INSERT INTO diogenes_logsessions (start) |
| 108 | VALUES (20030401000000)"; |
| 109 | $rows[] = "INSERT INTO diogenes_logsessions (start) |
| 110 | VALUES (20040401000000)"; |
| 111 | BulkQueries($rows); |
| 112 | |
| 113 | $this->assertEquals(array(), $this->o->_getMonths(2001)); |
| 114 | $this->assertEquals(array(0=>__('all'), 4=>4, 5=>5, 6=>6, 7=>7, 8=>8, |
| 115 | 9=>9, 10=>10, 11=>11, 12=>12), |
| 116 | $this->o->_getMonths(2002)); |
| 117 | $this->assertEquals(array(0=>__('all'), 1=>1, 2=>2, 3=>3, 4=>4, 5=>5, 6=>6, |
| 118 | 7=>7, 8=>8, 9=>9, 10=>10, 11=>11, 12=>12), |
| 119 | $this->o->_getMonths(2003)); |
| 120 | $this->assertEquals(array(0=>__('all'), 1=>1, 2=>2, 3=>3, 4=>4), |
| 121 | $this->o->_getMonths(2004)); |
| 122 | $this->assertEquals(array(), $this->o->_getMonths(2005)); |
| 123 | } |
| 124 | |
| 125 | function test_getYears() |
| 126 | { |
| 127 | global $globals; |
| 128 | |
| 129 | $rows[] = "DELETE FROM diogenes_logsessions"; |
| 130 | $rows[] = "INSERT INTO diogenes_logsessions (start) |
| 131 | VALUES (20020401000000)"; |
| 132 | $rows[] = "INSERT INTO diogenes_logsessions (start) |
| 133 | VALUES (20040401000000)"; |
| 134 | BulkQueries($rows); |
| 135 | |
| 136 | $this->assertEquals(array(0=>__('all'), 2002=>2002, 2003=>2003, 2004=>2004), |
| 137 | $this->o->_getYears()); |
| 138 | |
| 139 | } |
| 140 | } |