forked from contao/contao
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provide migration for folderUrl setting
- Loading branch information
Showing
3 changed files
with
266 additions
and
0 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
core-bundle/src/Migration/Version410/FolderUrlMigration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
core-bundle/tests/Migration/Version410/FolderUrlMigrationTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |