diff options
author | Thomas Steur <tsteur@users.noreply.github.com> | 2019-12-30 23:23:15 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-12-30 23:23:15 +0300 |
commit | 61317356e1eeeeac8361c97cb7dab778d9688c00 (patch) | |
tree | e77124b485e6598f2e18d5be227c36ded90415d0 /tests | |
parent | 520ba4881ab9d3e30c3d16da9e32a74633e6653c (diff) |
Prevent race condition when saving plugin settings (#15327)
* Prevent race condition when saving plugin settings
* use delete method
Diffstat (limited to 'tests')
-rw-r--r-- | tests/PHPUnit/Integration/Tracker/DbTest.php | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/tests/PHPUnit/Integration/Tracker/DbTest.php b/tests/PHPUnit/Integration/Tracker/DbTest.php index 3fc246e60f..b72ffc5191 100644 --- a/tests/PHPUnit/Integration/Tracker/DbTest.php +++ b/tests/PHPUnit/Integration/Tracker/DbTest.php @@ -21,6 +21,13 @@ use Piwik\Tracker; */ class DbTest extends IntegrationTestCase { + private $tableName; + public function setUp() + { + parent::setUp(); + $this->tableName = Common::prefixTable('option'); + } + public function test_rowCount_whenUpdating_returnsAllMatchedRowsNotOnlyUpdatedRows() { $db = Tracker::getDatabase(); @@ -43,4 +50,109 @@ class DbTest extends IntegrationTestCase $result = $db->query($sqlUpdate . " WHERE option_name = 'rowid'"); $this->assertSame(1, $db->rowCount($result)); } + + public function test_rowCount_whenInserting() + { + $db = Tracker::getDatabase(); + // insert one record + $result = $this->insertRowId(); + + $this->assertSame(1, $db->rowCount($result)); + } + + /** + * @expectedExceptionMessage doesn't exist + * @expectedException \Piwik\Tracker\Db\DbException + */ + public function test_fetchOne_notExistingTable() + { + $db = Tracker::getDatabase(); + $this->insertRowId(3); + $val = $db->fetchOne('SELECT option_value FROM foobarbaz where option_value = "rowid"'); + $this->assertEquals('3', $val); + } + + /** + * @expectedExceptionMessage Duplicate entry + * @expectedException \Piwik\Tracker\Db\DbException + */ + public function test_query_error_whenInsertingDuplicateRow() + { + $this->insertRowId(); + $this->insertRowId(); + } + + public function test_fetchOne() + { + $db = Tracker::getDatabase(); + $this->insertRowId(3); + $val = $db->fetchOne('SELECT option_value FROM `' . $this->tableName . '` where option_name = "rowid"'); + $this->assertEquals('3', $val); + } + + public function test_fetchOne_noMatch() + { + $db = Tracker::getDatabase(); + $val = $db->fetchOne('SELECT option_value from `' . $this->tableName . '` where option_name = "foobar"'); + $this->assertFalse($val); + } + + public function test_fetchRow() + { + $db = Tracker::getDatabase(); + $this->insertRowId(3); + $val = $db->fetchRow('SELECT option_value from `' . $this->tableName . '` where option_name = "rowid"'); + $this->assertEquals(array( + 'option_value' => '3' + ), $val); + } + + public function test_fetchRow_noMatch() + { + $db = Tracker::getDatabase(); + $val = $db->fetchRow('SELECT option_value from `' . $this->tableName . '` where option_name = "foobar"'); + $this->assertNull($val); + } + + public function test_fetch() + { + $db = Tracker::getDatabase(); + $this->insertRowId(3); + $val = $db->fetch('SELECT option_value from `' . $this->tableName . '` where option_name = "rowid"'); + $this->assertEquals(array( + 'option_value' => '3' + ), $val); + } + + public function test_fetch_noMatch() + { + $db = Tracker::getDatabase(); + $val = $db->fetch('SELECT option_value from `' . $this->tableName . '` where option_name = "foobar"'); + $this->assertNull($val); + } + + public function test_fetchAll() + { + $db = Tracker::getDatabase(); + $this->insertRowId(3); + $val = $db->fetchAll('SELECT option_value from `' . $this->tableName . '` where option_name = "rowid"'); + $this->assertEquals(array( + array( + 'option_value' => '3' + ) + ), $val); + } + + public function test_fetchAll_noMatch() + { + $db = Tracker::getDatabase(); + $val = $db->fetchAll('SELECT option_value from `' . $this->tableName . '` where option_name = "foobar"'); + $this->assertEquals(array(), $val); + } + + private function insertRowId($value = '1') + { + $db = Tracker::getDatabase(); + return $db->query("INSERT INTO `" . $this->tableName . "` VALUES ('rowid', '$value', false)"); + } } |