aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/Repair/OldGroupMembershipSharesTest.php
blob: 4678bd174eb3df184395bc5594b51d702700e5b9 (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
<?php
/**
 * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace Test\Repair;

use OC\Repair\OldGroupMembershipShares;
use OCP\Migration\IOutput;
use OCP\Share\IShare;

/**
 * Class OldGroupMembershipSharesTest
 *
 * @group DB
 *
 * @package Test\Repair
 */
class OldGroupMembershipSharesTest extends \Test\TestCase {
	/** @var OldGroupMembershipShares */
	protected $repair;

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

	/** @var \OCP\IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
	protected $groupManager;

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

		/** \OCP\IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
		$this->groupManager = $this->getMockBuilder('OCP\IGroupManager')
			->disableOriginalConstructor()
			->getMock();
		$this->connection = \OC::$server->getDatabaseConnection();

		$this->deleteAllShares();
	}

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

		parent::tearDown();
	}

	protected function deleteAllShares() {
		$qb = $this->connection->getQueryBuilder();
		$qb->delete('share')->execute();
	}

	public function testRun() {
		$repair = new OldGroupMembershipShares(
			$this->connection,
			$this->groupManager
		);

		$this->groupManager->expects($this->exactly(2))
			->method('isInGroup')
			->willReturnMap([
				['member', 'group', true],
				['not-a-member', 'group', false],
			]);

		$parent = $this->createShare(IShare::TYPE_GROUP, 'group', null);
		$group2 = $this->createShare(IShare::TYPE_GROUP, 'group2', $parent);
		$user1 = $this->createShare(IShare::TYPE_USER, 'user1', $parent);

		// \OC\Share\Constant::$shareTypeGroupUserUnique === 2
		$member = $this->createShare(2, 'member', $parent);
		$notAMember = $this->createShare(2, 'not-a-member', $parent);

		$query = $this->connection->getQueryBuilder();
		$result = $query->select('id')
			->from('share')
			->orderBy('id', 'ASC')
			->execute();
		$rows = $result->fetchAll();
		$this->assertEquals([['id' => $parent], ['id' => $group2], ['id' => $user1], ['id' => $member], ['id' => $notAMember]], $rows);
		$result->closeCursor();

		/** @var IOutput | \PHPUnit\Framework\MockObject\MockObject $outputMock */
		$outputMock = $this->getMockBuilder('\OCP\Migration\IOutput')
			->disableOriginalConstructor()
			->getMock();

		$repair->run($outputMock);

		$query = $this->connection->getQueryBuilder();
		$result = $query->select('id')
			->from('share')
			->orderBy('id', 'ASC')
			->execute();
		$rows = $result->fetchAll();
		$this->assertEquals([['id' => $parent], ['id' => $group2], ['id' => $user1], ['id' => $member]], $rows);
		$result->closeCursor();
	}

	/**
	 * @param string $shareType
	 * @param string $shareWith
	 * @param null|int $parent
	 * @return int
	 */
	protected function createShare($shareType, $shareWith, $parent) {
		$qb = $this->connection->getQueryBuilder();
		$shareValues = [
			'share_type' => $qb->expr()->literal($shareType),
			'share_with' => $qb->expr()->literal($shareWith),
			'uid_owner' => $qb->expr()->literal('user1'),
			'item_type' => $qb->expr()->literal('folder'),
			'item_source' => $qb->expr()->literal(123),
			'item_target' => $qb->expr()->literal('/123'),
			'file_source' => $qb->expr()->literal(123),
			'file_target' => $qb->expr()->literal('/test'),
			'permissions' => $qb->expr()->literal(1),
			'stime' => $qb->expr()->literal(time()),
			'expiration' => $qb->expr()->literal('2015-09-25 00:00:00'),
		];

		if ($parent) {
			$shareValues['parent'] = $qb->expr()->literal($parent);
		}

		$qb = $this->connection->getQueryBuilder();
		$qb->insert('share')
			->values($shareValues)
			->execute();

		return $this->connection->lastInsertId('*PREFIX*share');
	}
}