summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorStephan Orbaugh <62374139+sorbaugh@users.noreply.github.com>2024-07-30 15:54:56 +0200
committerGitHub <noreply@github.com>2024-07-30 15:54:56 +0200
commit18c0bcb2da2da1e046163e006ba581be165caed8 (patch)
tree38c7b98d59c70543632f6d3b1c53d40eced2df05 /tests
parent0705b6af4a9cf6be0ca691298c01b8d8b5fe6ba2 (diff)
parenta9e1dc668875f4247f6331096248b099afff5ae6 (diff)
downloadnextcloud-server-18c0bcb2da2da1e046163e006ba581be165caed8.tar.gz
nextcloud-server-18c0bcb2da2da1e046163e006ba581be165caed8.zip
Merge pull request #46476 from nextcloud/enh/noid/migration-attributes
Migration Attributes
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/DB/MigrationsTest.php181
-rw-r--r--tests/lib/Updater/ReleaseMetadataTest.php209
2 files changed, 386 insertions, 4 deletions
diff --git a/tests/lib/DB/MigrationsTest.php b/tests/lib/DB/MigrationsTest.php
index 2bdd705ff5d..a088ca1baf0 100644
--- a/tests/lib/DB/MigrationsTest.php
+++ b/tests/lib/DB/MigrationsTest.php
@@ -19,8 +19,21 @@ use Doctrine\DBAL\Types\Type;
use OC\DB\Connection;
use OC\DB\MigrationService;
use OC\DB\SchemaWrapper;
+use OC\Migration\MetadataManager;
+use OCP\App\IAppManager;
use OCP\IDBConnection;
+use OCP\Migration\Attributes\AddColumn;
+use OCP\Migration\Attributes\AddIndex;
+use OCP\Migration\Attributes\ColumnType;
+use OCP\Migration\Attributes\CreateTable;
+use OCP\Migration\Attributes\DropColumn;
+use OCP\Migration\Attributes\DropIndex;
+use OCP\Migration\Attributes\DropTable;
+use OCP\Migration\Attributes\IndexType;
+use OCP\Migration\Attributes\ModifyColumn;
use OCP\Migration\IMigrationStep;
+use OCP\Server;
+use PHPUnit\Framework\MockObject\MockObject;
/**
* Class MigrationsTest
@@ -28,10 +41,9 @@ use OCP\Migration\IMigrationStep;
* @package Test\DB
*/
class MigrationsTest extends \Test\TestCase {
- /** @var MigrationService | \PHPUnit\Framework\MockObject\MockObject */
- private $migrationService;
- /** @var \PHPUnit\Framework\MockObject\MockObject | IDBConnection $db */
- private $db;
+ private MigrationService|MockObject $migrationService;
+ private MockObject|IDBConnection $db;
+ private IAppManager $appManager;
protected function setUp(): void {
parent::setUp();
@@ -39,6 +51,8 @@ class MigrationsTest extends \Test\TestCase {
$this->db = $this->createMock(Connection::class);
$this->db->expects($this->any())->method('getPrefix')->willReturn('test_oc_');
$this->migrationService = new MigrationService('testing', $this->db);
+
+ $this->appManager = Server::get(IAppManager::class);
}
public function testGetters() {
@@ -755,4 +769,163 @@ class MigrationsTest extends \Test\TestCase {
self::invokePrivate($this->migrationService, 'ensureOracleConstraints', [$sourceSchema, $schema, 3]);
}
+
+
+ public function testExtractMigrationAttributes() {
+ $metadataManager = Server::get(MetadataManager::class);
+ $this->appManager->loadApp('testing');
+
+ $this->assertEquals($this->getMigrationMetadata(), json_decode(json_encode($metadataManager->extractMigrationAttributes('testing')), true));
+
+ $this->appManager->disableApp('testing');
+ }
+
+ public function testDeserializeMigrationMetadata() {
+ $metadataManager = Server::get(MetadataManager::class);
+ $this->assertEquals(
+ [
+ 'core' => [],
+ 'apps' => [
+ 'testing' => [
+ '30000Date20240102030405' => [
+ new DropTable('old_table'),
+ new CreateTable('new_table',
+ description: 'Table is used to store things, but also to get more things',
+ notes: ['this is a notice', 'and another one, if really needed']
+ ),
+ new AddColumn('my_table'),
+ new AddColumn('my_table', 'another_field'),
+ new AddColumn('other_table', 'last_one', ColumnType::DATE),
+ new AddIndex('my_table'),
+ new AddIndex('my_table', IndexType::PRIMARY),
+ new DropColumn('other_table'),
+ new DropColumn('other_table', 'old_column',
+ description: 'field is not used anymore and replaced by \'last_one\''
+ ),
+ new DropIndex('other_table'),
+ new ModifyColumn('other_table'),
+ new ModifyColumn('other_table', 'this_field'),
+ new ModifyColumn('other_table', 'this_field', ColumnType::BIGINT)
+ ]
+ ]
+ ]
+ ],
+ $metadataManager->getMigrationsAttributesFromReleaseMetadata(
+ [
+ 'core' => [],
+ 'apps' => ['testing' => $this->getMigrationMetadata()]
+ ]
+ )
+ );
+ }
+
+ private function getMigrationMetadata(): array {
+ return [
+ '30000Date20240102030405' => [
+ [
+ 'class' => 'OCP\\Migration\\Attributes\\DropTable',
+ 'table' => 'old_table',
+ 'description' => '',
+ 'notes' => [],
+ 'columns' => []
+ ],
+ [
+ 'class' => 'OCP\\Migration\\Attributes\\CreateTable',
+ 'table' => 'new_table',
+ 'description' => 'Table is used to store things, but also to get more things',
+ 'notes' =>
+ [
+ 'this is a notice',
+ 'and another one, if really needed'
+ ],
+ 'columns' => []
+ ],
+ [
+ 'class' => 'OCP\\Migration\\Attributes\\AddColumn',
+ 'table' => 'my_table',
+ 'description' => '',
+ 'notes' => [],
+ 'name' => '',
+ 'type' => ''
+ ],
+ [
+ 'class' => 'OCP\\Migration\\Attributes\\AddColumn',
+ 'table' => 'my_table',
+ 'description' => '',
+ 'notes' => [],
+ 'name' => 'another_field',
+ 'type' => ''
+ ],
+ [
+ 'class' => 'OCP\\Migration\\Attributes\\AddColumn',
+ 'table' => 'other_table',
+ 'description' => '',
+ 'notes' => [],
+ 'name' => 'last_one',
+ 'type' => 'date'
+ ],
+ [
+ 'class' => 'OCP\\Migration\\Attributes\\AddIndex',
+ 'table' => 'my_table',
+ 'description' => '',
+ 'notes' => [],
+ 'type' => ''
+ ],
+ [
+ 'class' => 'OCP\\Migration\\Attributes\\AddIndex',
+ 'table' => 'my_table',
+ 'description' => '',
+ 'notes' => [],
+ 'type' => 'primary'
+ ],
+ [
+ 'class' => 'OCP\\Migration\\Attributes\\DropColumn',
+ 'table' => 'other_table',
+ 'description' => '',
+ 'notes' => [],
+ 'name' => '',
+ 'type' => ''
+ ],
+ [
+ 'class' => 'OCP\\Migration\\Attributes\\DropColumn',
+ 'table' => 'other_table',
+ 'description' => 'field is not used anymore and replaced by \'last_one\'',
+ 'notes' => [],
+ 'name' => 'old_column',
+ 'type' => ''
+ ],
+ [
+ 'class' => 'OCP\\Migration\\Attributes\\DropIndex',
+ 'table' => 'other_table',
+ 'description' => '',
+ 'notes' => [],
+ 'type' => ''
+ ],
+ [
+ 'class' => 'OCP\\Migration\\Attributes\\ModifyColumn',
+ 'table' => 'other_table',
+ 'description' => '',
+ 'notes' => [],
+ 'name' => '',
+ 'type' => ''
+ ],
+ [
+ 'class' => 'OCP\\Migration\\Attributes\\ModifyColumn',
+ 'table' => 'other_table',
+ 'description' => '',
+ 'notes' => [],
+ 'name' => 'this_field',
+ 'type' => ''
+ ],
+ [
+ 'class' => 'OCP\\Migration\\Attributes\\ModifyColumn',
+ 'table' => 'other_table',
+ 'description' => '',
+ 'notes' => [],
+ 'name' => 'this_field',
+ 'type' => 'bigint'
+ ],
+ ]
+ ];
+ }
}
diff --git a/tests/lib/Updater/ReleaseMetadataTest.php b/tests/lib/Updater/ReleaseMetadataTest.php
new file mode 100644
index 00000000000..25fe3949152
--- /dev/null
+++ b/tests/lib/Updater/ReleaseMetadataTest.php
@@ -0,0 +1,209 @@
+<?php
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+namespace Test\Updater;
+
+use OC\Updater\ReleaseMetadata;
+use OCP\Http\Client\IClient;
+use OCP\Http\Client\IClientService;
+use OCP\Http\Client\IResponse;
+use PHPUnit\Framework\MockObject\MockObject;
+
+class ReleaseMetadataTest extends \Test\TestCase {
+ private IClientService| MockObject $clientService;
+
+ protected function setUp(): void {
+ parent::setUp();
+ $this->clientService = $this->getMockBuilder(IClientService::class)
+ ->disableOriginalConstructor()
+ ->getMock();
+ }
+
+ public function testDownloadMetadata() {
+ $client = $this->createMock(IClient::class);
+ $response = $this->createMock(IResponse::class);
+ $this->clientService->expects($this->once())
+ ->method('newClient')
+ ->with()
+ ->willReturn($client);
+ $client->expects($this->once())
+ ->method('get')
+ ->willReturn($response);
+ $response->expects($this->once())
+ ->method('getBody')
+ ->with()
+ ->willReturn($this->resultRequest());
+
+
+ $releaseMetadata = new ReleaseMetadata($this->clientService);
+ $this->assertSame($this->resultRequestArray(), $releaseMetadata->downloadMetadata('ouila'));
+ }
+
+ /**
+ * @dataProvider getMetadataUrlProvider
+ *
+ * @param string $version
+ * @param string $url
+ */
+ public function testGetMetadata(string $version, string $url) {
+ $client = $this->createMock(IClient::class);
+ $response = $this->createMock(IResponse::class);
+ $this->clientService->expects($this->once())
+ ->method('newClient')
+ ->with()
+ ->willReturn($client);
+ $client->expects($this->once())
+ ->method('get')
+ ->with($url)
+ ->willReturn($response);
+
+ $response->expects($this->once())
+ ->method('getBody')
+ ->with()
+ ->willReturn('{}');
+
+ $releaseMetadata = new ReleaseMetadata($this->clientService);
+ $releaseMetadata->getMetadata($version);
+ }
+
+ /**
+ * @return array
+ */
+ public function getMetadataUrlProvider(): array {
+ return [
+ [
+ '30.0.0',
+ 'https://download.nextcloud.com/server/releases/nextcloud-30.0.0.metadata'
+ ],
+ [
+ '30.0.0-beta1',
+ 'https://download.nextcloud.com/server/prereleases/nextcloud-30.0.0-beta1.metadata'
+ ],
+ [
+ '30',
+ 'https://download.nextcloud.com/server/releases/latest-30.metadata'
+ ]
+ ];
+ }
+
+ private function resultRequest(): string {
+ return json_encode($this->resultRequestArray());
+ }
+
+ private function resultRequestArray(): array {
+ return [
+ 'migrations' => [
+ 'core' => [],
+ 'apps' => [
+ 'testing' => [
+ '30000Date20240102030405' => [
+ 'class' => 'OCP\\Migration\\Attributes\\DropTable',
+ 'table' => 'old_table',
+ 'description' => '',
+ 'notes' => [],
+ 'columns' => []
+ ],
+ [
+ 'class' => 'OCP\\Migration\\Attributes\\CreateTable',
+ 'table' => 'new_table',
+ 'description' => 'Table is used to store things, but also to get more things',
+ 'notes' => [
+ 'this is a notice',
+ 'and another one, if really needed'
+ ],
+ 'columns' => []
+ ],
+ [
+ 'class' => 'OCP\\Migration\\Attributes\\AddColumn',
+ 'table' => 'my_table',
+ 'description' => '',
+ 'notes' => [],
+ 'name' => '',
+ 'type' => ''
+ ],
+ [
+ 'class' => 'OCP\\Migration\\Attributes\\AddColumn',
+ 'table' => 'my_table',
+ 'description' => '',
+ 'notes' => [],
+ 'name' => 'another_field',
+ 'type' => ''
+ ],
+ [
+ 'class' => 'OCP\\Migration\\Attributes\\AddColumn',
+ 'table' => 'other_table',
+ 'description' => '',
+ 'notes' => [],
+ 'name' => 'last_one',
+ 'type' => 'date'
+ ],
+ [
+ 'class' => 'OCP\\Migration\\Attributes\\AddIndex',
+ 'table' => 'my_table',
+ 'description' => '',
+ 'notes' => [],
+ 'type' => ''
+ ],
+ [
+ 'class' => 'OCP\\Migration\\Attributes\\AddIndex',
+ 'table' => 'my_table',
+ 'description' => '',
+ 'notes' => [],
+ 'type' => 'primary'
+ ],
+ [
+ 'class' => 'OCP\\Migration\\Attributes\\DropColumn',
+ 'table' => 'other_table',
+ 'description' => '',
+ 'notes' => [],
+ 'name' => '',
+ 'type' => ''
+ ],
+ [
+ 'class' => 'OCP\\Migration\\Attributes\\DropColumn',
+ 'table' => 'other_table',
+ 'description' => 'field is not used anymore and replaced by \'last_one\'',
+ 'notes' => [],
+ 'name' => 'old_column',
+ 'type' => ''
+ ],
+ [
+ 'class' => 'OCP\\Migration\\Attributes\\DropIndex',
+ 'table' => 'other_table',
+ 'description' => '',
+ 'notes' => [],
+ 'type' => ''
+ ],
+ [
+ 'class' => 'OCP\\Migration\\Attributes\\ModifyColumn',
+ 'table' => 'other_table',
+ 'description' => '',
+ 'notes' => [],
+ 'name' => '',
+ 'type' => ''
+ ],
+ [
+ 'class' => 'OCP\\Migration\\Attributes\\ModifyColumn',
+ 'table' => 'other_table',
+ 'description' => '',
+ 'notes' => [],
+ 'name' => 'this_field',
+ 'type' => ''
+ ],
+ [
+ 'class' => 'OCP\\Migration\\Attributes\\ModifyColumn',
+ 'table' => 'other_table',
+ 'description' => '',
+ 'notes' => [],
+ 'name' => 'this_field',
+ 'type' => 'bigint'
+ ]
+ ]
+ ]
+ ]
+ ];
+ }
+}