-
-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
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,80 @@ | ||
<?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\CoreBundle\Migration\AbstractMigration; | ||
use Contao\CoreBundle\Migration\MigrationResult; | ||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Schema\Column; | ||
use Doctrine\DBAL\Schema\TableDiff; | ||
use Doctrine\DBAL\Types\StringType; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class RoutingMigration extends AbstractMigration | ||
{ | ||
/** | ||
* @var Connection | ||
*/ | ||
private $connection; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $urlSuffix; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $prependLocale; | ||
|
||
public function __construct(Connection $connection, string $urlSuffix = '.html', bool $prependLocale = false) | ||
{ | ||
$this->connection = $connection; | ||
$this->urlSuffix = $urlSuffix; | ||
$this->prependLocale = $prependLocale; | ||
} | ||
|
||
public function shouldRun(): bool | ||
{ | ||
$schemaManager = $this->connection->getSchemaManager(); | ||
|
||
if (!$schemaManager->tablesExist('tl_page')) { | ||
return false; | ||
} | ||
|
||
$columns = $schemaManager->listTableColumns('tl_page'); | ||
|
||
return !isset($columns['languageprefix']) && !isset($columns['urlsuffix']); | ||
} | ||
|
||
public function run(): MigrationResult | ||
{ | ||
$languagePrefix = new Column('languagePrefix', new StringType()); | ||
$languagePrefix->setColumnDefinition("varchar(128) BINARY NOT NULL default ''"); | ||
|
||
$urlSuffix = new Column('urlSuffix', new StringType()); | ||
$urlSuffix->setColumnDefinition("varchar(16) NOT NULL default '.html'"); | ||
|
||
$diff = new TableDiff('tl_page', [$languagePrefix, $urlSuffix]); | ||
|
||
$sql = $this->connection->getDatabasePlatform()->getAlterTableSQL($diff); | ||
|
||
foreach ($sql as $statement) { | ||
$this->connection->exec($statement); | ||
} | ||
|
||
return new MigrationResult(true, ''); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
core-bundle/tests/Functional/Migration/RoutingMigrationTest.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,87 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of Contao. | ||
* | ||
* (c) Leo Feyer | ||
* | ||
* @license LGPL-3.0-or-later | ||
*/ | ||
|
||
namespace Contao\CoreBundle\Tests\Functional; | ||
|
||
use Contao\CoreBundle\Migration\MigrationResult; | ||
use Contao\CoreBundle\Migration\Version410\RoutingMigration; | ||
use Contao\TestCase\FunctionalTestCase; | ||
use Doctrine\DBAL\Connection; | ||
|
||
class RoutingMigrationTest extends FunctionalTestCase | ||
{ | ||
/** | ||
* @dataProvider shouldRunProvider | ||
*/ | ||
public function testShouldRun(array $dropFields, bool $expected) | ||
{ | ||
static::resetDatabaseSchema(); | ||
|
||
/** @var Connection $connection */ | ||
$connection = static::$container->get('database_connection'); | ||
|
||
foreach ($dropFields as $field) { | ||
$connection->exec('ALTER TABLE tl_page DROP '.$field); | ||
} | ||
|
||
$migration = new RoutingMigration($connection); | ||
|
||
$this->assertSame($expected, $migration->shouldRun()); | ||
} | ||
|
||
public function shouldRunProvider() | ||
{ | ||
yield 'should not run if both fields exist' => [ | ||
[], | ||
false, | ||
]; | ||
|
||
yield 'should not run if urlSuffix exist' => [ | ||
['languagePrefix'], | ||
false, | ||
]; | ||
|
||
yield 'should not run if languagePrefix exist' => [ | ||
['urlSuffix'], | ||
false, | ||
]; | ||
|
||
yield 'should run if both fields do not exist' => [ | ||
['languagePrefix', 'urlSuffix'], | ||
true, | ||
]; | ||
} | ||
|
||
public function testRun() | ||
{ | ||
static::resetDatabaseSchema(); | ||
|
||
/** @var Connection $connection */ | ||
$connection = static::$container->get('database_connection'); | ||
|
||
$connection->exec('ALTER TABLE tl_page DROP languagePrefix, DROP urlSuffix'); | ||
$columns = $connection->getSchemaManager()->listTableColumns('tl_page'); | ||
$this->assertFalse(isset($columns['languageprefix'])); | ||
$this->assertFalse(isset($columns['urlsuffix'])); | ||
|
||
$migration = new RoutingMigration($connection); | ||
|
||
$result = $migration->run(); | ||
|
||
$this->assertInstanceOf(MigrationResult::class, $result); | ||
$this->assertTrue($result->isSuccessful()); | ||
|
||
$columns = $connection->getSchemaManager()->listTableColumns('tl_page'); | ||
$this->assertTrue(isset($columns['languageprefix'])); | ||
$this->assertTrue(isset($columns['urlsuffix'])); | ||
} | ||
} |