From caffae906e3145e11b5b369876ede6cca2ae7945 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Sat, 27 Jul 2024 23:54:09 -0100 Subject: [PATCH] feat(migration-attributes): tests Signed-off-by: Maxence Lange d Signed-off-by: Maxence Lange --- .../composer/composer/autoload_classmap.php | 1 + .../composer/composer/autoload_static.php | 1 + .../Version30000Date20240102030405.php | 41 ++++ lib/private/Migration/MetadataManager.php | 4 +- lib/private/Updater/ReleaseMetadata.php | 3 +- lib/public/Migration/Attributes/AddColumn.php | 5 +- lib/public/Migration/Attributes/AddIndex.php | 3 +- .../Attributes/ColumnMigrationAttribute.php | 2 +- .../Migration/Attributes/DropColumn.php | 5 +- lib/public/Migration/Attributes/DropIndex.php | 4 +- .../Attributes/IndexMigrationAttribute.php | 2 +- .../Attributes/MigrationAttribute.php | 5 +- .../Migration/Attributes/ModifyColumn.php | 5 +- tests/lib/DB/MigrationsTest.php | 185 +++++++++++++++- tests/lib/Updater/ReleaseMetadataTest.php | 209 ++++++++++++++++++ 15 files changed, 449 insertions(+), 26 deletions(-) create mode 100644 apps/testing/lib/Migration/Version30000Date20240102030405.php create mode 100644 tests/lib/Updater/ReleaseMetadataTest.php diff --git a/apps/testing/composer/composer/autoload_classmap.php b/apps/testing/composer/composer/autoload_classmap.php index 079f887788100..6dce2e26361ba 100644 --- a/apps/testing/composer/composer/autoload_classmap.php +++ b/apps/testing/composer/composer/autoload_classmap.php @@ -16,6 +16,7 @@ 'OCA\\Testing\\Listener\\RegisterDeclarativeSettingsListener' => $baseDir . '/../lib/Listener/RegisterDeclarativeSettingsListener.php', 'OCA\\Testing\\Listener\\SetDeclarativeSettingsValueListener' => $baseDir . '/../lib/Listener/SetDeclarativeSettingsValueListener.php', 'OCA\\Testing\\Locking\\FakeDBLockingProvider' => $baseDir . '/../lib/Locking/FakeDBLockingProvider.php', + 'OCA\\Testing\\Migration\\Version30000Date20240102030405' => $baseDir . '/../lib/Migration/Version30000Date20240102030405.php', 'OCA\\Testing\\Provider\\FakeText2ImageProvider' => $baseDir . '/../lib/Provider/FakeText2ImageProvider.php', 'OCA\\Testing\\Provider\\FakeTextProcessingProvider' => $baseDir . '/../lib/Provider/FakeTextProcessingProvider.php', 'OCA\\Testing\\Provider\\FakeTextProcessingProviderSync' => $baseDir . '/../lib/Provider/FakeTextProcessingProviderSync.php', diff --git a/apps/testing/composer/composer/autoload_static.php b/apps/testing/composer/composer/autoload_static.php index 2332da70da97f..3be58e042894f 100644 --- a/apps/testing/composer/composer/autoload_static.php +++ b/apps/testing/composer/composer/autoload_static.php @@ -31,6 +31,7 @@ class ComposerStaticInitTesting 'OCA\\Testing\\Listener\\RegisterDeclarativeSettingsListener' => __DIR__ . '/..' . '/../lib/Listener/RegisterDeclarativeSettingsListener.php', 'OCA\\Testing\\Listener\\SetDeclarativeSettingsValueListener' => __DIR__ . '/..' . '/../lib/Listener/SetDeclarativeSettingsValueListener.php', 'OCA\\Testing\\Locking\\FakeDBLockingProvider' => __DIR__ . '/..' . '/../lib/Locking/FakeDBLockingProvider.php', + 'OCA\\Testing\\Migration\\Version30000Date20240102030405' => __DIR__ . '/..' . '/../lib/Migration/Version30000Date20240102030405.php', 'OCA\\Testing\\Provider\\FakeText2ImageProvider' => __DIR__ . '/..' . '/../lib/Provider/FakeText2ImageProvider.php', 'OCA\\Testing\\Provider\\FakeTextProcessingProvider' => __DIR__ . '/..' . '/../lib/Provider/FakeTextProcessingProvider.php', 'OCA\\Testing\\Provider\\FakeTextProcessingProviderSync' => __DIR__ . '/..' . '/../lib/Provider/FakeTextProcessingProviderSync.php', diff --git a/apps/testing/lib/Migration/Version30000Date20240102030405.php b/apps/testing/lib/Migration/Version30000Date20240102030405.php new file mode 100644 index 0000000000000..bacb187cb37a8 --- /dev/null +++ b/apps/testing/lib/Migration/Version30000Date20240102030405.php @@ -0,0 +1,41 @@ + * @since 30.0.0 */ public function extractMigrationAttributes(string $appId): array { @@ -144,7 +144,7 @@ private function createAttribute(array $item): MigrationAttribute { } try { - $attribute = new $class(); + $attribute = new $class($item['table'] ?? ''); return $attribute->import($item); } catch (\Error) { throw new AttributeException('cannot import Attribute'); diff --git a/lib/private/Updater/ReleaseMetadata.php b/lib/private/Updater/ReleaseMetadata.php index 22d8c84341262..665847037e78c 100644 --- a/lib/private/Updater/ReleaseMetadata.php +++ b/lib/private/Updater/ReleaseMetadata.php @@ -64,8 +64,7 @@ public function downloadMetadata(string $url): array { try { $response = $client->get($url, [ 'timeout' => 10, - 'connect_timeout' => 10, - 'verify' => false, + 'connect_timeout' => 10 ]); } catch (Exception $e) { throw new ReleaseMetadataException('could not reach metadata at ' . $url, previous: $e); diff --git a/lib/public/Migration/Attributes/AddColumn.php b/lib/public/Migration/Attributes/AddColumn.php index 14a43c26f9950..db8dcf8266ceb 100644 --- a/lib/public/Migration/Attributes/AddColumn.php +++ b/lib/public/Migration/Attributes/AddColumn.php @@ -21,9 +21,8 @@ class AddColumn extends ColumnMigrationAttribute { */ public function definition(): string { $type = is_null($this->getType()) ? '' : ' (' . $this->getType()->value . ')'; - $table = empty($this->getTable()) ? '' : ' to table \'' . $this->getTable() . '\''; return empty($this->getName()) ? - 'Addition of a new column' . $type . $table - : 'Addition of column \'' . $this->getName() . '\'' . $type . $table; + 'Addition of a new column' . $type . ' to table \'' . $this->getTable() . '\'' + : 'Addition of column \'' . $this->getName() . '\'' . $type . ' to table \'' . $this->getTable() . '\''; } } diff --git a/lib/public/Migration/Attributes/AddIndex.php b/lib/public/Migration/Attributes/AddIndex.php index 2a6f0628db162..bd472d26521e3 100644 --- a/lib/public/Migration/Attributes/AddIndex.php +++ b/lib/public/Migration/Attributes/AddIndex.php @@ -21,7 +21,6 @@ class AddIndex extends IndexMigrationAttribute { */ public function definition(): string { $type = is_null($this->getType()) ? '' : ' (' . $this->getType()->value . ')'; - $table = empty($this->getTable()) ? '' : ' to table \'' . $this->getTable() . '\''; - return 'Addition of a new index' . $type . $table; + return 'Addition of a new index' . $type . ' to table \'' . $this->getTable() . '\''; } } diff --git a/lib/public/Migration/Attributes/ColumnMigrationAttribute.php b/lib/public/Migration/Attributes/ColumnMigrationAttribute.php index dab636a3aaafa..b77555263d13b 100644 --- a/lib/public/Migration/Attributes/ColumnMigrationAttribute.php +++ b/lib/public/Migration/Attributes/ColumnMigrationAttribute.php @@ -15,7 +15,7 @@ */ class ColumnMigrationAttribute extends MigrationAttribute implements JsonSerializable { public function __construct( - string $table = '', + string $table, private string $name = '', private ?ColumnType $type = null, string $description = '', diff --git a/lib/public/Migration/Attributes/DropColumn.php b/lib/public/Migration/Attributes/DropColumn.php index 0bb3efb191726..02bf8ec484fb0 100644 --- a/lib/public/Migration/Attributes/DropColumn.php +++ b/lib/public/Migration/Attributes/DropColumn.php @@ -20,9 +20,8 @@ class DropColumn extends ColumnMigrationAttribute { * @since 30.0.0 */ public function definition(): string { - $table = empty($this->getTable()) ? '' : ' from table \'' . $this->getTable() . '\''; return empty($this->getName()) ? - 'Deletion of a column' . $table - : 'Deletion of column \'' . $this->getName() . '\'' . $table; + 'Deletion of a column from table \'' . $this->getTable() . '\'' + : 'Deletion of column \'' . $this->getName() . '\' from table \'' . $this->getTable() . '\''; } } diff --git a/lib/public/Migration/Attributes/DropIndex.php b/lib/public/Migration/Attributes/DropIndex.php index 0e72908ac353c..889e0ddbc7e15 100644 --- a/lib/public/Migration/Attributes/DropIndex.php +++ b/lib/public/Migration/Attributes/DropIndex.php @@ -20,8 +20,6 @@ class DropIndex extends IndexMigrationAttribute { * @since 30.0.0 */ public function definition(): string { - return empty($this->getTable()) ? - 'Deletion of an index' - : 'Deletion of an index from table \'' . $this->getTable() . '\''; + return 'Deletion of an index from table \'' . $this->getTable() . '\''; } } diff --git a/lib/public/Migration/Attributes/IndexMigrationAttribute.php b/lib/public/Migration/Attributes/IndexMigrationAttribute.php index 33c5177c8ac35..316384c2f58c7 100644 --- a/lib/public/Migration/Attributes/IndexMigrationAttribute.php +++ b/lib/public/Migration/Attributes/IndexMigrationAttribute.php @@ -15,7 +15,7 @@ */ class IndexMigrationAttribute extends MigrationAttribute implements JsonSerializable { public function __construct( - string $table = '', + string $table, private ?IndexType $type = null, string $description = '', array $notes = [], diff --git a/lib/public/Migration/Attributes/MigrationAttribute.php b/lib/public/Migration/Attributes/MigrationAttribute.php index b9d698241d4d0..6e8441d4b4c43 100644 --- a/lib/public/Migration/Attributes/MigrationAttribute.php +++ b/lib/public/Migration/Attributes/MigrationAttribute.php @@ -15,7 +15,7 @@ */ class MigrationAttribute implements JsonSerializable { public function __construct( - private string $table = '', + private string $table, private string $description = '', private array $notes = [], ) { @@ -93,8 +93,7 @@ public function definition(): string { * @since 30.0.0 */ public function import(array $data): self { - return $this->setTable($data['table'] ?? '') - ->setDescription($data['description'] ?? '') + return $this->setDescription($data['description'] ?? '') ->setNotes($data['notes'] ?? []); } diff --git a/lib/public/Migration/Attributes/ModifyColumn.php b/lib/public/Migration/Attributes/ModifyColumn.php index 216a911d90f3d..fe9f18d59ac5a 100644 --- a/lib/public/Migration/Attributes/ModifyColumn.php +++ b/lib/public/Migration/Attributes/ModifyColumn.php @@ -21,9 +21,8 @@ class ModifyColumn extends ColumnMigrationAttribute { */ public function definition(): string { $type = is_null($this->getType()) ? '' : ' to ' . $this->getType()->value; - $table = empty($this->getTable()) ? '' : ' from table \'' . $this->getTable() . '\''; return empty($this->getName()) ? - 'Modification of a column' . $table . $type - : 'Modification of column \'' . $this->getName() . '\'' . $table . $type; + 'Modification of a column from table \'' . $this->getTable() . '\'' . $type + : 'Modification of column \'' . $this->getName() . '\' from table \'' . $this->getTable() . '\'' . $type; } } diff --git a/tests/lib/DB/MigrationsTest.php b/tests/lib/DB/MigrationsTest.php index 2bdd705ff5d45..854f3dafe1d97 100644 --- a/tests/lib/DB/MigrationsTest.php +++ b/tests/lib/DB/MigrationsTest.php @@ -19,8 +19,22 @@ 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; +use Psr\Log\LoggerInterface; /** * Class MigrationsTest @@ -28,10 +42,12 @@ * @package Test\DB */ class MigrationsTest extends \Test\TestCase { - /** @var MigrationService | \PHPUnit\Framework\MockObject\MockObject */ + /** @var MigrationService | MockObject */ private $migrationService; - /** @var \PHPUnit\Framework\MockObject\MockObject | IDBConnection $db */ - private $db; + /** @var MockObject | IDBConnection $db */ + private LoggerInterface $logger; + private IAppManager $appManager; + protected function setUp(): void { parent::setUp(); @@ -39,6 +55,9 @@ protected function setUp(): void { $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); + $this->logger = Server::get(LoggerInterface::class); } public function testGetters() { @@ -755,4 +774,164 @@ public function testEnsureOracleConstraintsStringLength4000() { 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 0000000000000..25fe39491522c --- /dev/null +++ b/tests/lib/Updater/ReleaseMetadataTest.php @@ -0,0 +1,209 @@ +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' + ] + ] + ] + ] + ]; + } +}