From fb2566f0f0d064774813f85589be6af9552190b9 Mon Sep 17 00:00:00 2001 From: Fritz Michael Gschwantner Date: Sat, 25 Jul 2020 10:17:35 +0100 Subject: [PATCH] use existing RoutingMigration --- .../Version410/FolderUrlMigration.php | 98 ----------- .../Migration/Version410/RoutingMigration.php | 26 ++- .../src/Resources/config/migrations.yml | 8 +- .../ContaoCoreExtensionTest.php | 22 +++ .../Version410/FolderUrlMigrationTest.php | 165 ------------------ 5 files changed, 50 insertions(+), 269 deletions(-) delete mode 100644 core-bundle/src/Migration/Version410/FolderUrlMigration.php delete mode 100644 core-bundle/tests/Migration/Version410/FolderUrlMigrationTest.php diff --git a/core-bundle/src/Migration/Version410/FolderUrlMigration.php b/core-bundle/src/Migration/Version410/FolderUrlMigration.php deleted file mode 100644 index 304ce42fd04..00000000000 --- a/core-bundle/src/Migration/Version410/FolderUrlMigration.php +++ /dev/null @@ -1,98 +0,0 @@ -connection = $connection; - $this->framework = $framework; - } - - public function shouldRun(): bool - { - $schemaManager = $this->connection->getSchemaManager(); - - if (!$schemaManager->tablesExist('tl_page') || !$this->hasRootPages()) { - return false; - } - - if (!$this->getConfig()->has('folderUrl')) { - return false; - } - - return $this->getConfig()->get('folderUrl') && !$this->hasUpdatedRootPages(); - } - - public function run(): MigrationResult - { - $this->connection->update('tl_page', ['useFolderUrl' => '1'], ['type' => 'root']); - - $this->getConfig()->remove('folderUrl'); - $this->getConfig()->save(); - - return $this->createResult(true); - } - - private function getConfig(): Config - { - if (null !== $this->config) { - return $this->config; - } - - $this->framework->initialize(); - - $this->config = $this->framework->createInstance(Config::class); - - return $this->config; - } - - private function hasRootPages(): bool - { - $query = 'SELECT COUNT(id) FROM tl_page WHERE '.$this->connection->quoteIdentifier('type')." = 'root'"; - - return (int) $this->connection->executeQuery($query)->fetchColumn() > 0; - } - - private function hasUpdatedRootPages(): bool - { - $query = 'SELECT COUNT(id) FROM tl_page WHERE '.$this->connection->quoteIdentifier('type')." = 'root' AND useFolderUrl = '1'"; - - return (int) $this->connection->executeQuery($query)->fetchColumn() > 0; - } -} diff --git a/core-bundle/src/Migration/Version410/RoutingMigration.php b/core-bundle/src/Migration/Version410/RoutingMigration.php index 5e2ab6dc9b4..33750321c17 100644 --- a/core-bundle/src/Migration/Version410/RoutingMigration.php +++ b/core-bundle/src/Migration/Version410/RoutingMigration.php @@ -12,6 +12,8 @@ namespace Contao\CoreBundle\Migration\Version410; +use Contao\Config; +use Contao\CoreBundle\Framework\ContaoFramework; use Contao\CoreBundle\Migration\AbstractMigration; use Contao\CoreBundle\Migration\MigrationResult; use Doctrine\DBAL\Connection; @@ -29,6 +31,11 @@ class RoutingMigration extends AbstractMigration */ private $connection; + /** + * @var ContaoFramework + */ + private $framework; + /** * @var string */ @@ -39,9 +46,10 @@ class RoutingMigration extends AbstractMigration */ private $prependLocale; - public function __construct(Connection $connection, string $urlSuffix = '.html', bool $prependLocale = false) + public function __construct(Connection $connection, ContaoFramework $framework, string $urlSuffix = '.html', bool $prependLocale = false) { $this->connection = $connection; + $this->framework = $framework; $this->urlSuffix = $urlSuffix; $this->prependLocale = $prependLocale; } @@ -56,7 +64,7 @@ public function shouldRun(): bool $columns = $schemaManager->listTableColumns('tl_page'); - return !isset($columns['urlprefix']) && !isset($columns['urlsuffix']); + return !isset($columns['urlprefix']) && !isset($columns['urlsuffix']) && !isset($columns['usefolderurl']); } public function run(): MigrationResult @@ -67,7 +75,10 @@ public function run(): MigrationResult $urlSuffix = new Column('urlSuffix', new StringType()); $urlSuffix->setColumnDefinition("varchar(16) NOT NULL default '.html'"); - $diff = new TableDiff('tl_page', [$urlPrefix, $urlSuffix]); + $useFolderUrl = new Column('useFolderUrl', new StringType()); + $useFolderUrl->setColumnDefinition("char(1) NOT NULL default ''"); + + $diff = new TableDiff('tl_page', [$urlPrefix, $urlSuffix, $useFolderUrl]); $sql = $this->connection->getDatabasePlatform()->getAlterTableSQL($diff); foreach ($sql as $statement) { @@ -81,6 +92,15 @@ public function run(): MigrationResult ->execute(['suffix' => $this->urlSuffix]) ; + $this->framework->initialize(); + + /** @var Config $config */ + $config = $this->framework->getAdapter(Config::class); + + if ($config->get('folderUrl')) { + $this->connection->update('tl_page', ['useFolderUrl' => '1'], ['type' => 'root']); + } + return $this->createResult(true); } } diff --git a/core-bundle/src/Resources/config/migrations.yml b/core-bundle/src/Resources/config/migrations.yml index 727d8154469..393e67facd3 100644 --- a/core-bundle/src/Resources/config/migrations.yml +++ b/core-bundle/src/Resources/config/migrations.yml @@ -13,11 +13,13 @@ services: arguments: - '@database_connection' - Contao\CoreBundle\Migration\Version410\FolderUrlMigration: + Contao\CoreBundle\Migration\Version410\OrderFieldMigration: arguments: - '@database_connection' - - '@contao.framework' - Contao\CoreBundle\Migration\Version410\OrderFieldMigration: + Contao\CoreBundle\Migration\Version410\RoutingMigration: arguments: - '@database_connection' + - '@contao.framework' + - '%contao.url_suffix%' + - '%contao.prepend_locale%' diff --git a/core-bundle/tests/DependencyInjection/ContaoCoreExtensionTest.php b/core-bundle/tests/DependencyInjection/ContaoCoreExtensionTest.php index 16e787976e5..e00e5523d7f 100644 --- a/core-bundle/tests/DependencyInjection/ContaoCoreExtensionTest.php +++ b/core-bundle/tests/DependencyInjection/ContaoCoreExtensionTest.php @@ -96,6 +96,7 @@ use Contao\CoreBundle\Menu\BackendMenuBuilder; use Contao\CoreBundle\Migration\MigrationCollection; use Contao\CoreBundle\Migration\Version409\CeAccessMigration; +use Contao\CoreBundle\Migration\Version410\RoutingMigration; use Contao\CoreBundle\Monolog\ContaoTableHandler; use Contao\CoreBundle\Monolog\ContaoTableProcessor; use Contao\CoreBundle\OptIn\OptIn; @@ -3739,6 +3740,27 @@ public function testRegistersTheVersion409CeAccessMigration(): void ); } + public function testRegistersTheVersion410RoutingMigration(): void + { + $container = $this->getContainerBuilder(); + + $this->assertTrue($container->has(RoutingMigration::class)); + + $definition = $container->getDefinition(RoutingMigration::class); + + $this->assertTrue($definition->isPrivate()); + + $this->assertEquals( + [ + new Reference('database_connection'), + new Reference('contao.framework'), + '%contao.url_suffix%', + '%contao.prepend_locale%', + ], + $definition->getArguments() + ); + } + public function testRegistersThePredefinedImageSizes(): void { $container = $this->getContainerBuilder(); diff --git a/core-bundle/tests/Migration/Version410/FolderUrlMigrationTest.php b/core-bundle/tests/Migration/Version410/FolderUrlMigrationTest.php deleted file mode 100644 index d4329bc6815..00000000000 --- a/core-bundle/tests/Migration/Version410/FolderUrlMigrationTest.php +++ /dev/null @@ -1,165 +0,0 @@ -createMock(MySqlSchemaManager::class); - $schemaManager - ->expects($this->once()) - ->method('tablesExist') - ->with('tl_page') - ->willReturn(false) - ; - - $connection = $this->createMock(Connection::class); - $connection - ->expects($this->once()) - ->method('getSchemaManager') - ->willReturn($schemaManager) - ; - - $framework = $this->mockContaoFramework(); - $framework - ->expects($this->never()) - ->method('initialize') - ; - - $migration = new FolderUrlMigration($connection, $framework); - - $this->assertFalse($migration->shouldRun()); - } - - public function testDoesNothingIfNoRootPagesExist(): void - { - $schemaManager = $this->createMock(MySqlSchemaManager::class); - $schemaManager - ->expects($this->once()) - ->method('tablesExist') - ->with('tl_page') - ->willReturn(true) - ; - - $connection = $this->createMock(Connection::class); - $connection - ->expects($this->once()) - ->method('getSchemaManager') - ->willReturn($schemaManager) - ; - - $connection - ->expects($this->once()) - ->method('quoteIdentifier') - ->with('type') - ->willReturn('`type`') - ; - - $result = $this->createMock(ResultStatement::class); - $result - ->expects($this->once()) - ->method('fetchColumn') - ->willReturn('0') - ; - - $connection - ->expects($this->once()) - ->method('executeQuery') - ->with("SELECT COUNT(id) FROM tl_page WHERE `type` = 'root'") - ->willReturn($result) - ; - - $framework = $this->mockContaoFramework(); - $framework - ->expects($this->never()) - ->method('initialize') - ; - - $migration = new FolderUrlMigration($connection, $framework); - - $this->assertFalse($migration->shouldRun()); - } - - public function testDoesNothingIfFolderUrlNotEnabled(): void - { - $schemaManager = $this->createMock(MySqlSchemaManager::class); - $schemaManager - ->expects($this->once()) - ->method('tablesExist') - ->with('tl_page') - ->willReturn(true) - ; - - $connection = $this->createMock(Connection::class); - $connection - ->expects($this->once()) - ->method('getSchemaManager') - ->willReturn($schemaManager) - ; - - $connection - ->expects($this->once()) - ->method('quoteIdentifier') - ->with('type') - ->willReturn('`type`') - ; - - $result = $this->createMock(ResultStatement::class); - $result - ->expects($this->once()) - ->method('fetchColumn') - ->willReturn('1') - ; - - $connection - ->expects($this->once()) - ->method('executeQuery') - ->with("SELECT COUNT(id) FROM tl_page WHERE `type` = 'root'") - ->willReturn($result) - ; - - $config = $this->createMock(Config::class); - $config - ->expects($this->once()) - ->method('has') - ->with('folderUrl') - ->willReturn(false) - ; - - $framework = $this->mockContaoFramework(); - - $framework - ->expects($this->once()) - ->method('initialize') - ; - - $framework - ->expects($this->once()) - ->method('createInstance') - ->with(Config::class) - ->willReturn($config) - ; - - $migration = new FolderUrlMigration($connection, $framework); - - $this->assertFalse($migration->shouldRun()); - } -}