Skip to content

Commit

Permalink
provide migration for folderUrl setting
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzmg committed Jul 24, 2020
1 parent 54cab1a commit 5638df5
Show file tree
Hide file tree
Showing 3 changed files with 266 additions and 0 deletions.
96 changes: 96 additions & 0 deletions core-bundle/src/Migration/Version410/FolderUrlMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?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\Version409;

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());
}
}

0 comments on commit 5638df5

Please sign in to comment.