Commit 627acb78 authored by Ivan Doroshenko's avatar Ivan Doroshenko 🇺🇦
Browse files

Added process plugin for summary values and automated tests.

parent 249de4d4
......@@ -38,18 +38,15 @@ process:
no_stub: true
field_author: field_author/0/title
field_uw_blog_summary/value:
- plugin: single_value
source: body/0
- plugin: callback
callable: uw_migrate_summary
plugin: uw_summary
source: body
field_uw_blog_summary/format:
plugin: uw_formats
source: body/0/format
field_uw_meta_description:
- plugin: single_value
source: body/0
- plugin: callback
callable: uw_migrate_summary
plugin: uw_summary
trimmed: true
source: body
uid:
-
plugin: migration_lookup
......
......@@ -51,18 +51,15 @@ process:
source: field_event_image/0/fid
no_stub: true
field_uw_event_summary/value:
- plugin: single_value
source: body/0
- plugin: callback
callable: uw_migrate_summary
plugin: uw_summary
source: body
field_uw_event_summary/format:
plugin: uw_formats
source: body/0/format
field_uw_meta_description:
- plugin: single_value
source: body/0
- plugin: callback
callable: uw_migrate_summary
plugin: uw_summary
trimmed: true
source: body
uid:
-
plugin: migration_lookup
......
......@@ -32,18 +32,15 @@ process:
source: field_news_image/0/fid
no_stub: true
field_uw_news_summary/value:
- plugin: single_value
source: body/0
- plugin: callback
callable: uw_migrate_summary
plugin: uw_summary
source: body
field_uw_news_summary/format:
plugin: uw_formats
source: body/0/format
field_uw_meta_description:
- plugin: single_value
source: body/0
- plugin: callback
callable: uw_migrate_summary
plugin: uw_summary
trimmed: true
source: body
uid:
-
plugin: migration_lookup
......
......@@ -35,18 +35,15 @@ process:
source: field_profile_type
no_stub: true
field_uw_profile_summary/value:
- plugin: single_value
source: body/0
- plugin: callback
callable: uw_migrate_summary
plugin: uw_summary
source: body
field_uw_profile_summary/format:
plugin: uw_formats
source: body/0/format
field_uw_meta_description:
- plugin: single_value
source: body/0
- plugin: callback
callable: uw_migrate_summary
plugin: uw_summary
trimmed: true
source: body
uid:
-
plugin: migration_lookup
......
......@@ -28,10 +28,9 @@ process:
source: field_event_image/0/fid
no_stub: true
field_uw_meta_description:
- plugin: single_value
source: body/0
- plugin: callback
callable: uw_migrate_summary
plugin: uw_summary
trimmed: true
source: body
langcode:
plugin: default_value
source: language
......
<?php
namespace Drupal\uw_migrate\Plugin\migrate\process;
use Drupal\Component\Utility\Html;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
/**
* Custom process plugin to prepare the summary value from original body.
*
* @code
* process:
* type:
* plugin: uw_summary
* trimmed: true
* max_length: 200
* source: body
* @endcode
*
* @MigrateProcessPlugin(
* id = "uw_summary",
* handle_multiples = TRUE
* )
*/
class UwSummary extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$new_value = $value[0] ?? $value;
$trimmed = $this->configuration['trimmed'] ?? FALSE;
// Define settings based on the type of summary.
$format = 'uw_tf_standard';
$max_length = NULL;
if ($trimmed) {
$format = 'plain_text';
$max_length = $this->configuration['max_length'] ?? 160;
}
if (isset($new_value['summary']) && (string) $new_value['summary'] !== '') {
$summary = $new_value['summary'];
}
else {
$summary = $new_value['value'] ?? '';
}
// Remove extra spaces, if there are any.
$summary = trim($summary);
$filter_types_to_skip = [];
if ($trimmed) {
// Remove "filter_autop" filter to not append extra <p> tags.
$filter_types_to_skip[] = 'filter_autop';
}
// Strip tags because trimmed version should not have any HTML markup.
// Temporary replace <!--break--> string with custom tokens to preserve
// teaser breaks (HTML comments are always stripped by default).
// We replace it back after check_markup() call, because it can be stripped
// there as well.
$summary = str_replace('<!--break-->', '[uw-teaser-break]', $summary);
if ($trimmed) {
$summary = strip_tags($summary);
}
// Process text via configured text format filters to make sure the value
// is safe and valid.
$summary = (string) check_markup($summary, $format, '', $filter_types_to_skip);
$summary = str_replace('[uw-teaser-break]', '<!--break-->', $summary);
// Now retrieve the actual summary, trimmed if necessary.
$summary = text_summary($summary, $format, $max_length);
// Normalize HTML to fix unclosed tags.
// Usually, it's done by text_summary(), but it exits earlier if the teaser
// break is presented.
$summary = Html::normalize($summary);
return $summary;
}
}
<?php
namespace Drupal\Tests\uw_migrate\Kernel\process;
use Drupal\KernelTests\KernelTestBase;
use Drupal\uw_migrate\Plugin\migrate\process\UwSummary;
/**
* Tests the uw_taxonomy_term_lookup custom plugin.
*
* @coversDefaultClass \Drupal\uw_migrate\Plugin\migrate\process\UwSummary
* @group uw_migrate
*/
class UwSummaryTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
protected $strictConfigSchema = FALSE;
/**
* {@inheritdoc}
*/
protected static $modules = [
'system', 'migrate', 'text', 'filter', 'file', 'media', 'simplify_menu',
'editor', 'linkit', 'ckeditor', 'token', 'media_embed_extra', 'node',
'uw_cfg_common', 'taxonomy', 'user',
];
/**
* The source row.
*
* @var \Drupal\migrate\Row
*/
protected $row;
/**
* The migrate mock object.
*
* @var \Drupal\migrate\MigrateExecutable|\PHPUnit\Framework\MockObject\MockObject
*/
protected $migrateExecutable;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig('filter');
$this->installConfig('text');
// Install uw_cfg_common to create "uw_tf_standard" text format.
$this->installConfig('uw_cfg_common');
$this->row = $this->getMockBuilder('Drupal\migrate\Row')
->disableOriginalConstructor()
->getMock();
$this->migrateExecutable = $this->getMockBuilder('Drupal\migrate\MigrateExecutable')
->disableOriginalConstructor()
->getMock();
}
/**
* Lookup for the existing term by name.
*
* @covers ::transform
* @dataProvider providerTransform
*/
public function testTransform($source, $original, $trimmed) {
$plugin = new UwSummary(['trimmed' => FALSE], 'uw_summary', []);
$actual = $plugin->transform($source, $this->migrateExecutable, $this->row, 'destination');
$this->assertSame($original, $actual);
$plugin_trimmed = new UwSummary(['trimmed' => TRUE], 'uw_summary', []);
$actual_trimmed = $plugin_trimmed->transform($source, $this->migrateExecutable, $this->row, 'destination');
$this->assertSame($trimmed, $actual_trimmed);
}
/**
* Provider for self::testTransform().
*/
public function providerTransform() {
return [
'One line of text.' => [
[
'value' => '<p>Body text.</p>',
'summary' => '<p>Summary text.</p>'
],
'<p>Summary text.</p>',
'Summary text.',
],
'Multiple lines with some extra markup.' => [
[
'value' => '<h2>Stuff</h2>
<p>The stuff is <a href=https://google.ca>found here</a>.</p>
<p>It is really great stuff and here I am describing it for what will end up being more than 160 characters, at least I hope &ndash; it&rsquo;s not like I&rsquo;m keeping track. (enough characters to take us to say, 595 characters), and so on.</p>',
'summary' => '',
],
"<h2>Stuff</h2>
<p>The stuff is <a href=\"https://google.ca\">found here</a>.</p>
<p>It is really great stuff and here I am describing it for what will end up being more than 160 characters, at least I hope – it’s not like I’m keeping track. (enough characters to take us to say, 595 characters), and so on.</p>",
'Stuff
The stuff is found here.',
],
'Multiple lines with teaser break.' => [
[
'value' => '<h2>Stuff</h2>
<p>The stuff is <a href=https://google.ca>found here</a>.<!--break--></p>Some other text.',
],
'<h2>Stuff</h2>
<p>The stuff is <a href="https://google.ca">found here</a>.</p>',
'Stuff
The stuff is found here.',
]
];
}
}
......@@ -147,34 +147,3 @@ function uw_migrate_empty($value) {
function uw_migrate_get_component_delta(array $value) {
return $value[0] ? $value[1] : NULL;
}
/**
* Prepare text for teasers, using either the summary or trimmed value.
*
* @param array $value
* An array with keys summary or value from which to generate the summary.
*
* @return string
* The prepared summary.
*/
function uw_migrate_summary($value) {
$format = 'plain_text';
if (isset($value['summary']) && (string) $value['summary'] !== '') {
$summary = $value['summary'];
}
else {
$summary = $value['value'] ?? '';
}
// Strip tags and trim extra spaces.
$summary = trim(strip_tags($summary));
// Run text format filters.
// Remove "filter_autop" filter to avoid extra <p> tags.
$summary = (string) check_markup($summary, $format, '', ['filter_autop']);
// Trim if size is more than allowed (160 characters).
if (strlen($summary) > 160) {
$summary = text_summary($summary, $format, 160);
}
return $summary;
}
Supports Markdown
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