Commit 1c78c409 authored by anon's avatar anon Committed by anon

Issue #2751297 by anon: Update path form 4.x to 5.0

parent 8a43c27e
......@@ -2,5 +2,123 @@
/**
* @file
* Installation functions for Linkit module.
* Contains install and update functions for Linkit.
*/
/**
* Updates Linkit from 4 to 5.
*
* Removes linkit profile attributes.
* Replaces the linkit ckeditor plugin with drupallink.
* Enables the linkit filter on text formats where linkit is used.
* Adds 'data-entity-type' and 'data-entity-uuid' attributes to the <a> tag
* if filter_html is activated.
*/
function linkit_update_8500() {
$config_factory = \Drupal::configFactory();
// Iterate on all profile configuration entities.
foreach ($config_factory->listAll('linkit.linkit_profile.') as $id) {
$profile = $config_factory->getEditable($id);
// Remove all attributes.
$profile->clear('attributes')->save(TRUE);
}
// An array that will be filled with text format ids that we should enable the
// new linkit_filter for.
$linkit_enabled_formats = [];
// Iterate on all editor configuration entities.
foreach ($config_factory->listAll('editor.editor.') as $id) {
$editor_config = $config_factory->getEditable($id);
// Save the old settings.
$old_linkit_settings = $editor_config->get('settings.plugins.linkit');
$drupal_link_in_use = FALSE;
$old_linkit_button_path = NULL;
// If the editor has old linkit settings, perform update tasks.
if (!is_null($old_linkit_settings)) {
// Remove the old linkit settings.
$editor_config->clear('settings.plugins.linkit');
$editor_config->save(TRUE);
// Add the format id that the editor is using.
$linkit_enabled_formats[] = $editor_config->get('format');
// Remove the old linkit plugin from the toolbar, and check if DrupalLink
// is present in the toolbar as linkit now extends this plugin.
$toolbar_rows = $editor_config->get('settings.toolbar.rows');
foreach ($toolbar_rows as $row_index => $row) {
foreach ($row as $button_group_index => $button_group) {
foreach ($button_group['items'] as $item_index => $item__name) {
if ($item__name === 'Linkit') {
$old_linkit_button_path = 'settings.toolbar.rows.' . $row_index . '.' . $button_group_index . '.items';
}
if ($item__name === 'DrupalLink') {
$drupal_link_in_use = TRUE;
}
}
}
}
if ($old_linkit_button_path) {
$buttons = $editor_config->get($old_linkit_button_path);
$index = array_search('Linkit', $buttons);
// If the DrupalLink plugin is not present in the toolbar, lets add it
// in the same button group as the old linkit plugin was in.
if (!$drupal_link_in_use) {
$buttons[$index] = 'DrupalLink';
}
else {
unset($buttons[$index]);
}
$editor_config->set($old_linkit_button_path, array_values($buttons));
$editor_config->save(TRUE);
}
// Set the linkit settings to the DrupalLink.
$drupal_link_settings = [
'linkit_enabled' => TRUE,
'linkit_profile' => $old_linkit_settings['linkit_profile'],
];
$editor_config->set('settings.plugins.drupallink', $drupal_link_settings);
$editor_config->save(TRUE);
}
}
// Enable the linkit_filter for all formats that is used with linkit.
foreach ($linkit_enabled_formats as $filter_id) {
$config = $config_factory->getEditable('filter.format.' . $filter_id);
$filter_html_weight = $config->get('filters.filter_html.weight');
$linkit_filter = [
'id' => 'linkit',
'provider' => 'linkit',
'status' => TRUE,
'weight' => $filter_html_weight ? ($filter_html_weight - 1) : -15,
'settings' => [
'title' => FALSE,
],
];
$config->set('filters.linkit', $linkit_filter);
$allowed_html = $config->get('filters.filter_html.settings.allowed_html');
if (!empty($allowed_html)) {
preg_match_all('/<([\w]+)[^>]*>/', $allowed_html, $out);
$current_mapping = array_combine($out[1], $out[0]);
if (isset($current_mapping['a'])) {
$allowed_html = str_replace($current_mapping['a'], str_replace('>', ' data-entity-type data-entity-uuid>', $current_mapping['a']), $allowed_html);
}
else {
$allowed_html .= ' <a href hreflang data-entity-type data-entity-uuid>';
}
$config->set('filters.filter_html.settings.allowed_html', $allowed_html);
}
$config->save(TRUE);
}
}
<?php
namespace Drupal\linkit\Tests\Update;
use Drupal\filter\Entity\FilterFormat;
use Drupal\system\Tests\Update\UpdatePathTestBase;
/**
* Tests Linkit upgrade paths.
*
* @group Update
*/
class LinkitUpdateTest extends UpdatePathTestBase {
/**
* The config factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->configFactory = $this->container->get('config.factory');
}
/**
* Set database dump files to be used.
*/
protected function setDatabaseDumpFiles() {
$this->databaseDumpFiles = [
__DIR__ . '/../../../tests/fixtures/update/linkit-4-to-5/drupal-8.linkit-enabled.standard.php.gz',
__DIR__ . '/../../../tests/fixtures/update/linkit-4-to-5/linkit-4-to-5.php',
];
}
/**
* Tests linkit_update_8500().
*
* @see linkit_update_8500()
*/
public function testLinkitUpdate8500() {
$editor = $this->configFactory->get('editor.editor.format_1');
$this->assertTrue($editor->get('settings.plugins.linkit'), 'We got old linkit settings in the editor configuration.');
$format_1_linkit_profile = $editor->get('settings.plugins.linkit.linkit_profile');
$editor = $this->configFactory->get('editor.editor.format_2');
$this->assertTrue($editor->get('settings.plugins.linkit'), 'We got old linkit settings in the editor configuration.');
$format_2_linkit_profile = $editor->get('settings.plugins.linkit.linkit_profile');
$editor = $this->configFactory->get('editor.editor.format_3');
$this->assertTrue($editor->get('settings.plugins.linkit'), 'We got old linkit settings in the editor configuration.');
$format_3_linkit_profile = $editor->get('settings.plugins.linkit.linkit_profile');
$this->runUpdates();
$test_profile = $this->configFactory->get('linkit.linkit_profile.test_profile');
$this->assertEqual(NULL, $test_profile->get('attributes'), 'Attributes are deleted from the profile.');
$editor = $this->configFactory->get('editor.editor.format_1');
$this->assertNull($editor->get('settings.plugins.linkit'), 'Old linkit settings in the editor configuration is removed.');
$this->assertEqual($editor->get('settings.toolbar.rows.0.1.items.0'), 'DrupalLink', 'Drupal link plugin is in the toolbar.');
$this->assertNotEqual($editor->get('settings.toolbar.rows.0.1.items.1'), 'Linkit', 'Linkit plugin is removed from the toolbar.');
$this->assertTrue($editor->get('settings.plugins.drupallink.linkit_enabled'), 'Drupal link plugin has linkit enabled.');
$this->assertEqual($editor->get('settings.plugins.drupallink.linkit_profile'), $format_1_linkit_profile, 'Drupal link plugin uses the same profile as the old linkit plugin.');
$editor = $this->configFactory->get('editor.editor.format_2');
$this->assertNull($editor->get('settings.plugins.linkit'), 'Old linkit settings in the editor configuration is removed.');
$this->assertEqual($editor->get('settings.toolbar.rows.0.1.items.0'), 'DrupalLink', 'Drupal link plugin is in the toolbar.');
$this->assertTrue($editor->get('settings.plugins.drupallink.linkit_enabled'), 'Drupal link plugin has linkit enabled.');
$this->assertEqual($editor->get('settings.plugins.drupallink.linkit_profile'), $format_2_linkit_profile, 'Drupal link plugin uses the same profile as the old linkit plugin.');
$editor = $this->configFactory->get('editor.editor.format_3');
$this->assertNull($editor->get('settings.plugins.linkit'), 'Old linkit settings in the editor configuration is removed.');
$this->assertEqual($editor->get('settings.toolbar.rows.0.0.items.0'), 'DrupalLink', 'Drupal link plugin is in the toolbar.');
$this->assertTrue($editor->get('settings.plugins.drupallink.linkit_enabled'), 'Drupal link plugin has linkit enabled.');
$this->assertEqual($editor->get('settings.plugins.drupallink.linkit_profile'), $format_3_linkit_profile, 'Drupal link plugin uses the same profile as the old linkit plugin.');
$format = $this->configFactory->get('filter.format.format_1');
$this->assertNotNull($format->get('filters.linkit'), 'Linkit filter is enabled.');
$this->assertTrue($format->get('filters.linkit.weight') < $format->get('filters.filter_html.weight'), 'Linkit filter is running before filter_html.');
$format = $this->configFactory->get('filter.format.format_2');
$this->assertNotNull($format->get('filters.linkit'), 'Linkit filter is enabled.');
$format = $this->configFactory->get('filter.format.format_3');
$this->assertNotNull($format->get('filters.linkit'), 'Linkit filter is enabled.');
$htmlRestrictions = FilterFormat::load('format_1')->getHtmlRestrictions();
$this->assertTrue(array_key_exists("data-entity-type", $htmlRestrictions['allowed']['a']));
$this->assertTrue(array_key_exists("data-entity-uuid", $htmlRestrictions['allowed']['a']));
$htmlRestrictions = FilterFormat::load('format_3')->getHtmlRestrictions();
$this->assertTrue(array_key_exists("data-entity-type", $htmlRestrictions['allowed']['a']));
$this->assertTrue(array_key_exists("data-entity-uuid", $htmlRestrictions['allowed']['a']));
}
}
uuid: 4947dfdc-fca3-41fd-97a8-b9dcdb4d510c
langcode: en
status: true
dependencies:
config:
- filter.format.format_1
module:
- ckeditor
format: format_1
editor: ckeditor
settings:
toolbar:
rows:
-
-
name: Formatting
items:
- Bold
- Italic
-
name: Links
items:
- DrupalLink
- Linkit
- DrupalUnlink
-
name: Lists
items:
- BulletedList
- NumberedList
-
name: Media
items:
- Blockquote
- DrupalImage
-
name: Tools
items:
- Source
plugins:
stylescombo:
styles: ''
language:
language_list: un
linkit:
linkit_profile: test_profile
image_upload:
status: false
scheme: public
directory: inline-images
max_size: ''
max_dimensions:
width: null
height: null
uuid: fcc983a2-6cfd-44c4-a592-e6573180c2ac
langcode: en
status: true
dependencies:
config:
- filter.format.format_2
module:
- ckeditor
format: format_2
editor: ckeditor
settings:
toolbar:
rows:
-
-
name: Formatting
items:
- Bold
- Italic
-
name: Links
items:
- Linkit
- DrupalUnlink
-
name: Lists
items:
- BulletedList
- NumberedList
-
name: Media
items:
- Blockquote
- DrupalImage
-
name: Tools
items:
- Source
plugins:
stylescombo:
styles: ''
language:
language_list: un
linkit:
linkit_profile: test_profile
image_upload:
status: false
scheme: public
directory: inline-images
max_size: ''
max_dimensions:
width: null
height: null
uuid: 5600b759-8f91-49fa-88f0-a69f4c8ed440
langcode: en
status: true
dependencies:
config:
- filter.format.format_3
module:
- ckeditor
format: format_3
editor: ckeditor
settings:
toolbar:
rows:
-
-
name: Links
items:
- Linkit
plugins:
stylescombo:
styles: ''
language:
language_list: un
linkit:
linkit_profile: test_profile
uuid: 3e7bc0b9-3220-4b4d-aa62-e8e0c5c7e8c0
langcode: en
status: true
dependencies: { }
name: 'Format 1'
format: format_1
weight: 0
filters:
filter_html:
id: filter_html
provider: filter
status: true
weight: -86
settings:
allowed_html: '<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type=''1 A I''> <li> <dl> <dt> <dd> <h2 id=''jump-*''> <h3 id> <h4 id> <h5 id> <h6 id>'
filter_html_help: true
filter_html_nofollow: false
\ No newline at end of file
uuid: 69cd7d6b-3c80-4f49-9631-7fbd7b011d5d
langcode: en
status: true
dependencies: { }
name: 'Format 2'
format: format_2
weight: 0
filters: { }
uuid: ff6b71a0-5051-4857-a0a8-28a670b7317d
langcode: en
status: true
dependencies: { }
name: 'Format 3'
format: format_3
weight: 0
filters:
filter_html:
id: filter_html
provider: filter
status: true
weight: -10
settings:
allowed_html: '<em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type=''1 A I''> <li> <dl> <dt> <dd> <h2 id=''jump-*''> <h3 id> <h4 id> <h5 id> <h6 id>'
filter_html_help: true
filter_html_nofollow: false
\ No newline at end of file
<?php
/**
* @file
* Database fixture for testing the upgrade path for Linkit 4 to 5.
*
* Contains database additions to drupal-8.bare.standard.php.gz for testing the
* upgrade path for Linkit 4 to 5.
*/
use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Database\Database;
$connection = Database::getConnection();
// Configuration for linkit profiles.
$config = Yaml::decode(file_get_contents(__DIR__ . '/linkit.linkit_profile.test_profile.yml'));
$connection->insert('config')
->fields(array(
'collection',
'name',
'data',
))
->values(array(
'collection' => '',
'name' => 'linkit.linkit_profile.' . $config['id'],
'data' => serialize($config),
))
->execute();
// Configuration for text formats.
$configs = [];
$configs[] = Yaml::decode(file_get_contents(__DIR__ . '/filter.format.format_1.yml'));
$configs[] = Yaml::decode(file_get_contents(__DIR__ . '/filter.format.format_2.yml'));
$configs[] = Yaml::decode(file_get_contents(__DIR__ . '/filter.format.format_3.yml'));
foreach ($configs as $config) {
$connection->insert('config')
->fields(array(
'collection',
'name',
'data',
))
->values(array(
'collection' => '',
'name' => 'filter.format.' . $config['format'],
'data' => serialize($config),
))
->execute();
}
// Configuration for editors.
$configs = [];
$configs[] = Yaml::decode(file_get_contents(__DIR__ . '/editor.editor.format_1.yml'));
$configs[] = Yaml::decode(file_get_contents(__DIR__ . '/editor.editor.format_2.yml'));
$configs[] = Yaml::decode(file_get_contents(__DIR__ . '/editor.editor.format_3.yml'));
foreach ($configs as $config) {
$connection->insert('config')
->fields(array(
'collection',
'name',
'data',
))
->values(array(
'collection' => '',
'name' => 'editor.editor.' . $config['format'],
'data' => serialize($config),
))
->execute();
}
uuid: e91c2255-2146-46fb-af58-1b391113c352
langcode: en
status: true
dependencies:
module:
- file
- node
id: test_profile
label: 'Test profile'
description: 'This is a test profile'
attributes:
target:
id: target
weight: 0
settings:
widget_type: simple_checkbox
title:
id: title
weight: 0
settings:
automatic_title: false
accesskey:
id: accesskey
weight: 0
settings: { }
matchers:
fc48c807-2a9c-44eb-b86b-7e134c1aa252:
uuid: fc48c807-2a9c-44eb-b86b-7e134c1aa252
id: 'entity:node'
weight: 0
settings:
result_description: 'by [node:author] | [node:created:medium]'
bundles: { }
group_by_bundle: false
include_unpublished: false
b8d6d672-6377-493f-b492-3cc69511cf17:
uuid: b8d6d672-6377-493f-b492-3cc69511cf17
id: 'entity:file'
weight: 0
settings:
result_description: '[file:path] [file:url]'
bundles: null
group_by_bundle: null
images:
show_dimensions: false
show_thumbnail: false
thumbnail_image_style: null
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment