diff --git a/core/modules/system/src/Tests/Update/PathAliasUpdateTest.php b/core/modules/system/src/Tests/Update/PathAliasUpdateTest.php new file mode 100644 index 0000000..87156bc --- /dev/null +++ b/core/modules/system/src/Tests/Update/PathAliasUpdateTest.php @@ -0,0 +1,56 @@ +databaseDumpFiles = [ + __DIR__ . '/../../../tests/fixtures/update/drupal-8.bare.standard.php.gz', + ]; + } + + /** + * Ensures that the system_update_8012() runs as expected. + */ + public function testUpdate() { + $connection = \Drupal::database(); + $schema = $connection->schema(); + + if (!$schema->tableExists('url_alias')) { + return; + } + $fields = array( + 'source' => '/user', + 'alias' => '/User/PAge', + 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, + ); + $query = $connection->insert('url_alias') + ->fields($fields); + $pid = $query->execute(); + $this->runUpdates(); + + // Check that the mixed case path alias was converted to all lower case. + /** @var \Drupal\Core\Path\AliasStorageInterface $alias_storage */ + $alias_storage = \Drupal::service('path.alias_storage'); + + $alias = $alias_storage->load(['pid' => $pid]); + $this->assertIdentical(Unicode::strtolower($fields['alias']), $alias['alias']); + } + +} diff --git a/core/modules/system/system.install b/core/modules/system/system.install index 47f6dca..da34040 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -1808,5 +1808,37 @@ function system_update_8011() { } /** + * Load and save all path aliases to make sure they are lower case. + */ +function system_update_8012(&$sandbox = NULL) { + // The path alias storage interface has no way to count or iterate over all + // aliases, so we have to fall back to SQL for this function. + /** @var \Drupal\Core\Path\AliasStorageInterface $alias_storage */ + $alias_storage = \Drupal::service('path.alias_storage'); + $connection = \Drupal::database(); + $schema = $connection->schema(); + if ($schema->tableExists('url_alias')) { + if (!isset($sandbox['current'])) { + $sandbox['current'] = 0; + $sandbox['max'] = $connection->query('SELECT COUNT(pid) FROM {url_alias}') + ->fetchField(); + } + $aliases = $connection->queryRange('SELECT pid, source, alias, langcode FROM {url_alias} ORDER BY pid ASC', $sandbox['current'], $sandbox['current'] + 50) + ->fetchAllAssoc('pid', PDO::FETCH_ASSOC); + + foreach ($aliases as $alias) { + $alias_storage->save($alias['source'], $alias['alias'], $alias['langcode'], $alias['pid']); + $sandbox['current']++; + } + + $sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['current'] / $sandbox['max']); + return t('Path aliases converted'); + } + else { + return t('Path alias conversion skipped, because the {url_alias} table did not exist.'); + } +} + +/** * @} End of "addtogroup updates-8.0.0-beta". */