diff --git a/tests/data/db_structure.xml b/tests/data/db_structure.xml index 858c9ab100..371da94483 100644 --- a/tests/data/db_structure.xml +++ b/tests/data/db_structure.xml @@ -238,4 +238,59 @@ + + *dbprefix*uniconst + + + + id + integer + 0 + true + 1 + 4 + + + + + storage + integer + + true + 4 + + + + path_hash + text + + true + 32 + + + + etag + text + + false + 40 + + + + + true + + storage + ascending + + + path_hash + ascending + + + + + +
+ diff --git a/tests/lib/db.php b/tests/lib/db.php index aefbb3624e..73eef3a4d0 100644 --- a/tests/lib/db.php +++ b/tests/lib/db.php @@ -32,13 +32,18 @@ class Test_DB extends \Test\TestCase { */ private $table4; + /** + * @var string + */ + private $table5; + protected function setUp() { parent::setUp(); - $dbfile = OC::$SERVERROOT.'/tests/data/db_structure.xml'; + $dbFile = OC::$SERVERROOT.'/tests/data/db_structure.xml'; - $r = '_'.OC_Util::generateRandomBytes(4).'_'; - $content = file_get_contents( $dbfile ); + $r = $this->getUniqueID('_', 4).'_'; + $content = file_get_contents( $dbFile ); $content = str_replace( '*dbprefix*', '*dbprefix*'.$r, $content ); file_put_contents( self::$schema_file, $content ); OC_DB::createDbFromStructure(self::$schema_file); @@ -48,6 +53,7 @@ class Test_DB extends \Test\TestCase { $this->table2 = $this->test_prefix.'cntcts_cards'; $this->table3 = $this->test_prefix.'vcategory'; $this->table4 = $this->test_prefix.'decimal'; + $this->table5 = $this->test_prefix.'uniconst'; } protected function tearDown() { @@ -110,7 +116,7 @@ class Test_DB extends \Test\TestCase { } public function testinsertIfNotExist() { - $categoryentries = array( + $categoryEntries = array( array('user' => 'test', 'type' => 'contact', 'category' => 'Family', 'expectedResult' => 1), array('user' => 'test', 'type' => 'contact', 'category' => 'Friends', 'expectedResult' => 1), array('user' => 'test', 'type' => 'contact', 'category' => 'Coworkers', 'expectedResult' => 1), @@ -118,7 +124,7 @@ class Test_DB extends \Test\TestCase { array('user' => 'test', 'type' => 'contact', 'category' => 'School', 'expectedResult' => 1), ); - foreach($categoryentries as $entry) { + foreach($categoryEntries as $entry) { $result = OC_DB::insertIfNotExist('*PREFIX*'.$this->table3, array( 'uid' => $entry['user'], @@ -135,13 +141,13 @@ class Test_DB extends \Test\TestCase { } public function testInsertIfNotExistNull() { - $categoryentries = array( + $categoryEntries = array( array('addressbookid' => 123, 'fullname' => null, 'expectedResult' => 1), array('addressbookid' => 123, 'fullname' => null, 'expectedResult' => 0), array('addressbookid' => 123, 'fullname' => 'test', 'expectedResult' => 1), ); - foreach($categoryentries as $entry) { + foreach($categoryEntries as $entry) { $result = OC_DB::insertIfNotExist('*PREFIX*'.$this->table2, array( 'addressbookid' => $entry['addressbookid'], @@ -156,14 +162,14 @@ class Test_DB extends \Test\TestCase { $this->assertEquals(2, count($result->fetchAll())); } - public function testinsertIfNotExistDontOverwrite() { - $fullname = 'fullname test'; + public function testInsertIfNotExistDonTOverwrite() { + $fullName = 'fullname test'; $uri = 'uri_1'; $carddata = 'This is a vCard'; // Normal test to have same known data inserted. $query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`, `uri`, `carddata`) VALUES (?, ?, ?)'); - $result = $query->execute(array($fullname, $uri, $carddata)); + $result = $query->execute(array($fullName, $uri, $carddata)); $this->assertEquals(1, $result); $query = OC_DB::prepare('SELECT `fullname`, `uri`, `carddata` FROM `*PREFIX*'.$this->table2.'` WHERE `uri` = ?'); $result = $query->execute(array($uri)); @@ -176,7 +182,7 @@ class Test_DB extends \Test\TestCase { // Try to insert a new row $result = OC_DB::insertIfNotExist('*PREFIX*'.$this->table2, array( - 'fullname' => $fullname, + 'fullname' => $fullName, 'uri' => $uri, )); $this->assertEquals(0, $result); @@ -192,6 +198,25 @@ class Test_DB extends \Test\TestCase { $this->assertEquals($carddata, $rowset[0]['carddata']); } + public function testInsertIfNotExistsViolating() { + $result = OC_DB::insertIfNotExist('*PREFIX*'.$this->table5, + array( + 'storage' => 1, + 'path_hash' => md5('welcome.txt'), + 'etag' => $this->getUniqueID() + )); + $this->assertEquals(1, $result); + + $result = OC_DB::insertIfNotExist('*PREFIX*'.$this->table5, + array( + 'storage' => 1, + 'path_hash' => md5('welcome.txt'), + 'etag' => $this->getUniqueID() + ),['storage', 'path_hash']); + + $this->assertEquals(0, $result); + } + public function testUtf8Data() { $table = "*PREFIX*{$this->table2}"; $expected = "ะ‹รถ้›™ๅ–œ\xE2\x80\xA2";