2015-01-14 13:57:26 +03:00
|
|
|
<?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;
|
2016-02-29 11:44:40 +03:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2016-04-22 16:35:39 +03:00
|
|
|
use OCP\Migration\IOutput;
|
2015-01-14 13:57:26 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests for the cleaning the tags tables
|
|
|
|
*
|
2015-11-03 03:52:41 +03:00
|
|
|
* @group DB
|
|
|
|
*
|
2015-01-14 13:57:26 +03:00
|
|
|
* @see \OC\Repair\CleanTags
|
|
|
|
*/
|
2016-05-20 16:38:20 +03:00
|
|
|
class CleanTagsTest extends \Test\TestCase {
|
2015-01-14 13:57:26 +03:00
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
/** @var \OC\Repair\CleanTags */
|
2015-01-15 12:25:54 +03:00
|
|
|
protected $repair;
|
2015-01-14 13:57:26 +03:00
|
|
|
|
2015-07-06 13:34:19 +03:00
|
|
|
/** @var \OCP\IDBConnection */
|
2015-01-15 12:25:54 +03:00
|
|
|
protected $connection;
|
2015-01-14 13:57:26 +03:00
|
|
|
|
|
|
|
/** @var int */
|
|
|
|
protected $createdFile;
|
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
/** @var IOutput */
|
|
|
|
private $outputMock;
|
|
|
|
|
2015-01-14 13:57:26 +03:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
$this->outputMock = $this->getMockBuilder('\OCP\Migration\IOutput')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2015-01-14 13:57:26 +03:00
|
|
|
$this->connection = \OC::$server->getDatabaseConnection();
|
|
|
|
$this->repair = new \OC\Repair\CleanTags($this->connection);
|
2015-01-15 12:25:54 +03:00
|
|
|
$this->cleanUpTables();
|
2015-01-14 13:57:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown() {
|
2015-01-15 12:25:54 +03:00
|
|
|
$this->cleanUpTables();
|
|
|
|
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function cleanUpTables() {
|
2015-07-06 13:34:19 +03:00
|
|
|
$qb = $this->connection->getQueryBuilder();
|
2015-08-10 17:21:41 +03:00
|
|
|
$qb->delete('vcategory')
|
2015-01-14 13:57:26 +03:00
|
|
|
->execute();
|
|
|
|
|
2015-08-10 17:21:41 +03:00
|
|
|
$qb->delete('vcategory_to_object')
|
2015-01-14 13:57:26 +03:00
|
|
|
->execute();
|
|
|
|
|
2015-08-10 17:21:41 +03:00
|
|
|
$qb->delete('filecache')
|
2015-01-15 12:25:54 +03:00
|
|
|
->execute();
|
2015-01-14 13:57:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRun() {
|
|
|
|
$cat1 = $this->addTagCategory('TestRepairCleanTags', 'files'); // Retained
|
2015-01-15 12:25:54 +03:00
|
|
|
$cat2 = $this->addTagCategory('TestRepairCleanTags2', 'files'); // Deleted: Category will be empty
|
|
|
|
$this->addTagCategory('TestRepairCleanTags3', 'files'); // Deleted: Category is empty
|
2015-01-14 13:57:26 +03:00
|
|
|
$cat3 = $this->addTagCategory('TestRepairCleanTags', 'contacts'); // Retained
|
|
|
|
|
2015-01-15 12:25:54 +03:00
|
|
|
$this->addTagEntry($this->getFileID(), $cat2, 'files'); // Retained
|
|
|
|
$this->addTagEntry($this->getFileID() + 1, $cat1, 'files'); // Deleted: File is NULL
|
2015-01-14 13:57:26 +03:00
|
|
|
$this->addTagEntry(9999999, $cat3, 'contacts'); // Retained
|
2015-01-15 12:25:54 +03:00
|
|
|
$this->addTagEntry($this->getFileID(), $cat3 + 1, 'files'); // Deleted: Category is NULL
|
2015-01-14 13:57:26 +03:00
|
|
|
|
2015-08-10 17:21:41 +03:00
|
|
|
$this->assertEntryCount('vcategory_to_object', 4, 'Assert tag entries count before repair step');
|
|
|
|
$this->assertEntryCount('vcategory', 4, 'Assert tag categories count before repair step');
|
2015-01-15 12:25:54 +03:00
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
self::invokePrivate($this->repair, 'deleteOrphanFileEntries', [$this->outputMock]);
|
2015-08-10 17:21:41 +03:00
|
|
|
$this->assertEntryCount('vcategory_to_object', 3, 'Assert tag entries count after cleaning file entries');
|
|
|
|
$this->assertEntryCount('vcategory', 4, 'Assert tag categories count after cleaning file entries');
|
2015-01-15 12:25:54 +03:00
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
self::invokePrivate($this->repair, 'deleteOrphanTagEntries', [$this->outputMock]);
|
2015-08-10 17:21:41 +03:00
|
|
|
$this->assertEntryCount('vcategory_to_object', 2, 'Assert tag entries count after cleaning tag entries');
|
|
|
|
$this->assertEntryCount('vcategory', 4, 'Assert tag categories count after cleaning tag entries');
|
2015-01-15 12:25:54 +03:00
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
self::invokePrivate($this->repair, 'deleteOrphanCategoryEntries', [$this->outputMock]);
|
2015-08-10 17:21:41 +03:00
|
|
|
$this->assertEntryCount('vcategory_to_object', 2, 'Assert tag entries count after cleaning category entries');
|
|
|
|
$this->assertEntryCount('vcategory', 2, 'Assert tag categories count after cleaning category entries');
|
2015-01-14 13:57:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $tableName
|
|
|
|
* @param int $expected
|
|
|
|
* @param string $message
|
|
|
|
*/
|
|
|
|
protected function assertEntryCount($tableName, $expected, $message = '') {
|
2015-07-06 13:34:19 +03:00
|
|
|
$qb = $this->connection->getQueryBuilder();
|
2015-07-07 23:08:21 +03:00
|
|
|
$result = $qb->select($qb->createFunction('COUNT(*)'))
|
|
|
|
->from($tableName)
|
2015-01-14 13:57:26 +03:00
|
|
|
->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) {
|
2015-07-06 13:34:19 +03:00
|
|
|
$qb = $this->connection->getQueryBuilder();
|
2015-08-10 17:21:41 +03:00
|
|
|
$qb->insert('vcategory')
|
2015-01-14 13:57:26 +03:00
|
|
|
->values([
|
2015-07-07 23:08:21 +03:00
|
|
|
'uid' => $qb->createNamedParameter('TestRepairCleanTags'),
|
|
|
|
'category' => $qb->createNamedParameter($category),
|
|
|
|
'type' => $qb->createNamedParameter($type),
|
2015-01-14 13:57:26 +03:00
|
|
|
])
|
|
|
|
->execute();
|
|
|
|
|
2015-08-10 17:21:41 +03:00
|
|
|
return (int) $this->getLastInsertID('vcategory', 'id');
|
2015-01-14 13:57:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a new tag entry to the database
|
|
|
|
* @param int $objectId
|
|
|
|
* @param int $category
|
|
|
|
* @param string $type
|
|
|
|
*/
|
|
|
|
protected function addTagEntry($objectId, $category, $type) {
|
2015-07-06 13:34:19 +03:00
|
|
|
$qb = $this->connection->getQueryBuilder();
|
2015-08-10 17:21:41 +03:00
|
|
|
$qb->insert('vcategory_to_object')
|
2015-01-14 13:57:26 +03:00
|
|
|
->values([
|
2016-02-29 11:44:40 +03:00
|
|
|
'objid' => $qb->createNamedParameter($objectId, IQueryBuilder::PARAM_INT),
|
|
|
|
'categoryid' => $qb->createNamedParameter($category, IQueryBuilder::PARAM_INT),
|
2015-07-07 23:08:21 +03:00
|
|
|
'type' => $qb->createNamedParameter($type),
|
2015-01-14 13:57:26 +03:00
|
|
|
])
|
|
|
|
->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the last fileid from the file cache
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
protected function getFileID() {
|
2015-01-15 12:25:54 +03:00
|
|
|
if ($this->createdFile) {
|
|
|
|
return $this->createdFile;
|
|
|
|
}
|
|
|
|
|
2015-07-06 13:34:19 +03:00
|
|
|
$qb = $this->connection->getQueryBuilder();
|
2015-01-14 13:57:26 +03:00
|
|
|
|
|
|
|
// We create a new file entry and delete it after the test again
|
2015-01-15 12:25:54 +03:00
|
|
|
$fileName = $this->getUniqueID('TestRepairCleanTags', 12);
|
2015-08-10 17:21:41 +03:00
|
|
|
$qb->insert('filecache')
|
2015-01-14 13:57:26 +03:00
|
|
|
->values([
|
2015-07-07 23:08:21 +03:00
|
|
|
'path' => $qb->createNamedParameter($fileName),
|
|
|
|
'path_hash' => $qb->createNamedParameter(md5($fileName)),
|
2015-01-14 13:57:26 +03:00
|
|
|
])
|
|
|
|
->execute();
|
2015-01-15 12:25:54 +03:00
|
|
|
$fileName = $this->getUniqueID('TestRepairCleanTags', 12);
|
2015-08-10 17:21:41 +03:00
|
|
|
$qb->insert('filecache')
|
2015-01-15 12:25:54 +03:00
|
|
|
->values([
|
2015-07-07 23:08:21 +03:00
|
|
|
'path' => $qb->createNamedParameter($fileName),
|
|
|
|
'path_hash' => $qb->createNamedParameter(md5($fileName)),
|
2015-01-15 12:25:54 +03:00
|
|
|
])
|
|
|
|
->execute();
|
|
|
|
|
2015-08-10 17:21:41 +03:00
|
|
|
$this->createdFile = (int) $this->getLastInsertID('filecache', 'fileid');
|
2015-01-14 13:57:26 +03:00
|
|
|
return $this->createdFile;
|
|
|
|
}
|
2015-01-15 12:25:54 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $tableName
|
|
|
|
* @param $idName
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
protected function getLastInsertID($tableName, $idName) {
|
2015-11-19 19:18:22 +03:00
|
|
|
return $this->connection->lastInsertId("*PREFIX*$tableName");
|
2015-01-15 12:25:54 +03:00
|
|
|
}
|
2015-01-14 13:57:26 +03:00
|
|
|
}
|