diff options
author | diosmosis <diosmosis@users.noreply.github.com> | 2020-09-11 04:19:41 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-11 04:19:41 +0300 |
commit | 6b70e8cc7a12e596bfa08e35db52531ce989c2f2 (patch) | |
tree | 151cda62467fb0c5266adb6e901c3bd50499fb2b /tests | |
parent | d01085cec58a437e30b0dd4b149c52fc3e19abaa (diff) |
Add methods to remove invalidations. (#16400)
* Add methods to remove invalidations.
* Add check if site exists to beginning of queue consumers loop to find archive data
* Delete invalidations w/ no site in task and for site when deleting.
* fix test
* fix test
* another test fix
Diffstat (limited to 'tests')
-rw-r--r-- | tests/PHPUnit/Integration/DataAccess/ArchiveInvalidatorTest.php | 92 | ||||
-rw-r--r-- | tests/PHPUnit/Integration/DataAccess/ModelTest.php | 23 |
2 files changed, 114 insertions, 1 deletions
diff --git a/tests/PHPUnit/Integration/DataAccess/ArchiveInvalidatorTest.php b/tests/PHPUnit/Integration/DataAccess/ArchiveInvalidatorTest.php index e49a2c9699..506311287e 100644 --- a/tests/PHPUnit/Integration/DataAccess/ArchiveInvalidatorTest.php +++ b/tests/PHPUnit/Integration/DataAccess/ArchiveInvalidatorTest.php @@ -82,6 +82,82 @@ class ArchiveInvalidatorTest extends IntegrationTestCase $this->invalidator = new ArchiveInvalidator(new Model(), StaticContainer::get(ArchivingStatus::class)); } + public function test_removeInvalidations_removesAllForPlugin() + { + $this->insertInvalidations([ + ['name' => 'done.MyPlugin', 'idsite' => 1, 'date1' => '2012-03-04', 'date2' => '2015-03-04', 'period' => 1, 'report' => 'myReport'], + ['name' => 'done.MyPlugin', 'idsite' => 1, 'date1' => '2012-03-04', 'date2' => '2015-03-04', 'period' => 1, 'report' => null], + ['name' => 'done.MyOtherPlugin', 'idsite' => 1, 'date1' => '2012-03-04', 'date2' => '2015-05-05', 'period' => 1, 'report' => ''], + ['name' => 'done', 'idsite' => 1, 'date1' => '2012-03-04', 'date2' => '2015-05-05', 'period' => 1, 'report' => ''], + ['name' => 'done.MyPlugin', 'idsite' => 1, 'date1' => '2012-03-04', 'date2' => '2015-03-04', 'period' => 1, 'report' => 'myOtherReport'], + ]); + + $this->invalidator->removeInvalidations($idSite = 1, 'MyPlugin'); + + $invalidations = $this->getInvalidatedArchiveTableEntries(); + $expectedInvalidations = [ + ['idarchive' => null, 'idsite' => 1, 'date1' => '2012-03-04', 'date2' => '2015-05-05', 'period' => 1, 'name' => 'done.MyOtherPlugin', 'report' => null], + ['idarchive' => null, 'idsite' => 1, 'date1' => '2012-03-04', 'date2' => '2015-05-05', 'period' => 1, 'name' => 'done', 'report' => null], + ]; + + $this->assertEquals($expectedInvalidations, $invalidations); + } + + public function test_removeInvalidations_removesAllForSingleReport() + { + $this->insertInvalidations([ + ['name' => 'done.MyPlugin', 'idsite' => 1, 'date1' => '2012-03-04', 'date2' => '2015-03-04', 'period' => 1, 'report' => 'myReport'], + ['name' => 'done.MyPlugin', 'idsite' => 1, 'date1' => '2012-03-04', 'date2' => '2015-03-04', 'period' => 1, 'report' => null], + ['name' => 'done.MyOtherPlugin', 'idsite' => 1, 'date1' => '2012-03-04', 'date2' => '2015-05-05', 'period' => 1, 'report' => ''], + ['name' => 'done', 'idsite' => 1, 'date1' => '2012-03-04', 'date2' => '2015-05-05', 'period' => 1, 'report' => ''], + ['name' => 'done.MyPlugin', 'idsite' => 1, 'date1' => '2012-03-04', 'date2' => '2015-03-04', 'period' => 1, 'report' => 'myOtherReport'], + ]); + + $this->invalidator->removeInvalidations($idSite = 1, 'MyPlugin', 'myReport'); + + $invalidations = $this->getInvalidatedArchiveTableEntries(); + $expectedInvalidations = [ + [ + 'idarchive' => NULL, + 'idsite' => '1', + 'date1' => '2012-03-04', + 'date2' => '2015-03-04', + 'period' => '1', + 'name' => 'done.MyPlugin', + 'report' => NULL, + ], + [ + 'idarchive' => NULL, + 'idsite' => '1', + 'date1' => '2012-03-04', + 'date2' => '2015-05-05', + 'period' => '1', + 'name' => 'done.MyOtherPlugin', + 'report' => '', + ], + [ + 'idarchive' => NULL, + 'idsite' => '1', + 'date1' => '2012-03-04', + 'date2' => '2015-05-05', + 'period' => '1', + 'name' => 'done', + 'report' => '', + ], + [ + 'idarchive' => NULL, + 'idsite' => '1', + 'date1' => '2012-03-04', + 'date2' => '2015-03-04', + 'period' => '1', + 'name' => 'done.MyPlugin', + 'report' => 'myOtherReport', + ], + ]; + + $this->assertEquals($expectedInvalidations, $invalidations); + } + public function test_rememberToInvalidateArchivedReportsLater_shouldCreateAnEntryInCaseThereIsNoneYet() { //Updated for change to allow for multiple transactions to invalidate the same report without deadlock. @@ -1334,4 +1410,20 @@ class ArchiveInvalidatorTest extends IntegrationTestCase parent::configureFixture($fixture); $fixture->createSuperUser = true; } + + private function insertInvalidations(array $invalidations) + { + $table = Common::prefixTable('archive_invalidations'); + foreach ($invalidations as $invalidation) { + $sql = "INSERT INTO $table (name, idsite, date1, date2, period, ts_invalidated, report) VALUES (?, ?, ?, ?, ?, NOW(), ?)"; + Db::query($sql, [ + $invalidation['name'], + $invalidation['idsite'], + $invalidation['date1'], + $invalidation['date2'], + $invalidation['period'], + $invalidation['report'], + ]); + } + } } diff --git a/tests/PHPUnit/Integration/DataAccess/ModelTest.php b/tests/PHPUnit/Integration/DataAccess/ModelTest.php index 42f67fea92..532b0e48f7 100644 --- a/tests/PHPUnit/Integration/DataAccess/ModelTest.php +++ b/tests/PHPUnit/Integration/DataAccess/ModelTest.php @@ -14,6 +14,7 @@ use Piwik\DataAccess\ArchiveWriter; use Piwik\Date; use Piwik\Db; use Piwik\Period\Factory; +use Piwik\Tests\Framework\Fixture; use Piwik\Tests\Framework\TestCase\IntegrationTestCase; use Piwik\DataAccess\Model; @@ -482,6 +483,26 @@ class ModelTest extends IntegrationTestCase $this->assertEquals($expected, $actual); } + public function test_deleteInvalidationsForDeletedSites() + { + Fixture::createWebsite('2014-01-01 00:00:00'); + + $this->insertInvalidations([ + ['idsite' => 1, 'date1' => '2014-02-03', 'date2' => '2014-02-03', 'period' => 1, 'name' => 'done'], + ['idsite' => 2, 'date1' => '2014-02-01', 'date2' => '2014-02-28', 'period' => 2, 'name' => 'done'], + ['idsite' => 2, 'date1' => '2014-02-01', 'date2' => '2014-02-01', 'period' => 1, 'name' => 'done'], + ['idsite' => 3, 'date1' => '2014-02-01', 'date2' => '2014-02-01', 'period' => 1, 'name' => 'done'], + ]); + + $this->model->deleteInvalidationsForDeletedSites(); + + $invalidations = Db::fetchAll("SELECT idsite, idinvalidation FROM " . Common::prefixTable('archive_invalidations') . + " ORDER BY idinvalidation ASC"); + $this->assertEquals([ + ['idsite' => 1, 'idinvalidation' => 1], + ], $invalidations); + } + private function insertArchiveData($archivesToInsert) { $idarchive = 1; @@ -499,7 +520,7 @@ class ModelTest extends IntegrationTestCase $table = Common::prefixTable('archive_invalidations'); foreach ($invalidations as $invalidation) { $sql = "INSERT INTO `$table` (idsite, date1, date2, period, `name`) VALUES (?, ?, ?, ?, ?)"; - Db::query($sql, [1, $invalidation['date1'], $invalidation['date2'], $invalidation['period'], $invalidation['name']]); + Db::query($sql, [$invalidation['idsite'] ?? 1, $invalidation['date1'], $invalidation['date2'], $invalidation['period'], $invalidation['name']]); } } } |