diff options
author | Morris Jobke <hey@morrisjobke.de> | 2015-01-14 20:43:23 +0100 |
---|---|---|
committer | Morris Jobke <hey@morrisjobke.de> | 2015-01-14 20:43:23 +0100 |
commit | 64d7463ca369ccf7b141e6699f8973781e531bd4 (patch) | |
tree | 60ebffd5a98d57bb17931194129697ff4e19cf7f /tests | |
parent | 34d6a1596caa50edefc084e01a1f6b6bd757937a (diff) | |
parent | a22068cce3c0681e51e02965a03e2372518cd172 (diff) | |
download | nextcloud-server-64d7463ca369ccf7b141e6699f8973781e531bd4.tar.gz nextcloud-server-64d7463ca369ccf7b141e6699f8973781e531bd4.zip |
Merge pull request #13344 from owncloud/issue/13334-cleanup-tags-repairstep
Add a repair step to clean up orphan tags and tag entries
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/repair/cleantags.php | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/tests/lib/repair/cleantags.php b/tests/lib/repair/cleantags.php new file mode 100644 index 00000000000..29a1a8b432e --- /dev/null +++ b/tests/lib/repair/cleantags.php @@ -0,0 +1,143 @@ +<?php +/** + * Copyright (c) 2015 Joas Schilling <nickvergessen@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Repair; + +/** + * Tests for the cleaning the tags tables + * + * @see \OC\Repair\CleanTags + */ +class CleanTags extends \Test\TestCase { + + /** @var \OC\RepairStep */ + private $repair; + + /** @var \Doctrine\DBAL\Connection */ + private $connection; + + /** @var array */ + protected $tagCategories; + + /** @var int */ + protected $createdFile; + + protected function setUp() { + parent::setUp(); + + $this->connection = \OC::$server->getDatabaseConnection(); + $this->repair = new \OC\Repair\CleanTags($this->connection); + } + + protected function tearDown() { + $qb = $this->connection->createQueryBuilder(); + $qb->delete('*PREFIX*vcategory') + ->where('uid = ' . $qb->createNamedParameter('TestRepairCleanTags')) + ->execute(); + + $qb->delete('*PREFIX*vcategory_to_object') + ->where($qb->expr()->in('categoryid', ':ids')); + $qb->setParameter('ids', $this->tagCategories, \Doctrine\DBAL\Connection::PARAM_INT_ARRAY); + $qb->execute(); + + $qb->delete('*PREFIX*filecache') + ->where('fileid = ' . $qb->createNamedParameter($this->createdFile, \PDO::PARAM_INT)) + ->execute(); + + parent::tearDown(); + } + + public function testRun() { + $cat1 = $this->addTagCategory('TestRepairCleanTags', 'files'); // Retained + $cat2 = $this->addTagCategory('TestRepairCleanTags2', 'files'); // Deleted: Category is empty + $cat3 = $this->addTagCategory('TestRepairCleanTags', 'contacts'); // Retained + $file = $this->getFileID(); + + $this->addTagEntry($file, $cat2, 'files'); // Retained + $this->addTagEntry($file + 1, $cat1, 'files'); // Deleted: File is NULL + $this->addTagEntry(9999999, $cat3, 'contacts'); // Retained + $this->addTagEntry($file, $cat3 + 1, 'files'); // Deleted: Category is NULL + + $this->assertEntryCount('*PREFIX*vcategory', 3, 'Assert tag categories count before repair step'); + $this->assertEntryCount('*PREFIX*vcategory_to_object', 4, 'Assert tag entries count before repair step'); + $this->repair->run(); + $this->assertEntryCount('*PREFIX*vcategory', 2, 'Assert tag categories count after repair step'); + $this->assertEntryCount('*PREFIX*vcategory_to_object', 2, 'Assert tag entries count after repair step'); + } + + /** + * @param string $tableName + * @param int $expected + * @param string $message + */ + protected function assertEntryCount($tableName, $expected, $message = '') { + $qb = $this->connection->createQueryBuilder(); + $result = $qb->select('COUNT(*)') + ->from($tableName) + ->execute(); + + $this->assertEquals($expected, $result->fetchColumn(), $message); + } + + /** + * Adds a new tag category to the database + * + * @param string $category + * @param string $type + * @return int + */ + protected function addTagCategory($category, $type) { + $qb = $this->connection->createQueryBuilder(); + $qb->insert('*PREFIX*vcategory') + ->values([ + 'uid' => $qb->createNamedParameter('TestRepairCleanTags'), + 'category' => $qb->createNamedParameter($category), + 'type' => $qb->createNamedParameter($type), + ]) + ->execute(); + + $id = (int) $this->connection->lastInsertId(); + $this->tagCategories[] = $id; + return $id; + } + + /** + * Adds a new tag entry to the database + * @param int $objectId + * @param int $category + * @param string $type + */ + protected function addTagEntry($objectId, $category, $type) { + $qb = $this->connection->createQueryBuilder(); + $qb->insert('*PREFIX*vcategory_to_object') + ->values([ + 'objid' => $qb->createNamedParameter($objectId, \PDO::PARAM_INT), + 'categoryid' => $qb->createNamedParameter($category, \PDO::PARAM_INT), + 'type' => $qb->createNamedParameter($type), + ]) + ->execute(); + } + + /** + * Gets the last fileid from the file cache + * + * @return int + */ + protected function getFileID() { + $qb = $this->connection->createQueryBuilder(); + + // We create a new file entry and delete it after the test again + $qb->insert('*PREFIX*filecache') + ->values([ + 'path' => $qb->createNamedParameter('TestRepairCleanTags'), + ]) + ->execute(); + $this->createdFile = (int) $this->connection->lastInsertId(); + return $this->createdFile; + } +} |