Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CleanTagsTest.php « Repair « lib « tests - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3f3ac0bf18d15182cbc2d0d508a0a3868972991a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?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;

use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IUserManager;
use OCP\Migration\IOutput;

/**
 * Tests for the cleaning the tags tables
 *
 * @group DB
 *
 * @see \OC\Repair\CleanTags
 */
class CleanTagsTest extends \Test\TestCase {

	/** @var \OC\Repair\CleanTags */
	protected $repair;

	/** @var \OCP\IDBConnection */
	protected $connection;

	/** @var \OCP\IUserManager|\PHPUnit\Framework\MockObject\MockObject */
	protected $userManager;

	/** @var int */
	protected $createdFile;

	/** @var IOutput */
	private $outputMock;

	protected function setUp(): void {
		parent::setUp();

		$this->outputMock = $this->getMockBuilder('\OCP\Migration\IOutput')
			->disableOriginalConstructor()
			->getMock();

		$this->userManager = $this->getMockBuilder(IUserManager::class)
			->disableOriginalConstructor()
			->getMock();

		$this->connection = \OC::$server->getDatabaseConnection();
		$this->repair = new \OC\Repair\CleanTags($this->connection, $this->userManager);
		$this->cleanUpTables();
	}

	protected function tearDown(): void {
		$this->cleanUpTables();

		parent::tearDown();
	}

	protected function cleanUpTables() {
		$qb = $this->connection->getQueryBuilder();
		$qb->delete('vcategory')
			->execute();

		$qb->delete('vcategory_to_object')
			->execute();

		$qb->delete('filecache')
			->execute();
	}

	public function testRun() {
		$cat1 = $this->addTagCategory('TestRepairCleanTags', 'files'); // Retained
		$cat2 = $this->addTagCategory('TestRepairCleanTags2', 'files'); // Deleted: Category will be empty
		$this->addTagCategory('TestRepairCleanTags3', 'files'); // Deleted: Category is empty
		$cat3 = $this->addTagCategory('TestRepairCleanTags', 'contacts'); // Retained

		$this->addTagEntry($this->getFileID(), $cat2, 'files'); // Retained
		$this->addTagEntry($this->getFileID() + 1, $cat1, 'files'); // Deleted: File is NULL
		$this->addTagEntry(9999999, $cat3, 'contacts'); // Retained
		$this->addTagEntry($this->getFileID(), $cat3 + 1, 'files'); // Deleted: Category is NULL

		$this->assertEntryCount('vcategory_to_object', 4, 'Assert tag entries count before repair step');
		$this->assertEntryCount('vcategory', 4, 'Assert tag categories count before repair step');

		self::invokePrivate($this->repair, 'deleteOrphanFileEntries', [$this->outputMock]);
		$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');

		self::invokePrivate($this->repair, 'deleteOrphanTagEntries', [$this->outputMock]);
		$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');

		self::invokePrivate($this->repair, 'deleteOrphanCategoryEntries', [$this->outputMock]);
		$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');


		$this->addTagCategory('TestRepairCleanTags', 'contacts', 'userExists'); // Retained
		$this->assertEntryCount('vcategory', 3, 'Assert tag categories count before cleaning categories by users');

		$this->userManager->expects($this->exactly(2))
			->method('userExists')
			->willReturnMap([
				['userExists', true],
				['TestRepairCleanTags', false],
			]);

		self::invokePrivate($this->repair, 'deleteOrphanTags', [$this->outputMock]);
		$this->assertEntryCount('vcategory', 1, 'Assert tag categories count after cleaning categories by users');
	}

	/**
	 * @param string $tableName
	 * @param int $expected
	 * @param string $message
	 */
	protected function assertEntryCount($tableName, $expected, $message = '') {
		$qb = $this->connection->getQueryBuilder();
		$result = $qb->select($qb->func()->count('*'))
			->from($tableName)
			->execute();

		$this->assertEquals($expected, $result->fetchOne(), $message);
	}

	/**
	 * Adds a new tag category to the database
	 *
	 * @param string $category
	 * @param string $type
	 * @param string $user
	 * @return int
	 */
	protected function addTagCategory($category, $type, $user = 'TestRepairCleanTags') {
		$qb = $this->connection->getQueryBuilder();
		$qb->insert('vcategory')
			->values([
				'uid' => $qb->createNamedParameter($user),
				'category' => $qb->createNamedParameter($category),
				'type' => $qb->createNamedParameter($type),
			])
			->execute();

		return (int) $this->getLastInsertID('vcategory', '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->getQueryBuilder();
		$qb->insert('vcategory_to_object')
			->values([
				'objid' => $qb->createNamedParameter($objectId, IQueryBuilder::PARAM_INT),
				'categoryid' => $qb->createNamedParameter($category, IQueryBuilder::PARAM_INT),
				'type' => $qb->createNamedParameter($type),
			])
			->execute();
	}

	/**
	 * Gets the last fileid from the file cache
	 * @return int
	 */
	protected function getFileID() {
		if ($this->createdFile) {
			return $this->createdFile;
		}

		$qb = $this->connection->getQueryBuilder();

		// We create a new file entry and delete it after the test again
		$fileName = $this->getUniqueID('TestRepairCleanTags', 12);
		$qb->insert('filecache')
			->values([
				'path' => $qb->createNamedParameter($fileName),
				'path_hash' => $qb->createNamedParameter(md5($fileName)),
			])
			->execute();
		$fileName = $this->getUniqueID('TestRepairCleanTags', 12);
		$qb->insert('filecache')
			->values([
				'path' => $qb->createNamedParameter($fileName),
				'path_hash' => $qb->createNamedParameter(md5($fileName)),
			])
			->execute();

		$this->createdFile = (int) $this->getLastInsertID('filecache', 'fileid');
		return $this->createdFile;
	}

	/**
	 * @param $tableName
	 * @param $idName
	 * @return int
	 */
	protected function getLastInsertID($tableName, $idName) {
		return $this->connection->lastInsertId("*PREFIX*$tableName");
	}
}