diff --git a/tests/data/db_structure.xml b/tests/data/db_structure.xml index 8f6dc5e2ecd539b9571ec8a66da28563c97208e7..2e83bbb78c7577d3aef0c5b6039f12598ef36073 100644 --- a/tests/data/db_structure.xml +++ b/tests/data/db_structure.xml @@ -178,4 +178,25 @@ </declaration> </table> + <table> + <name>*dbprefix*timestamp</name> + <declaration> + <field> + <name>id</name> + <autoincrement>1</autoincrement> + <type>integer</type> + <default>0</default> + <notnull>true</notnull> + <length>4</length> + </field> + + <field> + <name>timestamptest</name> + <type>timestamp</type> + <default></default> + <notnull>false</notnull> + </field> + </declaration> + </table> + </database> diff --git a/tests/data/db_structure2.xml b/tests/data/db_structure2.xml index 6f12f81f477d94cb7a118288d9a41c0285f0ccdc..bbfb24985cb1c67c4628f4b10448b0df35bc56d9 100644 --- a/tests/data/db_structure2.xml +++ b/tests/data/db_structure2.xml @@ -75,4 +75,25 @@ </table> + <table> + <name>*dbprefix*timestamp</name> + <declaration> + <field> + <name>id</name> + <autoincrement>1</autoincrement> + <type>integer</type> + <default>0</default> + <notnull>true</notnull> + <length>4</length> + </field> + + <field> + <name>timestamptest</name> + <type>timestamp</type> + <default></default> + <notnull>false</notnull> + </field> + </declaration> + </table> + </database> diff --git a/tests/lib/db.php b/tests/lib/db.php index 1977025cf12def4e8ed25f8b916a48112891b21d..befb52ee19803c670a357d014a63137510b67362 100644 --- a/tests/lib/db.php +++ b/tests/lib/db.php @@ -145,4 +145,42 @@ class Test_DB extends PHPUnit_Framework_TestCase { $this->assertEquals(1, $result->numRows()); } + + /** + * Tests whether the database is configured so it accepts and returns dates + * in the expected format. + */ + public function testTimestampDateFormat() { + $table = '*PREFIX*'.$this->test_prefix.'timestamp'; + $column = 'timestamptest'; + + $expectedFormat = 'Y-m-d H:i:s'; + $now = new \DateTime; + + $query = OC_DB::prepare("INSERT INTO `$table` (`$column`) VALUES (?)"); + $result = $query->execute(array($now->format($expectedFormat))); + $this->assertEquals( + 1, + $result, + "Database failed to accept dates in the format '$expectedFormat'." + ); + + $id = OC_DB::insertid($table); + $query = OC_DB::prepare("SELECT * FROM `$table` WHERE `id` = ?"); + $result = $query->execute(array($id)); + $row = $result->fetchRow(); + + $dateFromDb = \DateTime::createFromFormat($expectedFormat, $row[$column]); + $this->assertInstanceOf( + '\DateTime', + $dateFromDb, + "Database failed to return dates in the format '$expectedFormat'." + ); + + $this->assertEquals( + $now->format('c u'), + $dateFromDb->format('c u'), + 'Failed asserting that the returned date is the same as the inserted.' + ); + } }