|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | + |
| 9 | +namespace OCA\Activity\Tests\Migration; |
| 10 | + |
| 11 | +use Doctrine\DBAL\Schema\Table; |
| 12 | +use OCA\Activity\Migration\Version8000Date20260603120000; |
| 13 | +use OCA\Activity\Tests\TestCase; |
| 14 | +use OCP\DB\ISchemaWrapper; |
| 15 | +use OCP\Migration\IOutput; |
| 16 | +use PHPUnit\Framework\MockObject\MockObject; |
| 17 | + |
| 18 | +/** |
| 19 | + * @see Version8000Date20260603120000 |
| 20 | + */ |
| 21 | +class Version8000Date20260603120000Test extends TestCase { |
| 22 | + protected IOutput&MockObject $output; |
| 23 | + protected Version8000Date20260603120000 $migration; |
| 24 | + |
| 25 | + protected function setUp(): void { |
| 26 | + parent::setUp(); |
| 27 | + |
| 28 | + $this->output = $this->createMock(IOutput::class); |
| 29 | + $this->migration = new Version8000Date20260603120000(); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * On a queue that still has the legacy single-column `amp_user` index the |
| 34 | + * migration adds the covering composite index and drops the now-redundant one. |
| 35 | + */ |
| 36 | + public function testAddsCompositeIndexAndDropsRedundantOne(): void { |
| 37 | + $table = $this->createMock(Table::class); |
| 38 | + $table->method('hasIndex') |
| 39 | + ->willReturnMap([ |
| 40 | + ['amp_user_send', false], |
| 41 | + ['amp_user', true], |
| 42 | + ]); |
| 43 | + $table->expects($this->once()) |
| 44 | + ->method('addIndex') |
| 45 | + ->with(['amq_affecteduser', 'amq_latest_send'], 'amp_user_send'); |
| 46 | + $table->expects($this->once()) |
| 47 | + ->method('dropIndex') |
| 48 | + ->with('amp_user'); |
| 49 | + |
| 50 | + $schema = $this->getSchemaMock($table); |
| 51 | + |
| 52 | + $result = $this->migration->changeSchema($this->output, fn (): ISchemaWrapper => $schema, []); |
| 53 | + $this->assertSame($schema, $result, 'The schema must be returned when it was changed'); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Running the migration again (composite index present, legacy index gone) |
| 58 | + * must be a no-op so re-runs / fresh installs are not touched. |
| 59 | + */ |
| 60 | + public function testIsIdempotentWhenAlreadyMigrated(): void { |
| 61 | + $table = $this->createMock(Table::class); |
| 62 | + $table->method('hasIndex') |
| 63 | + ->willReturnMap([ |
| 64 | + ['amp_user_send', true], |
| 65 | + ['amp_user', false], |
| 66 | + ]); |
| 67 | + $table->expects($this->never()) |
| 68 | + ->method('addIndex'); |
| 69 | + $table->expects($this->never()) |
| 70 | + ->method('dropIndex'); |
| 71 | + |
| 72 | + $schema = $this->getSchemaMock($table); |
| 73 | + |
| 74 | + $result = $this->migration->changeSchema($this->output, fn (): ISchemaWrapper => $schema, []); |
| 75 | + $this->assertSame($schema, $result, 'Re-running must not add or drop the indexes again'); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * The mail queue table does not exist on every install (e.g. before its |
| 80 | + * creation migration ran), so a missing table must be skipped gracefully. |
| 81 | + */ |
| 82 | + public function testSkipsWhenTableIsMissing(): void { |
| 83 | + $schema = $this->createMock(ISchemaWrapper::class); |
| 84 | + $schema->method('hasTable') |
| 85 | + ->with('activity_mq') |
| 86 | + ->willReturn(false); |
| 87 | + $schema->expects($this->never()) |
| 88 | + ->method('getTable'); |
| 89 | + |
| 90 | + $result = $this->migration->changeSchema($this->output, fn (): ISchemaWrapper => $schema, []); |
| 91 | + $this->assertNull($result); |
| 92 | + } |
| 93 | + |
| 94 | + protected function getSchemaMock(Table&MockObject $table): ISchemaWrapper&MockObject { |
| 95 | + $schema = $this->createMock(ISchemaWrapper::class); |
| 96 | + $schema->method('hasTable') |
| 97 | + ->with('activity_mq') |
| 98 | + ->willReturn(true); |
| 99 | + $schema->method('getTable') |
| 100 | + ->with('activity_mq') |
| 101 | + ->willReturn($table); |
| 102 | + |
| 103 | + return $schema; |
| 104 | + } |
| 105 | +} |
0 commit comments