Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Also migrate the "useFolderUrl" setting #1987

Merged
merged 6 commits into from
Jul 26, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
98 changes: 98 additions & 0 deletions core-bundle/src/Migration/Version410/FolderUrlMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

declare(strict_types=1);

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

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;

/**
* @internal
*/
class FolderUrlMigration extends AbstractMigration
{
/**
* @var Connection
*/
private $connection;

/**
* @var ContaoFramework
*/
private $framework;

/**
* @var Config
*/
private $config;

public function __construct(Connection $connection, ContaoFramework $framework)
{
$this->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;
}
}
5 changes: 5 additions & 0 deletions core-bundle/src/Resources/config/migrations.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ services:
arguments:
- '@database_connection'

Contao\CoreBundle\Migration\Version410\FolderUrlMigration:
arguments:
- '@database_connection'
- '@contao.framework'

Contao\CoreBundle\Migration\Version410\OrderFieldMigration:
arguments:
- '@database_connection'
165 changes: 165 additions & 0 deletions core-bundle/tests/Migration/Version410/FolderUrlMigrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

declare(strict_types=1);

/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/

namespace Contao\CoreBundle\Tests\Migration\Version410;

use Contao\Config;
use Contao\CoreBundle\Migration\Version410\FolderUrlMigration;
use Contao\CoreBundle\Tests\TestCase;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Schema\MySqlSchemaManager;

class FolderUrlMigrationTest extends TestCase
{
public function testDoesNothingIfPageTableDoesNotExist(): void
{
$schemaManager = $this->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());
}
}