summaryrefslogtreecommitdiffstats
path: root/apps/files_sharing/tests/Migration/SetPasswordColumnTest.php
blob: 0a1737ba37fbccd6e44608a6487e212f87df2305 (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
<?php
/**
 * @copyright Copyright (c) 2016, ownCloud, Inc.
 *
 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
 * @author Joas Schilling <coding@schilljs.com>
 * @author Roeland Jago Douma <roeland@famdouma.nl>
 *
 * @license AGPL-3.0
 *
 * This code is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License, version 3,
 * along with this program. If not, see <http://www.gnu.org/licenses/>
 *
 */

namespace OCA\Files_Sharing\Tests\Migration;

use OCA\Files_Sharing\Migration\SetPasswordColumn;
use OCA\Files_Sharing\Tests\TestCase;
use OCP\IConfig;
use OCP\Migration\IOutput;
use OCP\Share;

/**
 * Class SetPasswordColumnTest
 *
 * @group DB
 */
class SetPasswordColumnTest extends TestCase {

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

	/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
	private $config;

	/** @var SetPasswordColumn */
	private $migration;

	private $table = 'share';

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

		$this->connection = \OC::$server->getDatabaseConnection();
		$this->config = $this->createMock(IConfig::class);
		$this->migration = new SetPasswordColumn($this->connection, $this->config);

		$this->cleanDB();
	}

	protected function tearDown(): void {
		parent::tearDown();
		$this->cleanDB();
	}

	private function cleanDB() {
		$query = $this->connection->getQueryBuilder();
		$query->delete($this->table)->execute();
	}

	public function testAddPasswordColumn() {
		$this->config->expects($this->once())
			->method('getAppValue')
			->with('files_sharing', 'installed_version', '0.0.0')
			->willReturn('1.3.0');

		$shareTypes = [Share::SHARE_TYPE_USER, Share::SHARE_TYPE_GROUP, Share::SHARE_TYPE_REMOTE, Share::SHARE_TYPE_EMAIL, Share::SHARE_TYPE_LINK];

		foreach ($shareTypes as $shareType) {
			for ($i = 0; $i < 5; $i++) {
				$query = $this->connection->getQueryBuilder();
				$query->insert($this->table)
					->values([
						'share_type' => $query->createNamedParameter($shareType),
						'share_with' => $query->createNamedParameter('shareWith'),
						'uid_owner' => $query->createNamedParameter('user' . $i),
						'uid_initiator' => $query->createNamedParameter(null),
						'parent' => $query->createNamedParameter(0),
						'item_type' => $query->createNamedParameter('file'),
						'item_source' => $query->createNamedParameter('2'),
						'item_target' => $query->createNamedParameter('/2'),
						'file_source' => $query->createNamedParameter(2),
						'file_target' => $query->createNamedParameter('/foobar'),
						'permissions' => $query->createNamedParameter(31),
						'stime' => $query->createNamedParameter(time()),
					]);

				$this->assertSame(1, $query->execute());
			}
		}

		/** @var IOutput $output */
		$output = $this->createMock(IOutput::class);
		$this->migration->run($output);

		$query = $this->connection->getQueryBuilder();
		$query->select('*')
			->from('share');
		$allShares = $query->execute()->fetchAll();

		foreach ($allShares as $share) {
			if ((int)$share['share_type'] === Share::SHARE_TYPE_LINK) {
				$this->assertNull($share['share_with']);
				$this->assertSame('shareWith', $share['password']);
			} else {
				$this->assertSame('shareWith', $share['share_with']);
				$this->assertNull($share['password']);
			}
		}
	}
}